1

I'm trying to customize the PDF exported by Magento 2 but getting some issues:

etc/di.xml

<preference for="Magento\Sales\Model\Order\Pdf\AbstractPdf" 
            type="Vendor\Module\Model\Order\Pdf\AbstractPdf" />

Even after recompiling with bin/magento setup:di:compile, nothing seems to change when I download an invoice PDF (tested with exit("test"); in the constructor of the class).

So I tried also overriding Magento\Sales\Model\Order\Pdf\Invoice and this time, an error is thrown in the apache error.log:

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Magento\\Sales\\Model\\Order\\Pdf\\Items\\AbstractItems::setPdf() must be an instance of Magento\\Sales\\Model\\Order\\Pdf\\AbstractPdf, instance of Vendor\\Module\\Model\\Order\\Pdf\\Invoice given, called in /var/www/html/app/code/Vendor/Module/Model/Order/Pdf/AbstractPdf.php on line 841 and defined in /var/www/html/vendor/magento/module-sales/Model/Order/Pdf/Items/AbstractItems.php:139\nStack trace:

#0 /var/www/html/app/code/Vendor/Module/Model/Order/Pdf/AbstractPdf.php(841): Magento\\Sales\\Model\\Order\\Pdf\\Items\\AbstractItems->setPdf(Object(Vendor\\Module\\Model\\Order\\Pdf\\Invoice))

#1 /var/www/html/app/code/Vendor/Module/Model/Order/Pdf/Invoice.php(159): Vendor\\Module\\Model\\Order\\Pdf\\AbstractPdf->_drawItem(Object(Magento\\Sales\\Model\\Order\\Invoice\\Item), Object(Zend_Pdf_Page), Object(Magento\\Sales\\Model\\Order\\Interceptor))

#2 /var/www/html/vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php(57): Vendor\\Module\\Model\\Order\\Pdf\\Invoice->getPdf(Array)

2 Answers 2

1

From the first error message, I assume that your custom model does not extend from Magento\Sales\Model\Order\Pdf\AbstractPdf. This should be the case.

1
  • Correct. I was extending from custom AbstractPdf to directly customize any PDF Sep 28, 2018 at 11:59
0

Please change your construction as i given.

public function __construct(\Magento\Payment\Helper\Data $paymentData, \Magento\Framework\Stdlib\StringUtils $string, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Filesystem $filesystem, \Magento\Sales\Model\Order\Pdf\Config $pdfConfig, \Magento\Sales\Model\Order\Pdf\Total\Factory $pdfTotalFactory, \Magento\Sales\Model\Order\Pdf\ItemsFactory $pdfItemsFactory, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Sales\Model\Order\Address\Renderer $addressRenderer, array $data = array()) {
        parent::__construct($paymentData, $string, $scopeConfig, $filesystem, $pdfConfig, $pdfTotalFactory, $pdfItemsFactory, $localeDate, $inlineTranslation, $addressRenderer, $data);
    }

check after that.

1
  • Errors during compilation: Vendor\Module\Model\Order\Pdf\AbstractPdf Incompatible argument type: Required type: array. Actual type: \Magento\Payment\Helper\Data; File: /var/www/html/app/code/Vendor/Module/Model/Order/Pdf/AbstractPdf.php ^_^ Sep 28, 2018 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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