Hello
Today I saw you how to change label and add footer to PDF Invoice.
For doing this, we have to make module and override invoice file.
Create extension like this.
app/code/Czargroup/Pdfinvoice/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Czargroup_Pdfinvoice',
__DIR__
);
app/code/Czargroup/Pdfinvoice/etc/module.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Czargroup_Pdfinvoice" setup_version="1.0.0" />
</config>
app/code/Czargroup/Pdfinvoice/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sales\Model\Order\Pdf\Invoice"
type="Czargroup\Pdfinvoice\Model\Order\Pdf\Invoice"/>
</config>
app/code/Czargroup/Pdfinvoice/Model/Order/Pdf/Invoice.php
<?php
namespace Czargroup\Pdfinvoice\Model\Order\Pdf;
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
/**
* Draw header for item table
*
* @param \Zend_Pdf_Page $page
* @return void
*/
protected function _drawHeader(\Zend_Pdf_Page $page)
{
/* Add table head */
$this->_setFontRegular($page, 10);
$page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
$page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5));
$page->setLineWidth(0.5);
$page->drawRectangle(25, $this->y, 570, $this->y - 15);
$this->y -= 10;
$page->setFillColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));
//columns headers
$lines[0][] = ['text' => __('Products'), 'feed' => 35];
$lines[0][] = ['text' => __('SKU'), 'feed' => 290, 'align' => 'right'];
$lines[0][] = ['text' => __('Qty'), 'feed' => 435, 'align' => 'right'];
$lines[0][] = ['text' => __('Price'), 'feed' => 360, 'align' => 'right'];
$lines[0][] = ['text' => __('VAT'), 'feed' => 495, 'align' => 'right'];
$lines[0][] = ['text' => __('Subtotal'), 'feed' => 565, 'align' => 'right'];
$lineBlock = ['lines' => $lines, 'height' => 5];
$this->drawLineBlocks($page, [$lineBlock], ['table_header' => true]);
$page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
$this->y -= 20;
}
/**
* Return PDF document
*
* @param array|Collection $invoices
* @return \Zend_Pdf
*/
public function getPdf($invoices = [])
{
$this->_beforeGetPdf();
$this->_initRenderer('invoice');
$pdf = new \Zend_Pdf();
$this->_setPdf($pdf);
$style = new \Zend_Pdf_Style();
$this->_setFontBold($style, 10);
foreach ($invoices as $invoice) {
if ($invoice->getStoreId()) {
$this->_localeResolver->emulate($invoice->getStoreId());
$this->_storeManager->setCurrentStore($invoice->getStoreId());
}
$page = $this->newPage();
$order = $invoice->getOrder();
/* Add image */
$this->insertLogo($page, $invoice->getStore());
/* Add address */
$this->insertAddress($page, $invoice->getStore());
/* Add head */
$this->insertOrder(
$page,
$order,
$this->_scopeConfig->isSetFlag(
self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$order->getStoreId()
)
);
/* Add document text and number */
$this->insertDocumentNumber($page, __('Invoice # ') . $invoice->getIncrementId());
/* Add table */
$this->_drawHeader($page);
/* Add body */
foreach ($invoice->getAllItems() as $item) {
if ($item->getOrderItem()->getParentItem()) {
continue;
}
/* Draw item */
$this->_drawItem($item, $page, $order);
$page = end($pdf->pages);
}
/* Add totals */
$this->insertTotals($page, $invoice);
if ($invoice->getStoreId()) {
$this->_localeResolver->revert();
}
}
$this->_drawFooter($page); // the custom text is added here
$this->_afterGetPdf();
return $pdf;
}
/**
* Create new page and assign to PDF object
*
* @param array $settings
* @return \Zend_Pdf_Page
*/
public function newPage(array $settings = [])
{
/* Add new table head */
$page = $this->_getPdf()->newPage(\Zend_Pdf_Page::SIZE_A4);
$this->_getPdf()->pages[] = $page;
$this->y = 800;
if (!empty($settings['table_header'])) {
$this->_drawHeader($page);
}
return $page;
}
public function _drawFooter(\Zend_Pdf_Page $page)
{
$this->y =50;
//$page->setFillColor(new \Zend_Pdf_Color_RGB(1, 1, 1));
$page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
//$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), 11);
//$this->y -=0;
$page->drawText("OutdoorLivingUK Products Ltd, Unit C Oaklea House, Limesquare Estate, Peterborough PE3 8YQ", 70, $this->y, 'UTF-8');
$this->y -=15;
$page->drawText("Company Reg 07807363", 70, $this->y, 'UTF-8');
//$page->drawText("Tel: +123 456 676", 230, $this->y, 'UTF-8');
$page->drawText("VAT 169691651", 470, $this->y, 'UTF-8');
}
}
It will display result like this:
Today I saw you how to change label and add footer to PDF Invoice.
For doing this, we have to make module and override invoice file.
Create extension like this.
app/code/Czargroup/Pdfinvoice/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Czargroup_Pdfinvoice',
__DIR__
);
app/code/Czargroup/Pdfinvoice/etc/module.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Czargroup_Pdfinvoice" setup_version="1.0.0" />
</config>
app/code/Czargroup/Pdfinvoice/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Sales\Model\Order\Pdf\Invoice"
type="Czargroup\Pdfinvoice\Model\Order\Pdf\Invoice"/>
</config>
app/code/Czargroup/Pdfinvoice/Model/Order/Pdf/Invoice.php
<?php
namespace Czargroup\Pdfinvoice\Model\Order\Pdf;
class Invoice extends \Magento\Sales\Model\Order\Pdf\Invoice
{
/**
* Draw header for item table
*
* @param \Zend_Pdf_Page $page
* @return void
*/
protected function _drawHeader(\Zend_Pdf_Page $page)
{
/* Add table head */
$this->_setFontRegular($page, 10);
$page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
$page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5));
$page->setLineWidth(0.5);
$page->drawRectangle(25, $this->y, 570, $this->y - 15);
$this->y -= 10;
$page->setFillColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));
//columns headers
$lines[0][] = ['text' => __('Products'), 'feed' => 35];
$lines[0][] = ['text' => __('SKU'), 'feed' => 290, 'align' => 'right'];
$lines[0][] = ['text' => __('Qty'), 'feed' => 435, 'align' => 'right'];
$lines[0][] = ['text' => __('Price'), 'feed' => 360, 'align' => 'right'];
$lines[0][] = ['text' => __('VAT'), 'feed' => 495, 'align' => 'right'];
$lines[0][] = ['text' => __('Subtotal'), 'feed' => 565, 'align' => 'right'];
$lineBlock = ['lines' => $lines, 'height' => 5];
$this->drawLineBlocks($page, [$lineBlock], ['table_header' => true]);
$page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
$this->y -= 20;
}
/**
* Return PDF document
*
* @param array|Collection $invoices
* @return \Zend_Pdf
*/
public function getPdf($invoices = [])
{
$this->_beforeGetPdf();
$this->_initRenderer('invoice');
$pdf = new \Zend_Pdf();
$this->_setPdf($pdf);
$style = new \Zend_Pdf_Style();
$this->_setFontBold($style, 10);
foreach ($invoices as $invoice) {
if ($invoice->getStoreId()) {
$this->_localeResolver->emulate($invoice->getStoreId());
$this->_storeManager->setCurrentStore($invoice->getStoreId());
}
$page = $this->newPage();
$order = $invoice->getOrder();
/* Add image */
$this->insertLogo($page, $invoice->getStore());
/* Add address */
$this->insertAddress($page, $invoice->getStore());
/* Add head */
$this->insertOrder(
$page,
$order,
$this->_scopeConfig->isSetFlag(
self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$order->getStoreId()
)
);
/* Add document text and number */
$this->insertDocumentNumber($page, __('Invoice # ') . $invoice->getIncrementId());
/* Add table */
$this->_drawHeader($page);
/* Add body */
foreach ($invoice->getAllItems() as $item) {
if ($item->getOrderItem()->getParentItem()) {
continue;
}
/* Draw item */
$this->_drawItem($item, $page, $order);
$page = end($pdf->pages);
}
/* Add totals */
$this->insertTotals($page, $invoice);
if ($invoice->getStoreId()) {
$this->_localeResolver->revert();
}
}
$this->_drawFooter($page); // the custom text is added here
$this->_afterGetPdf();
return $pdf;
}
/**
* Create new page and assign to PDF object
*
* @param array $settings
* @return \Zend_Pdf_Page
*/
public function newPage(array $settings = [])
{
/* Add new table head */
$page = $this->_getPdf()->newPage(\Zend_Pdf_Page::SIZE_A4);
$this->_getPdf()->pages[] = $page;
$this->y = 800;
if (!empty($settings['table_header'])) {
$this->_drawHeader($page);
}
return $page;
}
public function _drawFooter(\Zend_Pdf_Page $page)
{
$this->y =50;
//$page->setFillColor(new \Zend_Pdf_Color_RGB(1, 1, 1));
$page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
//$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), 11);
//$this->y -=0;
$page->drawText("OutdoorLivingUK Products Ltd, Unit C Oaklea House, Limesquare Estate, Peterborough PE3 8YQ", 70, $this->y, 'UTF-8');
$this->y -=15;
$page->drawText("Company Reg 07807363", 70, $this->y, 'UTF-8');
//$page->drawText("Tel: +123 456 676", 230, $this->y, 'UTF-8');
$page->drawText("VAT 169691651", 470, $this->y, 'UTF-8');
}
}
It will display result like this:
No comments:
Post a Comment