Page 313 - Open Soource Technologies 304.indd
P. 313

Unit 12: Portable Document Format



                                                                                                  Notes
                                   Figure 12.12: Pattern Filling a Circle


































            12.4.4 Templates

            It is common to have parts of a document, such as header/footer sections or background
            watermarks, repeated on multiple pages. It would be trivial to write a little PHP function to
            generate such things on each page, but if you did this the final PDF file would end up containing
            the same sequence of PDF calls on every page. PDF has built-in functionality known as “Form
            XObjects” (renamed “Templates” in pdflib) to more efficiently handle repeating elements.
            To create a template, simply call pdf_begin_template( ), perform the various operations to create
            the PDF components you want this template to contain, then call pdf_end_template( ). It is a
            good idea to do a pdf_save( ) right after beginning the template and a pdf_restore( ) just before
            ending it to make sure that any context changes you perform in your template don’t leak out
            of this template into the rest of the document.

            The pdf_begin_template( ) function takes the dimensions of the template and returns a handle
            for the template:

            $template = pdf_begin_template(pdf, width, height);
            The pdf_end_template( ), pdf_save( ), and pdf_restore( ) functions take no arguments beyond
            the pdf handle:

            pdf_end_template(pdf); pdf_save(pdf); pdf_restore(pdf);
            The below Example uses templates to create a two-page document with the PHP logo in the
            top-left and top-right corners and the title “pdf Template Example” and a line at the top of each
            page. If you wanted to add something like a page number to your header, you would need to
            do that on each page. There is no way to put variable content in a template.





                                             LOVELY PROFESSIONAL UNIVERSITY                                   307
   308   309   310   311   312   313   314   315   316   317   318