Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fpdf to modify existing pdf in php

Tags:

php

pdf

fpdf

I have a bank of pdfs on my server which when downloaded need text appended to every page. I am using fpdf to try and open the file, append the text to each page, close the file and serve to the browser.

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

At the minute this just tries to put some text anywhere on the pdf and save it but I get the error

FPDF error: You have to add a page first!

If I can get it to do this I then need it to append the text to every page in the document rather than just 1, not sure how to do this having read the documentation

like image 672
Steve Smith Avatar asked Sep 20 '12 09:09

Steve Smith


People also ask

What is use of FPDF in php?

FPDF is a PHP class which allows generating PDF files with PHP code. It is free to use and it does not require any API keys. FPDF stands for Free PDF. It means that any kind of modification can be done in PDF files.

How do I print a php variable in FPDF?

php require ('fpdf/fpdf. php'); $departu = $_POST['departu']; $pdf = new FPDF('P', 'mm', 'A4'); $pdf->AddPage(); $pdf->SetFont('Courier', '', 11); $pdf->Cell(30, 7, $departu, 0, 1, 'L'); $pdf->Output(); ?>

What is FPDF error?

The FPDF Error Message will point you to the PHP Line that is sending some content. If you get no hint what File & Line send some content you probably have an encoding mismatch in your include / require Files.


2 Answers

Since you want all the pages with the text, one way to do it is putting the code in a loop.

Like this:

// 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"); 
}
like image 76
Flávio Silveira Avatar answered Oct 03 '22 18:10

Flávio Silveira


Try following

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

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

Use this page as template, then

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

Let me know if you have any errors

like image 30
GBD Avatar answered Oct 03 '22 17:10

GBD