Приглашаем посетить
Html (html.find-info.ru)

Section 17.1.  Getting Started

Previous
Table of Contents
Next

17.1. Getting Started

Creating a PDF document is similar to creating a picture in that, to get the desired end result, you state the list of drawing actions required to get theredrawing lines, text, adding fonts, etc. You need to track the PDF document you are working with at all times, because other PDF functions use it.

Even creating a simple PDF takes quite a few functions; this next code block does comparatively little:

    $pdf = pdf_new( );
    pdf_open_file($pdf, "/path/to/your.pdf");
    $font = pdf_findfont($pdf, "Times-Roman", "host");

    pdf_begin_page($pdf, 595, 842);
    pdf_setfont($pdf, $font, 30);
    pdf_show_xy($pdf, "Printing text is easy", 50, 750);
    pdf_end_page($pdf);

    pdf_close($pdf);
    pdf_delete($pdf);

Starting at line one, we use pdf_new( ) to create a new PDF document and store it in $pdf. This value will be used in all the subsequent functions, so it is important to keep.

The pdf_open_file( ) function is used to open a file for writing. Note that the free version of PDFlib does not allow alteration of existing PDFs; this function merely creates a new PDF of the given filename. Naturally, it will need to be somewhere your web server is able to write to; otherwise, you will receive an error along the lines of "Fatal error: PDFlib error: function 'PDF_set_info' must not be called in 'object' scope in yourscript.php on line XYZ".

The next line uses pdf_findfont( ) to find and load a font for use inside the generated PDF file. In the example, pdf_findfont( ) takes three parametersthe PDF document to work with, the name of the font to use, and which encoding to use. In the example above, $pdf is specified as the first parameter (as always). "Times-Roman" is specified as the font to use, which is one of the 14 standard internal PDFlib fonts. The next parameter can be set to either "winansi" (Windows), "macroman" (Macintosh), "ebcdic" (EBCDIC code page 1047 machines), "builtin" (for symbol fonts), or "host" (winansi for Windows, macroman for Macintosh, etc.; recommended).

When successful, pdf_findfont( ) returns a font resource which is stored in $font. You may wish to add error checking in your own scripts for extra reliability.

At this point, we're ready to start on the main part of PDF generation. The first three lines merely set things up for the document. The next fourlines four to sevenare the page itself. Reading the source, it should be easy to see that line four and line seven encapsulate one page in the generated PDF file. Objects and text outputted between a pdf_begin_page( ) and pdf_end_page( ) will affect that page, and multiple begin/end blocks are used to create multiple pages.

Note that pdf_begin_page( ) takes a second and third parameter: the X and Y point size of this page. The PDF format allows you to make your pages different point sizes from page to page, but you will most often want to choose one size and stick with it.

You need to pass three parameters to pdf_setfont( ): the first is the PDF resource, as usual; the second parameter is the return value from pdf_findfont for the font you wish to use; and the final parameter is the size to use, in points. Immediately afterward, we call pdf_show_xy( ) to place text into our page. Parameter two of pdf_show_xy( ) is the string to use, and parameters three and four are the X and Y coordinates at which to print the text.

Section 17.1.  Getting Started

Confusingly, there is a pdf_set_font( ) function that is deprecatedtry not to get mixed up!


The last parameter passed to pdf_show_xy( ) is the distance the text should appear above the page baseline in points. That is, setting this parameter to 0 will have the bottom of a lowercase "a" at the very bottom of the page, and the bottom of a lowercase "y" outside the margins of the page.

With pdf_end_page( ) called, the first and only page is completed, and all that is left to do is clean things up. This is done through the help of two functions, which are pdf_close( ) and pdf_delete( ). They may sound somewhat similar, but you do need to call them both: pdf_close( ) cleans up the PDFlib memory and document-related resources, whereas pdf_delete( ) cleans up PHP's reference to $pdf and any other internal resources. Be sure to call them in the order shown above.

When you run that script through your web browser, you won't see any "Success!" message printed out. However, you should find your PDF file has been created and is viewable in your PDF reader of choice.


Previous
Table of Contents
Next