1

add in 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\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction" type="Custom\Module\Controller\Adminhtml\Invoice\PrintAction" />
</config>

Add file

<?php
namespace Custom\Module\Controller\Adminhtml\Invoice;

use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Filesystem\DirectoryList;

class PrintAction extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction
{
    // Your custom implementation goes here

    public function execute()
    {
        $invoiceId = $this->getRequest()->getParam('invoice_id');
        if ($invoiceId) {
            $invoice = $this->_objectManager->create(
                \Magento\Sales\Api\InvoiceRepositoryInterface::class
            )->get($invoiceId);
            if ($invoice) {
                $pdf = $this->_objectManager->create(\Magento\Sales\Model\Order\Pdf\Invoice::class)->getPdf([$invoice]);
                $date = $this->_objectManager->get(
                    \Magento\Framework\Stdlib\DateTime\DateTime::class
                )->date('Y-m-d_H-i-s');
                $fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];

                return $this->_fileFactory->create(
                    'invoice' .  $invoiceId . '.pdf',
                    $fileContent,
                    DirectoryList::VAR_DIR,
                    'application/pdf'
                );
            }
        } else {
            return $this->resultForwardFactory->create()->forward('noroute');
        }
    }
}

But i am unable to any changes.

Is it possible to change the filename of the Invoice PDF-files in Magento 2 to a format like "invoice_1000000123.pdf"? We have tried with the lot of solution given by many developer's solution but we aren't able to change? I am using magento 2.4.4 version. Please help anyone. Thanks in advance.

4
  • Which situation do you want to change? Generate new or update entire of invoice in system?
    – HoangHieu
    Dec 6, 2023 at 5:05
  • Hi @HoangHieu We want this change during Generate new invoice pdf. Please help. Thank in advance. Dec 6, 2023 at 5:15
  • I am waiting your response. I need it immediately. @HoangHieu if you have any solution please suggest me. Dec 6, 2023 at 5:40
  • Update my question please check and reply please. Dec 6, 2023 at 7:33

2 Answers 2

1

Update 1 for clarify:

Did you try to change the file name in:

  • vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php:64
  • vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/Pdfinvoices.php:82

Update 2:

  • Changed the list of files.
  • How do you print the pdf? How can I reproduce?

Update 3:

Preference this class for printing invoice PDF in sales order view:

\Magento\Sales\Controller\Adminhtml\Order\Invoice\PrintAction

Preference this class for printing PDF in invoice view:

\Magento\Sales\Controller\Adminhtml\Invoice\PrintAction

Preference this class for printing PDF in sales order grid:

\Magento\Sales\Controller\Adminhtml\Order\Pdfinvoices

Preference this class for printing PDFs in Invoice grid

\Magento\Sales\Controller\Adminhtml\Invoice\Pdfinvoices
6
  • Hi @HoangHieu Thanks for reply. We are overriding this file but we are not able to any change it. Can you explain your solution. Please help me. Thanks in advance. We are waiting your input. Dec 6, 2023 at 6:58
  • Updated the message.
    – HoangHieu
    Dec 6, 2023 at 7:02
  • Can you give solution step by step. Regarding your solution given file is vendor files. If have change already as you suggested vendor files and working fine but without override we are not able to change and already we have tried already with the override. Please explain your solution step by step. Please Dec 6, 2023 at 7:05
  • Updated the answer
    – HoangHieu
    Dec 6, 2023 at 7:16
  • Update my question please check and reply please. Dec 6, 2023 at 7:33
0

You can modify it in the transport builder

$fileName = 'Invoice-'.$order->getIncrementId() . ".pdf";
$this->transportBuilder->addAttachment($pdfFileData['filestream'], $fileName, 'application/pdf')
4
  • Hi @surbhi Can you explain your solution. Where to make these changes? Please help me. We are waiting your reply. Thank in advance. Dec 6, 2023 at 7:01
  • You have to overwrite this file and modify the name here. Magento\Sales\Controller\Adminhtml\Order\pdfinvoices.
    – surbhi agr
    Dec 6, 2023 at 7:18
  • We are trying to override already as you suggest but it is not override. Can you tell me step by step. Please. Dec 6, 2023 at 7:29
  • Update my question please check and reply please. Dec 6, 2023 at 7:33

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.