skip to Main Content

I am trying to get a magento 2 shop up. I managed to install everything, but I am missing the link to get the invoice as a pdf on the frontend (which is mandatory for my clients). Here’s what I have:
enter image description here

As you can see, i have all the links to print the order, the invoice and All invoices, but they all take me to a html page, that prints like it, which is pretty annoying. I can’t manage to find any solution to this problem. Is this a base feature in magento or do i really need to pay and install another module in order to achieve this? Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    We managed to finally get the answer to this problem by just adding a module to our magento 2 installation. I provide the link of it. https://www.mageplaza.com/magento-2-pdf-invoice-extension/ It is working well for the past month now.


    1. Create your module Vendor_Module
    2. Create route app/code/Vendor/Module/etc/frontend/routes.xml
        <?xml version="1.0" ?>
        
        <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
            <router id="standard">
                <route id="myroute" frontName="myroute">
                    <module name="Vendor_Module"/>
                </route>
            </router>
        </config>
    
    1. Create front controller app/code/Vendor/Module/Controller/Getpdf/Invoice.php
    <?php
    declare(strict_types=1);
    
    namespace VendorModuleControllerGetpdf;
    
    use MagentoFrameworkAppActionHttpGetActionInterface;
    use MagentoFrameworkAppRequestInterface;
    use MagentoFrameworkAppResponseRedirectInterface;
    use MagentoFrameworkAppResponseInterface;
    use MagentoFrameworkAppFilesystemDirectoryList;
    use MagentoFrameworkControllerResultFactory;
    use MagentoSalesModelOrderPdfInvoice as PdfInvoice;
    use MagentoFrameworkStdlibDateTimeDateTime;
    use MagentoFrameworkAppResponseHttpFileFactory;
    use MagentoSalesModelResourceModelOrderInvoiceCollectionFactory;
    use MagentoFrameworkMessageManagerInterface;
    
    
    class Invoice implements HttpGetActionInterface
    {
        const SELECTED_PARAM = 'id';
    
        protected FileFactory $fileFactory;
        protected DateTime $dateTime;
        protected PdfInvoice $pdfInvoice;
        protected RequestInterface $request;
        protected RedirectInterface $redirect;
        protected ManagerInterface $messageManager;
        protected ResultFactory $resultFactory;
        protected CollectionFactory $collectionFactory;
    
        /**
         * @param DateTime $dateTime
         * @param FileFactory $fileFactory
         * @param PdfInvoice $pdfInvoice
         * @param CollectionFactory $collectionFactory
         * @param RequestInterface $request
         * @param RedirectInterface $redirect
         * @param ManagerInterface $messageManager
         * @param ResultFactory $resultFactory
         */
        public function __construct(
            DateTime          $dateTime,
            FileFactory       $fileFactory,
            PdfInvoice        $pdfInvoice,
            CollectionFactory $collectionFactory,
            RequestInterface  $request,
            RedirectInterface $redirect,
            ManagerInterface $messageManager,
            ResultFactory $resultFactory
        )
        {
            $this->fileFactory = $fileFactory;
            $this->dateTime = $dateTime;
            $this->pdfInvoice = $pdfInvoice;
            $this->request = $request;
            $this->redirect = $redirect;
            $this->messageManager = $messageManager;
            $this->resultFactory = $resultFactory;
            $this->collectionFactory = $collectionFactory;
        }
    
        /**
         * @return ResponseInterface|MagentoFrameworkControllerResultRedirect|MagentoFrameworkControllerResultInterface
         */
        public function execute()
        {
            try {
                $collection = $this->collectionFactory->create();
    
                $invoiceId = $this->request->getParam(self::SELECTED_PARAM);
                $filterIds = $invoiceId ? [$invoiceId] : [];
                $collection->addFieldToFilter(
                    $collection->getResource()->getIdFieldName(),
                    ['in' => $filterIds]
                );
    
                $pdf = $this->pdfInvoice->getPdf($collection);
                $fileContent = ['type' => 'string', 'value' => $pdf->render(), 'rm' => true];
    
                return $this->fileFactory->create(
                    sprintf('invoice%s.pdf', $this->dateTime->date('Y-m-d_H-i-s')),
                    $fileContent,
                    DirectoryList::VAR_DIR,
                    'application/pdf'
                );
            } catch (Exception $e) {
                $this->messageManager->addErrorMessage($e->getMessage());            
                $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                $resultRedirect->setUrl($this->redirect->getRefererUrl());
                return $resultRedirect;
            }
        }
    }
    
    1. Now you are ready to use this controller. Just create a link and put there href with a path to our controller (In some cases it is a good idea to use MagentoSalesBlockOrderInfo as a class to your block for getting order like $order = $block->getOrder();)

    2. Having an order information you can get invoices in your .phtml file

    <?php foreach ($order->getInvoiceCollection() as $invoice):?>
        <a href="<?= $block->getUrl('myroute/getpdf/invoice', ['id' => $invoice->getId()]);?>"><?php echo $this->escapeHtml(__('Invoice (PDF)')); ?></a>
    <?php endforeach;?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search