Creating PDF Documents with Zend Framework


Portable Printing

First released in 1993, Adobe’s Portable Document Format (PDF) is now ubiquitous. It’s a great way to create printable documents that work across multiple platforms without any loss of fidelity, and it also supports interactive features like bookmarks, annotations and forms.

PHP comes with a number of options to help developers dynamically generate PDF files from within their applications. The Haru and PDFlib extensions offer a complete API for dynamic PDF generation, and there also exist a number of open-source PHP components that can be used for the same purpose. This article will introduce you to one such component, the Zend_Pdf component that ships as part of the Zend Framework, and illustrate how it can be used to perform sophisticated PDF operations from within a PHP application.

The Name Of The Rose

Before diving into the code, a few notes and assumptions. I’ll assume throughout this article that you have a working Apache/PHP/MySQL development environment, and that you have downloaded and installed the latest version of the Zend Framework. I’ll also assume that you know the basics of working with classes and objects in PHP.

The Zend_Pdf component is a “pure PHP” implementation of the PDF standard, and no external libraries are required to use it. This can come in handy when operating in a shared hosting environment that does not allow individual access to custom extensions. The component supports most common PDF operations, including adding and deleting pages; inserting text and images on pages; drawing and colorizing shapes; and updating document meta-data.

Let’s begin with a simple example that illustrates Zend_Pdf in action:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
  
  // define font resource
  $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  
  // set font for page
  // write text to page
  $page->setFont($font, 24)
       ->drawText('That which we call a rose,', 72, 720)
       ->drawText('By any other name would smell as sweet.', 72, 620);
  
  // add page to document
  $pdf->pages[] = $page;
  
  // save as file
  $pdf->save('example.pdf');
  echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

This script begins by setting up the Zend auto-loader, which takes care of automatically loading Zend Framework components as needed. It then initializes an instance of Zend_Pdf, and creates a new page as an instance of the Zend_Pdf_Page object. This object constructor is passed a constant indicating the size of the required page.

Next, a font resource is defined, as an instance of Zend_Pdf_Font. This may be either one of the 14 built-in font constants (look in the Zend_Pdf_Font class definition for a list of font constants), or an external font file. This font resource is then attached to the page with a call to the Zend_Pdf_Page object’s setFont() method.

The next step is to write some text to the page, which is easily accomplished with the Zend_Pdf_Page object’s drawText() method. This method accepts the string to be written, together with the X and Y coordinates of the page location where writing should begin. Note that the Zend_Pdf coordinate system uses Postscript geometry: the origin is the bottom left corner of the page, and units are measured in points, where 1 point = 1/72 inch.

The final step is to attach the page to the Zend_Pdf document, via the latter’s $pages array, and then save the result to disk with itssave() method. Here’s what the output looks like:

Pictures and Words

If you’d prefer to use a custom font, instead of the built-in fonts, you can do this using the Zend_Pdf_Font object’s static fontWithPath()method. This is illustrated in the following variant of the previous example:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
  
  // define font resource
  $font = Zend_Pdf_Font::fontWithPath('/tmp/comic.ttf');
  
  // set font for page
  // write text to page
  $page->setFont($font, 24)
       ->drawText('That which we call a rose,', 72, 720)
       ->drawText('By any other name would smell as sweet.', 72, 620);
  
  // add page to document
  $pdf->pages[] = $page;
  
  // save as file
  $pdf->save('example.pdf');
  echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

Here’s what the revised output looks like:

If you’d like to insert an image into the PDF document, this is easily accomplished with the drawImage() method, which accepts a Zend_Pdf_Image resource and a set of coordinates, and then writes the image to that position in the document. JPEG, PNG and TIFF images are currently supported, and the system will auto-detect the image type based on the file extension. Here’s an example of it in use:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
  
  // define image resource
  $image = Zend_Pdf_Image::imageWithPath('IMG_0497.jpg');
  
  // write image to page
  $page->drawImage($image, 50, 50, 400, 400);
  
  // add page to document
  $pdf->pages[] = $page;
  
  // save as file
  $pdf->save('example.pdf');
  echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

