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
$pdf->save(‘path/to/file.pdf’);
This will save new file. To update already existing file, write
$pdf->save(‘path/to/file.pdf’,’true’);
Or you can render your pdf file in the browser as
$pdf->render();
To add pages to the pdf file
$pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
Or
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
To remove specific page from the document
unset($pdf->pages[$id]);
In order to write text to the page you will need the following code.
$page=$pdf->pages[0];// this will get reference to the first page.
$style = new Zend_Pdf_Style();
$style->setLineColor(new Zend_Pdf_Color_Rgb(0,0,0));
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$style->setFont($font,12);
$page->setStyle($style);
$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.
Give code is generate the PDF file but generated PDF file is not Opened.
You should save the file after all the operations…and it will open.
Thanks this has been a great help in getting me started, were just starting working on developing more on the Zend Framework since using Magento extensive.
Small typo error – single quote should be removed around true eg:
This will save new file. To update already existing file, write
$pdf->save(‘path/to/file.pdf’,true);