Magento – How to add a custom product attribute to PDF Invoice Magento 2

custominvoicemagento2pdfproduct-attribute

Solved this myself, but was a pain so thought I'd share the knowledge,

In vendor/magento/module-sales/Model/Order/Pdf/Items/Invoice/DefaultInvoice.php

in public function draw()

under lines[];

add :

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

Change manufacturer to whatever custom attribute code you want.

Quick and dirty – yes I know its a mod of core code and will translate to my own module now it's working, but hope this makes others life easier.

Best Answer

The above solution failed to show some attribute and this is what worked for me:

Add the following code In

vendor/magento/module-sales/Model/Order/Pdf/Items/Invoice/DefaultInvoice.php

in public function draw()

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

Then draw the Attribute:

        $lines[0][] = [
        'text' => $locationCode,
        'feed' => 290,
        'align' => 'right',
    ];
Related Topic