cancel
Showing results for 
Search instead for 
Did you mean: 

magento 2.4.2 showing Undefined property $appEmulation when I create pdf invoice preference

magento 2.4.2 showing Undefined property $appEmulation when I create pdf invoice preference

I have created preference for Magento\Sales\Model\Order\Pdf\Invoice file inside preference Invoice class I overwrite getPdf function but it is showing below error

Exception #0 (Exception): Notice: Undefined property: Webkeon\Custompdf\Model\Order\Pdf\Invoice::$appEmulation

 

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


/**
 * Sales Order Invoice PDF model
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice 
{
	
    /**
     * Draw header for item table
     *
     * @param \Zend_Pdf_Page $page
     * @return void
     */
    protected function _drawHeader(\Zend_Pdf_Page $page)
    {
        /* Add table head */
        $this->_setFontRegular($page, 10);
        $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
        $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5));
        $page->setLineWidth(0.5);
        $page->drawRectangle(25, $this->y, 570, $this->y - 15);
        $this->y -= 10;
        $page->setFillColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));

        //columns headers
        $lines[0][] = ['text' => __('Products'), 'feed' => 35];

        //$lines[0][] = ['text' => __('SKU'), 'feed' => 290, 'align' => 'right'];

        $lines[0][] = ['text' => __('Qty'), 'feed' => 435, 'align' => 'right'];

        $lines[0][] = ['text' => __('Price'), 'feed' => 360, 'align' => 'right'];

        $lines[0][] = ['text' => __('Tax'), 'feed' => 495, 'align' => 'right'];

        $lines[0][] = ['text' => __('Subtotal'), 'feed' => 565, 'align' => 'right'];

        $lineBlock = ['lines' => $lines, 'height' => 5];

        $this->drawLineBlocks($page, [$lineBlock], ['table_header' => true]);
        $page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
        $this->y -= 20;
    }

    /**
     * Return PDF document
     *
     * @param array|Collection $invoices
     * @return \Zend_Pdf
     */
    public function getPdf($invoices = [])
    {
        $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->appEmulation->startEnvironmentEmulation(
                    $invoice->getStoreId(),
                    \Magento\Framework\App\Area::AREA_FRONTEND,
                    true
                );
                $this->_storeManager->setCurrentStore($invoice->getStoreId());
            }
            $page = $this->newPage();
            $order = $invoice->getOrder();
			$this->y = 730;
            /* Add image */
            //$this->insertLogo($page, $invoice->getStore());
            /* Add address */
           // $this->insertAddress($page, $invoice->getStore());
            /* Add head */
            $this->insertOrder(
                $page,
				$invoice,
                $order,
                $this->_scopeConfig->isSetFlag(
                    self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID,
                    \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
                    $order->getStoreId()
                )
            );
				$this->y -= 50;
            /* Add document text and number */
			
            $this->insertDocumentNumber($page, __('Invoice # ') . $invoice->getIncrementId());
            /* Add table */
            $this->_drawHeader($page);
            /* Add body */
            foreach ($invoice->getAllItems() as $item) {
                if ($item->getOrderItem()->getParentItem()) {
                    continue;
                }
                /* Draw item */
                $this->_drawItem($item, $page, $order);
                $page = end($pdf->pages);
            }
            /* Add totals */
            $this->insertTotals($page, $invoice);
            if ($invoice->getStoreId()) {
                $this->appEmulation->stopEnvironmentEmulation();
            }
        }
        $this->_afterGetPdf();
        return $pdf;
    }

    
}
1 REPLY 1

Re: magento 2.4.2 showing Undefined property $appEmulation when I create pdf invoice preference

Hello @webkeonsanjeev 

 

Update your custom Invoice class to include the parent constructor call and ensure all dependencies are injected properly.

 

Correct the Custom Class

 

namespace Webkeon\Custompdf\Model\Order\Pdf;

use Magento\Sales\Model\Order\Pdf\Invoice as BaseInvoice;
use Magento\Framework\App\State;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
class Invoice extends BaseInvoice

{
    /**

     * Custom Invoice constructor.

     *

     * @param State $appState

     * @param \Magento\Framework\Filesystem $filesystem

     * @param DirectoryList $directoryList

     * @param CollectionFactory $invoiceCollectionFactory

     * @param \Magento\Store\Model\App\Emulation $appEmulation

     * @param \Magento\Framework\Translate\Inline\StateInterface $translateInline

     * @param \Magento\Sales\Model\Order\Pdf\Config $pdfConfig

     * @param \Magento\Sales\Model\Order\Pdf\Total\Factory $pdfTotalFactory

     * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate

     * @param array $data

     */

    public function __construct(

        State $appState,

        \Magento\Framework\Filesystem $filesystem,

        DirectoryList $directoryList,

        CollectionFactory $invoiceCollectionFactory,

        \Magento\Store\Model\App\Emulation $appEmulation,

        \Magento\Framework\Translate\Inline\StateInterface $translateInline,

        \Magento\Sales\Model\Order\Pdf\Config $pdfConfig,

        \Magento\Sales\Model\Order\Pdf\Total\Factory $pdfTotalFactory,

        \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,

        array $data = []

    ) {

        parent::__construct(

            $appState,

            $filesystem,

            $directoryList,

            $invoiceCollectionFactory,

            $appEmulation,

            $translateInline,

            $pdfConfig,

            $pdfTotalFactory,

            $localeDate,

            $data

        );

    }
    /**

     * Override the getPdf function

     * @param array $invoices

     * @return \Zend_Pdf

     */

    public function getPdf($invoices = [])

    {
        // Your custom logic here
        return parent::getPdf($invoices);
    }
}

Verify di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:frameworkSmiley SurprisedbjectManager/etc/config.xsd">

    <preference for="Magento\Sales\Model\Order\Pdf\Invoice" type="Webkeon\Custompdf\Model\Order\Pdf\Invoice" />

</config>

 

php bin/magento setup:di:compile 

php bin/magento cache:flush

 

 

 

 

Hope it helps ! 

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

 

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9