0

I am facing a problem to separate the products images in pdf invoice in Magento 2.x. How can I fix the image size to separate the multiple order item's images?

Also, I want to make the invoice id to Barcode. Please check my attached images.

1. ivoice.png where you can the issues for products images which I want to separate it according to 2nd image.

enter image description here

2. invoice 2.png which pdf invoice I want to make. Invoice 2.png this invoice design I want to get.

enter image description here

Please help me to solve this issue.

Thanks

4

I'll take a stab at question #1 to include the invoice barcode in the pdf header. The header is draw in Magento\Sales\Model\Order\Pdf\Invoice::insertDocumentNumber which currently uses the function from the parent class Magento\Sales\Model\Order\Pdf\AbsractPdf. A plugin can be used fro adding the invoice barcode.

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">
  <type name="Magento\Sales\Model\Order\Pdf\Invoice">
    <plugin name="vendor_module_sales_model_order_pdf_invoice"
            type="Vendor\Module\Plugin\Invoice" sortOrder="10" />
  </type>
</config>

Vendor/Module/Plugin/Invoice.php

<?php

namespace Vendor\Module\Plugin;

class Invoice
{
  /**
   * Add Invoice # barcode to invoice PDF.
   * @param \Magento\Sales\Model\Order\Pdf\Invoice $subject
   * @param \Zend_Pdf_Page $page
   * @param string $text
   */
  public function beforeInsertDocumentNumber($subject, $page, $text) {
    $docHeader = $subject->getDocHeaderCoordinates();

    $image = $this->_generateBarcode($text);

    //Convert barcode px dimensions to points
    $width = $image->getPixelWidth() * 72 / 96;
    $height = $image->getPixelHeight() * 72 / 96;

    $page->drawImage($image, $docHeader[2] - $width, $docHeader[1] - $height, $docHeader[2], $docHeader[1]);
  }

  /**
   * @param string $text
   * @return \Zend_Pdf_Resource_Image_Png
   */
  protected function _generateBarcode($text) {
    $config = new \Zend_Config([
      'barcode' => 'code128',
      'barcodeParams' => [
        'text' => $this->_extractInvoiceNumber($text),
        'drawText' => false
      ],
      'renderer' => 'image',
      'rendererParams' => ['imageType' => 'png']
    ]);

    $barcodeResource = \Zend_Barcode::factory($config)->draw();

    ob_start();
    imagepng($barcodeResource);
    $barcodeImage = ob_get_clean();

    $image = new \Zend_Pdf_Resource_Image_Png('data:image/png;base64,'.base64_encode($barcodeImage));

    return $image;
  }

  /**
   * Strip "Invoice # " from input string.
   * @param string $text
   * @return string
   */
  protected function _extractInvoiceNumber($text) {
    preg_match("/.*#\s(.*)/", $text, $matches);

    return $matches[1];
  }
}

Sample output: enter image description here

3
  • I have already a custom module for the pdf invoice. Could you please tell where I have to put your code? – Shakib Rahman Jul 10 '17 at 6:40
  • Could you please give me your contact details like Skype. – Shakib Rahman Jul 10 '17 at 6:56
  • Skype ID: klantho.pothik – Shakib Rahman Dec 11 '17 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.