Create PDF

From Phpmaniac

Jump to: navigation, search

Return to Contents

Contents

Why create a PDF in PHP

Creating PDF's in PHP allows us to dynamically create great looking dynamic documents that will look the same no matter what platform the end user is using. Typical uses for this can be creating invoices, advertisments, and various other personalized documents.

How do we create a PDF in PHP

There are a number of options for creating PDF's in PHP, some are free like FPDF, some are not like pdflib, this tutorial will focus on the excellent FPDF library.

FPDF website

What is FPDF

FPDF is a pure PHP PDF library, meaning that it has been written from the ground up in PHP and has no external dependancies.

Downloading FPDF

Go to the download section of the FPDF website, create a folder on your webserver called fpdf, and unzip the contents of the download there.

Creating your first PDF

This example will create a PDF and write 'Hello World!'.

  1. <?php
  2.          require("fpdf/fpdf.php");            // Load the fpdf library
  3.  
  4.          $pdf=new FPDF();                     // Create a new instance of the fpdf object
  5.          $pdf->AddPage();                     // Add Page 1
  6.  
  7.          $pdf->SetFont('Arial','',12);        // Set the font to Arial, no bold, italics or underline, 12 point
  8.          $pdf->SetXY(85, 145);                // Move the current co-ordinates for the write
  9.          $pdf->Cell(40,6,'Hello World!');     // Create a text cell, 40mm wide, 6mm high, with the text "Hello World!"
  10.  
  11.          $pdf->Output();                      // Output the PDF
  12. ?>
  • Line 2: Include the FPDF library so that we can use it.
  • Line 4: Create a new instance of an FPDF object which we will name $pdf.
  • Line 5: Create the first page
  • Line 7: Set the font to Arial, using 12 point
  • Line 8: Set the cordinates to create the cell to 85mm across and 145 mm down
  • Line 9: Create a cell and write "Hello World!"
  • Line 11: Out put the PDF and the appropriate PDF HTTP headers

Adding an image

To Add an image we need to use the fpdf image method. FPDF supports PNG and JPEG formats, that are on the local file system. If you want to use an image from a different source such as raw GD data you will need to output the pdf to a temp file, and then reference that temp file in the the FPDF image method.

For PNG images the alpha channel is not supported (transparency).

Image(string file, float x, float y [, float w [, float h [, string type [, mixed link]]]])
  1. <?php
  2.         require("fpdf/fpdf.php");            // Load the fpdf library
  3.  
  4.         $pdf=new FPDF();                     // Create a new instance of the fpdf object
  5.         $pdf->AddPage();                     // Add Page 1
  6.  
  7.         $pdf->Image("php.png", 40, 40);      // Place the file php.png 40 mm across, and 40 mm down the page
  8.         $pdf->Output();                      // Output the PDF
  9. ?>

Troubleshooting

Alpha channel is present

FPDF does not support the alpha channel/transparency, resave the image without an alpha channel.

Error:

FPDF error: Alpha channel not supported: php.png

Return to Contents

Personal tools