PHP
  Home arrow PHP arrow Page 4 - Generate PDF Documents with PHP on the Windows Platform
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Generate PDF Documents with PHP on the Windows Platform
By: K.K.Sou
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-09-16


    Table of Contents:
  • Generate PDF Documents with PHP on the Windows Platform
  • Hello world PDF
  • Finding the filename of the truetype font
  • Generating PDF documents for browsers
  • Producing a simple bar graph in a PDF document

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Generate PDF Documents with PHP on the Windows Platform - Generating PDF documents for browsers
    ( Page 4 of 5 )

    Now that we are able to generate PDF documents through the command prompt, let us move on to generate PDF documents for browsers.

    Copy the code below and save it in a file on your web server.


    <?php

    # pdf_example3

    # for the browser


    // create a new pdf document

    $pdf = pdf_new();

    $filename = 'c:pdf_example3.pdf';

    pdf_open_file($pdf, $filename);


    // start a new page (Letter size)

    pdf_begin_page($pdf, 612, 792);


    // setup font and print hello world

    pdf_set_parameter($pdf, 'FontOutline',

    'Arial=c:windowsfontsarial.ttf');

    $font = pdf_findfont($pdf, "Arial", "host", 1);

    pdf_setfont($pdf, $font, 24);

    pdf_show_xy($pdf, "pdf_example3 for the browser", 50, 680);


    // done

    pdf_end_page($pdf);

    pdf_close($pdf);

    pdf_delete($pdf);


    // send it to browser

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

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

    readfile($filename);

    unlink($filename);

    ?>


    You will find that the first part of the code is exactly the same as the command line version of the hello world example.

    Once the PDF file is generated, all we need to do is to push the content to the browser:


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

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

    readfile($filename);


    Note that the pdf file 'c: pdf_example3.pdf ' is a temporary one. You can save this in any directory you want. Just make sure that the web server has write permission to that directory. Once you have pushed it to the browser, you can delete this file with:


    unlink($filename);


    Launch the page in your browser. You should see the PDF document displayed right inside your browser as shown below:




    Troubleshooting

    Below are described some of the most common errors encountered by users.

    If you get the following error message:


    PHP Warning: PHP Startup: pdf: Unable to initialize module

    Module compiled with module API=20060613, debug=0, thread-safety=0

    PHP compiled with module API=20060613, debug=0, thread-safety=1

    These options need to match


    It means your PHP is the thread-safe version, but the php_pdf.dll is non thread-safe. Download the thread-safe version of php_pdf.dll from the official PHP site. It should fix the problem.

    If you get the following error message:


    PHP Fatal error: Call to undefined function pdf_new() in helloworld_pdf.php on line 3


    It means your php_pdf.dll has not been loaded. Check your php.ini and see if you have added the following line:

    extension=php_pdf.dll


    If the above line already exists and you still get the error, most likely it means the php_pdf.dll is not compatible with your version of PHP. There are many versions of php_pdf.dll floating around on the Internet. Try downloading some of those and see if it works.

    There are some compiled php_pdf.dll’s (for older versions of PHP) on pecl4win.php.net.


    If you get the following error message:

    PHP Fatal error: Uncaught exception 'PDFlibException' with message 'Couldn't open font file 'c:windowsfontsarial1.ttf' for reading (file not found)' in helloworld_pdf.php

    It means your have misspelled the name of the font, or the font does not exist in your Windows font folder.

    If you get the following error message:


    PHP Fatal error: Uncaught exception 'PDFlibException' with message 'Function must not be called in 'object' scope' in helloworld_pdf.php

    Check to see if the output pdf file is still open in your Adobe Acrobat Reader. If it is indeed open in the Acrobat Reader, close the file and try again. The error message should go away.

    Lastly, suppose the hello world example given above runs fine from the command line. However, you see nothing when you run the code from the browser.

    First, try turning on error reporting in your php.ini and see if there is any error message displayed.

    Check to see if the web server has write permission to the directory of the temporary pdf file. I have used 'c:pdf_example3.pdf' in the example above. So check if your web server has write permission to c:

    You may also comment the line: unlink($filename). Reload the page, and verify that the pdf file pdf_example3.pdf has indeed been generated.



     
     
    >>> More PHP Articles          >>> More By K.K.Sou
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT