Welcome to PotatoCommerce - Top Quality Magento Extensions Store

Home > Blog > How to add product attributes to PDF

How to add custom product attributes to Magento 2 Invoice PDF

Posted on 19 July 2018

Invoice is a crucial document in e-Commerce as it’s an official confirmation of a payment. Being essentially a contract, the invoice obliges a seller to deliver purchased goods to a buyer. Such a document gives users confidence in their purchase and trust in a store.

Magento 2 platform has a built-in mechanism for generating PDF files. The list of purchased products is displayed there as a list. If the product is purchased with configurable or custom options (size, color, etc.), these options will be displayed in a PDF document.

However, if you want to display some attributes of the product (eg, warranty, supplier, manufacturer, etc.) that users do not choose when purchasing, you will have to customize the PDF invoice templates.

There are 2 ways to add custom product attribute to Magento 2.

Fast and easy approach

The first way is fast and easy. With PDF Customizer Magento 2 extension you can easily customize PDF templates as this extension uses HTML files for PDF templates.

  1. Go to Content > PDF Customizer > Templates and click on the button to create a new template
  2. Add the following code wherever you want inside the foreach directive:
    {{var item.getProduct().getAttributeText('your_attribute_code')}}
    where you should put the code of the attribute instead of your_attribute_code.
  3. Save the template
  4. Select your template in the extension settings (Content > PDF Customizer > Settings) and save the extension settings

That’s it, done

Coding approach

If you want to change the native Magento 2 PDF templates, then you need to override the following file vendor/magento/module-sales/Model/Order/Pdf/Items/Invoice/DefaultInvoice.php. This will require a knowledge of PHP and Magento 2 coding practices. In the overriden file, you need to add the following code after lines[]:

$id = $item->getProductid();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($id);
$brand = $product->getAttributeText('your_attribute_code');

where you need to replace your_attribute_code with the identifier (code) of your custom product attribute.

Conclusion

Sometimes it’s necessary to customize Magento 2 PDF templates. For example, you may want to add product images or custom product attributes, change styles, fonts, colors, and the layout. In this case, PDF Customizer by PotatoCommerce is the perfect solution. You can check the comparison table of Magento 2 PDF extensions where you can see that our extension is the right choice.

Explore PDF Customizer

About the Author

Author Stanislav Golodov
Stanislav Golodov, Product Management & Marketing in PotatoCommerce

Stanislav has written most of PotatoCommerce blog posts. He belives that blog posts should be really helpful and be written primarily for people, not for Googlebot. Stanislav started working with Magento in 2011 as QA, and later as Product Manager. He joined PotatoCommerce in 2017, where he is responsible for marketing, web analytics, product specifications and finalizing the scope of our products and major extension updates.