Magento: Barcode on Printed Invoice using Zend Barcode and Zend PDF

Magento: Barcode on Printed Invoice using Zend Barcode and Zend PDF

September 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Barcode on Printed Invoice using Zend Barcode and Zend PDF

Magento: Rendering Barcodes on Printed Invoices Using Zend Barcode and Zend PDF

As an experienced Magento developer at Laravel Company, I often encounter complex scenarios involving barcode rendering in printed invoices using Zend Barcode and Zend PDF libraries. Let’s explore a common issue faced by developers when trying to integrate these components.

The Problem: Barcodes Failing to Render on Magento PDF Invoices

You’ve likely encountered the following error while attempting to render barcodes on Magento invoice PDFs:

Call to a member function getHeight() on a non-object in lib/Zend/Barcode/Renderer/Pdf.php on line 124.

This error occurs within the Zend Barcode library when trying to access the height of the PDF page. The cause is usually due to the way Magento initializes its PDF generation process.

Understanding the Problem: Magento’s PDF Initialization

Magento initializes its PDF documents in a specific manner, which sometimes conflicts with the expectations of the Zend Barcode library:

  1. Magento creates an empty array for pages before adding any content.
  2. The Zend_Pdf object is set to the PDF instance created by Magento.
$pdf = new Zend_Pdf();
$this->setPdf($pdf);
  1. When you try to access the first page of the PDF ($this->getPdf()->pages[0]), it returns a non-object because no pages have been added yet.

The Solution: Correctly Initializing the Barcode Renderer

To resolve this issue, we need to ensure that the Zend Barcode renderer is initialized with a properly configured Zend_Pdf object containing at least one page. Let’s modify your existing barcode script:

$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;

Zend_Barcode::setBarcodeFont('/path/to/gothic.ttf');

$barcodeOptions = [
    'text' => $trackingNumber,
    'barHeight' => 74,
    'factor' => 3.98
];

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

In this updated script:

  • We create a new Zend_Pdf object and add a single page to it.
  • The barcode is then rendered using the properly configured PDF resource.

Integrating the Solution into Your Magento Invoice PDF

Now, let’s integrate this solution into your Magento invoice PDF generation process. Update the relevant section of your code as follows:

// ... (previous code)

// Start Barcode
$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20);
Zend_Barcode::setBarcodeFont('/srv/magento/media/fonts/gothic.ttf');

$barcodeOptions = [
    'text' => $trackingNumber,
    'barHeight' => 74,
    'factor' => 3.98
];

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

// End Barcode

// ... (remaining code)

Conclusion: Rendering Barcodes in Magento PDFs

As you’ve learned in this blog post from Laravel Company, the key to successfully rendering barcodes on Magento invoice PDFs lies in properly initializing the Zend_Pdf object and ensuring it contains at least one page before calling the barcode renderer.

By following the solution outlined above and integrating the updated code into your Magento invoice generation process, you should now be able to render barcodes without encountering the getHeight() error.

Orders Errors

Our network