I added pdf.xml in custom modules etc folder. But it is not reflecting in Invoice PDF. I have debug and found that in the class \Magento\Sales\Model\Order\Pdf\AbstractPdf, on line no. 676 I get custom row values but those row values gets omitted after the statement $total->setOrder($order)->setSource($source);

protected function insertTotals($page, $source)
        $order = $source->getOrder();
        $totals = $this->_getTotalsList();
        $lineBlock = ['lines' => [], 'height' => 15];
        foreach ($totals as $total) {
            echo "<pre>";print_r($total->getData());// I get custom row values here
            $total->setOrder($order)->setSource($source);//But those get lost here

This is a protected function so I cannot add plugin also.



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/pdf_file.xsd">
        <total name="custfee">
            <title translate="true">Custom Fee</title>

After debugging the \Magento\Sales\Model\Order\Pdf\AbstractPdf::insertTotals():


On this line, Magento simply sets two Objects data to the current total object for using later. You can get the Order and Source (Invoice object - \Magento\Sales\Model\Order\Invoice) data:

$total->getOrder()->getData(); // Order data
$total->getSource()->getData(); // Invoice data
//Get specific data from total object

enter image description here

If we're trying to var dump or print the total object, the page can be crashed because this object is "heavy" after set order and invoice source object.

I think your problem comes $total->canDisplay() or $total->getTotalsForDisplay().

1) Magento\Sales\Model\Order\Pdf\Total\DefaultTotal::canDisplay(). This method will check:

  • Amount is not equal to zero.
  • Or, in your pdf.xml config, you have declare <display_zero>true</display_zero>. Set to true if you want to display zero amount.

2) Magento\Sales\Model\Order\Pdf\Total\DefaultTotal::getTotalsForDisplay(): Get array of arrays with totals information for displaying in PDF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.