HomePHP Generate PDF Documents with PHP on the Windows Platform
Generate PDF Documents with PHP on the Windows Platform
If you're a PHP programmer who has tried to generate PDF documents on the Windows platform, you may have run into some very irritating issues. This article will discuss those issues, show you how to generate PDF documents that can be viewed on a desktop and through a browser, and help you troubleshoot if you run into problems.
Try Googling for "generate pdf with php." You can probably find hundreds of articles with many sample codes here and there.
However, if you try these sample codes on a Windows platform, you will most likely get the following error:
"PHP Fatal error: Uncaught exception 'PDFlibException' with message 'Metrics data for font 'Arial' not found'. "
So where and how do you specify this "Metrics data for font" that is required to generate the PDF documents with PHP on Windows?
Setting up the PDF library
Before you can generate PDF documents, first you need to make sure the php_pdf.dll containing the PDFLib library is properly set up.
You can find a copy of php_pdf.dll in the download area of the official PHP web site.
If you are running a thread-safe version of PHP, download the “PECL 5.2.5 Win32 binaries” file. If you are running a non thread-safe version of PHP, download the “PECL 5.2.5 Non-thread-safe Win32 binaries” file. If you do not know whether your PHP is thread-safe or non thread-safe, just try one of these first. If it doesn't work, try the other.
Unzip the file to a folder. You will find a lot of dlls in there. You only need one of these -- the php_pdf.dll. Copy just that file to the “ext” folder where you have installed your PHP.
Next open up php.ini in your favorite editor and add the following line:
Now we are ready to generate pdf documents using PHP on Windows.
Notes on the different versions of php_pdf.dll
One of the major problems people face is that there are so many different versions of php_pdf.dll floating around on the Internet. Also, different people are running different versions of PHP on their servers. On top of this, there are the thread-safe and non thread-safe compilations of PHP and the dll. The two need to match.
This is complicated by the fact that the PDFLib library is now maintained by PDFlib GmbH. While the library is free for testing and developing purposes, you need to purchase a license for commercial use. If you go to PDFlib GmbH, you can see that the latest version of PDFLib is now version 7. However, the php_pdf.dll available from the official PHP site is still version 5.
This partly explains why many of the sample codes you found on the Internet and the official PHP site do not work. Those examples might work on Linux, as it is easy for Linux users to recompile PHP with the latest pdf library. But they might not work on Windows, as most of the available php_pdf.dll is relatively outdated.