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');
General, PHP

Get Productive With Frameworks!

A friend of mine was learning PHP and like for those of use who started when frameworks were still in the minds of their creators, it was a torturous experience. I don't miss those complex, mind blowing solutions to simple problems I created in those early days.

To spare my friend of the trouble, I recently introduced him to CakePHP, a PHP 4 & 5 compatible framework, and he was shocked at how much work CakePHP does for the developer.

Why use CakePHP or other Frameworks? To get productive! The bottom line for every developer is productivity. Frameworks spare developers from reinventing the wheel just to move a bunch of bricks a few miles. Developers can focus on the real work on developing applications.

While its inevitable to learn the basics of a particular programming language, I'll recommend any newbie developer to find a framework as soon as possible.

The trouble of learning a framework is well worth it.