As with any other programming language, the best way to start exploring the PHP PDF library is a hello world example. First, we create a new pdf document named "helloworld.pdf": $pdf = pdf_new(); pdf_open_file($pdf, 'helloworld.pdf'); Then we start a new page: pdf_begin_page($pdf, 612, 792); // Letter size In case you are wondering why we're using the strange numbers of 612 and 792, these are the dimensions for a letter size paper. A letter size paper is 8.5” by 11”. The 612 is obtained by 8.5 * 72. Similarly, 11 * 72 = 792. Hence the two numbers. If you need to produce an A4-size pdf document, use: pdf_begin_page($pdf, 595, 842); // A4 size An A4-size paper is 210mm x 297mm, which is 8.27” by 11.69”. Multiply these by 72, and you get 595 by 842. After we have created a new page, before we can start printing any text on the new page, we need to set up the font first. pdf_set_parameter($pdf, 'FontOutline', 'Arial=c:windowsfontsarial.ttf'); $font = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf, $font, 48); We are now ready to print “hello world” on the page: pdf_show_xy($pdf, "hello world, PDF!", 50, 680); And we wrap up the pdf document with: pdf_end_page($pdf); pdf_close($pdf); pdf_delete($pdf); Below is the complete code. Copy the code below and save it in a file, say helloworld_pdf.php. <?php # helloworld_pdf // create a new pdf document $pdf = pdf_new(); pdf_open_file($pdf, 'helloworld.pdf'); // start a new page (Letter size) pdf_begin_page($pdf, 612, 792); // Letter size // 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, 48); pdf_show_xy($pdf, "hello world, PDF!", 50, 680); // done pdf_end_page($pdf); pdf_close($pdf); pdf_delete($pdf); echo "helloworld.pdf has been generatedn"; ?> To run the code above, open a command window and cd to the directory where you saved the script. Suppose you have installed your PHP in c:php, and you have saved the script as helloworld_pdf.php. From the command prompt, type c:phpphp helloworld_pdf.php You should now have a new file named “helloworld.pdf” created in the folder where you run the script. Double click the pdf file. You will see the pdf document as shown below.
Specifying the font information The most important line in this example is: pdf_set_parameter($pdf, 'FontOutline', 'Arial=c:windowsfontsarial.ttf'); This is the "magic" line that specifies the font information. This is also the line that will fix the following error which many people have encountered: "PHP Fatal error:” Uncaught exception 'PDFlibException' with message 'Metrics data for font 'Arial' not found' " You can select any Windows truetype fonts on your system. For example, if you want to use Times New Roman Bold Italic, replace the codes with: pdf_set_parameter($pdf, 'FontOutline', ' Times New Roman Bold Italic =c:windowsfontsTIMESI.TTF'); $font = pdf_findfont($pdf, "Times New Roman Bold Italic", "host", 1); Run the script again, and you will get the following PDF document:
blog comments powered by Disqus |
|
|
|
|
|
|
|