2013-04-04 31 views
1

我想从Zend的条形码从Zend的PDF生成的Magento的发票渲染条形码的Magento:使用Zend条码和Zend PDF

我的独立测试条码脚本是这样的,并会生成一个条形码上打印的发票包含条形码和文本“测试”的PDF文档。

$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); 



Magento的发票PDF开始像这样。

$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()); 

.../*继续建设INVOICE */...

适配的条形码脚本插入下面的一些像这样的项目。

/* 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 */ 

然而,在Magento的发票PDF文档运行时,它返回错误调用一个成员函数的getHeight()在LIB非对象上/ Zend的/条形码/渲染器/ Pdf.php上线124

124行包含

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()); 
} 

这似乎是请求pdfPage高度,但我不明白为什么当我把我的条码脚本和/或为什么pdfPage->的getHeight将只会失败在那里失败。

回答

1

我的解决方案是分离出来的过程,而不是在创建PDF时自动创建条形码。在将条形码号码导入Magento时,我创建了条形码图像并将其保存到条形码号码作为文件名。然后,在PDF创建过程中,我使用$page->drawImage(),并通过名称/条形码从目录中调用图像。

3

我已经来到翻过同样problem.The问题是在下面的代码:

你给serResource
$renderer = Zend_Barcode::factory('code128', 'pdf', $barcodeOptions, 
       $rendererOptions)->setResource($pdf, $page)->draw(); 

的$ page变量是一个实际的PDF页面。它应该是页码。例如下面的代码将在第一页上绘制条形码。只需根据您的需要更改$ pageNo。

/* 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 */ 
+0

谢谢,我还需要先添加页面。例如$ pdf-> pages [] = $ pdf-> newPage(Zend_Pdf_Page :: SIZE_A4); – 2016-02-11 00:21:42