Adding Custom Text to Magento Invoice PDF
Welcome to our guide on adding custom text to your Magento invoice PDFs! As a new developer, you may be looking for ways to personalize your invoices with terms and conditions or additional information.
Understanding the Invoice Process
Before we dive into the solution, let’s briefly understand how Magento generates PDF invoices. When an order is marked as invoiced in the backend, Magento uses the Mage_Sales_Model_Order_PdfInvoice model to generate a new PDF file containing invoice details.
Overriding the Default Invoice Template
To add custom text at the bottom of your invoices, you’ll need to override the default PDF invoice layout. Here’s how:
-
Create a Custom Layout File
Navigate to
app/design/frontend/{your_package}/{your_theme}/layout/and create a new XML file namedsales.xml.<layout version="0.1.0"> <!-- Override the default invoice layout --> <update handle="invoice_pdf_items_order"/> </layout> -
Add Custom Text
In your theme’s template directory, create a new folder named
templateif it doesn’t exist. Inside this folder, make another folder calledsales. Finally, inside thesalesfolder, create an XML file namedorder_invoice_pdf_items.xml.<layout version="0.1.0"> <!-- Define your custom text --> <block type="core/text" name="additional.information" template="sales/items.phtml"/> </layout> -
Define the Custom Text
In
app/design/frontend/{your_package}/{your_theme}/template/sales/, create a new PHP file nameditems.phtml.<?php echo $this->getLayout()->createBlock('core/text')->setText('<p>This is your custom text at the bottom of the invoice.</p>'); ?> -
Update the Layout XML File
Open your
sales.xmlfile from step 1 and update it as follows:<layout version="0.1.0"> <!-- Override the default invoice layout --> <update handle="invoice_pdf_items_order"/> <reference name="root"> <action method="setTemplate" ifconfig="design/package/name" template="page/html/pager.phtml"/> </reference> </layout>
Testing Your Changes
After making these changes, you’ll need to refresh your cache using the Magento admin panel (System > Cache Management) or by running php bin/magento cache:flush in your terminal if you’re on a recent version.
To test your custom invoice layout:
- Create a new order and mark it as invoiced.
- Click ‘PDF Invoice’ to generate the PDF file.
- Open the generated PDF to see your custom text at the bottom of the invoice!
Conclusion
Adding custom text to Magento’s invoice PDFs requires some knowledge of theme development and layout management. By following this guide, you should now be able to add terms and conditions or any other necessary information to the footer of your invoices.
Remember, when working with Magento themes, it’s always a good practice to create child themes for your modifications. This ensures that your changes are preserved even if you update your parent theme in the future.
If you found this guide helpful, feel free to explore more of our Magento tutorials at Laravel Company. Happy coding!