Jul
6th
 

Generating PDF Files Using PHP

Posted by admin in PHP

Creating PDF files with PHP is actually much less confusing than it may at first sound. One extension in PHP 4, named the PDFLib extension, enables you to dynamically create PDF files with the PHP scripts that you have.

If you have Windows, you already have a pre-installed PDF library that came with your package, so all you need to do is uncomment, or delete the # sign, from the appropriate lines. Linux users can download this extension at the website, http://www.pdflib.com/, but no matter which type of system you are working with, you need to have this installed before you can start. You also need to have Adobe Acrobat PDF reader installed on your computer. If you do not have this, you can easily download it for free at http://www.adobe.com/.

To read existing PDF files with PHP, you can also install the XPDF package on the website, http://www.foolabs.com/xpdf/about.html. This program includes “pdftotext”.

If you have these programs, you are ready to begin. First, you need to create a simple PDF file and save it to your computer.

// create handle for new PDF document
$pdf = pdf_new();
 
// open a file
pdf_open_file($pdf, "dogs.pdf");
 
// start a new page (A4)
pdf_begin_page($pdf, 550, 860);
 
// set a fill colour
pdf_setcolor($pdf, "fill", "rgb", 1, 1, 0);
 
// set a stroke colour
pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);
 
// draw a rectangle
pdf_rect($pdf, 45, 480, 210, 320);
pdf_fill_stroke($pdf);
 
// set a fill colour
pdf_setcolor($pdf, "fill", "rgb", 0, 1, 0);
 
// set a stroke colour
pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 1);
 
// get and use a font object
$arial = pdf_findfont($pdf, "verdana", "host", 1); pdf_setfont($pdf, $verdana, 12);
 
// print text
pdf_show_xy($pdf, "Dogs have made excellent companions to humans for hundreds of years,",50, 750);
pdf_show_xy($pdf, "so much so that they have been known as \"man's best friend\"", 60,745);
 
// add an image under the text
$image = pdf_open_image_file($pdf, "jpeg", "goldenretriever.jpg");
pdf_place_image($pdf, $image, 40, 700, 0.75);
 
// end page
pdf_end_page($pdf);
 
// close and save file
pdf_close($pdf);

The next step is to locate the file through your web browser. Once you have done this, PHP will implement the script, and a new PDF file will be created and stored on your computer in the particular location listed at the top of the document.

Listed are the basic steps to generating a PDF file using PHP:

  • First, you need to create a handle for the PDF script, which is also shown in the first line of the code above. Returning a handle to the document is done through the pdf_new() function.
  • Secondly, you have to name the file (second line of the code). This is done through the pdf_open_file() function. This requires the handle that was returned from the previous step, along with a file name that you create.
  • In the next step (third and sixth lines of the code), you are able to insert new pages into the document and end them with the pdf_begin_page() and pdf_end_page() functions. In between these two functions is the code that adds something into the document, including images, text, and geometric shapes.
  • Choosing and registering a font (fourth line of code) is done through the pdf_findfont() function, which is the selection part and requires the font name, the encoding that is to be used, and the Boolean values (the numbers or text that convert to “true”, or permit, and “false”, or not permit) and pdf_setfont(), which actually registers the font.
  • After the font is selected and registered, next is writing a line of text (fifth line). This is done through the pdf_show_xy() function. This requires a handle to the PDF file, a mention of the font type that is to be used in the document, the line of text that you want added into the document, and the X and Y coordinates, which give the points at where you want the text to start at. The coordinate numbers are based from the origin, which is located at (0,0).
  • The final step (seventh line) is to close the document, which is done through the pdf_close() function. This will also save the file to the location specified in the second step with the pdf_open_file() function and will get rid of the handle that was created in the document.

The PDFLib extension will not only allow you to write text to a page, but will also enable you to insert images, which occurs through the the pdf_open_image_file() and pdf_place_image() functions and geometric shapes, such as lines and circles. The drawing of a line, for example, will happen through pdf_moveto(), pdf_lineto() and pdf_stroke() functions.

Another addition you can insert into your document is a grid. This code shows how to draw a rectangular grid using horizontal and vertical lines:

// create new handle
$pdf = pdf_new();
 
// open PDF file
pdf_open_file($pdf, "my_grid.pdf");
 
// new page (A4)
pdf_begin_page_ext($pdf, 595, 842);
 
// set a color
pdf_setcolor($pdf, "stroke", "rgb", 0, 0, 0);
 
// draw vertical lines
for ($x=0; $x<=595; $x+=25) {
    pdf_moveto($pdf, $x, 0);
    pdf_lineto($pdf, $x, 842);
    pdf_stroke($pdf);
}
 
// draw horizontal lines
for ($y=0; $y<=842; $y+=25){
    pdf_moveto($pdf, 0, $y);
    pdf_lineto($pdf, 595, $y);
    pdf_stroke($pdf);
}
 
// finish page
pdf_end_page($pdf);
 
// close and save PDF document
pdf_end_document($pdf);

Once you know the steps and coding formats, creating PDF files with PHP is really quite simple and does not involve as much as some may first perceive.




Leave a Reply

Search



Categories