
PDF is one of the most widely used formats for sharing digital documents. In a Magento 2 store, whether you want to share invoices, quotes, or order summaries with your customers, PDF is a secure and device-friendly format. However, the default Magento 2 PDF generation methods are often rigid and limited in flexibility. This guide will walk you through the steps to programmatically generate a PDF file in Magento 2.
Sharing essential store data like invoices, quotes, or customer information in a secure and professional format is a common requirement for Magento 2 store owners. Among all formats, PDF stands out as the most reliable due to its universal compatibility, structured layout, and support for password protection.
While Magento 2 provides built-in methods for PDF generation, they often fall short in terms of flexibility and customization. That’s why many developers prefer a custom approach. In this article, we’ll walk you through the process of Magento 2 PDF generation programmatically. With just a few lines of code, you’ll be able to generate dynamic PDF files that can display any store data — from order details and customer info to product listings and more.
Why Use PDF Format in Magento 2?
Before diving into the implementation, here are a few reasons why PDF is a preferred choice:
- Device Compatibility: PDFs can be opened on any device without formatting issues.
- Security: They offer password protection and restricted access.
- Professional Look: PDF files maintain a clean and standardized appearance.
Limitations of Default PDF Generation in Magento 2
Magento 2 does come with built-in methods for PDF generation (like for invoices or shipments), but they are:
- Tightly coupled with specific modules
- Hard to customize for additional use-cases
- Limited in terms of styling and layout options
That’s why creating a custom PDF generation method can be more beneficial.
Let’s dive into how you can implement this custom PDF generation method in your Magento 2 module.
How to Generate PDF Programmatically in Magento 2?
Follow the steps below to generate a PDF using a custom controller in your Magento 2 module:
Step 1. Create a Controller File
All you'd be required to do is, to implement the below code in your controller action file:
<?php namespace Vendor\Extension\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Response\Http\FileFactory; class Index extends Action { protected $fileFactory; public function __construct( Context $context, FileFactory $fileFactory ) { $this->fileFactory = $fileFactory; parent::__construct($context); } public function execute() { $pdf = new \Zend_Pdf(); $pdf->pages[] = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4); $page = $pdf->pages[0]; // this will get reference to the first page. $style = new \Zend_Pdf_Style(); $style->setLineColor(new \Zend_Pdf_Color_Rgb(0,0,0)); $font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_TIMES); $style->setFont($font,15); $page->setStyle($style); $width = $page->getWidth(); $hight = $page->getHeight(); $x = 30; $pageTopalign = 700; $this->y = 700 - 100; $style->setFont($font,15); $page->setStyle($style); $page->drawRectangle(30, $this->y + 10, $page->getWidth()-30, $this->y +70, \Zend_Pdf_Page::SHAPE_DRAW_STROKE); $style->setFont($font,15); $page->setStyle($style); $page->drawText(__("Cutomer Details"), $x + 5, $this->y+50, 'UTF-8'); $style->setFont($font,11); $page->setStyle($style); $page->drawText(__("Name : %1", "Test MageDelight"), $x + 5, $this->y+33, 'UTF-8'); $page->drawText(__("Email : %1","[email protected] "), $x + 4, $this->y+15, 'UTF-8'); $style->setFont($font,11); $page->setStyle($style); $page->drawText(__("PRODUCT NAME"), $x + 60, $this->y-10, 'UTF-8'); $page->drawText(__("PRODUCT PRICE"), $x + 200, $this->y-10, 'UTF-8'); $page->drawText(__("QTY"), $x + 310, $this->y-10, 'UTF-8'); $page->drawText(__("SUB TOTAL"), $x + 440, $this->y-10, 'UTF-8'); $style->setFont($font,10); $page->setStyle($style); $add = 9; $page->drawText("$12.00", $x + 210, $this->y-30, 'UTF-8'); $page->drawText(10, $x + 330, $this->y-30, 'UTF-8'); $page->drawText("$120.00", $x + 470, $this->y-30, 'UTF-8'); $pro = "TEST product"; $page->drawText($pro, $x + 65, $this->y-30, 'UTF-8'); $page->drawRectangle(30, $this->y -62, $page->getWidth()-30, $this->y + 10, \Zend_Pdf_Page::SHAPE_DRAW_STROKE); $page->drawRectangle(30, $this->y -62, $page->getWidth()-30, $this->y - 100, \Zend_Pdf_Page::SHAPE_DRAW_STROKE); $style->setFont($font,15); $page->setStyle($style); $page->drawText(__("Total : %1", "$50.00"), $x + 435, $this->y-85, 'UTF-8'); $style->setFont($font,10); $page->setStyle($style); $page->drawText(__("Test Footer example"), ($page->getWidth()/2)-50, $this->y-200); $fileName = 'magedelight.pdf'; $this->fileFactory->create( $fileName, $pdf->render(), \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR, // this pdf will be saved in var directory with the name magedelight.pdf 'application/pdf' ); } }
Step 2. Hit the Controller URL
Once your module and routing are configured correctly, visit the controller URL (e.g., yourdomain.com/module/index/pdf) to trigger the PDF download.
Customize the Layout
You can enhance your PDF file by:
- Adding tables, logos, and custom fonts
- Drawing rectangles and borders for better formatting
- Using data dynamically fetched from your Magento 2 store
Also read: How to Generate PDF Catalog in Magento 2?
Final Thoughts
We hope this way of generating PDFs helped you communicate better with your customer and offer a better experience in your store. If you face any issues while implementing, our Magento 2 Experts will be happy to help. Feel free to reach us out.