3

Magento 2:

I was looking to add a custom text on the footer in pdf invoice. I have done this by adding a small piece of code here in this file

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

I know it's not good to edit core files, so how can i override this file?

I tried by placing the same file with directories in app/code/Local/Sales/Model/Order/Pdf/Invoice.php but not working.

2 Answers 2

2

Seem that you tried to follow the Magento 1 way. In Magento 2, there are no local, community code pool.

We need to use Dependency injection to override the class.

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

<?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>

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

<?php

namespace Vendor\Module\Model\Order\Pdf;

class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
  //Your custom code lines
}

Recommended way: We can use Plugin to modify the behaviour of methods in Magento 2: http://devdocs.magento.com/guides/v2.1/extension-dev-guide/plugins.html

4
  • I have tried with same but not working. app/code/Vendor/Module/Model/Order/Pdf/Invoice.php in this file i override the getpdf function. public function getPdf($invoices = []) { $this->_drawFooter($page); $this->_afterGetPdf(); return $pdf; } protected function _drawFooter(\Zend_Pdf_Page $page) { $this->y =70; $page->drawText("Tel: +123 456 676", 230, $this->y, 'UTF-8'); } Commented Jan 15, 2017 at 14:05
  • You know how to create a custom module in Magento 2? magento.stackexchange.com/questions/122287/… Commented Jan 15, 2017 at 14:08
  • We can use Plugin: devdocs.magento.com/guides/v2.1/extension-dev-guide/…. Commented Jan 15, 2017 at 16:15
  • @WaqasShakeel we need to learn more about Magento 2. Magento 2 is a big application. We need to spend more time to understand Magento system. The good place to start is : devdocs.magento.com Commented Jan 15, 2017 at 16:16
0

For Magento 2.3.5 you have to:

  • Create your own module in /app/code (example: Vendor/PdfInvoice )
  • Create a file in Vendor/PdfInvoice/registration.php
  • Create a file in Vendor/PdfInvoice/etc/di.xml
  • Create a file in Vendor/PdfInvoice/etc/module.xml
  • Override the section concerned without forgeting to mention in your di.xml
  • Disable cache ( bin/magento cache:disable )
  • Activate your new module: bin/magento module:enable Vendor_PdfInvoice & bin/magento setup:upgrade
  • Enable cache ( php bin/magento cache:enable )
  • Disco

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.