Reputation: 3283
I am trying to get a barcode rendered from Zend Barcode on a Magento invoice generated from Zend PDF
My standalone test barcode script looks like this and will generate a PDF document with the barcode and text 'testing' as intended.
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20);
//$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
Zend_Barcode::setBarcodeFont('gothic.ttf');
$filename= 'barcode.pdf';
$barCodeNo = '99700161BST004008501022006714';
$barcodeOptions = array(
'text' => $barCodeNo,
'barHeight'=> 74,
'factor'=>3.98,
//'fontSize' =>20,
//'drawText'=> false
);
$rendererOptions = array();
$renderer = Zend_Barcode::factory(
'code128', 'pdf', $barcodeOptions, $rendererOptions
)->setResource($pdf, $page)->draw();
$page->drawText('Test', 25, 720, 'UTF-8');
$pdf->pages[] = $page;
$pdf->save($filename);
The Magento invoices PDF is initiated like so.
$pdf = new Zend_Pdf();
$this->_setPdf($pdf);
$style = new Zend_Pdf_Style();
$this->_setFontBold($style, 10);
foreach ($invoices as $invoice) {
if ($invoice->getStoreId()) {
Mage::app()->getLocale()->emulate($invoice->getStoreId());
Mage::app()->setCurrentStore($invoice->getStoreId());
}
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
$order = $invoice->getOrder();
/* Add image */
$this->insertLogo($page, $invoice->getStore());
.../* CONTINUE BUILDING INVOICE */...
The adapted barcode script is inserted below some of the items like so.
/* Start Barcode */
$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20);
Zend_Barcode::setBarcodeFont('/srv/magento/media/fonts/gothic.ttf');
$barcodeOptions = array(
'text' => $trackingNumber,
'barHeight'=> 74,
'factor'=>3.98
);
$rendererOptions = array('height'=> 800,'width'=> 800);
$renderer = Zend_Barcode::factory(
'code128', 'pdf', $barcodeOptions, $rendererOptions
)->setResource($pdf, $page)->draw();
/* End Barcode */
However when running in the Magento invoice PDF doc it returns the error Call to a member function getHeight() on a non-object in lib/Zend/Barcode/Renderer/Pdf.php on line 124.
Line 124 contains
protected function _initRenderer()
{
if ($this->_resource === null) {
$this->_resource = new Zend_Pdf();
$this->_resource->pages[] = new Zend_Pdf_Page(
Zend_Pdf_Page::SIZE_A4
);
}
$pdfPage = $this->_resource->pages[$this->_page];
$this->_adjustPosition($pdfPage->getHeight(), $pdfPage->getWidth());
}
Which appears to be requesting the pdfPage height, but I can't see why it would only fail when I put in my barcode script and/or why the pdfPage->getHeight would fail there.
Upvotes: 1
Views: 3647
Reputation: 3283
My solution was to separate out the process and not automate the barcode creation during pdf creation. On import of the barcode numbers into Magento I created barcode images and saved to a directory with the barcode number as the filename. Then during PDF creation I used $page->drawImage()
and called the image from the directory by it's name/barcode.
Upvotes: 1
Reputation: 115
I have came accross the same problem.The problem is in the code below:
$renderer = Zend_Barcode::factory('code128', 'pdf', $barcodeOptions,
$rendererOptions)->setResource($pdf, $page)->draw();
The $page variable that you gave the serResource is an actual pdf page. It should be the page number. For example the code below will draw the barcode on the first page. Just change the $pageNo according to your need.
/* Start Barcode */
$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20);
Zend_Barcode::setBarcodeFont('/srv/magento/media/fonts/gothic.ttf');
$barcodeOptions = array(
'text' => $trackingNumber,
'barHeight'=> 74,
'factor'=>3.98
);
$pageNo = 0;
$rendererOptions = array('height'=> 800,'width'=> 800);
$renderer = Zend_Barcode::factory(
'code128', 'pdf', $barcodeOptions, $rendererOptions
)->setResource($pdf, $pageNo)->draw();
/* End Barcode */
Upvotes: 3