0

I need to override Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice.php file to render a custom row in Magento 2.4.4

I tried with preferences like

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

it gives the following error while print the invoice pdf

Error: Cannot instantiate abstract class Magento\Framework\Model\ResourceModel\AbstractResource in /app/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:50

This is related to gift wrap and I want to render item wise wrap items data like wrap image, wrap name, etc. like the below

If there are 2 items in order then 2 wrap data is available, so I need to show 2 raw but when I add

$this->_drawItemWrap($item, $page, $order);

line in getPdf function it throw error

Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice\Interceptor::drawWrap

I rendered that function like below

protected function _drawItemWrap(
  \Magento\Framework\DataObject $item,
  \Zend_Pdf_Page $page,
  \Magento\Sales\Model\Order $order
) {
  $type = $item->getOrderItem()->getProductType();
  $renderer = $this->_getRenderer($type);
  $renderer->setOrder($order);
  $renderer->setItem($item);
  $renderer->setPdf($this);
  $renderer->setPage($page);
  $renderer->setRenderedModel($this);
  $renderer->drawWrap();

  return $renderer->getPage();
}

I need to show wrap history like below image.

enter image description here

If anyone has the solution to rendering the custom row the same as the order item row then please suggest it.

Thank you so much.

1 Answer 1

0

If you want to change Invoice Item renderer for PDF the better way is overwrite it with pdf.xml

app/code/Acme/StackExchange/etc/pdf.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/pdf_file.xsd">
    <renderers>
        <page type="invoice">
            <renderer product_type="default">Acme\StackExchange\Model\Order\Pdf\Items\Invoice\DefaultInvoice</renderer>
        </page>
    </renderers>
</config>

app/code/Acme/StackExchange/Model/Order/Pdf/Items/Invoice/DefaultInvoice.php

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Model\Order\Pdf\Items\Invoice;

use Magento\Sales\Model\Order\Pdf\Items\AbstractItems;

class DefaultInvoice extends AbstractItems
{
    /**
     * @inheritDoc
     */
    public function draw()
    {
        // ...
    }

}
6
  • Thanks for your answer. but I need to create another public function same as draw(). I created the public function drawWrap() and it gives an error Invalid method Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice\Interceptor::drawWrap [] [] What can I do about this?. Commented Jul 22, 2022 at 5:24
  • Please provide full code of your renderer Commented Jul 22, 2022 at 9:46
  • Thanks for your reply. Sorry for the late reply from my end. I edited my question, please check once and give me a better solution. Thank you so much. Commented Jul 25, 2022 at 7:25
  • @MonarkBhawani You've provided another one fragment and I still cannot see full picture of issue for any advise Commented Jul 25, 2022 at 10:25
  • I want to show wrap items data same as product data in pdf. can you render public function drawWrap() && public function draw() in Invoice/DefaultInvoice.php with same time?. Make sure these 2 functions are working and render the data. Commented Jul 25, 2022 at 11:25

Your Answer

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

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