Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Create PDF files with PHP

Friday, July 31st, 2015 by Servage

pdf-phpPDF files are useful for many purposes. It is an acronym for Portable Document Format which provides a file that will display a document the same way on any device, regardless of operating system and other software, as long as a PDF viewer is available. PDF files are nowadays viewable by default on most devices including smartphones and tables.

In comparison to web content, PDFs do not have the same intelligent features such as styling and responsiveness. There is also no underlying script language as advanced as Javascript available to web browsers. However, that is not really a problem because PDFs fulfill an entirely different purpose. They are especially useful when it comes to sharing documents, and wanting to ensure anyone who opens the document sees the exact same thing. There is no mis- formatting by different programs or versions of word processors etc.

For web projects you can use this to create printable versions of content, and wanting to make sure it looks better than the user simply printing a website. A common use case for PDFs is invoicing. While you want to provide the user with a good and responsive way to review his account on your website, and also view order and invoicing data in a good way on any device, you may also wish to send a well formatted PDF document.

Example PDF document with PHP


// Include the FPDF library

// Create your PDF document class, which extends the FPDF class
class PDF extends FPDF
	// Method for header section of document
	function Header()
	    // Logo
	    // Arial bold 15
	    // Move to the right
	    // Title
	    // Line break
	// Method for footer section of document
	function Footer()
	    // Position at 1.5 cm from bottom
	    // Arial italic 8
	    // Page number
	    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
	// Create PDF document with content
	$pdf = new PDF();
	// Add some dummy content lines
	    $pdf->Cell(0,10,'Printing line number '.$i,0,1);
	// Generate the PDF output


FPDF library

The example above creates a new PDF document with some dummy line content. It illustrates how easily you can start creating PDF documents for any purpose.

The FPDP library used in the example is one of the most popular PDF generation PHP libraries. It offers easy to use and a good object oriented implementation. You basically create a PHP class for any new document you wish to create, which can be used both for static document templates such as invoices, but also easily extends to add various dynamic content and formatting.

Create PDF files with PHP, 4.9 out of 5 based on 7 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.