2

I'm trying to read the invoice pdf from an external source. Can anybody tell me how can I make the external pdf available to the client overriding the one made available by default?

Thanks !

2

1 Answer 1

0

Ok guys so, it's actually pretty simple. You have to write a preference for "Magento\Sales\Controller\Order\PrintInvoice".

In your di.xml at (vendor\module\etc\di.xml)

<preference for="Magento\Sales\Controller\Order\PrintInvoice"  type="vendor\module\Controller\MyPrintInvoice"/> 

Ok now we just have to write the vendor\module\Controller\MyPrintInvoice.php

Just create the file in the path above, it should look something like this:

class MyPrintInvoice extends \Magento\Sales\Controller\Order\PrintInvoice
{
    public function execute()
    {
        $invoiceId = (int)$this->getRequest()->getParam('invoice_id');
        if ($invoiceId) {
            $invoice = $this->_objectManager->create(
                \Magento\Sales\Api\InvoiceRepositoryInterface::class
            )->get($invoiceId);
            $order = $invoice->getOrder();
        } else {
            $orderId = (int)$this->getRequest()->getParam('order_id');
            $order = $this->_objectManager->create(\Magento\Sales\Model\Order::class)->load($orderId);
        }

        if ($this->orderAuthorization->canView($order)) 
        {
                //In here put the code necessary to get your $fileName //
                //In here put the code necessary to get your $file //

                header('Content-type: ' . 'application/octet-stream');
                header('Content-Disposition: ' . "attachment; filename=$fileName");
                echo $file;
            }              
        } 
        else {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) {
                $resultRedirect->setPath('*/*/history');
            } else {
                $resultRedirect->setPath('sales/guest/form');
            }
            return $resultRedirect;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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