1

I want to update Logo in Invoice Pdf.

Also, I am looking for update google fonts.

I am looking for any solution but not getting an exact answer. Should I go through custom module and override pdf module Or is there any solution is available in back-end?

If anyone does it please let me know.

1

Please follow below code ....

Magento\Sales\Model\Order\Pdf\Invoice.php

Need to override this class into your custom module..

create di.xml under app/code/[VendorName]/[ModuleName]/etc/adminhtml/di.xml

<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="[VendorName]\[ModuleName]\Model\Order\Pdf\Invoice" />
</config>

[VendorName][ModuleName]\Model\Order\Pdf\Invoice.php

/**
 * Set font as regular
 *
 * @param  \Zend_Pdf_Page $object
 * @param  int $size
 * @return \Zend_Pdf_Resource_Font
 */
protected function _setFontRegular($object, $size = 7)
{
    $font = \Zend_Pdf_Font::fontWithPath(
        $this->_rootDirectory->getAbsolutePath('lib/internal/LinLibertineFont/LinLibertine_Re-4.4.1.ttf')
    );
    $object->setFont($font, $size);
    return $font;
}

/**
 * Set font as bold
 *
 * @param  \Zend_Pdf_Page $object
 * @param  int $size
 * @return \Zend_Pdf_Resource_Font
 */
protected function _setFontBold($object, $size = 7)
{
    $font = \Zend_Pdf_Font::fontWithPath(
        $this->_rootDirectory->getAbsolutePath('lib/internal/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf')
    );
    $object->setFont($font, $size);
    return $font;
}

/**
 * Set font as italic
 *
 * @param  \Zend_Pdf_Page $object
 * @param  int $size
 * @return \Zend_Pdf_Resource_Font
 */
protected function _setFontItalic($object, $size = 7)
{
    $font = \Zend_Pdf_Font::fontWithPath(
        $this->_rootDirectory->getAbsolutePath('lib/internal/LinLibertineFont/LinLibertine_It-2.8.2.ttf')
    );
    $object->setFont($font, $size);
    return $font;
}
 /**
     * Insert logo to pdf page
     *
     * @param \Zend_Pdf_Page &$page
     * @param null $store
     * @return void
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    protected function insertLogo(&$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"
                $width = $image->getPixelWidth();
                $height = $image->getPixelHeight();

                //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;
            }
        }
    }

Now just customise those function with your requirement. you can use your own font lib.

| improve this answer | |
0

Mr R DNATH's answer above worked a treat!

Note when you override invoice.php, by placing your own version in your own Magento module, you'll need the following at the top of the file:

namespace <VENDOR>\<MODULE>\Model\Order\Pdf;

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

Also if you use system fonts, as opposed to fonts inline in your code base, you'll need to change the references to those files too.

i.e.

/**
 * Set font as regular
 *
 * @param  \Zend_Pdf_Page $object
 * @param  int $size
 * @return \Zend_Pdf_Resource_Font
 */
protected function _setFontRegular($object, $size = 7)
{
    $font = \Zend_Pdf_Font::fontWithPath('/usr/share/fonts/some-font.ttf');
    $object->setFont($font, $size);
    return $font;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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