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 !


1 Answer 1


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(
            $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()) {
            } else {
            return $resultRedirect;

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.