Git

Quick Start to Git Submodule

The Problem

As a developer, having loads of reusable code makes work much easier and enjoyable. I’ve got into the habit of breaking the applications I develop in CakePHP into plugins so I can reuse the plugins in other projects. It wasn’t long before I hit some issues. For example, I used plugin X in project A. Then copied plugin X to project B and added new functionality. Not so long after that I extended plugin X in project A. So now I have two “branches” of plugin X and merging them is another task altogether. That project A and project B are git repos doesn’t help either since I don’t want to merge project A and project B.

Continue reading

Standard
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