skip to Main Content

I am trying to create Barcode in PDF file in Magento 2.3.x Version,

Below code was working in Magento 2.1.8

However this is throwing below error –

PHP Fatal error: Uncaught TypeError: Argument 1 passed to
ZendBarcodeRendererPdf::setResource() must be an instance of
ZendPdfPdfDocument, instance of ZendBarcodeRendererPdf given,
called in /var/www/magento/app/code/MyCompany/Ups/Helper/Custom.php on
line 81

<?php
namespace MycompanyUpsHelper;

use ZendBarcodeBarcode;
use ZendBarcodeRenderer;

use ZendBarcodeRendererPdf;

class Custom extends MagentoFrameworkAppHelperAbstractHelper
{
    protected $_logger;

    protected $_fontPath;

    protected $_pdf;

    public function __construct(
        PsrLogLoggerInterface $logger,
        MagentoFrameworkModuleDirReader $configReader,
        Pdf $pdf
    )
    {
        $this->_logger = $logger;
        $this->_fontPath = $configReader->getModuleDir('etc', 'Mycompany_Ups') . '/lib/fonts/';
        $this->_pdf = $pdf;
    }

    public function getOrderConfirmationPdf($order)
    {
        try {
            $pdf = new Zend_Pdf();
            $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);

            $color1 = new Zend_Pdf_Color_GrayScale(0.0);   // (float number). 0.0 (black) - 1.0 (white)

            $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
            $page->setFont($font,40);

            $page->setFillColor($color1)->drawText('ORDER CONFIRMATION', 60, 720);

            $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
            $page->setFont($font,30.5);
            $page->setFillColor($color1)->drawText('THIS IS NOT A SHIPPING LABEL', 60, 620);

            $page->setFont($font,16.5);
            $page->setFillColor($color1)->drawText('PLACE THIS INSIDE THE SHIPPING CARTON WITH THE', 75, 520);
            $page->setFillColor($color1)->drawText('CONTROL BOARD', 220, 500);
            $page->setFillColor($color1)->drawText('WRITE THE BELOW ORDER CONFIRMATION NUMBER ON', 65, 460);
            $page->setFillColor($color1)->drawText('THE OUTSIDE OF THE SHIPPING CARTON', 125, 440);

            $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
            $page->setFont($font,15);
            $page->setFillColor($color1)->drawText('(Order Confirmation Number: '.$order->getIncrementId().')', 145, 370);


            $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
            $page->setFont($font,30.5);
            $page->setFillColor($color1)->drawText('THIS IS NOT A SHIPPING LABEL', 60, 100);

            $pdf->pages[] = $page;

/*          Zend_Barcode::setBarcodeFont($this->_fontPath.'helvetica-normal.ttf');
            $barcodeOptions = array('text' => $order->getIncrementId(),'barHeight' => 100,'factor'=> 2.1,'drawText' => TRUE, 'withChecksum' => TRUE, 'stretchText' => TRUE, 'withChecksumInText' => TRUE, 'barThickWidth' => 2, 'barThinWidth' => 2);
            $rendererOptions = array('topOffset' => 530,'leftOffset' => 185); 
            $pdfWithBarcode = Zend_Barcode::factory('code128', 'pdf', $barcodeOptions, $rendererOptions)->setResource($pdf,0)->draw();*/

            Barcode::setBarcodeFont($this->_fontPath.'helvetica-normal.ttf');
            $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
            $rendererOptions = array();
            $renderer = Barcode::factory('code128', 'pdf', $barcodeOptions, $rendererOptions);
            $renderer->setResource($pdf, $page)->draw();

            $pdf_str = $pdf->render();
            //$pdf->save("/var/www/html/magento2/mage2/pub/From_Helper_".$order->getIncrementId().".pdf");
            unset($pdf);
            return $pdf_str;
        } catch(Exception $e) {
            mail("[email protected]","ORDER CONFIRMATION PDF ERROR: ".$order->getIncrementId(),$e->getMessage);
            return FALSE;
        }
    }
}

I am using this code in Helper in my custom module.

Any help would be appreciated. Thanks in advance.

Below is sample of PDF I want to generate.

enter image description here

2

