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.
2
Answers
Try this code
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);