• home
  • forum
  • my
  • kt
  • download
  • Create PDF with PHP

    Author: 2009-04-17 11:25:12 From:

    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
    view plaincopy to clipboardprint?
    1. <?php echo phpinfo(); ?>  

    Now look for the line below:

    pdf

    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.

    view plaincopy to clipboardprint?
    1. <?php   
    2.   
    3. $DocumentType = array('A0' => array('width' => 2380,   
    4.                                      'height' => 3368),   
    5.                        'A1' => array('width' => 1684,   
    6.                                      'height' => 2380),   
    7.                        'A2' => array('width' => 1190,   
    8.                                      'height' => 1684),   
    9.                        'A3' => array('width' => 842,   
    10.                                      'height' => 1190),   
    11.                        'A4' => array('width' => 595,   
    12.                                      'height' => 842),   
    13.                        'A5' => array('width' => 421,   
    14.                                      'height' => 595),   
    15.                        'A6' => array('width' => 297,   
    16.                                      'height' => 421),   
    17.                        'B5' => array('width' => 501,   
    18.                                      'height' => 709),   
    19.                        'letter' => array('width' => 612,   
    20.                                      'height' => 792),   
    21.                        'legal' => array('width' => 612,   
    22.                                      'height' => 1008),   
    23.                        'ledger' => array('width' => 1224,   
    24.                                      'height' => 792),   
    25.                        '11/17inches' => array('width' => 792,   
    26.                                      'height' => 1224));  

    Next, I will store my desired height and width in a separate variable for easy access.

    view plaincopy to clipboardprint?
    1. $width = $DocumentType['A6']['width'];   
    2. $height = $DocumentType['A6']['height'];  

    Now comes the fun part.

    view plaincopy to clipboardprint?
    1. $pdf = pdf_new();   
    2.   
    3. //Create or write a pdf document   
    4. pdf_open_file($pdf"C:\wamp\www\pdfgeneration\greatquotes.pdf");   
    5.   
    6. //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.

    view plaincopy to clipboardprint?
    1. //Choose a font you wish to use   
    2.   
    3. $FontDir = "C:\WINDOWS\Fonts"//Directory where the font files are   
    4.   
    5. pdf_set_parameter($pdf"FontOutline""arialMyName=$FontDir\arial.ttf");   
    6. $FontArial = pdf_findfont($pdf"arialMyName""host", 0);   
    7.   
    8. //Set the font for the document   
    9. 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.

    view plaincopy to clipboardprint?
    1. //Write some contents   
    2. pdf_show_xy($pdf"\"The only real valuable thing is intuition.\"", 50, 220);   
    3. 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.

    view plaincopy to clipboardprint?
    1. //Publish the page   
    2. pdf_end_page($pdf);   
    3.   
    4. //Close the documentpdf_close($pdf);   
    5.   
    6. ?>  

    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

    view plaincopy to clipboardprint?
    1. <?php   
    2.   
    3. $DocumentType = array('A0' => array('width' => 2380,   
    4.                                      'height' => 3368),   
    5.                        'A1' => array('width' => 1684,   
    6.                                      'height' => 2380),   
    7.                        'A2' => array('width' => 1190,   
    8.                                      'height' => 1684),   
    9.                        'A3' => array('width' => 842,   
    10.                                      'height' => 1190),   
    11.                        'A4' => array('width' => 595,   
    12.                                      'height' => 842),   
    13.                        'A5' => array('width' => 421,   
    14.                                      'height' => 595),   
    15.                        'A6' => array('width' => 297,   
    16.                                      'height' => 421),   
    17.                        'B5' => array('width' => 501,   
    18.                                      'height' => 709),   
    19.                        'letter' => array('width' => 612,   
    20.                                      'height' => 792),   
    21.                        'legal' => array('width' => 612,   
    22.                                      'height' => 1008),   
    23.                        'ledger' => array('width' => 1224,   
    24.                                      'height' => 792),   
    25.                        '11/17inches' => array('width' => 792,   
    26.                                      'height' => 1224));   
    27.   
    28. $width = $DocumentType['A6']['width'];   
    29. $height = $DocumentType['A6']['height'];   
    30.   
    31. $pdf = pdf_new();   
    32.   
    33. //Create or write a pdf document   
    34. pdf_open_file($pdf"C:\wamp\www\pdfgeneration\greatquotes.pdf");   
    35.   
    36. //Create a New Page   
    37. pdf_begin_page($pdf$width$height);   
    38.   
    39. //Choose a font you wish to use   
    40. $FontDir = "C:\WINDOWS\Fonts";   
    41. pdf_set_parameter($pdf"FontOutline""arialMyName=$FontDir\arial.ttf");   
    42. $FontArial = pdf_findfont($pdf"arialMyName""host", 0);   
    43.   
    44. //Set the font for the document   
    45. pdf_setfont($pdf$FontArial, 10);   
    46.   
    47. //Write some contents   
    48. pdf_show_xy($pdf"\"The only real valuable thing is intuition.\"", 50, 220);   
    49. pdf_show_xy($pdf"\"Imagination is more important than knowledge.\"",40, 200);   
    50. pdf_show_xy($pdf"- Albert Einstein.",40, 180);    
    51.   
    52. //Publish the page   
    53. pdf_end_page($pdf);   
    54.   
    55. //Close the document   
    56. pdf_close($pdf);   
    57.   
    58. echo "<a href='./greatquotes.pdf'>View the generated document here.</a>";   
    59.   
    60. ?>  

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (46)
      Cookies and Sessions (12)
      Counters (16)
      Database Related (36)
      Date and Time (15)
      Development (26)
      Discussion Boards (8)
      E Commerce (9)
      Email Systems (15)
      Error Handling (8)
      File Manipulation (38)
      Flash and PHP (6)
      Form Processing (25)
      Guestbooks (13)
      Image Manipulation (26)
      Installing PHP (7)
      Introduction to PHP (30)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (60)
      Networking (9)
      News Publishing (9)
      OOP (28)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (7)
      Postcards (1)
      Randomizing (15)
      Redirection (12)
      Searching (10)
      Security (32)
      Site Navigation (16)
      User Authentication (16)
      WAP and WML (7)
      Web Fetching (10)
      Web Traffic Analysis (15)
      XML and PHP (18)

    New

    Hot