Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^254) CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK
is created and populated with Zend_Pdf_Page objects in a numerical index, with zero being
page one.


Creating New PDF Pages.


You can create new pages in two ways:


  • Create a new instance of Zend_Pdf_Page. This creates a page that is independent of a
    document.


•Use the $pdf->newPage() method. This creates an attached page.

The distinction between an independent and attached page is internal. It relates to the
sharing of resources like fonts between multiple pages in the same document.
The framework offers four default page sizes: SIZE_ A4, A4_LANDSCAPE, LETTER, and
LETTER_LANDSCAPE. Listing 16-20 demonstrates adding a new page to a PDF instance.

Listing 16-20. Adding a Page to a PDF Instance

$pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);

//Alternatively
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_LETTER);

//Make a copy of page 1 and add it to the PDF
$pdf->pages[] = new Zend_Pdf_Page($pdf->pages[0]);

If you wish to remove a page, simply unset the array index in the pages collection.

Drawing on PDF Pages.


The next step is drawing on a page. With PDFs, drawing coordinates start from the bottom left
as 0,0 instead of the top left, as web developers will expect. For drawing in the y axis on a PDF
page, you need to think of plus as minus and zero as the bottom.
You can draw shapes and lines, as well as fonts and images. All the drawing systems work
on a graphics style system, which allows you to set line and fill colors, widths, and patterns.
Listing 16-21 shows some of the options for PDF drawing.

Listing 16-21. Drawing to a PDF (in IndexController.php)

public function pdfAction() {
//Disable the view renderer
$this->getHelper('ViewRenderer')->setNoRender();

//Get the response object and set content type http header
$response = $this->getResponse();
$response->setHeader('Content-Type', 'application/pdf');

McArthur_819-9C16.fm Page 254 Friday, February 29, 2008 5:07 PM

Free download pdf