Reputation: 726
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 Zend\Barcode\Renderer\Pdf::setResource() must be an instance of ZendPdf\PdfDocument, instance of Zend\Barcode\Renderer\Pdf given, called in /var/www/magento/app/code/MyCompany/Ups/Helper/Custom.php on line 81
<?php
namespace Mycompany\Ups\Helper;
use Zend\Barcode\Barcode;
use Zend\Barcode\Renderer;
use Zend\Barcode\Renderer\Pdf;
class Custom extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $_logger;
protected $_fontPath;
protected $_pdf;
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Module\Dir\Reader $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.
Upvotes: 1
Views: 1802
Reputation: 24
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 Picqer\Barcode\BarcodeGeneratorHTML();
echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
Upvotes: 0
Reputation: 1580
Try this code
<?php
namespace MyCompany\Ups\Helper;
include_once '/var/www/magento/fpdf181/fpdf.php';
use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Framework\App\Filesystem\DirectoryList;
use Zend\Config\Config;
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(\Magento\Framework\View\Element\Template\Context $context, \Psr\Log\LoggerInterface $custLogger, \Magento\Framework\Module\Dir\Reader $configReader, \Magento\Framework\Filesystem $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;
}
}
Upvotes: 2