【问题标题】:Magento 2 - How to print invoices in pdf from frontend (user account)Magento 2 - 如何从前端(用户帐户)以 pdf 格式打印发票
【发布时间】:2019-01-28 10:26:13
【问题描述】:

我正在尝试购买 magento 2。我设法安装了所有东西,但我错过了在前端获取 pdf 格式发票的链接(这对我的客户来说是强制性的)。这是我所拥有的:

如您所见,我有打印订单、发票和所有发票的所有链接,但它们都将我带到一个 html 页面,打印出来的内容很像,这很烦人。我无法找到解决此问题的任何方法。这是magento的基本功能还是我真的需要付费并安装另一个模块才能实现这一点?提前致谢。

【问题讨论】:

    标签: pdf magento printing pdf-generation magento2


    【解决方案1】:

    我们最终通过在我们的 magento 2 安装中添加一个模块来解决这个问题。 我提供它的链接。 https://www.mageplaza.com/magento-2-pdf-invoice-extension/ 过去一个月运行良好。

    【讨论】:

      【解决方案2】:
      1. 创建您的模块Vendor_Module
      2. 创建路由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. 创建前端控制器app/code/Vendor/Module/Controller/Getpdf/Invoice.php
      <?php
      declare(strict_types=1);
      
      namespace Vendor\Module\Controller\Getpdf;
      
      use Magento\Framework\App\Action\HttpGetActionInterface;
      use Magento\Framework\App\RequestInterface;
      use Magento\Framework\App\Response\RedirectInterface;
      use Magento\Framework\App\ResponseInterface;
      use Magento\Framework\App\Filesystem\DirectoryList;
      use Magento\Framework\Controller\ResultFactory;
      use Magento\Sales\Model\Order\Pdf\Invoice as PdfInvoice;
      use Magento\Framework\Stdlib\DateTime\DateTime;
      use Magento\Framework\App\Response\Http\FileFactory;
      use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
      use Magento\Framework\Message\ManagerInterface;
      
      
      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|\Magento\Framework\Controller\Result\Redirect|\Magento\Framework\Controller\ResultInterface
           */
          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. 现在您可以使用此控制器了。只需创建一个链接并在此处放置带有我们控制器路径的 href(在某些情况下,使用 Magento\Sales\Block\Order\Info 作为获取订单的块的类是个好主意喜欢$order = $block-&gt;getOrder();)

      2. 拥有订单信息,您可以在 .phtml 文件中获取发票

      <?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;?>
      

      【讨论】:

        猜你喜欢
        • 2015-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多