Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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="MagentoSalesModelOrderPdfInvoice" type="VendornameModulenameModelOrderPdfInvoice"/>
- </config>
- <?php
- namespace ModulenameModulenameModelOrderPdf;
- use MagentoSalesModelOrderPdfConfig;
- class Invoice extends MagentoSalesModelOrderPdfInvoice
- {
- /**
- * @var MagentoStoreModelStoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var MagentoFrameworkLocaleResolverInterface
- */
- protected $_localeResolver;
- /**
- * @param MagentoPaymentHelperData $paymentData
- * @param MagentoFrameworkStdlibStringUtils $string
- * @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
- * @param MagentoFrameworkFilesystem $filesystem
- * @param Config $pdfConfig
- * @param MagentoSalesModelOrderPdfTotalFactory $pdfTotalFactory
- * @param MagentoSalesModelOrderPdfItemsFactory $pdfItemsFactory
- * @param MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate
- * @param MagentoFrameworkTranslateInlineStateInterface $inlineTranslation
- * @param MagentoSalesModelOrderAddressRenderer $addressRenderer
- * @param MagentoStoreModelStoreManagerInterface $storeManager
- * @param MagentoFrameworkLocaleResolverInterface $localeResolver
- * @param array $data
- *
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- MagentoPaymentHelperData $paymentData,
- MagentoFrameworkStdlibStringUtils $string,
- MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
- MagentoFrameworkFilesystem $filesystem,
- Config $pdfConfig,
- MagentoSalesModelOrderPdfTotalFactory $pdfTotalFactory,
- MagentoSalesModelOrderPdfItemsFactory $pdfItemsFactory,
- MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
- MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
- MagentoSalesModelOrderAddressRenderer $addressRenderer,
- MagentoStoreModelStoreManagerInterface $storeManager,
- MagentoFrameworkLocaleResolverInterface $localeResolver,
- array $data = []
- )
- {
- parent::__construct(
- $paymentData,
- $string,
- $scopeConfig,
- $filesystem,
- $pdfConfig,
- $pdfTotalFactory,
- $pdfItemsFactory,
- $localeDate,
- $inlineTranslation,
- $addressRenderer,
- $storeManager,
- $localeResolver,
- $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,
- MagentoStoreModelScopeInterface::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);
- $this->_afterGetPdf();
- return $pdf;
- }
- /**
- * Set font as regular
- *
- * @param Zend_Pdf_Page $object
- * @param int $size
- * @return Zend_Pdf_Resource_Font
- */
- protected function _setFontRegular($object, $size = 7)
- {
- $font = Zend_Pdf_Font::fontWithPath(
- $this->_rootDirectory->getAbsolutePath('lib/internal/open-sans/OpenSans-Regular.ttf')
- );
- $object->setFont($font, $size);
- return $font;
- }
- /**
- * Set font as bold
- *
- * @param Zend_Pdf_Page $object
- * @param int $size
- * @return Zend_Pdf_Resource_Font
- */
- protected function _setFontBold($object, $size = 7)
- {
- $font = Zend_Pdf_Font::fontWithPath(
- $this->_rootDirectory->getAbsolutePath('lib/internal/open-sans/OpenSans-Bold.ttf')
- );
- $object->setFont($font, $size);
- return $font;
- }
- /**
- * Set font as italic
- *
- * @param Zend_Pdf_Page $object
- * @param int $size
- * @return Zend_Pdf_Resource_Font
- */
- protected function _setFontItalic($object, $size = 7)
- {
- $font = Zend_Pdf_Font::fontWithPath(
- $this->_rootDirectory->getAbsolutePath('lib/internal/open-sans/OpenSans-Italic.ttf')
- );
- $object->setFont($font, $size);
- return $font;
- }
- protected function insertLogo(&$page, $store = null)
- {
- $this->y = $this->y ? $this->y : 815;
- $image = $this->_scopeConfig->getValue(
- 'sales/identity/logo',
- MagentoStoreModelScopeInterface::SCOPE_STORE,
- $store
- );
- if ($image) {
- $imagePath = '/sales/store/logo/' . $image;
- if ($this->_mediaDirectory->isFile($imagePath)) {
- $image = Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($imagePath));
- $top = 830;
- //top border of the page
- $widthLimit = 270;
- //half of the page width
- $heightLimit = 270;
- //assuming the image is not a "skyscraper"
- $width = $image->getPixelWidth();
- $height = $image->getPixelHeight();
- //preserving aspect ratio (proportions)
- $ratio = $width / $height;
- if ($ratio > 1 && $width > $widthLimit) {
- $width = $widthLimit;
- $height = $width / $ratio;
- } elseif ($ratio < 1 && $height > $heightLimit) {
- $height = $heightLimit;
- $width = $height * $ratio;
- } elseif ($ratio == 1 && $height > $heightLimit) {
- $height = $heightLimit;
- $width = $widthLimit;
- }
- $y1 = $top - $height;
- $y2 = $top;
- $x1 = 158;
- $x2 = $x1 + $width;
- //coordinates after transformation are rounded by Zend
- $page->drawImage($image, $x1, $y1, $x2, $y2);
- $this->y = $y1 - 10;
- }
- }
- }
- /**
- * 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;
- }
- protected function _drawFooter(Zend_Pdf_Page $page)
- {
- $this->y =40;
- $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));
- $font = Zend_Pdf_Font::fontWithPath(
- $this->_rootDirectory->getAbsolutePath('lib/internal/open-sans/OpenSans-Regular.ttf')
- );
- $page->setFont($font, 8);
- $this->y -=15;
- $page->drawText("Mailing Address: ABC Road sector-1 Noida", 285, $this->y, 'UTF-8');
- $this->y -=10;
- $page->drawText("help@abc.com", 285, $this->y, 'UTF-8');
- $store = null;
- $this->y = $this->y ? $this->y : 815;
- $image = $this->_scopeConfig->getValue(
- 'sales/identity/logo',
- MagentoStoreModelScopeInterface::SCOPE_STORE,
- $store
- );
- if ($image) {
- $imagePath = '/sales/store/logo/' . $image;
- if ($this->_mediaDirectory->isFile($imagePath)) {
- $image = Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($imagePath));
- $top = 830;
- //top border of the page
- $widthLimit = 270;
- //half of the page width
- $heightLimit = 270;
- //assuming the image is not a "skyscraper"
- $width = $image->getPixelWidth();
- $height = $image->getPixelHeight();
- //preserving aspect ratio (proportions)
- $ratio = $width / $height;
- if ($ratio > 1 && $width > $widthLimit) {
- $width = $widthLimit;
- $height = $width / $ratio;
- } elseif ($ratio < 1 && $height > $heightLimit) {
- $height = $heightLimit;
- $width = $height * $ratio;
- } elseif ($ratio == 1 && $height > $heightLimit) {
- $height = $heightLimit;
- $width = $widthLimit;
- }
- $y1 = $top - $height;
- $y2 = $top;
- $x1 = 455;
- $x2 = $x1 + $width;
- //coordinates after transformation are rounded by Zend
- $width = 260;
- $height = 40;
- $y = $height /2.5;
- $page->drawImage($image, 80, $y, 35+ $width / 1.5, $y + $height/2);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment