2012-09-20 2 views
6

다운로드 할 때 모든 페이지에 텍스트를 추가해야하는 내 서버에 pdfs 은행이 있습니다. 나는 fpdf를 사용하여 파일을 열어보고, 각 페이지에 텍스트를 추가하고, 파일을 닫고 브라우저에 서비스를 제공합니다. 내가이 작업을 수행 할 수 있다면 나는 그 때 추가하는 데 필요한 순간에fpdf를 사용하여 PHP의 기존 pdf 수정

$pdf = new FPDI(); 

$pdf->setSourceFile($filename); 
// import page 1 
$tplIdx = $pdf->importPage(1); 
//use the imported page and place it at point 0,0; calculate width and height 
//automaticallay and ajust the page size to the size of the imported page 
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 

// now write some text above the imported page 
$pdf->SetFont('Arial', '', '13'); 
$pdf->SetTextColor(0,0,0); 
//set position in pdf document 
$pdf->SetXY(20, 20); 
//first parameter defines the line height 
$pdf->Write(0, 'gift code'); 
//force the browser to download the output 
$pdf->Output('gift_coupon_generated.pdf', 'D'); 

header("location: ".$filename); 

이 바로 PDF의 아무 곳이나 텍스트를 넣어 저장하려고하지만 오류

FPDF error: You have to add a page first! 

를 얻을 수 문서보다는 1이 아니라이 작업을 수행하는 방법을 잘 모든 페이지에있는 텍스트는 다음 문서

답변

9

시도를 읽은

require_once('fpdf.php'); 
require_once('fpdi.php'); 

$pdf =& new FPDI(); 
$pdf->AddPage(); 

사용 후 템플릿으로이 페이지

$pdf->setSourceFile($filename); 
// import page 1 
$tplIdx = $pdf->importPage(1); 
//use the imported page and place it at point 0,0; calculate width and height 
//automaticallay and ajust the page size to the size of the imported page 
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 

는 당신이 루프의 코드를 가하고 할 수있는 한 가지 방법 오류

+2

$ x & $ y'$ outPdf-> useTemplate ($ outPdf-> importPage ($ i), null, null, 0, 0, true); '에서 Null을 사용할 때만 효과가있었습니다. 그렇지 않으면 페이지를 A4로 자릅니다. – juanmf

5

당신이 텍스트로 모든 페이지를 원하기 때문에가있는 경우 알려주세요 .

// Get total of the pages 
$pages_count = $pdf->setSourceFile('your_file.pdf'); 

for($i = 1; $i <= $pages_count; $i++) 
{ 
    $pdf->AddPage(); 

    $tplIdx = $pdf->importPage($i); 

    $pdf->useTemplate($tplIdx, 0, 0); 


    $pdf->SetFont('Arial'); 
    $pdf->SetTextColor(255,0,0); 
    $pdf->SetXY(25, 25); 
    $pdf->Write(0, "This is just a simple text"); 
} 
관련 문제