Zend Framework provide component Zend_Pdf for creating pdf files. It is very easy to use this component.
Let’s have a look how to create pdf file using Zend_Pdf component.
First create instance of Zend_Pdf as
$pdf = new Zend_Pdf();
If you want to load existing pdf file and make changes, write this instead
$pdf = Zend_Pdf::load(‘path/to/file.pdf’);
If you want to save the file, simply write
This will save new file. To update already existing file, write
Or you can render your pdf file in the browser as
To add pages to the pdf file
$pdf->pages = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
$pdf->pages = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
To remove specific page from the document
In order to write text to the page you will need the following code.
$page=$pdf->pages;// this will get reference to the first page.
$style = new Zend_Pdf_Style();
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$page->drawText(‘example text here’,100,($page->getHeight()-100));
In the code above we first get reference to the first page. In the next line we create instance of Zend_Pdf_Style and then setLineColor.
Next we set font by using Zend_Pdf_Font class.
Next two lines set the font to style and set style to page.
That’s it we have now set font and style.
Now its time to write text to the pdf file.
$page->drawText() does the magic for us.
That’s it. Have fun.