0

I'm trying to get the order comment history to show on the invoice PDF. My set up is as follows: di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Denial\InvoiceComments\Model\Rewrite\Order\Pdf\Invoice" />
</config>

app/code/Denial/InvoiceComments/Model/Rewrite/Order/Pdf/Invoice.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Denial\InvoiceComments\Model\Rewrite\Order\Pdf;

use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection;

class Invoice extends AbstractPdf
{

    /**
 * Return PDF document
 *
 * @param array|Collection $invoices
 * @return \Zend_Pdf
 */
public function getPdf($invoices = [])
{
   echo "Model Rewrite Working"; die();        
   $this->_beforeGetPdf();
    $this->_initRenderer('invoice');

    $pdf = new \Zend_Pdf();
    $this->_setPdf($pdf);
    $style = new \Zend_Pdf_Style();
    $this->_setFontBold($style, 10);

    foreach ($invoices as $invoice) {
        if ($invoice->getStoreId()) {
            $this->_localeResolver->emulate($invoice->getStoreId());
            $this->_storeManager->setCurrentStore($invoice->getStoreId());
        }
        $page = $this->newPage();
        $order = $invoice->getOrder();
         foreach ($_entity->getOrder()->getStatusHistoryCollection() as $status){
         $textChunk = wordwrap($status->getComment(), 120, "\n");
         foreach(explode("\n", $textChunk) as $textLine){
            if ($textLine!=='') {
                    $page->drawText(strip_tags(ltrim($textLine)), 35, $this->y, 'UTF-8');
                    $this->y -= 15;
            }
        }
    }
    $this->_afterGetPdf();
    return $pdf;
}

}

However I'm getting this error: Fatal error: Class 'Denial\InvoiceComments\Model\Rewrite\Order\Pdf\AbstractPdf' not found in /home/denialuk/public_html/magento2/app/code/Denial/InvoiceComments/Model/Rewrite/Order/Pdf/Invoice.php on line 10 What silly little mistake have I made, because I can't see it!

1 Answer 1

1

Add following line of code:


use Magento\Sales\Model\Order\Pdf\AbstractPdf;

Now your code looks like:


<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Denial\InvoiceComments\Model\Rewrite\Order\Pdf;

use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection;
use Magento\Sales\Model\Order\Pdf\AbstractPdf;

class Invoice extends AbstractPdf
{

    /**
 * Return PDF document
 *
 * @param array|Collection $invoices
 * @return \Zend_Pdf
 */
public function getPdf($invoices = [])
{
   echo "Model Rewrite Working"; die();        
   $this->_beforeGetPdf();
    $this->_initRenderer('invoice');

    $pdf = new \Zend_Pdf();
    $this->_setPdf($pdf);
    $style = new \Zend_Pdf_Style();
    $this->_setFontBold($style, 10);

    foreach ($invoices as $invoice) {
        if ($invoice->getStoreId()) {
            $this->_localeResolver->emulate($invoice->getStoreId());
            $this->_storeManager->setCurrentStore($invoice->getStoreId());
        }
        $page = $this->newPage();
        $order = $invoice->getOrder();
         foreach ($_entity->getOrder()->getStatusHistoryCollection() as $status){
         $textChunk = wordwrap($status->getComment(), 120, "\n");
         foreach(explode("\n", $textChunk) as $textLine){
            if ($textLine!=='') {
                    $page->drawText(strip_tags(ltrim($textLine)), 35, $this->y, 'UTF-8');
                    $this->y -= 15;
            }
        }
    }
    $this->_afterGetPdf();
    return $pdf;
}

}

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.