php is a very robust web programming language. And generating a pdf document with php has never been better. In this useful tutorial I will show you how to use pdflib extension to generate a basic pdf document with php. Now lets get started and create!
Prerequisites
Before you begin, you need to make sure your server has the required extensions. Take the following steps to test your PHP installation and the existence of pdf library.
- Make a PHP file (e.g. test.php)
- Enter the following code
- <?php echo phpinfo(); ?>
<?php echo phpinfo(); ?>
Now look for the line below:
If you can see that and other information (e.g. PDFlib GmbH Version, PECL Version) then your server has PDFLib already bundled in.
If you don’t see that then please consult your web hosting provider or install it yourself in the server. Because without this library you can’t create your own pdf file with PHP.
- Go to http://www.pdflib.com/
- Click Download
- Click Product Family
- Under your platform choose PHP
- Open the readme.txt file for specific installation steps.
Basic PDF Generation
There are different kinds of document sizes available. You are free to generate any size you want, but I like to store the standard sizes in an array.
- <?php
- $DocumentType = array('A0' => array('width' => 2380,
- 'height' => 3368),
- 'A1' => array('width' => 1684,
- 'height' => 2380),
- 'A2' => array('width' => 1190,
- 'height' => 1684),
- 'A3' => array('width' => 842,
- 'height' => 1190),
- 'A4' => array('width' => 595,
- 'height' => 842),
- 'A5' => array('width' => 421,
- 'height' => 595),
- 'A6' => array('width' => 297,
- 'height' => 421),
- 'B5' => array('width' => 501,
- 'height' => 709),
- 'letter' => array('width' => 612,
- 'height' => 792),
- 'legal' => array('width' => 612,
- 'height' => 1008),
- 'ledger' => array('width' => 1224,
- 'height' => 792),
- '11/17inches' => array('width' => 792,
- 'height' => 1224));
<?php
$DocumentType = array('A0' => array('width' => 2380,
'height' => 3368),
'A1' => array('width' => 1684,
'height' => 2380),
'A2' => array('width' => 1190,
'height' => 1684),
'A3' => array('width' => 842,
'height' => 1190),
'A4' => array('width' => 595,
'height' => 842),
'A5' => array('width' => 421,
'height' => 595),
'A6' => array('width' => 297,
'height' => 421),
'B5' => array('width' => 501,
'height' => 709),
'letter' => array('width' => 612,
'height' => 792),
'legal' => array('width' => 612,
'height' => 1008),
'ledger' => array('width' => 1224,
'height' => 792),
'11/17inches' => array('width' => 792,
'height' => 1224));Next, I will store my desired height and width in a separate variable for easy access.
- $width = $DocumentType['A6']['width'];
- $height = $DocumentType['A6']['height'];
$width = $DocumentType['A6']['width']; $height = $DocumentType['A6']['height'];
Now comes the fun part.
- $pdf = pdf_new();
- //Create or write a pdf document
- pdf_open_file($pdf, "C:\wamp\www\pdfgeneration\greatquotes.pdf");
- //Create a New Pagepdf_begin_page($pdf, $width, $height);
$pdf = pdf_new(); //Create or write a pdf document pdf_open_file($pdf, "C:\wamp\www\pdfgeneration\greatquotes.pdf"); //Create a New Pagepdf_begin_page($pdf, $width, $height);
In the variable $pdf I created a pdf document resource. So, everything we do any thing related to pdf generation, we will call back to $pdf to make the changes under that variable.
Choosing fonts for your PDF document is bit tricky but not hard at all. The method I am showing will require the .ttf file of a font to be present in the server.
- //Choose a font you wish to use
- $FontDir = "C:\WINDOWS\Fonts"; //Directory where the font files are
- pdf_set_parameter($pdf, "FontOutline", "arialMyName=$FontDir\arial.ttf");
- $FontArial = pdf_findfont($pdf, "arialMyName", "host", 0);
- //Set the font for the document
- pdf_setfont($pdf, $FontArial, 10);
//Choose a font you wish to use $FontDir = "C:\WINDOWS\Fonts"; //Directory where the font files are pdf_set_parameter($pdf, "FontOutline", "arialMyName=$FontDir\arial.ttf"); $FontArial = pdf_findfont($pdf, "arialMyName", "host", 0); //Set the font for the document pdf_setfont($pdf, $FontArial, 10);
The set paramter function basically makes a variable to use only within the PDFLib. It takes the $pdf resource first, next the parameter type, then the name of the variable and finally followed by an equal sign, the value.
pdf_findfont looks for the font and makes sure it is available for use. And lastly pdf_setfont function loads the font to be used in the pdf document.
- //Write some contents
- pdf_show_xy($pdf, "\"The only real valuable thing is intuition.\"", 50, 220);
- pdf_show_xy($pdf, "\"Imagination is more important than knowledge.\"",40, 200);pdf_show_xy($pdf, "- Albert Einstein.",40, 180);
//Write some contents pdf_show_xy($pdf, "\"The only real valuable thing is intuition.\"", 50, 220); pdf_show_xy($pdf, "\"Imagination is more important than knowledge.\"",40, 200);pdf_show_xy($pdf, "- Albert Einstein.",40, 180);
pdf_show_xy writes the text in the specified X,Y cordinate of the PDF document. The X, and Y cordinates starts from bottom left. So, line 180 will be the third line and line 220 will be first.
And here are the last few lines to complete our PDF generator.
- //Publish the page
- pdf_end_page($pdf);
- //Close the documentpdf_close($pdf);
- ?>
//Publish the page pdf_end_page($pdf); //Close the documentpdf_close($pdf); ?>
Tomorrow I will show you how to do some advanced pdf generation with PHP. This is the end of Basic pdf file generation for today. You can also subscribe to my RSS Feed or Email subscription to get notification when I publish the advanced tutorials.
Recommended Tutorials
- Detect Browser with PHP the Easy Way
- Securing a PHP Application
- PHP Pattern Matching with Regular Expressions
- PHP and AJAX Calendar Date Picker
Full Source
- <?php
- $DocumentType = array('A0' => array('width' => 2380,
- 'height' => 3368),
- 'A1' => array('width' => 1684,
- 'height' => 2380),
- 'A2' => array('width' => 1190,
- 'height' => 1684),
- 'A3' => array('width' => 842,
- 'height' => 1190),
- 'A4' => array('width' => 595,
- 'height' => 842),
- 'A5' => array('width' => 421,
- 'height' => 595),
- 'A6' => array('width' => 297,
- 'height' => 421),
- 'B5' => array('width' => 501,
- 'height' => 709),
- 'letter' => array('width' => 612,
- 'height' => 792),
- 'legal' => array('width' => 612,
- 'height' => 1008),
- 'ledger' => array('width' => 1224,
- 'height' => 792),
- '11/17inches' => array('width' => 792,
- 'height' => 1224));
- $width = $DocumentType['A6']['width'];
- $height = $DocumentType['A6']['height'];
- $pdf = pdf_new();
- //Create or write a pdf document
- pdf_open_file($pdf, "C:\wamp\www\pdfgeneration\greatquotes.pdf");
- //Create a New Page
- pdf_begin_page($pdf, $width, $height);
- //Choose a font you wish to use
- $FontDir = "C:\WINDOWS\Fonts";
- pdf_set_parameter($pdf, "FontOutline", "arialMyName=$FontDir\arial.ttf");
- $FontArial = pdf_findfont($pdf, "arialMyName", "host", 0);
- //Set the font for the document
- pdf_setfont($pdf, $FontArial, 10);
- //Write some contents
- pdf_show_xy($pdf, "\"The only real valuable thing is intuition.\"", 50, 220);
- pdf_show_xy($pdf, "\"Imagination is more important than knowledge.\"",40, 200);
- pdf_show_xy($pdf, "- Albert Einstein.",40, 180);
- //Publish the page
- pdf_end_page($pdf);
- //Close the document
- pdf_close($pdf);
- echo "<a href='./greatquotes.pdf'>View the generated document here.</a>";
- ?>
discuss this topic to forum
