How to use Zend_Barcode library in Magento 2?

1k Views Asked by At

I want to generate a bar code using Zend library and I have this error:

Class 'Namespace\Module\Model\Order\Pdf\Zend_Barcode' not found in 'Namespace\Module\Model\Order\Pdf\Shipment.php'.

This is my code with error:

$imageResource = Zend_Barcode::draw(
            'code39', 'image', $barcodeOptions, $rendererOptions
        );

In Magento 1.9 works but when I change it on Magento 2 doesn't work. I don't know how to include Zend_Barcode class of Zend Library in my Shippment class.

1

There are 1 best solutions below

0
On BEST ANSWER

You can also import Zend_Barcode class at the top of your class file:

use Zend_Barcode;

Then, your code will work as normal:

$imageResource = Zend_Barcode::draw(
        'code39', 'image', $barcodeOptions, $rendererOptions
    );

OR, you can directly instantiate the object of the class. For this, you don't need to import the class but have to use "backslash" infront of the Zend_Barcode class name.

$imageResource = \Zend_Barcode::draw(
        'code39', 'image', $barcodeOptions, $rendererOptions
    );

Reference: PHP Documentation on Using namespaces: Aliasing/Importing