Adding content to an existing PDF using FPDI PHP library

PHP has got lot of libraries for generating PDF documents programmatically. But it would actually be better to add required content to an existing PDF document (say a template) rather than generating  new PDF documents every time it is needed. Because, oftentimes the dynamic content in the PDF document you will generate, will be very less than the static content in the PDF. For instance, an invoice, a ticket, a receipt, watermarking an existing document, etc.

Importing an existing PDF document

You can either choose FPDF or TCPDF. But both of these libraries does not have the importing functionality inbuilt. So we will use the FPDI, which will load the specified PDF document over which we can make desired programmatic content additions. Download FPDI Library. I’m writting this tutorial assuming you are using FPDF, but TCPDF implementation is very similar.



You can create an object of the FPDI class. The FDPI class, by default detects end extends the TCPDF or FPDF class (whichever is available), so you need not create a new TCPDF or FPDF object.

$pdf = new FPDI();


Specify the source PDF document by calling setSourceFile function.



Specify which page of the document is to be imported. I’m importing 1st page and setting the second parameter – boxtype to ‘/Mediabox’.

$tplIdx = $pdf->importPage(1, '/MediaBox');
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 

The 2nd paramenter – boxType, can take any one of the following values,

  • /MediaBox
  • /BleedBox
  • /TrimBox
  • /CropBox
  • /ArtBox

To know what each of these values mean – read The PDF page boxes.

ALSO READ  Sending Bulk emails using PHP with mandrill API

Adding content to the imported PDF document

Now the document and the page to be used as template is successfully loaded. Text or image can be added anywhere on the loaded page by specifying XY co-ordinates of the position (You should use HTML tags to do this in TCPDF).

$pdf->SetTextColor(0, 0, 0);
$pdf->SetXY(58, 45);

To write text, call the write() function. The first parameter takes line height value.



To add image to the the document call Image() function.

  • 1st parameter takes URL of the image.
  • 2nd & 3rd parameter takes X & Y position respectively. (optional)
  • 4th & 5th parameter takes width and height values respectively. (optional)
  • 6th parameter takes image type – JPG, JPEG, PNG and GIF. (optional and case insensitive)
  • 7th parameter takes image anchor link. (optional)

And when you are done. Call the Output() function to output the PDF document on the clients browser.


Limitations of FPDI Library

The FPDI has the following limitations.

  1. FPDI cannot import a encrypted/password protected document.
  2. PDF documents of version 1.5 or higher and with compression involving structure changes cannot be handled with the open source version of FPDI. Check FPDI PDF Parser, a commercial add-on for the FPDI PHP library.
  3. Link annotations, form fields and page annotations cannot be created.
  4. Importing bookmark outlines, layers (in a layered pdf), actions, etc are not possible.


One Response

  1. Erick November 30, 2016

Leave a Reply