Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Adding More Pages and More Style

Adding more pages is done by calling pdf_begin_page() and pdf_end_page() repeatedly, like this:

    for ($i = 1; $i < 10; ++$i) {
            pdf_begin_page($pdf, 595, 842);
            pdf_setfont($pdf, $font, 30);
            pdf_show_xy($pdf, "This is page $i", 50, 750);
            pdf_end_page($pdf);
    }

A good start is to have a selection of typefaces ready for various parts of your document. In our first example, we have just one—Times-Roman is stored in $font. However, that could be easily modified to this:

    $times = pdf_findfont($pdf, "Times-Roman", "host");
    $timesb = pdf_findfont($pdf, "Times-Bold", "host");
    $timesi = pdf_findfont($pdf, "Times-Italic", "host");

Combined with the use of pdf_setfont()'s third parameter, we can create headers and subheaders like this:

    for ($i = 1; $i < 10; ++$i) {
            pdf_begin_page($pdf, 595, 842);

            pdf_setfont($pdf, $times, 24);
            pdf_show_xy($pdf, "This is page $i", 50, 750);

            pdf_setfont($pdf, $timesb, 16);
            pdf_show_xy($pdf, "Subheader", 100, 700);

            pdf_setfont($pdf, $timesi, 16);
            pdf_show_xy($pdf, "This is some standard text.", 100, 700);

            pdf_end_page($pdf);
    }

We can even throw in the pdf_setcolor() function, which takes two text values followed by color values for its fourth, fifth, sixth, and (optionally) its seventh parameters, and uses them to set the color of fills and objects that follow.

Try adding this line just before the first pdf_setfont()...

    pdf_setcolor($pdf, "both", "rgb", 1.0 - (0.1 * $i), 0.0, 0.0);

And adding this line just before the second pdf_setfont() ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page