2013-04-04 64 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