skip to Main Content

I want to change the filenames of the PDF files in Magento 2.

The default names are not clear and I want invoices to be searchable when saved to a location on my pc.

Is it possible to change the filenames of the PDF-files in Magento 2 to a format like “invoice_1000000123.pdf”?

3

Answers


  1. Yes it is possible to change invoice pdf filename.

    Please go to following path:

    /vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php
    

    You can change filename from above file.

    Login or Signup to reply.
  2. You should never edit core files. Seriously, don’t.

    Since /vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php is an abstract class you have to use a plugin or preference in your module in order to override it.

    What you need to achieve that:

    The usual minimum files:
    /Vendor/Module/composer.json, /Vendor/Module/registration.php, /Vendor/Module/etc/module.xml

    In /Vendor/Module/etc/module.xml you should sequence Magento_Sales

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Vendor_Module" setup_version="1.0.0">
            <sequence>
                <module name="Magento_Sales"/>
            </sequence>
        </module>
    </config>
    

    Then you can either use a plugin or a preference in /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="MagentoSalesControllerAdminhtmlInvoiceAbstractInvoicePrintAction" type="VendorModuleControllerAdminhtmlInvoiceAbstractInvoicePrintAction"/>      
    </config>
    

    Plugin would look something like this:

    <type name="MagentoSalesControllerAdminhtmlInvoiceAbstractInvoicePrintAction">
        <plugin name="Vendor_Module::aroundPrintInvoice" type="VendorModuleBlockClass" sortOrder="0"/>
    </type>
    

    Now add a PrintAction.php to the path specified in your preference / plugin (Example for preference)

    <?php
    namespace VendorModuleControllerAdminhtmlInvoiceAbstractInvoice;
    
    class PrintAction extends MagentoSalesControllerAdminhtmlInvoiceAbstractInvoicePrintAction
    {
        /* Write code here */
    }
    
    Login or Signup to reply.
  3. You need override the invoice admin controller.

    <?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="MagentoSalesControllerAdminhtmlOrderInvoice" 
          type="VendorModuleControllerAdminhtmlOrderInvoice" />
    </config>
    

    In your custom module admin controller, You need to change the name of Pdf file.

    public function execute() 
    {
      $invoiceId = $this->getRequest()->getParam('invoice_id');
      return $this->_fileFactory->create(
                        'invoice_13012020' . $invoiceId . '.pdf', //<== Change the pdf name here.
                        $pdf->render(),
                        DirectoryList::VAR_DIR,
                        'application/pdf'
                    );
            ...
        }
    

    Reference Link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search