How to create personalized PDF Templates using TCPDF and FPDI

I recently had to output a personalized copy of a document to a few hundreds of users. Unfortunately, TCPDF cannot open/read an existing PDF nor can it use a PDF as template.

Fortunately, I stumbled on FPDI which extends TCPDF with this feature. You can load an existing PDF page and use it as a template. It’s pretty easy to implement so I won’t go into the details. Just checkout the demo.



Example (From FPDI Site – Link Above)

// just require TCPDF instead of FPDF

class PDF extends FPDI {
 * "Remembers" the template id of the imported page
 var $_tplIdx;

 * include a background template for every page
 function Header() {
 if (is_null($this->_tplIdx)) {
 $this->_tplIdx = $this->importPage(1);

 $this->SetFont('freesans', 'B', 9);
 $this->SetXY(60.5, 24.8);
 $this->Cell(0, 8.6, "TCPDF and FPDI");

 function Footer() {}

// initiate PDF
$pdf = new PDF();
$pdf->SetAutoPageBreak(true, 40);

// add a page

// get esternal file content
$utf8text = file_get_contents("cache/utf8test.txt", true);

$pdf->SetFont("freeserif", "", 12);
// now write some text above the imported page
$pdf->Write(5, $utf8text);

$pdf->Output('newpdf.pdf', 'D');

10 thoughts on “How to create personalized PDF Templates using TCPDF and FPDI

  1. flyking says:

    hi,Iuse this example ,but it doesn’t work,it show this:

    Strict Standards: Declaration of FPDF::_putstream() should be compatible with that of TCPDF::_putstream() in E:\LingwenWorkspace\workspaceFlyking\tcpdf_5_9_155\tcpdf\FPDI-1.4.2\fpdi2tcpdf_bridge.php on line 167

    That’s why.

  2. Juan Manuel Rivera says:

    Flyking…. I can’t write in english … but in spanish … Amigo, tienes que editar los respectivos archivos que te sacan las Advertencias…y colocar en cada función del fpdi con las variables que solicita… o puedes editar el php.ini y quitar el modo Strict… espero haber podido ayudarte…!

    Dude, you have to edit the respective files that take the warnings … and place in each function of the variables FPDI requesting … or you can edit the php.ini and remove Strict mode … I hope I could help you …!

  3. Hi Sajayi,
    Thank you for the example. The same worked for me as well. Thank you once again !

    I have a different situation :
    “I need to export an existing PDF file . It may contain more than 1 page. I need to write an image on the “Nth” page of the PDF.”

    Do you have an idea / solution for this? How will i know how many pages are there ? :/
    I am really confused !
    It would be of great help if you could help me through this..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s