Answers


  1. Try this code

    <?php
    namespace MyCompanyUpsHelper;
    include_once '/var/www/magento/fpdf181/fpdf.php';
    use MagentoFrameworkAppHelperAbstractHelper;
    use MagentoFrameworkAppFilesystemDirectoryList;
    use ZendConfigConfig;
    use Zend_Barcode;
    // use Zend_Barcode;
    class Data extends AbstractHelper {
        const LABEL_FOLDER = 'media/sales/order/labels/';
        const FONT_FOLDER = 'media/fonts/';
        protected $_filePointer = NULL;
        protected $_directoryList;
        protected $_logger;
        protected $_fontPath;
        protected $_rootDirectory;
        public function __construct(MagentoFrameworkViewElementTemplateContext $context, PsrLogLoggerInterface $custLogger, MagentoFrameworkModuleDirReader $configReader, MagentoFrameworkFilesystem $filesystem, DirectoryList $directoryList) {
            $this->_logger = $custLogger;
            $this->_directoryList = $directoryList;
            $this->_pubPath = $directoryList->getPath('pub');
            $this->_rootDirectory = $filesystem->getDirectoryRead(DirectoryList::ROOT);
            $this->_fontPath = $configReader->getModuleDir('etc', 'MyCompany_Ups') . '/lib/fonts/';
            $this->_storeManager = $context->getStoreManager();
        }
        public function getOrderConfirmationPdf($order) {
            try {
                $pdf = new Zend_Pdf();
                $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
                $color1 = new Zend_Pdf_Color_GrayScale(0.0); // (float number). 0.0 (black) - 1.0 (white)
                $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
                $page->setFont($font, 40);
                $page->setFillColor($color1)->drawText('ORDER CONFIRMATION', 60, 720);
                $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
                $page->setFont($font, 30.5);
                $page->setFillColor($color1)->drawText('THIS IS NOT A SHIPPING LABEL', 60, 620);
                $page->setFont($font, 16.5);
                $page->setFillColor($color1)->drawText('PLACE THIS INSIDE THE SHIPPING CARTON WITH THE', 75, 520);
                $page->setFillColor($color1)->drawText('CONTROL BOARD', 220, 500);
                $page->setFillColor($color1)->drawText('WRITE THE BELOW ORDER CONFIRMATION NUMBER ON', 65, 460);
                $page->setFillColor($color1)->drawText('THE OUTSIDE OF THE SHIPPING CARTON', 125, 440);
                $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
                $page->setFont($font, 15);
                $page->setFillColor($color1)->drawText('(Order Confirmation Number: ' . $order->getIncrementId() . ')', 145, 370);
                $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
                $page->setFont($font, 30.5);
                $page->setFillColor($color1)->drawText('THIS IS NOT A SHIPPING LABEL', 60, 100);
                $pdf->pages[] = $page;
    
    
                Zend_Barcode::setBarcodeFont($this->_fontPath . 'helvetica-normal.ttf');
                $barcodeConfig = array('text' => $order->getIncrementId(), 'barHeight' => 100, 'factor' => 2.1, 'drawText' => TRUE, 'withChecksum' => TRUE, 'stretchText' => TRUE, 'withChecksumInText' => TRUE, 'barThickWidth' => 2, 'barThinWidth' => 2);
                $rendererConfig = array('topOffset' => 530, 'leftOffset' => 185);
                $renderer = Zend_Barcode::factory('code128', 'pdf', $barcodeConfig, $rendererConfig)->setResource($pdf, 0);
                $renderer->draw();
    
    
    
                $pdf_str = $pdf->render();
                $pdf->save("/var/www/magento/pub/media/sales/order/labels/From_Helper_" . $order->getIncrementId() . ".pdf");
                unset($pdf);
                return $pdf_str;
            }
            catch(Exception $e) {
                mail("[email protected]", "ORDER CONFIRMATION PDF ERROR: " . $order->getIncrementId(), $e->getMessage);
                return FALSE;
            }
        }
    
    Login or Signup to reply.
  2. Try https://github.com/picqer/php-barcode-generator

    Install through composer:

    composer require picqer/php-barcode-generator

    Usage
    Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want.

    $generator = new PicqerBarcodeBarcodeGeneratorHTML();
    echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search