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() ...

Get PHP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.