And here’s a sample of the output:

Wrapping Up

There’s one drawback to the drawText() method discussed in the previous section: it won’t automatically wrap text to the next line. This can create problems when you have a large block of text to be dynamically written to a PDF document. Fortunately, there’s a solution: a community enhancement to Zend_Pdf_Page by Nico Edtinger, which adds a few methods to handle this task.

These additional methods calculate the width of the string using the given font face and size, and then automatically determine where the string should be wrapped, given the width of the containing text block. Word boundaries are taken into account when making this determination. To see how it all works, replace your original Zend_Pdf_Page with the updated definition (remember to backup the original), and then try out the example below:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

// define long string
$str = "Mary had a little lamb. It's fleece was white as snow. And everywhere that Mary went, the lamb was sure to go";

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

  // define font resource
  $font = Zend_Pdf_Font::fontWithPath('/tmp/comic.ttf');

  // set font
  $page->setFont($font, 14);
  
  // wrap lines of text
  // start at (10,600) and use a block of dimensions 500x500
  $page->drawTextBlock($str, 10, 600, 500, 500, Zend_Pdf_Page::ALIGN_LEFT);  

  // wrap lines of text
  // start at (10,500) and use a block of dimensions 200x300
  $page->setFont($font, 20);
  $page->drawTextBlock($str, 10, 500, 200, 300, Zend_Pdf_Page::ALIGN_RIGHT);  
      
  // add page to document
  $pdf->pages[] = $page;
  
  // save as file
  $pdf->save('example.pdf');
  echo 'SUCCESS: Document saved!';  
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

This script introduces the custom drawTextBlock() method, which creates a text container that supports line wrapping. The method accepts the text string, the starting coordinates, width and height of the container, and an optional constant indicating how the text should be aligned. It then writes the text to this container, automatically wrapping long lines as needed. The method is intelligent enough to break lines at word boundaries as well. Here’s an example of the output:

The Shortest Distance Between Two Points

Why stop there? Zend_Pdf comes with a whole bag of functions designed to let you draw lines, circles and other shapes in your PDF document. Consider the following example, which demonstrates the process of drawing a line:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

  // define font resource
  $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  
  // set font for page
  $page->setFont($font, 10);
    
  // draw a line at the top of the page
  $page->setLineColor(new Zend_Pdf_Color_Rgb(0,0,0));
  $page->drawLine(10, 790, ($page->getWidth()-10), 790);
  
  // draw another line near the bottom of the page
  $page->drawLine(10, 25, ($page->getWidth()-10), 25);

  // define image resource
  $image = Zend_Pdf_Image::imageWithPath('logo.jpg');
  
  // write image to page
  $page->drawImage($image, 25, 800, ($image->getPixelWidth()+25), (800+$image->getPixelHeight()));
     
  // add footer text   
  $page->drawText('Copyright My Company 2010. All rights reserved.', ($page->getWidth()/3), 10);
  
  // add page to document
  $pdf->pages[] = $page;
    
  // save as file
  $pdf->save('example.pdf');
  echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

With Zend_Pdf, drawing a line is a one-step process: call the drawLine() method with the X and Y coordinates of the line’s start and end points, and Zend_Pdf will take care of drawing a line between those points. The line color is set via a call to the Zend_Pdf_PagesetLineColor() method, which accepts a Zend_Pdf_Color object corresponding to the required color. Zend_Pdf_Color objects can be defined using RGB, CMYK, hexadecimal or HTML color notation.

Now, look closely at the script above and you’ll see that it actually draws more than just a single line. It actually draws two lines, one in the page header and one in the page footer, and it also mixes things up a little by adding an image to the top left corner of the page and footer text to the bottom right corner, using methods you’ve seen in previous examples. Here’s an example of what the final output looks like:

Shape-Shifting

