---
title: "Creating PDF documents with PHP in 10 steps  "
author: "love~ruby+rails"
date: "2009-11-09T08:33:53Z"
tags: []
slug: "creating-pdf-documents-with-php-in-10-steps"
draft: false
lastmod: "2009-11-09T08:33:53Z"
id: 512640
url: "https://cursed.iteye.com/blog/512640"
---

## Step 1 - Install the PDFlib extension

PDFlib is not included with the default PHP 5.3 distribution, so you will have to install it. Download the PHP version of the PHPlib library from [here](http://www.pdflib.com/download/pdflib-family/pdflib-7/) .

The Windows build of PHP 5 comes in many different varieties. I'll assume that you are using the VC9 non-thread safe build, and that you have installed PHP to the default location.

Extract the **libpdf\_php.dll** file from the **PDFlib-7.0.4p6-MSWin32-php\\bind\\php5\\php-530-nozts\_VS9\\** directory in the PHPlib zip file to **C:\\Program Files\\PHP\\ext** .

Then add the following two lines to the **PHP.ini** file located in **C:\\Program Files\\PHP** :

**\[PHP\_PDF]**

**extension=libpdf\_php.dll**

&lt;!-- AD\_PLACEHOLDER\_4--&gt;

## Step 2 - Create the PDF document

Open up a new try block, create a new PDFlib instance, and then start a new PDF document by calling **begin\_document** .

**try {**

**$pdf=new PDFlib();**

**if(!$pdf-&gt;begin\_document("",""))**

**{**

**throw new PDFlibException("Error creating PDF document. ".$pdf-&gt;get\_errmsg());**

**}**

&lt;!-- AD\_PLACEHOLDER\_5--&gt;

## Step 3 - Set the PDF properties

The **set\_info** function allows you set some optional document properties. Here we define the author and title of the PDF document.

**$pdf-&gt;set\_info("Author","Matthew Casperson");**

**$pdf-&gt;set\_info("Title","Create a PDF document with PHP");**

&lt;!-- AD\_PLACEHOLDER\_6--&gt;![PHP PDF Screenshot](http://images.brighthub.com/09/F/09FFD350DF540EBC06471F0E4B9CD44C00A42190_small.jpg "PHP PDF Screenshot")&lt;!-- AD\_PLACEHOLDER\_7--&gt;

## Step 4 - Create a new page

The **begin\_page\_ext** function is called to start a new page in the PDF document. Here we have specified the page to have dimensions of 595 x 842 points, making it an A4 page.

**$pdf-&gt;begin\_page\_ext(595,842,"");**

Other common page sizes (in points) are:

**a4** 595 x 842

**a5** 421 x 595

**a6** 297 x 421

**letter** 612 x 792

**legal** 612 x 1008

**ledger** 1224 x 792

**11x17** 792 x 1224

&lt;!-- AD\_PLACEHOLDER\_8--&gt;

## Step 5 - Prepare a font

To print any text to the PDF document we first need to prepare the font that will be used. The **load\_font** function takes a locally installed font and prepares it for use with PDFlib. The **setfont** function specifies that we will be using this font when writing text.

**$font=$pdf-&gt;load\_font("Helvetica-Bold","winansi","");**

**$pdf-&gt;setfont($font,24.0);**

&lt;!-- AD\_PLACEHOLDER\_9--&gt;

## Step 6 - Write some text

Using the **set\_text\_pos** and **show** functions we can write some text to the PDF document at the specified location.

**$pdf-&gt;set\_text\_pos(50,800);**

**$pdf-&gt;show("Hello World");**

&lt;!-- AD\_PLACEHOLDER\_10--&gt;

## Step 7 - End the page

The **end\_page\_ext** function ends the page that we started in step 4.

**$pdf-&gt;end\_page\_ext("");**

&lt;!-- AD\_PLACEHOLDER\_11--&gt;

## Step 8 - End the document

The end\_document function ends the PDF document we started in step 2.

**$pdf-&gt;end\_document("");**

&lt;!-- AD\_PLACEHOLDER\_12--&gt;

## Step 9 - Send the PDF document

Typically a PHP script is used to send a HTML document. In this case we need to override this default behaviour to tell the recieving browser that it is about to recieve a PDF document. The following code creates the HTTP headers required to identify the data being sent as a PDF document, which will cause the browser to open up Adobe Reader (or the default PDF application) when the data is transmitted.

**$buffer=$pdf-&gt;get\_buffer();**

**$len=strlen($buffer);**

**header("Content-type: application/pdf");**

**header("Content-Length: $len");**

**header("Content-Disposition: inline; filename=example.pdf");**

**echo $buffer;**

&lt;!-- AD\_PLACEHOLDER\_13--&gt;

## Step 10 - Deal with any errors

The try block is closed, and a catch block is created. If there were any errors creating the PDF document, the user is notified.

**}**

**catch (PDFlibException $e)**

**{**

**echo 'Error Number:'.$e-&gt;get\_errnum()."n";**

**echo 'Error Message:'.$e-&gt;get\_errmsg();**

**}**

&lt;!-- AD\_PLACEHOLDER\_14--&gt;

## Conclusion

PDFlib makes creating PDF documents a breeze. You could extend this example to allow users to download pre-filled PDF documents, or to create PDF versions of a web page for easy printing.