Hoping someone can point me in the right direction.

I'd like for Magento 2 to automatically create the invoice PDF and save it somewhere on the server when an invoice is generated/created.

Is this possible?

Magento 2.2.7



I don't think there is something "out-of-the-box" in Magento 2 for this. But you can easily create an observer for the event sales_order_invoice_register. This event is called after a new invoice is created. You can realize that with something like this:

1. register observer in events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  <event name="sales_order_invoice_register">
    <observer name="yourObserverName" instance="YourCompany\YourModule\Observer\YourObserver" />

2. create YourObserver.php

namespace YourCompany\YourModule\Observer;

use Magento\Framework\Event\ObserverInterface;

class SavePdfInvoices implements ObserverInterface
    protected $_pdfInvoiceModel;
    protected $_outputDirectory;
    private $_myPdfStorageSubDirectory = "pdfinvoices";

    public function __construct(
        \Magento\Sales\Model\Order\Pdf\Invoice $pdfInvoiceModel,
        \Magento\Framework\Filesystem $filesystem
        ) {
            $this->_pdfInvoiceModel = $pdfInvoiceModel;
            $this->_outputDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR);

    public function execute(\Magento\Framework\Event\Observer $observer)
            $order = $observer->getData('order');
            if (!$order->hasInvoices()){
               return $this;
            $invoice =  $order->getInvoiceCollection()->getFirstItem();
            $pdfContent = $this->_pdfInvoiceModel->getPdf([$invoice])->render();
            //save to file wherever you want, in this example in var/pdfinvoices/[IncrementID].pdf
            $this->_outputDirectory->writeFile($this->_myPdfStorageSubDirectory. "/" . $invoice->getIncrementId() . ".pdf" ,$pdfContent);
        } catch (Exception $e){
            //some usefull exception handling if you need
        return $this;
  • Please let me know if it helps you. – HelgeB Feb 1 '19 at 19:34
  • Thank you, it is working but with one bug. Invoice ID is missing in both the PDF file that is generated and the name of the pdf. It is saving as just .pdf and when opening the pdf invoice# at the top is empty – user1155594 Feb 2 '19 at 6:00
  • Sorry, I had a little bug in the code above, I've corrected it. The event sales_order_invoice_register doesn't provide the invoice object, but the order. The initial code would work in the event sales_order_invoice_save_after but that would mean you regenerate the invoice on every save action. You can make that also. So you have two choices: Change only the event to sales_order_invoice_save_after in your events.xml or change the execute method in YourObserver.php. – HelgeB Feb 2 '19 at 7:55
  • @HelgeB I'm getting error. Invalid template file: 'Magento_Payment::info/pdf/default.phtml' in module: 'Magento_Braintree' block's name: 'info_0' – Jignesh Baldha Dec 26 '19 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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