Zend_Pdf comes with methods to draw other two-dimensional shapes as well. Consider the next example, which uses thedrawRectangle() method to great effect:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

  // set line color
  $page->setLineColor(new Zend_Pdf_Color_Rgb(0,0,0));

  // draw a face
  $page->drawRectangle(100, 300, 500, 700, Zend_Pdf_Page::SHAPE_DRAW_STROKE);
  
  // draw the left eye
  $page->setFillColor(new Zend_Pdf_Color_Rgb(255,0,0));
  $page->drawRectangle(150, 600, 200, 650);

  // draw the right eye
  $page->setFillColor(new Zend_Pdf_Color_Rgb(255,0,0));
  $page->drawRectangle(400, 600, 450, 650);

  // draw the nose
  $page->setFillColor(new Zend_Pdf_Color_Rgb(0,255,0));
  $page->drawRectangle(260, 450, 340, 550);
      
  // draw the mouth
  $page->setFillColor(new Zend_Pdf_Color_Rgb(0,0,255));
  $page->drawRectangle(200, 350, 400, 400);
  
  // add page to document
  $pdf->pages[] = $page;
    
  // save as file
  $pdf->save('example.pdf');
  echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

The drawRectangle() method draws a rectangle using the supplied bottom left and top right coordinates. It can accept an additional constant that indicates whether the rectangle should be drawn in outline (stroked), filled or both. The stroke color is defined by thesetLineColor() method, while the fill color is defined by the setFillColor() method.

Here’s what the output looks like:

You can also draw rectangles, circles, ellipses and polygons. Take a look at the next example, which revises the previous one to add a few different shapes:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

  // set line color
  $page->setLineColor(new Zend_Pdf_Color_Rgb(0,0,0));

  // draw a face
  $page->drawRectangle(100, 300, 500, 700, Zend_Pdf_Page::SHAPE_DRAW_STROKE);
  
  // draw the left eye
  $page->setFillColor(new Zend_Pdf_Color_Rgb(255,0,0));
  $page->drawRectangle(150, 600, 200, 650);

  // draw the right eye
  $page->setFillColor(new Zend_Pdf_Color_Rgb(255,0,0));
  $page->drawRectangle(400, 600, 450, 650);

  // draw the nose
  $page->setFillColor(new Zend_Pdf_Color_Rgb(0,255,0));
  $page->drawCircle(300, 500, 50);
      
  // draw the mouth
  $page->setFillColor(new Zend_Pdf_Color_Rgb(0,0,255));
  $page->drawEllipse(200, 350, 400, 400);
  
  // draw a hat
  $page->setFillColor(new Zend_Pdf_Color_Rgb(250,250,0));
  $page->drawPolygon(array(125,300,475), array(700,800,700));
  
  // add page to document
  $pdf->pages[] = $page;
    
  // save as file
  $pdf->save('example.pdf');
  echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

Here’s what the output looks like:

And here’s a quick overview of the methods used:

  • The drawCircle() method draws a circle using the supplied center coordinates and radius length. A starting and ending angle can be optionally provided to adjust the shape of the circle.
  • The drawEllipse() method draws an ellipse within a rectangular box, using the supplied bottom left and top right box coordinates. A start and end angle can be optionally provided to adjust the ellipse angle.
  • The drawPolygon() method draws a polygon using the supplied X and Y coordinate arrays.

As before, all of these methods can be passed an additional constant indicating whether or not the shape should be filled.

Here’s another example, this one accepting a set of numeric values as a GET variable, and converting them into slices of a pie chart:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

// define pie radius
$radius = 100;

// define pie values
$slices = explode(',', $_GET['data']);

// calculate sum of pie values
$sum = array_sum($slices);

// convert each slice into corresponding percentage of 360-degree circle
for ($y = 0; $y < sizeof($slices); $y++) {
  $degrees[$y] = ($slices[$y] / $sum) * 360;
}

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

  // set font  
  $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  $page->setFont($font, 14);
  
  $lastAngle = 90;  
  for ($z=0; $zdrawLine(250, 250, $endX, $endY);
    
    // reset the last angle value
    $lastAngle = $lastAngle + $degrees[$z]; 
  }  
  
  // draw the circle outline
  $page->drawCircle(250, 250, 100, Zend_Pdf_Page::SHAPE_DRAW_STROKE);
  
  // add page to document
  $pdf->pages[] = $page;
  
  // save document
  $pdf->save('example.pdf');
  echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

