Magento 2 – add new Column to the sales Invoice PDF

We recently had to update Magento’s sales invoices to include an additional product attribute (Pack Size) into the PDF. Without purchasing a large extension this update can be made pretty quickly with some custom code.

As ever with a system of Magento’s complexity this does take a little setup and it’s not just editing a few files.

Step 1 – create new module

As we’re going to be doing a few updates I created an AdminOverrides module.

app/code/Develo/AdminOverrides

First we need to register the module,
app/code/Develo/AdminOverrides/registration.php


Create the basic module at

app/code/Develo/AdminOverrides/etc/module.xml



To override the PDF classes function in an upgrade safe way we must use the Dependancy Injection system,

app/code/Develo/AdminOverrides/etc/di.xml



Here’s the final file structure of the module.

Magento 2 module file structure

Then in your local files override just the functions that you need. In my case it was

  • The _drawHeader(\Zend_Pdf_Page $page) function of Magento\Sales\Model\Order\Pdf\Invoice
  • The draw() function of Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice

Just define your call to override the original class and override just the functions you need.

<?php
namespace Develo\AdminOverrides\Model\Sales\Order\Pdf;
 
/**
* Sales Order Invoice PDF model
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
 
    /**
     * Draw header for item table
     *
     * @param \Zend_Pdf_Page $page
     * @return void
     */
    protected function _drawHeader(\Zend_Pdf_Page $page)
    {

If you enjoyed this article, read our other Learn articles to see more from our Magento developers or browse the site to see what else we do with Adobe Commerce, PunchOut Catalog and more.

Need a hand or want to consult with our developers on your latest project?


...
Luke

share:

Ready to start your next project?

See how we can help you accelerate your business