Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 16 ■ ADVANCED ZEND FRAMEWORK^255

//Create a new PDF and add a letter-sized page
$pdf = new Zend_Pdf();
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_LETTER);

//Get a reference to that page
$page = $pdf->pages[0];

//Create a graphics style and set options
$style = new Zend_Pdf_Style();

//Set the font color to black
$style->setLineColor(new Zend_Pdf_Color_Rgb(0,0,0));

//Load a font and add it to the style at size 12
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$style->setFont($font, 12);

//Apply the style
$page->setStyle($style);

//Draw text offset 100 x 100 from the top left of the page
$page->drawText('Your first dynamic PDF', 100, ($page->getHeight()-100));

/*
Make sure there are no previous echoes or prior information
to wreck the PDF output. You need a clean output buffer
when outputting binary data.
*/
$response->clearBody();

//Output the PDF
$response->setBody($pdf->render());
}

Drawing images is slightly more complicated. Because PDF is a print format, PDF docu-
ments work on a 1:72 inches:points ratio system, which translates 1:1 with standard screen-
resolution graphics. However, for print-resolution graphics, you must take the dots per inch
(DPI) of the image into account when drawing. Listing 16-22 shows how to correctly draw an
image based on a set image DPI.

Listing 16-22. Drawing Images at a Specific DPI

//Create an image object
$imageFile = dirname(__FILE__). '/test.jpg';
$image = Zend_Pdf_Resource_ImageFactory::factory($imageFile);

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

Free download pdf