Magento 2 - 如何从前端(用户帐户)打印 pdf 格式的发票

Magento 2 - How to print invoices in pdf from frontend (user account)

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

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

我们通过向我们的 magento 2 安装添加一个模块,终于找到了这个问题的答案。 我提供了它的link。 https://www.mageplaza.com/magento-2-pdf-invoice-extension/ 它在过去的一个月里运行良好。

  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. 现在您可以使用这个控制器了。只需创建一个 link 并将带有我们控制器路径的 href 放在那里(在某些情况下,使用 Magento\Sales\Block\Order\Info 作为 class 到你的街区以获得像 $order = $block->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;?>