How to Generate PDF Programmatically in Magento 2
Magento 2 store admin often want to share their store data. Either be it an invoice to be shared with customer or quote to be shared with the potential visitor! PDF is the preferred format to share the data as it can be easily viewed on any device and it’s highly trusted for security and password protection.
The default Magento methods to generate PDF files are rather inflexible. That’s why I’ve come up with the method to generate PDF programmatically in Magento 2. The method allows to generate PDF displaying any type of the store data, be it grid or invoice or customer data, etc.
Method to generate PDF programmatically in Magento 2:
Implement the below code in your controller action file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
<?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,13); $page->setStyle($style); $width = $page->getWidth(); $hight = $page->getHeight(); $x = 30; $pageTopalign = 850; $this->y = 850 - 100; $style->setFont($font,14); $page->setStyle($style); $page->drawRectangle(30, $this->y + 10, $page->getWidth()-30, $this->y +70, \Zend_Pdf_Page::SHAPE_DRAW_STROKE); $style->setFont($font,13); $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 Meetanshi"), $x + 5, $this->y+33, '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 = 'meetanshi.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 meetanshi.pdf 'application/pdf' ); } } |
With the above code, generate PDF programmatically in Magento 2 and share the data securely and user-friendly!
You can also Generate attractive PDF catalogs for various product categories by using our Magento 2 PDF Catalog.
Hopefully, the post is helpful to the readers and easy to implement. However, doubts about the implementation of the topic are welcome in the Comments section for me to solve!
Rate the post with 5 stars to appreciate the work 🙂
Keep Sharing
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
12 Comments
hii,i want to add same content in every new page how to do that?
Hello Ami,
You’ll need to override newPage function and add particular code into that function.
Thank You
Hi @Sanjay Jethva, after creating the pdf i want to open in the browser, currently it will save then i have to open the pdf manually. I want if pdf is created just after creation it automatically open in my browser.
Hello Sangam,
You need to apply custom code for such requirement
Thank You
HI,
I want to continue to next content on next page in pdf.
Hello Ankit,
Line number 24 is for getting a reference of the first page,
Put that line wherever you want to get a new page and replace 0 with 1 in ‘ $page = $pdf->pages[0]; ‘
Thank You
Hi, I want to generate the page dynamically in the loop.
Thanks.
Hello Priyanka,
To generate new page, check the below solution:
$pdf->pages[] = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
Thank you.
Thanks Sanjay! I already added this line and its creates the new page but the new pages are getting the first page content. It needs to continue to next content.
Hey,
Please try using index variable. Something like:
$pdf->pages[0] , $pdf->pages[1] $pdf->pages[2]
Thanks.
Hi, I need to generate the pdf file in multiple pages. Please let me know how to do?
Hello,
You need to use $pdf->newPage(\Zend_Pdf_Page::SIZE_A4); for new page.
Thanks.