Magento 2 TutorialsMarch 30, 2024Simon Walker

Customizing Invoice PDF in Magento 2: An Overview

Customizing Invoice PDF in Magento 2: An Overview

What is Magento 2 Invoice PDF?

In Magento 2, an invoice PDF is a document generated by the system that represents a formal invoice in a Portable Document Format (PDF). An invoice is a crucial document in the context of e-commerce and retail, as it serves as a record of a customer's purchase and outlines the details of the transaction.

When a customer places an order and completes the checkout process, Magento 2 generates an order and associated invoices for that order. An invoice PDF contains important information such as:
  • Order Details: The invoice PDF includes the order number, date, and other relevant order-related information.
  • Billing and Shipping Information: It provides the billing and shipping addresses for the customer.
  • Product Details: The products purchased by the customer are listed, along with their names, quantities, prices, and subtotal.
  • Tax and Discounts: Any applicable taxes, discounts, and promotions are detailed on the invoice.
  • Shipping and Handling Charges: If the order includes shipping and handling charges, these are also specified.
  • Total Amount: The total amount payable by the customer, including taxes and other charges, is prominently displayed.
  • Payment Information: Information about the payment method used by the customer is often included on the invoice.
  • Contact Information: Contact details for the store, including customer service information, might be included.

Magento 2 provides a default template for generating invoice PDFs. However, as part of its customization capabilities, you can modify this template to tailor the appearance and content of the invoice to match your store's branding and specific requirements.

Customizing the invoice PDF template allows you to create a professional and cohesive experience for your customers while maintaining consistency with your overall store design.

Send Invoices to Customers

Although it is not a common practice, but it is better to send invoices to the customer. Similarly, customers should demand an invoice. Let’s discuss why.
  • Proof
  • The invoice can be used as a proof in the court of law to show that you have purchase the said product. Similarly, the merchant can use it to prove that they have sold the product. In case issues arise related to the sale/purchase, the invoice can be used for conflict resolution.

  • Record Keeping
  • As an individual, you should keep an eye on your expenses. It helps you plan better. With invoices, you can keep a record of all expenses going back several years. It can help you identify areas where to cut expenses. Likewise, from the merchant perspective, proper record keeping can help you keep an eye on your store’s progress.

  • Professionalism
  • Sending a proper invoice to a customer enhances your store’s professionalism. Remember, in the business world, your product quality or level of customer service is not the only basis on which customers judge you. They evaluate every possible aspect, including the quality and types of documents.

Sending Invoices to Customers on Magento 2

This brings us to our next point - Invoice Email Extension for Magento 2. With this extension, customers can provide you with an invoice email during the registration or checkout phases. Once they complete the purchase, they will get a professional-looking invoice.

magento-2-invoice-email

As you can see from the above image, this is how easy it is for customers to get invoices in their email.

How to Customize Invoice PDF in Magento 2?

Customizing invoice PDFs in Magento 2 involves modifying the templates and layouts used to generate PDF documents. The process requires some technical knowledge, including familiarity with Magento's module structure and the use of XML layout files and templates. Here's a general overview of the steps involved:

1. Create a Custom Module (if not already done):

If you haven't created a custom module in Magento 2 before, you'll need to create one to house your customizations. You can follow these steps to create a basic module structure:
  • Create the necessary directories and files for your module in the 'app/code' directory of your Magento installation.
  • Register your module by creating a 'registration.php' file.
  • Create a 'etc/module.xml' file to define your module.

2. Override the Invoice PDF Template:

Magento 2 uses templates and layout files to generate PDF documents. You'll need to override the default template used for invoice PDFs. Here's how:
  • Create a 'view' directory in your module's directory if it doesn't exist already.
  • Inside the 'view' directory, create a 'adminhtml' subdirectory, and within it, create a 'templates' subdirectory.
  • Copy the original 'invoice.phtml' template file from 'vendor/magento/module-sales/view/adminhtml/templates/order/invoice' to your module's template directory.
  • Customize the copied 'invoice.phtml' template as per your requirements.

3. Create a Layout XML File:

Create a layout XML file in your module to specify the new template for the invoice PDF.
  • Inside your module's 'view' directory, create a 'layout' subdirectory.
  • Create an XML file (e.g., 'sales_order_invoice_print.xml') within the 'layout' directory.
  • In this XML file, specify the new template for the invoice PDF:
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
<update handle="sales_email_order_invoice_items" />
<referenceContainer name="content">
<block class="Magento\Sales\Block\Order\Invoice" name="invoice" template="Vendor_Module::path_to_your_invoice_phtml_file.phtml"/>
</referenceContainer>
</layout>

Replace "Vendor_Module" with the actual vendor and module name, and replace "path_to_your_invoice_phtml_file.phtml" with the path to your custom invoice template file.

4. Apply the Layout Update:

To apply the layout update, you might need to clear the cache and reindex. Run the following commands in your Magento root directory:
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento indexer:reindex

5. Test the Customization:

After completing the above steps, create a test invoice in your Magento store and generate its PDF. The customized invoice PDF template should now be used.

Please note that this is a general overview, and the actual implementation might vary based on your specific requirements and the structure of your Magento 2 installation. Always make backups and follow best practices when making customizations to ensure the stability and future upgrade compatibility of your Magento store.