0

Here I want to design and change the invoice PDF of a particular Invoice. I have added an observer to but it's creating a normal invoice. But I want to design and change the PDF of that particular invoice. I also have access to invoice's ID but I do not have any idea how to design the PDF and assign it to that particular invoice.

Here is my Observer

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;


class GenerateInvoice implements ObserverInterface
{

    /**
     * @inheritDoc
     */
    /**
     * @var \Magento\Sales\Api\OrderRepositoryInterface
     */
    protected $_orderRepository;

    /**
     * @var \Magento\Sales\Model\Service\InvoiceService
     */
    protected $_invoiceService;

    /**
     * @var \Magento\Framework\DB\Transaction
     */
    protected $_transaction;




    protected $invoiceSender;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Sales\Model\Service\InvoiceService $invoiceService,
        \Magento\Framework\DB\Transaction $transaction,
        \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender,      
        array $data = []
    )
    {

        $this->_orderRepository = $orderRepository;
        $this->_invoiceService = $invoiceService;
        $this->_transaction = $transaction;
        $this->_logger = $logger;
            $this->invoiceSender = $invoiceSender;
    }


    public function execute(Observer $observer)
    {

     $invoice = $observer->getEvent()->getInvoice();
    $order = $invoice->getOrder();
    $orderIncrementId = $order->getIncrementId();
    $date = date('d-M-Y');

    $entity_id=$invoice["order_id"];

        $order = $this->_orderRepository->get($entity_id);
        if($order->canInvoice()) {
            $invoice = $this->_invoiceService->prepareInvoice($order);
            $invoice->register();
            $invoice->save();
            $transactionSave = $this->_transaction->addObject(
                $invoice
            )->addObject(
                $invoice->getOrder()
            );
            $transactionSave->save();
            $this->invoiceSender->send($invoice);


            $invoiceId=$invoice->getId();
            $invoiceId=$invoiceId+1;

                $incrementId=$invoice->getIncrementId();
                $incrementId=$incrementId+1;

        $this->_logger->info("Latest Invoice id ".$invoiceId);
        $this->_logger->info("Latest Invoice increment ".$incrementId);


            $order->addStatusHistoryComment(
                __('Notified customer Custom about invoice #%1.', $invoice->getId())
            )
            ->setIsCustomerNotified(true)
            ->save();


        }


        }


    }

But its just generating Invoice I want this invoice's design to be done and changed.

How can I do this?

Small Help will be greatly appreciated.

Thanks in Advance :)

0

To modify Invoice PDF you must need to modify below files as per your requirement.

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

This File is used for all PDF generation so Make sure to modify as per compatibility with other PDF generation.

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

you can check and modify it as per your requirement.

Thank you

2
  • What if want to change in observer during the invoice generattion – Sayyed Hasan Apr 15 '20 at 11:14
  • Observer is used to perform additional task or code to run for adding additional functionality. by using observer it will not forgot the default behaviour of it's own. so If you want to change the design of your invoice pdf you must need to override files as I mentioned in this solution. – Nirav Patel Apr 16 '20 at 5:25

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.