Skip to content

Magento2 Invoice Print PDF editing

I wan to edit the totals of Invoice print in PDF format. If it was HTML page than its easy to inspect it. But I can’t find where i can adjust the widht of a cell.

As you can see the Discount text is overlapping. The file involved in printing is this.
enter image description here

And here the code of my printing php file

app/code/Catwalk/InvoicePdf/Model/Order/Pdf/Items/Invoice/DefaultInv.php

 <?php

namespace CatwalkInvoicePdfModelOrderPdfItemsInvoice;

use MagentoCatalogModelProduct;
use MagentoSalesModelOrderPdfItemsInvoiceDefaultInvoice;

class DefaultInv extends DefaultInvoice
{
    protected $product;
    protected $_assetRepo;
    protected $_storeManager;
    protected $dirctory;
    public function __construct(
        MagentoFrameworkModelContext $context,
        MagentoFrameworkRegistry $registry,
        MagentoTaxHelperData $taxData,
        MagentoFrameworkFilesystem $filesystem,
        MagentoFrameworkFilterFilterManager $filterManager,
        MagentoFrameworkStdlibStringUtils $string,
        Product $_product,
        MagentoFrameworkViewAssetRepository $assetRepo,
         MagentoFrameworkAppFilesystemDirectoryList  $dirctory,
        MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
        MagentoStoreModelStoreManagerInterface $storeManager,
        MagentoFrameworkModelResourceModelAbstractResource $resource = null, 
        MagentoFrameworkDataCollectionAbstractDb $resourceCollection = null,
        array $data = []
    )
    {
        $this->product = $_product;
        $this->dirctory = $dirctory;
        $this->_storeManager = $storeManager;
        $this->_assetRepo = $assetRepo;
        parent::__construct($context, $registry, $taxData, $filesystem, $filterManager, $string, $resource, $resourceCollection, $data);
    }

    /**
     * Draw item line
     *
     * @return void
     */
    public function draw()
    {
        $order = $this->getOrder();
        $item = $this->getItem();
        $pdf = $this->getPdf();
        $page = $this->getPage();
        $lines = [];

        $productImage = $this->getProductImage($item, $page);
        // draw Product name
        $lines[0] = [['text' => $this->string->split($item->getName(), 35, true, true), 'feed' => 35]];
        $lines[0][] = array(
            'text'  => $productImage,
            'is_image'  => 1,
            'feed'  => 210
        );
        // draw SKU
        $lines[0][] = [
            'text' => $this->string->split($this->getSku($item), 17),
            'feed' => 290,
            'align' => 'right',
        ];

        // draw QTY
        $lines[0][] = ['text' => $item->getQty() * 1, 'feed' => 435, 'align' => 'right'];

        // draw item Prices
        $i = 0;
        $prices = $this->getItemPricesForDisplay();
        $feedPrice = 355;
        $feedSubtotal = $feedPrice + 160;
        foreach ($prices as $priceData) {
            if (isset($priceData['label'])) {
                // draw Price label
                $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedPrice, 'align' => 'right'];
                // draw Subtotal label
                $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedSubtotal, 'align' => 'right'];
                $i++;
            }
            // draw Price
            $lines[$i][] = [
                'text' => $priceData['price'],
                'feed' => $feedPrice,
                'font' => 'bold',
                'align' => 'right',
            ];
            // draw Subtotal
            $lines[$i][] = [
                'text' => $priceData['subtotal'],
                'feed' => $feedSubtotal,
                'font' => 'bold',
                'align' => 'right',
            ];
            $i++;
        }

        // draw Tax
        $lines[0][] = [
            'text' => $order->formatPriceTxt($item->getTaxAmount()),
            'feed' => 465,
            'font' => 'bold',
            'align' => 'right',
        ];

        // custom options
        $options = $this->getItemOptions();
        if ($options) {
            foreach ($options as $option) {
                // draw options label
                $lines[][] = [
                    'text' => $this->string->split($this->filterManager->stripTags($option['label']), 40, true, true),
                    'font' => 'italic',
                    'feed' => 35,
                ];

                if ($option['value']) {
                    if (isset($option['print_value'])) {
                        $printValue = $option['print_value'];
                    } else {
                        $printValue = $this->filterManager->stripTags($option['value']);
                    }
                    $values = explode(', ', $printValue);
                    foreach ($values as $value) {
                        $lines[][] = ['text' => $this->string->split($value, 30, true, true), 'feed' => 40];
                    }
                }
            }
        }

        $lineBlock = ['lines' => $lines, 'height' => 20];

        $page = $pdf->drawLineBlocks($page, [$lineBlock], ['table_header' => true],1);
        $this->setPage($page);
    }

    /*
    * Return Value of custom attribute
    * */
    private function getProductImage($item,  &$page)
    {
        $productId = $item->getOrderItem()->getProductId();
        $product = $this->product->load($productId);
        $image = $product->getSmallImage();
        $filePath = null;
        $extension = strtolower(pathinfo($image, PATHINFO_EXTENSION));

        if (!is_null($image) && $extension !== "gif") {
            $imagePath = '/catalog/product'.$image;
            $media = $this->dirctory->getPath(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
            $filePath = $media.$imagePath;
            if (!file_exists($filePath)) {
                $filePath = $this->getProductPlaceholderPath();
            }
        } else {
            $filePath = $this->getProductPlaceholderPath();
        }

        if (!file_exists($filePath)) {
           return null;
        }

        return $filePath;
    }
    public function getProductPlaceholderPath()
    {
        $placeholderPath = $this->_assetRepo->createAsset('Magento_Catalog::images/product/placeholder/small_image.jpg')->getPath();
        $staticPath = $this->dirctory->getPath('static');
        return $staticPath . '/' . $placeholderPath;
    }
}

line 77

 $lines[$i][] = ['text' => $priceData['label'], 'feed' => $feedPrice, 'align' => 'right'];

I want to give more width to this total section, to adjust bigger text values
enter image description here