We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Follow the below steps to generate invoice PDF & save it on the server at invoice creation time.

    1. Create file event.xml on app/code/Magemonkeys/Saveinvoice/etc

    <?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_save_after">
        <observer name="save_invoice_pdf" instance="MagemonkeysSaveinvoiceObserverSaveinvoice" />
      </event>
    </config>

    2. Create file Saveinvoice.php on app/code/Magemonkeys/Saveinvoice/Observer

    <?php
    namespace MagemonkeysSaveinvoiceObserver;
    
    use MagentoFrameworkEventObserverInterface;
    
    class Saveinvoice implements ObserverInterface
    {
        protected $_pdfInvoiceModel;
        protected $_outputDirectory;
        private $_myPdfStorageSubDirectory = "pdfinvoices";
    
        public function __construct(
            MagentoSalesModelOrderPdfInvoice $pdfInvoiceModel,
            MagentoFrameworkFilesystem $filesystem
            ) {
                $this->_pdfInvoiceModel = $pdfInvoiceModel;
                $this->_outputDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR);
        }
    
        public function execute(MagentoFrameworkEventObserver $observer)
        {
            try{            
                $invoice = $observer->getEvent()->getInvoice();            
                $pdfContent = $this->_pdfInvoiceModel->getPdf([$invoice])->render();
                //save to file  var/pdfinvoices/[IncrementID].pdf
                $this->_outputDirectory->writeFile($this->_myPdfStorageSubDirectory. "/" . $invoice->getIncrementId() . ".pdf" ,$pdfContent);
            } catch (Exception $e){
                $message = $e->getMessage();
            }
            return $this;
        }
    }

     

    Fill the below form if you need any Magento relate help/advise/consulting.

    With Only Agency that provides a 24/7 emergency support.

      Get a Free Quote