This script begins by reading a comma-separated list of numbers from the $_GET request variable into an array, and then converting those numbers into percentages of a circle. Given the radius and center coordinates of the circle, it’s now possible to calculate the end-points of each pie slice, and draw line segments to these points from the center. Once all the slices are marked out, a circle is drawn around them to complete the pie.

Style Gallery

Zend_Pdf also introduces the concept of “styles”, which are essentially collections of pre-defined formatting rules for line color, fill color, font face, font size and line style. The setStyle() method allows developers to quickly switch between styles when creating PDF pages, making it possible to efficiently and consistently apply different formatting to different sections of a document.

Here’s an example, which defines two styles, ‘cerulean’ and ‘crimson’, and switches between them during the course of creating a page:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
  
  // define a style  
  $cerulean = new Zend_Pdf_Style();
  $cerulean->setFillColor(new Zend_Pdf_Color_Rgb(0, 161, 224));
  $cerulean->setLineColor(new Zend_Pdf_Color_Rgb(0, 161, 224));
  $cerulean->setLineWidth(5);
  $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  $cerulean->setFont($font, 32);  

  // define another style
  $crimson = new Zend_Pdf_Style();
  $crimson->setFillColor(new Zend_Pdf_Color_Rgb(194, 0, 0));
  $crimson->setLineColor(new Zend_Pdf_Color_Rgb(194, 0, 0));
  $crimson->setLineWidth(2);
  $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
  $crimson->setFont($font, 20);  
    
  // write text to page 
  // using different styles
  $page->setStyle($cerulean)
       ->drawText('That which we call a rose,', 72, 720);
       
  $page->setStyle($crimson)
       ->drawText('By any other name would smell as sweet.', 72, 620);
  
  // add page to document
  $pdf->pages[] = $page;
  
  // send to browser as download
  header("Content-Disposition: attachment; filename=example.pdf");
  header('Content-Type: application/pdf');
  echo $pdf->render();
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

Here’s an example of what the output looks like:

Turning The Tables

A common requirement when building PDFs dynamically involves representing data in a table. Zend_Pdf doesn’t support this function natively and so, one way to accomplish this is to manually perform all the calculations necessary to precisely draw a table line by line, split it into rows, columns and cells, and then write text to each cell. This can be a very tedious and not at all maintainable approach, requiring recalculation every time the table content or structure changes, and so, you should instead consider using Zend_Pdf_Table, an unofficial open-source component that was written to address just this problem.

To use Zend_Pdf_Table, download it from SourceForge, copy it to your Zend Framework library directory, and then try it out using the following script:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // set up database access parameters  
  $params = array ('host'     => '127.0.0.1',
                   'username' => 'user',
                   'password' => 'pass',
                   'dbname'   => 'world');
  
  // configure adapter and query database                   
  $db = Zend_Db::factory('PDO_MYSQL', $params);
  $stmt = $db->query('SELECT Name, Code, Region FROM country LIMIT 0, 150');

  // create PDF
  $pdf = new My_Pdf_Document('example.pdf', '.');
    
  // create page
  $page = $pdf->createPage();
  
  // define font resource
  $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

  // set font
  $page->setFont($font, 24);
  
  // create table
  $table = new My_Pdf_Table(3);
  
  // iterate over record set
  // set up table content
  while ($record = $stmt->fetch()) {
    $row = new My_Pdf_Table_Row();
    $cols = array();
    foreach ($record as $k => $v) {
      $col = new My_Pdf_Table_Column();
      $col->setText($v);
      $cols[] = $col;      
    }
    $row->setColumns($cols);
    $row->setFont($font, 14);
    $row->setBorder(My_Pdf::TOP, new Zend_Pdf_Style());
    $row->setBorder(My_Pdf::BOTTOM, new Zend_Pdf_Style());
    $row->setBorder(My_Pdf::LEFT, new Zend_Pdf_Style());
    $row->setCellPaddings(array(10,10,10,10));
    $table->addRow($row);
  }
  
  // add table to page
  $page->addTable($table, 0, 0);
  
  // add page to document
  $pdf->addPage($page);

  // save as file
  $pdf->save();
  echo 'SUCCESS: Document saved!';  
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

