Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top