Magento 2: Pdf - Add New PageHow to change save path of PDF files in magento 2Overriding Invoice /...

Comparing two arrays of unequal length

How can i do a custom maintenance message on magento 2.2.4

Traveling through the asteriod belt?

Use two 8s and two 3s to make the number 24

What does it mean for a caliber to be flat shooting?

When can a QA tester start his job?

Why did the villain in the first Men in Black movie care about Earth's Cockroaches?

What are the exceptions to Natural Selection?

kill -0 <PID> は何をするのでしょうか?

use of 4/2 chord more compelling than root position?

Porting Linux to another platform requirements

Why exactly do action photographers need high fps burst cameras?

What is the purpose of easy combat scenarios that don't need resource expenditure?

Why did Democrats in the Senate oppose the Born-Alive Abortion Survivors Protection Act (2019 S.130)?

Can a person refuse a presidential pardon?

Why are the books in the Game of Thrones citadel library shelved spine inwards?

How did Ancient Greek 'πυρ' become English 'fire?'

Which one of these password policies are more secure?

How to deal with an incendiary email that was recalled

Can a hotel cancel a confirmed reservation?

Quickly creating a sparse array

What's a good word to describe a public place that looks like it wouldn't be rough?

Publishing research using outdated methods

How do you funnel food off a cutting board?



Magento 2: Pdf - Add New Page


How to change save path of PDF files in magento 2Overriding Invoice / Interceptor, modifying the PDF generation for an Invoice orderMagento 2 add prefix in Invoice numberdisplay brand column invoice PDFMagento 2 : How to add horizontal lines to separate the line items in sales invoice PDF ?Magento 2: PDF set page margins for InvoiceMagento 2 Adding Total Due and Total Paid to PDF orderMagento2 : Remove shipping charge and credit cart type and number from order invoiceMagento 2: Set table structure in order invoice pdfget invoice item using order_item_id in magento 2













0















I want to add custom image in a invoice pdf. The image to display is from my custom table based on order id. I am able to add the image in pdf using the below code.



app/code/Vendor/Module/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 type="MagentoSalesModelOrderPdfInvoice"
for="VendorModuleModelOrderPdfInvoice"/>
</config>


app/code/Vendor/Module/Model/Order/Pdf/Invoice.php



<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace VendorModuleModelOrderPdf;

