magento\app\code\Namespace\Rewrite\etc\di.xml
magento\app\code\Namespace\Rewrite\etc\di.xml
magento\app\code\Namespace\Rewrite\Magento\Sales\Model\Order\Pdf\Invoice.php
<?php
namespace Namespace\Rewrite\Magento\Sales\Model\Order\Pdf;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Model\Order\Pdf\Config;
use Magento\Sales\Model\Order\Pdf\Invoice as BaseInvoice;
class Invoice extends BaseInvoice {
magento\app\code\Namespace\Rewrite\Magento\Sales\Model\Order\Pdf\Invoice.php
protected $_layout; <?php
namespace Namespace\Rewrite\Magento\Sales\Model\Order\Pdf;
/**
* Invoice constructor.
* @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 LayoutInterface $layout
* @param array $data
*/
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,
LayoutInterface $layout,
array $data = [])
{
$this->_layout = $layout;
parent::__construct($paymentData,
$string,
$scopeConfig,
$filesystem,
$pdfConfig,
$pdfTotalFactory,
$pdfItemsFactory,
$localeDate,
$inlineTranslation,
$addressRenderer,
$storeManager,
$localeResolver,
use $data);
}Magento\Framework\View\LayoutInterface;
/**
* @param \Zend_Pdf_Page $object
* @param int $size
* @return \Zend_Pdf_Resource_Font
* @throws \Zend_Pdf_Exception
*/
protected function _setFontRegular($object, $size = 12)
{
$font = \Zend_Pdf_Font::fontWithPath(
$this->_rootDirectory->getAbsolutePath('app/code/Namespace/Rewrite/Magento/Sales/web/fonts/arial.ttf')
);
$object->setFont($font, $size);
returnuse $font;
}Magento\Sales\Model\Order\Pdf\Config;
/**
* @param \Zend_Pdf_Page $object
* @param int $size
* @return \Zend_Pdf_Resource_Font
* @throws \Zend_Pdf_Exception
*/
protected function _setFontBold($object, $size = 12)
{
$font = \Zend_Pdf_Font::fontWithPath(
$this->_rootDirectory->getAbsolutePath('app/code/Namespace/Rewrite/Magento/Sales/web/fonts/arialbd.ttf')
);
$object->setFont($font, $size);
use Magento\Sales\Model\Order\Pdf\Invoice returnas $font;
}BaseInvoice;
/**
* @param \Zend_Pdf_Page $object
* @param int $size
* @return \Zend_Pdf_Resource_Font
* @throws \Zend_Pdf_Exception
*/
protected function _setFontItalic($object, $size = 12)
{
$font = \Zend_Pdf_Font::fontWithPath(
$this->_rootDirectory->getAbsolutePath('app/code/Namespace/Rewrite/Magento/Sales/web/fonts/ariali.ttf')
);
$object->setFont($font, $size);
return $font;
}
/**
* @param array $invoices
* @return \Zend_Pdf
* @throws \Zend_Pdf_Exception
*/
public functionclass getPdf($invoicesInvoice =extends [])BaseInvoice
{
$this->_beforeGetPdf();
$this->_initRenderer('invoice');protected $_layout;
$pdf/**
= new \Zend_Pdf(); * Invoice constructor.
$this->_setPdf($pdf); * @param \Magento\Payment\Helper\Data $paymentData
$style =* new@param \Zend_Pdf_Style\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 LayoutInterface $layout
* @param array $data
*/
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,
LayoutInterface $layout,
array $data = []);
{
$this->_setFontBold>_layout = $layout;
parent::__construct($style$paymentData,
10 $string,
$scopeConfig,
$filesystem,
$pdfConfig,
$pdfTotalFactory,
$pdfItemsFactory,
$localeDate,
$inlineTranslation,
$addressRenderer,
$storeManager,
$localeResolver,
$data);
}
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 header@param text\Zend_Pdf_Page */$object
* @param int $this->drawHeaderText($page);$size
/* Add address@return */\Zend_Pdf_Resource_Font
* $this->insertAddress($page,@throws $invoice->getStore());\Zend_Pdf_Exception
/* Add head */
protected function $this->insertOrder_setFontRegular(
$page,
$order$object,
$size = $this->_scopeConfig->isSetFlag(12)
self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID,{
$font = \Magento\Store\Model\ScopeInterface\Zend_Pdf_Font::SCOPE_STORE,fontWithPath(
$order$this->getStoreId>_rootDirectory->getAbsolutePath()
'app/code/Namespace/Rewrite/Magento/Sales/web/fonts/arial.ttf')
);
$object->setFont($font, $size);
return $font;
}
/**
* Add@param document\Zend_Pdf_Page text$object
and number * @param int $size
* @return \Zend_Pdf_Resource_Font
* @throws \Zend_Pdf_Exception
*/
protected function _setFontBold($object, $size = 12)
{
$font = \Zend_Pdf_Font::fontWithPath(
$this->insertDocumentNumber>_rootDirectory->getAbsolutePath($page,'app/code/Namespace/Rewrite/Magento/Sales/web/fonts/arialbd.ttf')
__('Invoice # ' );
. $invoice $object->getIncrementId>setFont()$font, $size);
return $font;
}
/**
* Add@param table\Zend_Pdf_Page $object
* @param int $size
* @return \Zend_Pdf_Resource_Font
* @throws \Zend_Pdf_Exception
*/
protected function _setFontItalic($object, $size = 12)
{
$font = \Zend_Pdf_Font::fontWithPath(
$this->_drawHeader>_rootDirectory->getAbsolutePath($page'app/code/Namespace/Rewrite/Magento/Sales/web/fonts/ariali.ttf')
);
$object->setFont($font, $size);
return $font;
}
/**
* Add@param bodyarray $invoices
* @return \Zend_Pdf
* @throws \Zend_Pdf_Exception
*/
public function getPdf($invoices = foreach[])
{
$this->_beforeGetPdf($invoice);
$this->getAllItems>_initRenderer('invoice');
$pdf = new \Zend_Pdf();
$this->_setPdf($pdf);
$style = new \Zend_Pdf_Style();
$this->_setFontBold($style, 10);
foreach ($invoices as $item$invoice) {
if ($item$invoice->getOrderItem>getStoreId()) {
$this->getParentItem>_localeResolver->emulate($invoice->getStoreId()) {;
continue;$this->_storeManager->setCurrentStore($invoice->getStoreId());
}
$page = $this->newPage();
$order = $invoice->getOrder();
/* Add image */
$this->insertLogo($page, $invoice->getStore());
/* Add header text */
$this->drawHeaderText($page);
/* 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();
}
/* Add footer content */
$this->drawFooter($page);
}
/* Add footer content */$this->_afterGetPdf();
$this->drawFooter($page);return $pdf;
}
$this->_afterGetPdf();
return $pdf;
}
/**
* @param \Zend_Pdf_Page $page
* @param String $value
*/
public function drawFooter(\Zend_Pdf_Page $page)
{
try {
$font = $this->_setFontRegular($page, 10);
$this->y -= 10;
$value = $this->getFooterContent();
$line = 52;
if ($value !== '') {
$value = preg_replace('/<br[^>]*>/i', "\n", $value);
$page->setFillColor(new \Zend_Pdf_Color_RGB(0, 0, 0));
$page->setLineColor(new \Zend_Pdf_Color_GrayScale(0.5));
foreach(explode("\n", $value) as $textLine){
$feed = $this->getAlignCenter($textLine, 30, 520, $font, 10);
$page->drawText(strip_tags($textLine), $feed, $line, 'UTF-8');
$line -=16;
}
$page->setFillColor(new \Zend_Pdf_Color_GrayScale(0));
}
$page$this->setFillColor(new>y \Zend_Pdf_Color_GrayScale-= 20;
} catch (0\Exception $e) {
$this->_logger->critical($e);
}
$this->y -= 20;
} catch (\Exception $e) {
$this->_logger->critical($e);
}
}
/**
* @param \Zend_Pdf_Page $page
*/
public function drawHeaderText(\Zend_Pdf_Page $page)
{
try {
$this->_setFontRegular($page, 16);
$page->drawText('CUSTOMER INVOICE', 410, 800, 'UTF-8');
}
catch (\Exception $e) {
$this->_logger->critical($e);
}
}
}
/**
* @return mixed
*/
public function getFooterContent()
{ */
if($footerCmspublic =function $this->_layout->createBlock('Magento\Cms\Block\Block')->setBlockIdgetFooterContent('pdf_invoice_text'))
{
if($footerCms = $this->_layout->createBlock('Magento\Cms\Block\Block')->setBlockId('pdf_invoice_text'))
{
return $footerCms->toHtml();
}
}
}
}
magento\app\code\Namespace\Rewrite\registration.php
magento\app\code\Namespace\Rewrite\registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Namespace_Rewrite',
__DIR__
);
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Namespace_Rewrite', DIR );