PDF 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
<?php // Include the FPDF library require('fpdf.php'); // Create your PDF document class, which extends the FPDF class class PDF extends FPDF { // Method for header section of document function Header() { // Logo $this->Image('logo.png',10,6,30); // Arial bold 15 $this->SetFont('Arial','B',15); // Move to the right $this->Cell(80); // Title $this->Cell(30,10,'Title',1,0,'C'); // Line break $this->Ln(20); } // Method for footer section of document function Footer() { // Position at 1.5 cm from bottom $this->SetY(-15); // Arial italic 8 $this->SetFont('Arial','I',8); // Page number $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); } } // Create PDF document with content $pdf = new PDF(); $pdf->AliasNbPages(); $pdf->AddPage(); $pdf->SetFont('Times','',12); // Add some dummy content lines for($i=1;$i<=40;$i++) { $pdf->Cell(0,10,'Printing line number '.$i,0,1); } // Generate the PDF output $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.
No comments yet (leave a comment)