Documents and Pages

A PDF document is made up of a number of pages. Each page contains text and/or images. This section shows you how to make a document, create pages in that document, put text onto the pages, and send the pages back to the browser when you’re done.

Note

The examples in this chapter assume that you have at least the Adobe PDF document viewer installed as an add-on to your web browser. These examples will not work otherwise. You can get the add-on from the Adobe website.

A Simple Example

Let’s start with a simple PDF document. Example 10-1 simply places “Hello Out There!” on a page and then displays the resulting PDF document.

Example 10-1. “Hello Out There!” in PDF

<?php
require("../fpdf/fpdf.php"); // path to fpdf.php

$pdf = new FPDF();
$pdf->addPage();

$pdf->setFont("Arial", 'B', 16);
$pdf->cell(40, 10, "Hello Out There!");

$pdf->output();

Example 10-1 follows the basic steps involved in creating a PDF document: creating a new PDF object instance, creating a page, setting a valid font for the PDF text, and writing the text to a “cell” on the page. Figure 10-1 shows the output of Example 10-1.

“Hello Out There!” PDF example

Figure 10-1. “Hello Out There!” PDF example

Initializing the Document

In Example 10-1, we started by making reference to the FPDF library with the require function. Then the code created a new instance of the FPDF object. You will note that all the calls to the new FPDF instance are ...

Get Programming PHP, 3rd Edition 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.