Skip to content

Instantly share code, notes, and snippets.

@josip
Created September 6, 2009 11:55
Show Gist options
  • Save josip/181769 to your computer and use it in GitHub Desktop.
Save josip/181769 to your computer and use it in GitHub Desktop.
<?php
/*
Usage:
class Custom_Pdf_Generator extends Pdf_Generator {
public drawEntityInRow($order) {
$this->drawRow(array(
$order->getId(),
$order->getCreatedDate(),
...
));
}
}
$pdf = new Custom_Pdf_Generator();
$pdf->cellWidths = array(
30, // Purchased on
95, // Order #
135, // PO #
185, // Company
255, // City
285, // State
320, // Country
360, // ZIP
390, // Order amount
450, // Commission
510, // Balance due
600 // Right margin of the page
);
$pdf->tableHeader = array(
'Purchased on', 'Order #', 'PO #',
'Company', 'City', 'State', 'Country', 'ZIP',
'Order amount', 'Commission', 'Balance due');
$pdf->getTotalsFrom = 'commission';
$items = Mage::getModel('order')->getItems();
$pdf->render($items);
// or from controller:
$pdf->renderAndSend($items, 'example.pdf', $this->response);
*/
class Pdf_Generator {
protected $pdf, $page, $font, $y = 800;
public $fsize = 9;
public $docTitle = 'Document title';
public $docSubtitle = 'Subtitle';
public $getTotalsFrom = '';
public $cellWidths = array();
public $tableHeader = array();
public function __construct() {
$this->pdf = new Zend_Pdf();
$this->page = $this->pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$this->pdf->pages[] = $this->page;
$this->font = $this->setRegularFont();
}
public function render($data) {
$this->drawPageHeader();
$this->drawTableHeading();
$this->drawRows($data);
return $this->pdf->render();
}
public function renderAndSend($data, $fileName, $response) {
$content = $this->render($data);
$response->setHeader('HTTP/1.1 200 OK','');
$response->setHeader('Pragma', 'public', true);
$response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
// Replace 'inline' with 'attachment' if you wish for browser to force download
$response->setHeader('Content-Disposition', 'inline; filename='.$fileName);
$response->setHeader('Last-Modified', date('r'));
$response->setHeader('Accept-Ranges', 'bytes');
$response->setHeader('Content-Length', strlen($content));
$response->setHeader('Content-type', ' application/pdf');
$response->setBody($content);
$response->sendResponse();
die;
}
private function drawPageHeader() {
/*
$font = $this->setRegularFont();
$this->page->setFont($font, 12);
$this->drawText($this->docTile, 25);
$this->y -= 13;
$this->page->setFont($font, 10);
$this->drawText($this->docSubtitle, 25);
$logo = Zend_Pdf_Image::imageWithPath(Mage::getBaseDir() . '/path/to/logo.png');
$this->page->drawImage($logo, 460, 778, 492, 810);
$this->page->setFont($font, 12);
$this->drawText('Company Name, inc', 500, 800);
$this->page->setFont($font, 6);
$this->drawText('Address Line #1', 500, 793);
$this->drawText('Address Line #2', 500, 787);
$this->drawText('Phone #', 500, 780);
$this->y -= 20;
*/
}
protected function insertPage($drawHeader = true) {
$pages = $this->pdf->pages;
$pages_count = count($pages);
if($pages_count == 1)
$pages[0]->drawText(1, 300, 20);
$this->page = $this->pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$this->pdf->pages[] = $this->page;
$this->setRegularFont();
$this->y = 800;
if($drawHeader)
$this->drawTableHeading();
$this->page->drawText($pages_count + 1, 300, 20);
return $this->page;
}
protected function drawRow($cells) {
$x = $this->cellWidths;
$i = 0;
while($i <= 10)
$this->drawTextInCell($cells[$i], $x[$i], $x[++$i]);
}
protected function drawTableHeading() {
$this->page->setFont($this->font, $this->fsize);
$this->page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
$this->page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
$this->page->setLineWidth(0.5);
$this->page->drawRectangle(25, $this->y, 570, $this->y - 15);
$this->y -= 10;
$this->page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
$this->drawRow($this->tableHeader);
$this->page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
$this->y -= 15;
}
/** Make sure you overwrite this method **/
public function drawEntityInRow($entity) {
/* $this->drawRow(array(
Mage::helper('core')->formatDate($order->getCreatedAtDate(), 'short'),
$order->getData('increment_id'),
$order->getPayment()->getData('po_number'),
$order->getData('billing_company'),
$order->getData('billing_city'),
$order->getData('billing_state'),
$order->getData('billing_country'),
$order->getData('billing_zip'),
'US$ ' . number_format($order->getData('subtotal'), 2),
'US$ ' . number_format($order->getData('commission'), 2),
'US$ ' . number_format($order->getData('balance_due'), 2)
)); */
}
/* Feel free to overwrite */
public function drawRows($entities) {
$total = 0;
foreach($entitess as $id => $entity) {
$this->drawEntityInRow($entity);
$total += $entity->getData($this->getTotalsFrom);
$this->y -= 13;
if($this->shouldInsertPage())
$this->insertPage();
}
$this->drawTotals($total);
}
protected function drawTotals($total) {
$this->page->drawLine(25, $this->y+7, 570, $this->y+7);
$this->y -= 3;
$this->setBoldFont($this->fsize + 1);
$this->drawText('US$ '. number_format($total, 2), 450);
$this->setRegularFont();
}
protected function shouldInsertPage() {
return $this->y < 50;
}
protected function drawText($text, $x, $y = -1) {
$this->page->drawText($text, $x, $y < 0 ? $this->y : $y, 'UTF-8');
}
protected function drawTextInCell($text, $x1, $x2, $y = -1, $font = null, $fontSize = null) {
if(!$font)
$font = $this->font;
if(!$fontSize)
$fontSize = $this->fsize;
$width = $this->widthForString($text, $font, $fontSize);
$charWidth = $this->widthForString('w', $font, $fontSize);
if($width > $x2 - $x1) {
$widthS = $this->widthForString($text, $font, $fontSize - 1);
$this->page->setFont($font, $fontSize - 1);
// Could need some love:
if($widthS > $x2 - $x1)
$text = substr((string)$text, 0, -ceil(($widthS - $x2 - $x1)/$charWidth)) . '...';
}
$this->drawText($text, $x1, $y);
if(isset($widthS))
$this->page->setFont($font, $fontSize);
}
public function widthForString($string, $font, $fontSize) {
$drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $string);
$strlen_drawingString = strlen($drawingString);
$characters = array();
for($i = 0; $i < $strlen_drawingString; $i++)
$characters[] = (ord($drawingString[$i++]) << 8) | ord($drawingString[$i]);
$glyphs = $font->glyphNumbersForCharacters($characters);
$widths = $font->widthsForGlyphs($glyphs);
$stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
return $stringWidth;
}
protected function setRegularFont($size = -1) {
$font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertineC_Re-2.8.0.ttf');
$this->page->setFont($font, $size < 0 ? $this->fsize : $size);
return $font;
}
protected function setBoldFont($size = -1) {
$font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf');
$this->page->setFont($font, $size < 0 ? $this->fsize : $size);
return $font;
}
protected function setItalicFont($size = -1) {
$font = Zend_Pdf_Font::fontWithPath(Mage::getBaseDir() . '/lib/LinLibertineFont/LinLibertine_It-2.8.2.ttf');
$this->page->setFont($font, $size < 0 ? $this->fsize : $size);
return $font;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment