Created
September 10, 2021 21:58
-
-
Save scgoeswild/b84cb365c2c1c065a5659a930dc10c26 to your computer and use it in GitHub Desktop.
Magento2 order invoice with custom footer field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* add this file to /var/www/html/app/code/Simeone/InvoicePdf/Pdf | |
* Copyright © Magento, Inc. All rights reserved. | |
* See COPYING.txt for license details. | |
*/ | |
namespace Magento\Sales\Model\Order\Pdf; | |
use Magento\Sales\Model\ResourceModel\Order\Invoice\Collection; | |
/** | |
* Sales Order Invoice PDF model | |
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | |
*/ | |
class Invoice extends AbstractPdf | |
{ | |
/** | |
* @var \Magento\Store\Model\StoreManagerInterface | |
*/ | |
protected $_storeManager; | |
/** | |
* @var \Magento\Framework\Locale\ResolverInterface | |
*/ | |
protected $_localeResolver; | |
/** | |
* @param \Magento\Payment\Helper\Data $paymentData | |
* @param \Magento\Framework\Stdlib\StringUtils $string | |
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig | |
* @param \Magento\Framework\Filesystem $filesystem | |
* @param Config $pdfConfig | |
* @param \Magento\Sales\Model\Order\Pdf\Total\Factory $pdfTotalFactory | |
* @param \Magento\Sales\Model\Order\Pdf\ItemsFactory $pdfItemsFactory | |
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate | |
* @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation | |
* @param \Magento\Sales\Model\Order\Address\Renderer $addressRenderer | |
* @param \Magento\Store\Model\StoreManagerInterface $storeManager | |
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver | |
* @param array $data | |
* | |
* @SuppressWarnings(PHPMD.ExcessiveParameterList) | |
*/ | |
public function __construct( | |
\Magento\Payment\Helper\Data $paymentData, | |
\Magento\Framework\Stdlib\StringUtils $string, | |
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, | |
\Magento\Framework\Filesystem $filesystem, | |
Config $pdfConfig, | |
\Magento\Sales\Model\Order\Pdf\Total\Factory $pdfTotalFactory, | |
\Magento\Sales\Model\Order\Pdf\ItemsFactory $pdfItemsFactory, | |
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, | |
\Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, | |
\Magento\Sales\Model\Order\Address\Renderer $addressRenderer, | |
\Magento\Store\Model\StoreManagerInterface $storeManager, | |
\Magento\Framework\Locale\ResolverInterface $localeResolver, | |
array $data = [] | |
) { | |
$this->_storeManager = $storeManager; | |
$this->_localeResolver = $localeResolver; | |
parent::__construct( | |
$paymentData, | |
$string, | |
$scopeConfig, | |
$filesystem, | |
$pdfConfig, | |
$pdfTotalFactory, | |
$pdfItemsFactory, | |
$localeDate, | |
$inlineTranslation, | |
$addressRenderer, | |
$data | |
); | |
} | |
/** | |
* 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' => __('Tax'), '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->_afterGetPdf(); | |
return $pdf; | |
} | |
/* Add Your Custom Information you want to show in this function */ | |
protected function _drawFooter(\Zend_Pdf_Page $page) | |
{ | |
$this->y =100; | |
$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(70, $this->y, 510, $this->y -60); | |
$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("MI-NY SRL Unipersonale - Via Friuli, 3 24052 Azzano San Paolo (BG) – Italy", 180, $this->y, 'UTF-8'); | |
$page->drawText("Tel.: +39 035 0067511 – Fax: +39 035 0067599 - Email: direzione@mi-nycosmetics.com", 160, $this->y -= 15 , 'UTF-8'); | |
$page->drawText("P.IVA/C.F./Registro Imprese BG 03754540163 – REA 405442", 200, $this->y -= 15 , 'UTF-8'); | |
$page->drawText("Capitale Sociale € 10.000,00 i.v.", 250, $this->y -= 15 , 'UTF-8'); | |
//$page->drawText("Registered in Countryname", 430, $this->y, 'UTF-8'); | |
} | |
/** | |
* 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->_drawFooter($page); // This line is adding information on each pages of PDF | |
$this->y = 800; | |
if (!empty($settings['table_header'])) { | |
$this->_drawHeader($page); | |
} | |
return $page; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment