0

I wrote an override to \vendor\magento\module-sales\Model\Order\Pdf\AbstractPdf.php

I did enable my override, I did run magento setup, di compile and flush the cache. Everything went smooth. But seems still using the old AbstractPdf.php. I don't see anything from the log, what can I do about it?

Thank you

Update Question I want to override AbstractPdf class in magento2

Here is my code

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\AbstractPdf" type="ProjectA\ShipmentPdf\Model\Order\Pdf\AbstractPdf" />
</config>

AbstractPdf.php(ProjectA/ShipmentPdf/Model/Order/Pdf/AbstractPdf.php)

<?php

// @codingStandardsIgnoreFile

namespace ProjectA\ShipmentPdf\Model\Order\Pdf;

// use Magento\Eav\Model\Entity\Attribute as EntityAttribute;
// use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
// use Magento\Framework\DB\Select;
// use Magento\Framework\Model\AbstractModel;
// use Magento\Framework\App\Filesystem\DirectoryList;
// use Magento\Framework\DataObject;

/**
 * Sales Order PDF abstract model
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class AbstractPdf extends \Magento\Sales\Model\Order\Pdf\AbstractPdf
{

    /**
     * Insert logo to pdf page
     *
     * @param \Zend_Pdf_Page &$page
     * @param null $store
     * @return void
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    protected function insertLogo(&$page, $store = null)
    {
        echo "override";die;
    }

}

Before I was modify on the core and its work, then I roll it back and create above for to override

6

1 Answer 1

2

You can not override a protected function. However you can override the public method which is calling that protected method. In my case I needed to override the method called insertLogo. However this being the protected method I could not override. So I override the Invoice.php which was calling insertLogo in getPdf method. In the same file I redefined the code for insertLogo

Here is the file

app/code/ProjectA/ShipmentPdf/Model/Order/Pdf/Invoice.php

Code

namespace ProjectA/ShipmentPdf\Model\Order\Pdf;
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
    public function getPdf($invoices = [])
    {
            //some code

            $order = $invoice->getOrder();
            /* Add image */
            //$this->insertLogo($page, $invoice->getStore());
            /* Calling custom function*/
            $this->insertLogoCustom($page, $invoice->getStore());
            /* Add address */
            $this->insertAddress($page, $invoice->getStore());

            //some more code

        return $pdf;
    }
    protected function insertLogoCustom(&$page, $store = null)
    {
        $this->y = $this->y ? $this->y : 815;
        $image = $this->_scopeConfig->getValue(
            'sales/identity/logo',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $store
        );
        if ($image) {
            $imagePath = '/sales/store/logo/' . $image;
            if ($this->_mediaDirectory->isFile($imagePath)) {
                $image = \Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($imagePath));
                $top = 830;
                //top border of the page
                $widthLimit = 270;
                //half of the page width
                $heightLimit = 270;
                //assuming the image is not a "skyscraper"
                /* Modified this code to convert pixel in points */
                $width = $image->getPixelWidth()* 72 / 96;
                $height = $image->getPixelHeight()* 72 / 96;

                //preserving aspect ratio (proportions)
                $ratio = $width / $height;

                if ($ratio > 1 && $width > $widthLimit) {
                    $width = $widthLimit;
                    $height = $width / $ratio;
                } elseif ($ratio < 1 && $height > $heightLimit) {
                    $height = $heightLimit;
                    $width = $height * $ratio;
                } elseif ($ratio == 1 && $height > $heightLimit) {
                    $height = $heightLimit;
                    $width = $widthLimit;
                }

                $y1 = $top - $height;
                $y2 = $top;
                $x1 = 25;
                $x2 = $x1 + $width;


                //coordinates after transformation are rounded by Zend
                $page->drawImage($image, $x1, $y1, $x2, $y2);

                $this->y = $y1 - 10;
            }
        }
    }

}

Hope it helps!

5
  • Thanks, that's mean I should copy the AbstractPDF function, override child class and put the copied function to the child class. am I correct?
    – Paul Fan
    Dec 16, 2019 at 19:14
  • 1
    correct. let me know if you have any prolem. Dec 16, 2019 at 19:15
  • Thanks! I will try this out now and get back to you
    – Paul Fan
    Dec 16, 2019 at 19:15
  • Thanks man! it had been overrride!
    – Paul Fan
    Dec 16, 2019 at 20:13
  • Already answered here. magento.stackexchange.com/questions/297724/… Dec 18, 2019 at 6:51

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.