use MagentoSalesModelOrderPdfConfig;
/**
* Sales Order Invoice PDF model
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Invoice extends MagentoSalesModelOrderPdfInvoice
{
private $ordermoduleCollectionFactory;
private $directoryList;
private $filesystem;

public function __construct(
MagentoPaymentHelperData $paymentData,
MagentoFrameworkStdlibStringUtils $string,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
VendorModuleModelResourceModelOrdermoduleGridCollectionFactory $ordermoduleCollectionFactory,
MagentoFrameworkFilesystem $filesystem,
Config $pdfConfig,
MagentoSalesModelOrderPdfTotalFactory $pdfTotalFactory,
MagentoSalesModelOrderPdfItemsFactory $pdfItemsFactory,
MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
MagentoSalesModelOrderAddressRenderer $addressRenderer,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkLocaleResolverInterface $localeResolver,
MagentoFrameworkFilesystemDirectoryList $directoryList,
array $data = []
) {
parent::__construct(
$paymentData,
$string,
$scopeConfig,
$filesystem,
$pdfConfig,
$pdfTotalFactory,
$pdfItemsFactory,
$localeDate,
$inlineTranslation,
$addressRenderer,
$storeManager,
$localeResolver,
$data
);
$this->filesystem = $filesystem;
$this->directoryList = $directoryList;
$this->ordermoduleCollectionFactory = $ordermoduleCollectionFactory;
}

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();
}

}

$orderId = $order->getId();
$module = $this->getAvailableModule($orderId);
if ($module != '') {
$this->insertConditions($page,$module);
}
$this->_afterGetPdf();
return $pdf;
}


public function insertConditions($page,$module)
{
$module =$$module //custom image
$image = Zend_Pdf_Image::imageWithPath($imageFile,$imageWidth,$imageHeight);
$y=$pdf->y - $imageHeight /3;
$page->drawImage($image, 35,$y, 35+ $imageWidth / 2, $y + $imageHeight/2);
}

public function getAvailableModule($orderId)
{
$collection = $this->ordermoduleCollectionFactory->create()
->addFieldToFilter('order_id', $orderId)
->addFieldToFilter('main_table.dealer_id', array('neq' => -1));
$image = '';
foreach($collection as $col) {
$image = $col->getModule();
}
if (!is_null($image)) {
try
{
$imagePath = '/modulefiles/'.$image;
$media_dir = $this->filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
if ($media_dir->isFile($imagePath)) {
return $media_dir->getAbsolutePath($imagePath);
}
else
return null;
} catch (Exception $e) {
return false;
}
}
}

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;
}
}


The issue is:



Invoice with a single item in pdf -> my image is displaying properly
Invoice with multiple items in order -> don't have space in first page. So displaying as
http://i.prntscr.com/CRm-KGvjSimZCFDj2TcLEQ.png



So I have added new page function $page = $this->newPage(); in function insertConditions()



But now my custom image is always showing n the second page. Even if we have space remaining in the first page.



How can I solve this:adding new page only when no remaining space in the first page.









share



























    0















    I want to add custom image in a invoice pdf. The image to display is from my custom table based on order id. I am able to add the image in pdf using the below code.



    app/code/Vendor/Module/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 type="MagentoSalesModelOrderPdfInvoice"
    for="VendorModuleModelOrderPdfInvoice"/>
    </config>


    app/code/Vendor/Module/Model/Order/Pdf/Invoice.php



    <?php
    /**
    * Copyright © 2013-2017 Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */
    namespace VendorModuleModelOrderPdf;

    use MagentoSalesModelOrderPdfConfig;
    /**
    * Sales Order Invoice PDF model
    * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
    */
    class Invoice extends MagentoSalesModelOrderPdfInvoice
    {
    private $ordermoduleCollectionFactory;
    private $directoryList;
    private $filesystem;

    public function __construct(
    MagentoPaymentHelperData $paymentData,
    MagentoFrameworkStdlibStringUtils $string,
    MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
    VendorModuleModelResourceModelOrdermoduleGridCollectionFactory $ordermoduleCollectionFactory,
    MagentoFrameworkFilesystem $filesystem,
    Config $pdfConfig,
    MagentoSalesModelOrderPdfTotalFactory $pdfTotalFactory,
    MagentoSalesModelOrderPdfItemsFactory $pdfItemsFactory,
    MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
    MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
    MagentoSalesModelOrderAddressRenderer $addressRenderer,
    MagentoStoreModelStoreManagerInterface $storeManager,
    MagentoFrameworkLocaleResolverInterface $localeResolver,
    MagentoFrameworkFilesystemDirectoryList $directoryList,
    array $data = []
    ) {
    parent::__construct(
    $paymentData,
    $string,
    $scopeConfig,
    $filesystem,
    $pdfConfig,
    $pdfTotalFactory,
    $pdfItemsFactory,
    $localeDate,
    $inlineTranslation,
    $addressRenderer,
    $storeManager,
    $localeResolver,
    $data
    );
    $this->filesystem = $filesystem;
    $this->directoryList = $directoryList;
    $this->ordermoduleCollectionFactory = $ordermoduleCollectionFactory;
    }

    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();
    }

    }

    $orderId = $order->getId();
    $module = $this->getAvailableModule($orderId);
    if ($module != '') {
    $this->insertConditions($page,$module);
    }
    $this->_afterGetPdf();
    return $pdf;
    }


    public function insertConditions($page,$module)
    {
    $module =$$module //custom image
    $image = Zend_Pdf_Image::imageWithPath($imageFile,$imageWidth,$imageHeight);
    $y=$pdf->y - $imageHeight /3;
    $page->drawImage($image, 35,$y, 35+ $imageWidth / 2, $y + $imageHeight/2);
    }

    public function getAvailableModule($orderId)
    {
    $collection = $this->ordermoduleCollectionFactory->create()
    ->addFieldToFilter('order_id', $orderId)
    ->addFieldToFilter('main_table.dealer_id', array('neq' => -1));
    $image = '';
    foreach($collection as $col) {
    $image = $col->getModule();
    }
    if (!is_null($image)) {
    try
    {
    $imagePath = '/modulefiles/'.$image;
    $media_dir = $this->filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
    if ($media_dir->isFile($imagePath)) {
    return $media_dir->getAbsolutePath($imagePath);
    }
    else
    return null;
    } catch (Exception $e) {
    return false;
    }
    }
    }

    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;
    }
    }


    The issue is:



    Invoice with a single item in pdf -> my image is displaying properly
    Invoice with multiple items in order -> don't have space in first page. So displaying as
    http://i.prntscr.com/CRm-KGvjSimZCFDj2TcLEQ.png



    So I have added new page function $page = $this->newPage(); in function insertConditions()



    But now my custom image is always showing n the second page. Even if we have space remaining in the first page.



    How can I solve this:adding new page only when no remaining space in the first page.









    share

























      0












      0








      0








      I want to add custom image in a invoice pdf. The image to display is from my custom table based on order id. I am able to add the image in pdf using the below code.



      app/code/Vendor/Module/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 type="MagentoSalesModelOrderPdfInvoice"
      for="VendorModuleModelOrderPdfInvoice"/>
      </config>


      app/code/Vendor/Module/Model/Order/Pdf/Invoice.php



      <?php
      /**
      * Copyright © 2013-2017 Magento, Inc. All rights reserved.
      * See COPYING.txt for license details.
      */
      namespace VendorModuleModelOrderPdf;

      use MagentoSalesModelOrderPdfConfig;
      /**
      * Sales Order Invoice PDF model
      * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
      */
      class Invoice extends MagentoSalesModelOrderPdfInvoice
      {
      private $ordermoduleCollectionFactory;
      private $directoryList;
      private $filesystem;

      public function __construct(
      MagentoPaymentHelperData $paymentData,
      MagentoFrameworkStdlibStringUtils $string,
      MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
      VendorModuleModelResourceModelOrdermoduleGridCollectionFactory $ordermoduleCollectionFactory,
      MagentoFrameworkFilesystem $filesystem,
      Config $pdfConfig,
      MagentoSalesModelOrderPdfTotalFactory $pdfTotalFactory,
      MagentoSalesModelOrderPdfItemsFactory $pdfItemsFactory,
      MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
      MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
      MagentoSalesModelOrderAddressRenderer $addressRenderer,
      MagentoStoreModelStoreManagerInterface $storeManager,
      MagentoFrameworkLocaleResolverInterface $localeResolver,
      MagentoFrameworkFilesystemDirectoryList $directoryList,
      array $data = []
      ) {
      parent::__construct(
      $paymentData,
      $string,
      $scopeConfig,
      $filesystem,
      $pdfConfig,
      $pdfTotalFactory,
      $pdfItemsFactory,
      $localeDate,
      $inlineTranslation,
      $addressRenderer,
      $storeManager,
      $localeResolver,
      $data
      );
      $this->filesystem = $filesystem;
      $this->directoryList = $directoryList;
      $this->ordermoduleCollectionFactory = $ordermoduleCollectionFactory;
      }

      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();
      }

      }

      $orderId = $order->getId();
      $module = $this->getAvailableModule($orderId);
      if ($module != '') {
      $this->insertConditions($page,$module);
      }
      $this->_afterGetPdf();
      return $pdf;
      }


      public function insertConditions($page,$module)
      {
      $module =$$module //custom image
      $image = Zend_Pdf_Image::imageWithPath($imageFile,$imageWidth,$imageHeight);
      $y=$pdf->y - $imageHeight /3;
      $page->drawImage($image, 35,$y, 35+ $imageWidth / 2, $y + $imageHeight/2);
      }

      public function getAvailableModule($orderId)
      {
      $collection = $this->ordermoduleCollectionFactory->create()
      ->addFieldToFilter('order_id', $orderId)
      ->addFieldToFilter('main_table.dealer_id', array('neq' => -1));
      $image = '';
      foreach($collection as $col) {
      $image = $col->getModule();
      }
      if (!is_null($image)) {
      try
      {
      $imagePath = '/modulefiles/'.$image;
      $media_dir = $this->filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
      if ($media_dir->isFile($imagePath)) {
      return $media_dir->getAbsolutePath($imagePath);
      }
      else
      return null;
      } catch (Exception $e) {
      return false;
      }
      }
      }

      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;
      }
      }


      The issue is:



      Invoice with a single item in pdf -> my image is displaying properly
      Invoice with multiple items in order -> don't have space in first page. So displaying as
      http://i.prntscr.com/CRm-KGvjSimZCFDj2TcLEQ.png



      So I have added new page function $page = $this->newPage(); in function insertConditions()



      But now my custom image is always showing n the second page. Even if we have space remaining in the first page.



      How can I solve this:adding new page only when no remaining space in the first page.









      share














      I want to add custom image in a invoice pdf. The image to display is from my custom table based on order id. I am able to add the image in pdf using the below code.



      app/code/Vendor/Module/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 type="MagentoSalesModelOrderPdfInvoice"
      for="VendorModuleModelOrderPdfInvoice"/>
      </config>


      app/code/Vendor/Module/Model/Order/Pdf/Invoice.php



      <?php
      /**
      * Copyright © 2013-2017 Magento, Inc. All rights reserved.
      * See COPYING.txt for license details.
      */
      namespace VendorModuleModelOrderPdf;

      use MagentoSalesModelOrderPdfConfig;
      /**
      * Sales Order Invoice PDF model
      * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
      */
      class Invoice extends MagentoSalesModelOrderPdfInvoice
      {
      private $ordermoduleCollectionFactory;
      private $directoryList;
      private $filesystem;

      public function __construct(
      MagentoPaymentHelperData $paymentData,
      MagentoFrameworkStdlibStringUtils $string,
      MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
      VendorModuleModelResourceModelOrdermoduleGridCollectionFactory $ordermoduleCollectionFactory,
      MagentoFrameworkFilesystem $filesystem,
      Config $pdfConfig,
      MagentoSalesModelOrderPdfTotalFactory $pdfTotalFactory,
      MagentoSalesModelOrderPdfItemsFactory $pdfItemsFactory,
      MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
      MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
      MagentoSalesModelOrderAddressRenderer $addressRenderer,
      MagentoStoreModelStoreManagerInterface $storeManager,
      MagentoFrameworkLocaleResolverInterface $localeResolver,
      MagentoFrameworkFilesystemDirectoryList $directoryList,
      array $data = []
      ) {
      parent::__construct(
      $paymentData,
      $string,
      $scopeConfig,
      $filesystem,
      $pdfConfig,
      $pdfTotalFactory,
      $pdfItemsFactory,
      $localeDate,
      $inlineTranslation,
      $addressRenderer,
      $storeManager,
      $localeResolver,
      $data
      );
      $this->filesystem = $filesystem;
      $this->directoryList = $directoryList;
      $this->ordermoduleCollectionFactory = $ordermoduleCollectionFactory;
      }

      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();
      }

      }

      $orderId = $order->getId();
      $module = $this->getAvailableModule($orderId);
      if ($module != '') {
      $this->insertConditions($page,$module);
      }
      $this->_afterGetPdf();
      return $pdf;
      }


      public function insertConditions($page,$module)
      {
      $module =$$module //custom image
      $image = Zend_Pdf_Image::imageWithPath($imageFile,$imageWidth,$imageHeight);
      $y=$pdf->y - $imageHeight /3;
      $page->drawImage($image, 35,$y, 35+ $imageWidth / 2, $y + $imageHeight/2);
      }

      public function getAvailableModule($orderId)
      {
      $collection = $this->ordermoduleCollectionFactory->create()
      ->addFieldToFilter('order_id', $orderId)
      ->addFieldToFilter('main_table.dealer_id', array('neq' => -1));
      $image = '';
      foreach($collection as $col) {
      $image = $col->getModule();
      }
      if (!is_null($image)) {
      try
      {
      $imagePath = '/modulefiles/'.$image;
      $media_dir = $this->filesystem->getDirectoryRead(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
      if ($media_dir->isFile($imagePath)) {
      return $media_dir->getAbsolutePath($imagePath);
      }
      else
      return null;
      } catch (Exception $e) {
      return false;
      }
      }
      }

      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;
      }
      }


      The issue is:



      Invoice with a single item in pdf -> my image is displaying properly
      Invoice with multiple items in order -> don't have space in first page. So displaying as
      http://i.prntscr.com/CRm-KGvjSimZCFDj2TcLEQ.png



      So I have added new page function $page = $this->newPage(); in function insertConditions()



      But now my custom image is always showing n the second page. Even if we have space remaining in the first page.



      How can I solve this:adding new page only when no remaining space in the first page.







      magento2 page invoice-pdf new





      share












      share










      share



      share










      asked 5 mins ago









      VindhujaVindhuja

      665520




      665520






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263865%2fmagento-2-pdf-add-new-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f263865%2fmagento-2-pdf-add-new-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

          How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

          變成蝙蝠會怎樣? 參考資料 外部連結 导航菜单Thomas Nagel, "What is it like to be a...