Guest Post from our Partner PotatoCommerce – How to Make Custom PDF in Magento 2?

Magento 2 is a great platform in many aspects, but the implementation of PDF files leaves much to be desired. The thing is that PDF files are implemented through Zend_pdf of Zend framework, which creates great difficulties if you want to customize PDF print-outs.

Native PDF templates do not allow you to do many things that would be available if PDF templates were HTML files with CSS support. We recommend using PDF Customizer from PotatoCommerce to implement any of your ideas regarding PDF documents. This extension allows you to create PDF templates as simple as an HTML page.

https://potatocommerce.com/print-pdf-invoice-customizer-m2.html

To create custom PDF templates in Magento 2 based on native templates, you need to use the exact coordinates (X and Y) that will specify the absolute position of the objects (text, rectangle, etc.) on the PDF document. And this is not an easy task, we must say. Nevertheless, in this article, you will find all the answers to all your questions about the customization of native PDF files.

To modify PDF templates you need to override the following files:

  1. Magento\Sales\Model\Order\Pdf\Invoice
  2. Magento\Sales\Model\Order\Pdf\AbstratcPdf
  3. Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice
  4. Etc.

You can modify these files to add a custom footer, change the layout and add variables to a PDF document (brand, manufacturer, or any other custom product attribute). You can view the code of these documents on Magento 2 GitHub page.

How to modify Magento 2 PDF templates

Let’s start at the beginning (zend_pdf method):

$pdf = new \Zend_Pdf();
$this->_setPdf($pdf);

Now we can create a new page in the pdf:

$page = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);

Let’s say you want to change the color to gray. To do this, use the following command

$page->setFillColor(new \Zend_Pdf_Color_RGB(0, 0, 0));

You can also change the font-family:

$page->setFont(\Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_TIMES), 8);

You can also set a style:

$style = new \Zend_Pdf_Style();

And set bold font this way:

$this->_setFontBold($style, 12);
$page->drawText($title, 220, $this->y, ‘UTF-8’);

Let’s add some text to PDF templates:

$this->x = 130;
$this->y = 700;
$page->drawText(‘Hello PDF’, $this->x, $this->y, ‘UTF-8’);

The coordinates in Zend_PDF start at the bottom-left corner, and it may take a while to find the needed coordinate values manually.

Change Invoice PDF in Magento 2

Now, let’s customize Invoice PDF in Magento 2 in this file Magento\Sales\Model\Order\Pdf\Invoice

class Invoicepdf extends \Magento\Sales\Model\Order\Pdf\Invoice{

  public function getPdf($invoices = [])

   {

   //custom code of getpdf

     //add new footer section this is our custom code function…

     $this->_drawFooter($page);

    $this->_afterGetPdf();

    return $pdf;

   }

   protected function _drawFooter(\Zend_Pdf_Page $page)

   {

   $this->y =50;  

   $page->setFillColor(new \Zend_Pdf_Color_RGB(1, 1, 1));

   $page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5));

   $page->setLineWidth(0.5);

   $page->drawRectangle(60, $this->y, 510, $this->y -30);

   $page->setFillColor(new \Zend_Pdf_Color_RGB(0.1, 0.1, 0.1));

   $page->setFont(\Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA), 7);

   $this->y -=10;

   $page->drawText(“Company”, 75, $this->y, ‘UTF-8’);

   $page->drawText(“Tel: +123 456 789”, 220, $this->y, ‘UTF-8’);

   $page->drawText(“Skype: abcdefg”, 410, $this->y, ‘UTF-8’);

   }

}

You can learn more about in the Zend_PDF drawing guide here.

Here you can also find a video guide on how to change Magento 2 packing slips

https://www.youtube.com/watch?v=_baegIF0v48

Conclusion

It’s not easy to change invoice/order PDF in Magento 2 as it uses Zend_PDF. In this article, we provided an example of how to modify invoice PDF. We hope that you found this article helpful. If you found the customization of native Magento 2 PDF templates difficult, you can use PotatoCommerce PDF Customizer which is much easier to work with as it supports HTML and CSS.