1

How to show order comments history in PDF shipping

vendor/magento/module-sales/Model/Order/Pdf/invoice.php

1 Answer 1

1

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="Vendor\Module\Model\Rewrite\Order\Pdf\Invoice" />
</config>

app/code/Vendor/Module/Model/Rewrite/Order/Pdf/Invoice.php


<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Module\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;
}

}
3
  • Thank you so much sir
    – DK Thakur
    Commented Jan 9, 2020 at 14:01
  • most welcome :) Commented Jan 9, 2020 at 15:01
  • I'm getting below error Fatal error: Uncaught Error: Call to a member function suspend() on null
    – Jaimin
    Commented Apr 30, 2020 at 16:30

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.