PHP

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.

TCPDF – http://www.tcpdf.org

FPDI – http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/tcpdf-demo/

Example (From FPDI Site – Link Above)

<?php
// just require TCPDF instead of FPDF
require_once('tcpdf.php');
require_once('fpdi.php');

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->setSourceFile('PDFDocument.pdf');
 $this->_tplIdx = $this->importPage(1);
 }
 $this->useTemplate($this->_tplIdx);

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

 function Footer() {}
}

// initiate PDF
$pdf = new PDF();
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);

// add a page
$pdf->AddPage();

// 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');
?>
Standard
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.

Standard