【问题标题】:Magento: Barcode on Printed Invoice using Zend Barcode and Zend PDFMagento:使用 Zend Barcode 和 Zend PDF 打印发票上的条形码
【发布时间】:2013-04-04 23:42:00
【问题描述】:

我正在尝试从 Zend PDF 生成的 Magento 发票上获取从 Zend Barcode 呈现的条形码

我的独立测试条码脚本如下所示,它将生成一个带有条码和文本“测试”的 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());

.../* 继续建造发票 */...

修改后的条码脚本被插入到一些类似这样的项目下方。

   /* 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 文档中运行时,它会返回错误 Call to a member function getHeight() on a non-object in lib/Zend/Barcode/Renderer/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 会在那里失败。

【问题讨论】:

    标签: php magento zend-framework


    【解决方案1】:

    我也遇到了同样的问题,问题出在下面的代码中:

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

    您提供给 serResource 的 $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 */
    

    【讨论】:

    • 谢谢,我还需要先添加页面。例如$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多