This script initializes a Zend_Db instance, connects it to a MySQL database and then executes a SELECT query that returns a result set. This result set is then converted to a PDF table via the My_Pdf_Table component. The elements of each record are represented as My_Pdf_Table_Cell objects, and these cells are grouped together into rows; the rows are then attached to the parent My_Pdf_Table object via its addRow() method. Once the table is complete, it’s added to the page via the page object’s addTable() method, and the entire document is saved to disk. If the table is too large for a single page, the My_Pdf_Table component will automatically detect this, creating and attaching additional pages as necessary.

Here’s an example of what the output looks like:

Birds-Eye View

Zend_Pdf also allows developers to programmatically create a document outline, or a bookmark tree, that allows users to quickly jump to specific destinations of a document. These destinations are represented by Zend_Pdf_Destination objects, and they can be attached to the Zend_Pdf object’s ‘outlines’ property in a tree structure, as shown below:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create root node of outline tree
  $pdf->outlines[0] = Zend_Pdf_Outline::create('Table of Contents', null);
  
  // add pages to document
  // and page links to outline
  for ($x=1; $x<=10; $x++) {
    $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
    $pdf->pages[] = $page;
    $destination{$x} = Zend_Pdf_Destination_Fit::create($page);
    $pdf->setNamedDestination('page_'.$x, $destination{$x});    
    $pdf->outlines[0]->childOutlines[] = Zend_Pdf_Outline::create('Page '.$x, $pdf->getNamedDestination('page_'.$x));    
  }

  // save documents
  $pdf->save('example.pdf');    
  echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

Here’s what this outline tree would look like in a PDF viewer:

Taking Credit

Finally, you can set document information with the Zend_Pdf object’s ‘properties’ property; this information identifies the document creator, title, subject and content keywords. You can also set the document’s creation and last modification date in a similar manner. The following example demonstrates:

<?php
// include auto-loader class
require_once 'Zend/Loader/Autoloader.php';

// register auto-loader
$loader = Zend_Loader_Autoloader::getInstance();

try {
  // create PDF
  $pdf = new Zend_Pdf();
  
  // create A4 page
  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

  // set font  
  $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  $page->setFont($font, 14);
  
  // write text to page 
  $page->drawText('That which we call a rose,', 72, 720)
       ->drawText('By any other name would smell as sweet.', 72, 620);
  
  // add page to document
  $pdf->pages[] = $page;
  
  // set document properties
  $pdf->properties['Author'] = 'William Shakespeare';
  $pdf->properties['Title'] = 'Romeo and Juliet';
  $pdf->properties['Subject'] = 'Act II, Scene II';
  $pdf->properties['Keywords'] = 'shakespeare, romeo, juliet, capulet, montague';
  $pdf->properties['CreationDate'] = "D:201007210634Z00'00'";
  $pdf->properties['ModDate'] = "D:201008150634Z00'00'";
  
  // save as file
  $pdf->save('example.pdf');
  echo 'SUCCESS: Document saved!';
} catch (Zend_Pdf_Exception $e) {
  die ('PDF error: ' . $e->getMessage());  
} catch (Exception $e) {
  die ('Application error: ' . $e->getMessage());    
}
?>

Here’s what the output looks like in a PDF viewer:

And that’s about all we have time for. The previous examples were just the tip of the iceberg – there’s a lot more you can do with Zend_Pdf, so don’t be afraid to try experimenting with it. Happy coding!

This article copyright devzone.zend.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s