How to add store information and terms and conditions inside pdf invoice file in magento 2?
Also how to add footer in each page of pdf in magento 2 invoice pages?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityFor pdf,
Pdf Starts Calculation from Bottom-Left Corner of Page and by default is measured in points. Start with X and Y axis at left bottom page.
We have to set based on our requirements set $this->x and $this->y axis.
Page size can be retrieved from a page object's get widht and height function, using $page object:
//start with zend_pdf method of zend frameworks,
$pdf = new \Zend_Pdf();
$this->_setPdf($pdf);
//create new page in pdf
$page = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
//set color
$page->setFillColor(new \Zend_Pdf_Color_RGB(0, 0, 0));
//set font
$page->setFont(\Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA), 8);
//set stye for pdf,
$style = new \Zend_Pdf_Style();
//set bold font
$this->_setFontBold($style, 10);
$page->drawText($title, 225, $this->y, 'UTF-8');
Page Height and widht for pdf,
SIZE_A4 = '595:842:';
SIZE_A4_LANDSCAPE = '842:595:';
SIZE_LETTER = '612:792:';
SIZE_LETTER_LANDSCAPE = '792:612:';
//get page widht
$width = $page->getWidth(); //width for A4 page, 595
//get page height
$height = $page->getHeight(); //Height for A4 page,842
//write text into pdf,
$this->x = 30;
$this->y = 800;
$page->drawText('This is test example', $this->x, $this->y, 'UTF-8');
Override invoice php file for invoice pdf customization, 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 name", 70, $this->y, 'UTF-8');
$page->drawText("Tel: +123 456 676", 230, $this->y, 'UTF-8');
$page->drawText("Registered in Countryname", 430, $this->y, 'UTF-8');
}
}
The x and y coordinate in Pdf aren't easy, we should calcuate by ourself.
First of all, we need to override the getPdf
method of Invoice Pdf
Module/Vendor/Model/Order/Pdf/Invoice.php
public function getPdf($invoices = [])
{
......
$this->insertStoreInformation($page);
......
$this->insertFooterInvoice($page);
......
}
Second, build our methods:
public function insertStoreInformation(&$page)
{
$page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
$font = $this->_setFontRegular($page, 10);
$page->setLineWidth(0);
$this->y = $this->y ? $this->y : 815;
$storeName =$this->scopeConfig->getValue(
'general/store_information/name',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
//Draw store name
if($storeName) {
foreach ($this->string->split($storeName, 45, true, true) as $_value) {
$page->drawText(
trim(strip_tags($_value)),
$this->getAlignRight($_value, 130, 440, $font, 10),
$top,
'UTF-8'
);
$top -= 10;
}
}
$this->y = $this->y > $top ? $top : $this->y;
}
public function insertFooterInvoice(&$page)
{
$this->y = $this->y ? $this->y : 100;
$text = 'Footer Invoice';
//Draw store name
if($text) {
foreach ($this->string->split($text, 45, true, true) as $_value) {
$page->drawText(
trim(strip_tags($_value)),
$this->getAlignRight($_value, 130, 440, $font, 10),
$top,
'UTF-8'
);
$top -= 10;
}
}
}
I am answering based on magento current version i.e. Magento2.6.The getPdf
function is slightly changed.It has private variable $this->appEmulation
due to this limitation we can't use preference to rewrite full file.
Based on it we can append footer using below code.
You can achieve this by using a plugin for the getPdf()
method. Here's an updated example:
Create a custom module if you haven't already done so.
Create a di.xml file in your module's etc directory: app/code/YourNamespace/YourModule/etc/di.xml.
Add the plugin configuration in the di.xml file to specify your plugin class:
<type name="Magento\Sales\Model\Order\Pdf\Invoice">
<plugin name="your_plugin_name" type="YourNamespace\YourModule\Plugin\Sales\Order\Pdf\InvoicePlugin"
sortOrder="1" disabled="false"/>
</type>
Create the plugin class InvoicePlugin.php in the directory app/code/YourNamespace/YourModule/Plugin/Sales/Order/Pdf:
<?php
namespace YourNamespace\YourModule\Plugin\Sales\Order\Pdf;
class InvoicePlugin
{
public function aroundGetPdf(
\Magento\Sales\Model\Order\Pdf\Invoice $subject,
\Closure $proceed,
$invoices = []
) {
// Call the original method to generate the invoice PDF
$pdf = $proceed($invoices);
// Add footer to each page of the PDF
foreach ($pdf->pages as $page) {
$this->addFooterToPage($page);
}
return $pdf;
}
protected function addFooterToPage($page)
{
// Set font style for the footer
$font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA);
$page->setFont($font, 10);
// Set footer text and position
$footerText = __('Your Footer Text Here');
$footerPosition = 50; // Adjust the position as needed
// Draw the footer
$page->drawText($footerText, 25, $footerPosition, 'UTF-8');
}
}