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.
blog comments powered by Disqus |
|
|
|
|
|
|
|