How to Create a PDF File using PHP

How to Create a PDF File using PHP

To create a PDF file from PHP code.

PHP has a free library that allows us to work with pdf’s files is called FPDF.

FPDF is a PHP class that allows you to generate PDF documents directly from PHP, ie, without using the PDFlib library. The F from FPDF stands for Free can be used for any purpose and modify it to your liking to fit your needs.

FPDF has other advantages: high level functions. This is a list of its main features:

  • Choice of measurement unit, page format and margins
  • Management headers and footers
  • Automatic page break
  • Line break and text justification automatic
  • Admission images (JPEG, PNG and GIF)
  • Colors
  • links
  • Admission of TrueType, Type1 and encoding
  • Page Compression

FPDF does not need any PHP extension (except zlib to activate compression and GD for GIF support) and works with PHP4 (≥ 4.3.10) and PHP5.

The tutorials let you quickly start using FPDF. Full documentation (online) is here and the download area, here . It is strongly recommended that you read the FAQ , which list the most common questions and problems (particularly those related to browsers): (Frequently Asked Questions or PMF).

There is a section of scripts that includes some useful extensions.

Let’s see a small example where we will simply write a headline to PDF.

$ Pdf = new PDF ();
$ Pdf-> AliasNbPages ();
$ Pdf-> AddPage ();
$ Pdf-> SetFont ( 'Arial', 'B', 20);
$ Pdf-> Cell (100.50, utf8_decode ( 'Holder page), 0.1);

To begin to understand this small piece of code we must download the FPDF class and create an extended class:

class PDF extends FPDF {

Inside we include some features that we may be very useful such as the header () function and footer ().

By using above small piece of code we create an instance of the class, create a page, determine the style of font and type in such a position the holder of the page.

Lucid Kit

Related Posts
Leave a reply
Captcha Click on image to update the captcha .