2012-09-20 452 views
6

Tengo un banco de pdfs en mi servidor que, cuando se descargan, necesitan texto adjunto a cada página. Estoy usando fpdf para tratar de abrir el archivo, anexar el texto a cada página, cerrar el archivo y enviarlo al navegador.Uso de fpdf para modificar el pdf existente en php

$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); 

En el minuto esto sólo trata de poner un poco de texto en cualquier lugar en el pdf y guardarlo, pero me sale el error

FPDF error: You have to add a page first! 

Si puedo conseguir que haga esto entonces necesito para anexar el texto para cada página del documento en lugar de sólo 1, no está seguro de cómo hacer esto después de haber leído la documentación

Respuesta

9

Trate de seguir

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

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

Utilice esta página como plantilla, a continuación,

$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); 

, hágamelo saber si usted tiene cualquier error

+2

funcionó solo para mí usando nulos en $ x & $ y '$ outPdf-> useTemplate ($ outPdf-> importPage ($ i), null, null, 0, 0, true);'. De lo contrario, corta páginas a A4. – juanmf

5

Desde desea que todas las páginas con el texto, una forma de hacerlo es poner el código en un bucle .

De esta manera:

// 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"); 
} 
Cuestiones relacionadas