1

sorry to bother but I'm really stuck with a strange problem. My goal is to have the invoice PDF changed but the class I've created to do this (which is currently a bunch of code from another guy) does not appear in the generated/code folder. When I try to "print" the invoice I'm receiving the "Class not found" exception but no exception during setup:di:compile. I already studied every thread concerning this task but couldn't solve my problem. I've tried various things like renaming the files, add leading backslashes, remove leading backslashes, fixed dir permissions, deleted everything manually but nothing worked. I've checked the config.php file and I could find the module itself. So what am I doing wrong. Many thanks in advance for any hint!

Here's my code which is currently placed in app/code:

schraubermarkt/module-sales/Model/Order/Pdf/CustomInvoice.php


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

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

/**
 * Sales Order Invoice PDF model
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class CustomInvoice extends Invoice
{
       /**
     * 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/dejavu-sans/DejaVuSansCondensed.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/dejavu-sans/DejaVuSansCondensed.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/dejavu-sans/DejaVuSansCondensed.ttf')
        );
        $object->setFont($font, $size);
        return $font;
    }
}

schraubermarkt/module-sales/etc/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="Schraubermarkt\Sales\Model\Order\Pdf\CustomInvoice" />
</config>

schraubermarkt/module-sales/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Schraubermarkt_Sales" setup_version="0.0.1"> <sequence>
<module name="Magento_Sales"/> </sequence>
    </module>
</config>

schraubermarkt/module-sales/registration.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Schraubermarkt_Sales', __DIR__);
0

The directory path of your module is wrong.

It should be

Schraubermarkt/Sales/Model/Order/Pdf/CustomInvoice.php

Schraubermarkt/Sales/etc/di.xml

Schraubermarkt/Sales/etc/module.xml

Schraubermarkt/Sales/registration.php

instead of

schraubermarkt/module-sales

4
  • Wow ... thanks. It works, obviously the registration.php is just used in combination with composer, which lead to my faulty assumption that it'll map the DIR to the vendor_module combination. Gosh, so obvious. Many many thanks.
    – daill
    Feb 18 at 19:26
  • @daill great, happy to help you. As you confirmed that it works, you can accept my answer and upvote. Thanks !
    – Haijerome
    Feb 19 at 0:11
  • I already did but as I do not have more then 15 rep my vote has been recorded but is not shown in public. Sorry
    – daill
    Feb 19 at 7:17
  • Hi, thanks, that's alright !
    – Haijerome
    Feb 19 at 8:27

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.