3

The error:

Invalid template file: 'Magento_Payment::info/pdf/default.phtml' in module: 'Magento_Payment' block's name: 'info\instructions_0'

The class:

<?php
namespace Vendor\PDF\Response\Renderer;

use Magento\Framework\Webapi\Exception;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Sales\Api\InvoiceRepositoryInterface;
use Magento\Framework\Webapi\Rest\Response\RendererInterface;
use Magento\Sales\Model\Order\Pdf\Invoice as InvoicePdf;

class PdfRenderer implements RendererInterface
{
    private $request;
    private $invoiceRepository;
    private $invoicePdf;

    /**
     * Pdf constructor.
     * @param \Magento\Framework\Webapi\Rest\Request $request
     * @param \Magento\Sales\Api\InvoiceRepositoryInterface $invoiceRepository
     * @param \Magento\Sales\Model\Order\Pdf\Invoice $invoicePdf
     */
    public function __construct(
        Request $request,
        InvoiceRepositoryInterface $invoiceRepository,
        InvoicePdf $invoicePdf
    ) {
        $this->request = $request;
        $this->invoiceRepository = $invoiceRepository;
        $this->invoicePdf = $invoicePdf;
    }

    /**
     * Render content in a certain format.
     *
     * @param object|array|int|string|bool|float|null $data
     * @return string
     * @throws \Magento\Framework\Webapi\Exception
     */
    public function render($data)
    {
        if (!strstr($this->request->getPathInfo(), '/V1/invoices')) {
            throw new Exception(__('PDF rendering is not supported for this URI'));
        }

        if (isset($data['entity_id'])) {
            $invoice = $this->invoiceRepository->get($data['entity_id']);

            try {
                $pdf = $this->invoicePdf->getPdf([$invoice]);

            } catch (\Exception $e) {
                header("Content-type: application/json");
                exit(json_encode([
                    "operationPerformed" => false,
                    "message" => $e->getMessage()
                ]));
            }

            return $pdf->render();
        } 

        throw new Exception(__('Invoice not found'));
    }

    /**
     * Get MIME type generated by renderer.
     *
     * @return string
     */
    public function getMimeType()
    {
        return 'application/pdf';
    }
}

The error happens when doing $this->invoicePdf->getPdf([$invoice]).

UPDATE:

Solved as suggested from the comments, by emulating the area code 'adminhtml'. For reference, this is my code:

$this->appState->emulateAreaCode("adminhtml",
    function($invoice, &$pdf) {    
        $pdf = $this->invoicePdf->getPdf([$invoice]);
    },
    [$invoice, &$pdf]
);
3

the area code you are running your code with is not the admin. Depending on the payment method used, the fault occurs when the template for the Info block payment is called.

for instance, an order using check / money payment method will have the error at the function \Magento\OfflinePayments\Block\Info\Checkmo::toPdf

I attach a screenshot showing you the payment module templates admin area comes withenter image description here a pdf folder and frontend does not.

  • Oh I understand. So do you think is not possible to render the pdf via REST API (because as you said, that's not an adminhtml code area)? What could be the workaround? – Manuel Di Iorio Sep 17 '18 at 4:36
  • 1
    you can use the way magento has to emulate. please see \Magento\Framework\App\State::emulateAreaCode – Herve Tribouilloy Sep 17 '18 at 5:42
  • It worked by simulating the area code, thank you so much! I'm updating the question with the correct code. – Manuel Di Iorio Sep 17 '18 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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