How to Display Extra Fee to the Total of Order Invoice PDF Magento 2?

You can add an extra fee to the total section of the Order invoice in Magento 2 and also display that field value in the order invoice PDF.

If some customer has a requirement to add a custom predefined fee to the order and save that extra fee to the sales order and all the other operations of Order processing.

Once the Order is placed successfully and generates an invoice, you can able to download an Invoice PDF for that specific record or bulk invoices using the Print All Option.

Go To, Sales -> Orders and Sales -> Invoice Page of the admin panel.

TO Display Extra Fee to the Total section of a pdf file, You must able to create a pdf.xml file in your module.

pdf.xml resides under the module etc folder.

Path: Jesadiya/ExtraFee/etc/pdf.xml

<?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">
    <totals>
        <total name="extra_fee">
            <title translate="true">Extra Fee</title>
            <source_field>extra_fee</source_field>
            <font_size>7</font_size>
            <display_zero>true</display_zero>
            <model>Jesadiya\ExtraFee\Model\Sales\Pdf\ExtraFee</model>
            <sort_order>350</sort_order>
        </total>
    </totals>
</config>

From the XML Code snippet,
We have added extra_fee that’s the actual field we have saved in the database table.
extra_fee stores the extra fee amount that the order has to pay.
source_field -> A field that contains the extra fee for an order.
font_size -> Display font size
display_zero -> Want to display 0 as an extra fee in the pdf if yes then Keep Yes.
model -> Write your logic for the display of the extra fee.
sort_order -> Sort order used to the position of your field in the total section.

Create a Model file to add logic for the display extra fee in a PDF file,

<?php
declare(strict_types=1);

namespace Jesadiya\ExtraFee\Model\Sales\Pdf;

use Magento\Sales\Model\Order\Pdf\Total\DefaultTotal;

class ExtraFee extends DefaultTotal
{
    /**
     * Get array of arrays with totals information for display in PDF
     * array(
     *  $index => array(
     *      'amount'   => $amount,
     *      'label'    => $label,
     *      'font_size'=> $font_size
     *  )
     * )
     * @return array
     */
    public function getTotalsForDisplay(): array
    {
        $extraFee = $this->getOrder()->getExtraFee();
        if ($extraFee === null) {
            return [];
        }
        $amountInclTax = $this->getOrder()->formatPriceTxt($extraFee);
        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;

        return [
            [
                'amount' => $this->getAmountPrefix() . $amountInclTax,
                'label' => __($this->getTitle()) . ':',
                'font_size' => $fontSize,
            ]
        ];
    }
}

In the given method, We are fetching the Order object with our extra fee method. You must have an extra_fee column in the sales_order table.

We have to override the getTotalsForDisplay() method from the parent class DefaultTotal to make changes to the parent method.

Step to Generate PDF from the Invoice backend,

Go To Sales -> Invoices Page from the Admin Panel by clicking on Sales Menu. View any invoice by clicking on that and pressing the Print button from the invoice page.

You can see your changes from the newly generated Invoice PDF.

You can add extra_fee value by saving Custom Fee to the Sales order table before placing an order.

This article will help while you are developing a module that contains an extra fee to the Grand Total in Checkout and wants to display that field to the entire order operation.