0

I want to edit my Invoice in Magento 2.3. I have copied the file from the core file.

app\Vendor\Magento\magento-sales\Model\Order\Pdf\Invoice.php

To :

app\design\frontend{Theme}{Name}\Magento_Sales\Model\Order\Pdf\Invoice.php

Changes I have made in my custom theme PDF file(Invoice.php) it's not reflecting in the PDF.

Please let me know is there anything else I have to change.

Thanks.

3
  • You cannot override like that, you need to use preference to do that on your custom module.... Nov 6, 2019 at 6:27
  • @PrathapGunasekaran Can you please send any reference link, Thanks. Nov 6, 2019 at 6:31
  • Added answer below. Nov 6, 2019 at 6:36

3 Answers 3

1

You can create your custom module in your app/code directory and then you can override below file in that module

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

You need to create one di.xml file here in your Custom module

app/code/Vendor/Module/etc/di.xml

Content for this file is..

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Vendor\Module\Model\Order\Pdf\Invoice" />
</config>

And then you need to override your Invoice.php file in your custom module here on this path

app/code/Vendor/Module/Model/Order/Pdf/Invoice.php

Content for this file is..

<?php
namespace Vendor\Module\Model\Order\Pdf;

use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection;

class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
    /*You can override function of 'Magento\Sales\Model\Order\Pdf\Invoice' file here based on your requirement. */

    public function __construct(
        \Magento\Payment\Helper\Data $paymentData,
        \Magento\Framework\Stdlib\StringUtils $string,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Sales\Model\Order\Pdf\Config $pdfConfig,
        \Magento\Sales\Model\Order\Pdf\Total\Factory $pdfTotalFactory,
        \Magento\Sales\Model\Order\Pdf\ItemsFactory $pdfItemsFactory,
        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
        \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
        \Magento\Sales\Model\Order\Address\Renderer $addressRenderer,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Locale\ResolverInterface $localeResolver,
        array $data = []
    ) {
        parent::__construct(
            $paymentData,
            $string,
            $scopeConfig,
            $filesystem,
            $pdfConfig,
            $pdfTotalFactory,
            $pdfItemsFactory,
            $localeDate,
            $inlineTranslation,
            $addressRenderer,
            $storeManager,
            $localeResolver,
            $data
        );
    }
}

How to create your custom module ? You can check here

Hope this will help you!

15
  • I am getting this error Class 'Canisys\Pdf\Model\Order\Pdf\Magento\Sales\Model\Order\Pdf\Invoice' not found in /var/www/html/beta/app/code/Canisys/Pdf/Model/Order/Pdf/Invoice.php. Nov 6, 2019 at 7:02
  • @SudhakarSJ, You've created custom module and run deployment commands after that Right ? Nov 6, 2019 at 7:03
  • Yes. I have run this command. php bin/magento setup:upgrade, php bin/magento setup:di:compile Nov 6, 2019 at 7:05
  • After running the compile command I am getting above error. Nov 6, 2019 at 7:06
  • Okay let me check from my side and inform you. Give me some time Nov 6, 2019 at 7:07
1

Create custom module if you do not have any which will be under app/code folder

For more info about creating custom module : Visit here

Then add di.xml under

app/code/Vendor/ModuleName/etc/di.xml

then add the below code to there

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">    
  <preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Vendor\ModuleName\Model\Order\Pdf\Invoice" />
</config>

then add the destination class which you want to override the core file i.e., Invoice.php

Add the Invoice.php in the below path

app/code/Vendor/ModuleName/Model/Order/Pdf/Invoice.php

then modify the changes as per your requirements.

More about overriding using preference visit here

Hope this helps.

2
  • I have created the custom module, copied the Invoice file from the core file. When I tried to print pdf from the admin side. I got this error (1 exception(s): Exception #0 (ReflectionException): Class Canisys\Pdf\Model\Order\Pdf\Invoice does not exist) Nov 6, 2019 at 7:04
  • @SudhakarSJ Please check if there are any spelling mistakes as I got the same problem
    – Duke
    Jun 22, 2020 at 9:26
0

Please use preference to ovveride,it will works,or use plugin to change the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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