Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento 2: generate pdf programmatically

04 Jan 2022 » magento2, php

helper code

<?php
namespace Firstflight\Firstship\Helper;

use Laminas\Barcode\Barcode;
use Magento\Framework\App\Filesystem\DirectoryList;

class PdfData extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Framework\Message\ManagerInterface
     */
    protected $messageManager;
    protected $config;
    protected $orderAddressRepositoryInterface;
    protected $shipmentRepositoryInterface;
    protected $pdf;
    protected $moduleDir;
    protected $filesystem;
    protected $directoryList;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Sales\Api\ShipmentRepositoryInterface $shipmentRepositoryInterface,
        \Magento\Sales\Api\OrderAddressRepositoryInterface $orderAddressRepositoryInterface,
        \Magento\Framework\Module\Dir $moduleDir,
        \Magento\Framework\Filesystem $filesystem,
        ConfigData $config,
        DirectoryList $directoryList
    ) {
        $this->messageManager = $messageManager;
        $this->shipmentRepositoryInterface = $shipmentRepositoryInterface;
        $this->orderAddressRepositoryInterface = $orderAddressRepositoryInterface;
        $this->config = $config;
        $this->moduleDir = $moduleDir;
        $this->filesystem = $filesystem;
        $this->directoryList = $directoryList;

        parent::__construct($context);
    }

    public function getPdf()
    {
        $pdf = new \Zend_Pdf();
        $pdf->pages[] = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
        $page = $pdf->pages[0]; // this will get reference to the first page.

        $style = new \Zend_Pdf_Style();
        $style->setLineColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));

        $pageWidth = $page->getWidth(); //595
        $pageHight = $page->getHeight(); //842

        $page->setLineWidth(1.5);

        $page->drawRectangle(
            10,
            10,
            $pageWidth - 10,
            $pageHight - 10,
            \Zend_Pdf_Page::SHAPE_DRAW_STROKE
        );

        $this->x = 30;
        $this->y = 770;

        $page->drawLine(
            $this->x - 20,
            $this->y - 610,
            $this->x + 555,
            $this->y - 610
        );

        // drow dashed line
        $page->setLineDashingPattern(
            [1.5],
            1.6
        );

        $page->drawLine(
            $this->x - 20,
            $this->y - 60,
            $this->x + 555,
            $this->y - 60
        );

        $page->drawLine(
            $this->x - 20,
            $this->y - 120,
            $this->x + 555,
            $this->y - 120
        );

        $page->drawLine(
            $this->x - 20,
            $this->y - 215,
            $this->x + 555,
            $this->y - 215
        );

        $page->drawLine(
            $this->x - 20,
            $this->y - 280,
            $this->x + 555,
            $this->y - 280
        );

        $page->drawLine(
            $this->x - 20,
            $this->y - 470,
            $this->x + 555,
            $this->y - 470
        );

        $page->drawLine(
            $this->x + 240,
            $this->y + 70,
            $this->x + 240,
            $this->y - 60
        ); // vertical

        $page->drawLine(
            $this->x + 270,
            $this->y - 60,
            $this->x + 270,
            $this->y - 120
        ); // vertical

        $page->drawLine(
            $this->x + 140,
            $this->y - 120,
            $this->x + 140,
            $this->y - 215
        ); // vertical

        $page->drawLine(
            $this->x + 300,
            $this->y - 120,
            $this->x + 300,
            $this->y - 215
        ); // vertical

        $page->drawLine(
            $this->x + 300,
            $this->y - 170,
            $this->x + 555,
            $this->y - 170
        );
        
        $page->drawLine(
            $this->x + 320,
            $this->y - 215,
            $this->x + 320,
            $this->y - 280
        ); // vertical

        $page->drawLine(
            $this->x + 420,
            $this->y - 215,
            $this->x + 420,
            $this->y - 280
        ); // vertical

        $page->drawLine(
            $this->x + 270,
            $this->y - 280,
            $this->x + 270,
            $this->y - 470
        ); // vertical
        
        // Add logo image
        $viewPath = $this->moduleDir->getDir('Firstflight_Firstship', \Magento\Framework\Module\Dir::MODULE_VIEW_DIR);
        $logoFile = DIRECTORY_SEPARATOR."logo.png";
        $filePath = DIRECTORY_SEPARATOR."adminhtml".DIRECTORY_SEPARATOR."web".DIRECTORY_SEPARATOR."image".$logoFile;
        $imagePath = $viewPath.$filePath;
        
        $image = \Zend_Pdf_Image::imageWithPath($imagePath);
        
        //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 = 800 - $height;
        $y2 = 800;
        $x1 = 330;
        $x2 = $x1 + $width;
        
        //coordinates after transformation are rounded by Zend
        $page->drawImage(
            $image,
            $x1,
            $y1,
            $x2,
            $y2
        );

        $rootDirectory = $this->filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::ROOT);
        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Medium.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $page->drawText(__("AE-DXB"), 60, 760, 'UTF-8');
 
        $style->setFillColor(new \Zend_Pdf_Color_Html('#626a70'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("Sharjah"), 110, $pageHight - 160, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerif.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("ORGN"), 115, $pageHight - 185, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Medium.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#626a70'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("Dubai"), 415, $pageHight - 160, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/GnuFreeFont/FreeSerif.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("DEST"), 420, $pageHight - 185, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Medium.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#626a70'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("DOMESTIC"), 35, $pageHight - 230, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Medium.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("PKGT"), 60, $pageHight - 280, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Medium.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#626a70'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("PARCEL"), 215, $pageHight - 230, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Medium.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("PDTT"), 225, $pageHight - 280, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Regular.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 26);
        $page->setStyle($style);
        $page->drawText(__("NOR"), 425, $pageHight - 230, 'UTF-8');

        $page->setLineDashingPattern(
            [4],
            1.6
        );
        $page->drawLine(
            $this->x + 395,
            $this->y - 160,
            $this->x + 450,
            $this->y - 160
        ); // vertical

        $page->drawText(__("Amount"), $this->x + 325, $pageHight - 275, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Medium.ttf')
        );
        $style->setFont($font, 44);
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("AED 0"), $this->x + 465, $pageHight - 275, 'UTF-8');

        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("Ref:"), $this->x + 5, $pageHight - 315, 'UTF-8');

        $style->setFillColor(new \Zend_Pdf_Color_Html('#626a70'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("AE245845475"), $this->x + 65, $pageHight - 315, 'UTF-8');

        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("Date:"), $this->x + 5, $pageHight - 345, 'UTF-8');

        $style->setFillColor(new \Zend_Pdf_Color_Html('#626a70'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("2020-06-04 17:40"), $this->x + 65, $pageHight - 345, 'UTF-8');
        
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 20);
        $page->setStyle($style);
        $page->drawText(__("1"), $this->x + 360, $pageHight - 310, 'UTF-8');

        $style->setFont($font, 21);
        $page->setStyle($style);
        $page->drawText(__("PCS"), $this->x + 350, $pageHight - 340, 'UTF-8');

        $style->setFillColor(new \Zend_Pdf_Color_Html('#626a70'));
        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("1.500 Kgs"), $this->x + 440, $pageHight - 315, 'UTF-8');
        
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 20);
        $page->setStyle($style);
        $page->drawText(__("WT"), $this->x + 470, $pageHight - 340, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Regular.ttf')
        );
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 18);
        $page->setStyle($style);
        $page->drawText(__("Sender"), $this->x - 6, $pageHight - 375, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf')
        );
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#000000'));
        $style->setFont($font, 26);
        $page->setStyle($style);
        $page->drawText(__("Meraj Ahmad"), $this->x - 6, $pageHight - 410, 'UTF-8');

        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#626a70'));
        $style->setFont($font, 20);
        $page->setStyle($style);
        $page->drawText(__("Meraj Ahmad"), $this->x - 6, $pageHight - 440, 'UTF-8');
        $page->drawText(__("this is test"), $this->x - 6, $pageHight - 460, 'UTF-8');
        $page->drawText(__("Sharjah"), $this->x - 6, $pageHight - 480, 'UTF-8');

        $style->setFont($font, 24);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#000000'));
        $page->setStyle($style);
        $page->drawText(__("Ph: "), $this->x - 6, $pageHight - 530, 'UTF-8');
        $page->drawText(__("+91 9876543210"), $this->x + 30, $pageHight - 530, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/Roboto/Roboto-Regular.ttf')
        );
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $style->setFont($font, 18);
        $page->setStyle($style);
        $page->drawText(__("Reciever"), $this->x + 280, $pageHight - 375, 'UTF-8');

        $font = \Zend_Pdf_Font::fontWithPath(
            $rootDirectory->getAbsolutePath('lib/internal/LinLibertineFont/LinLibertine_Bd-2.8.1.ttf')
        );
        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#000000'));
        $style->setFont($font, 26);
        $page->setStyle($style);
        $page->drawText(__("Cartlow"), $this->x + 280, $pageHight - 410, 'UTF-8');

        $page->setStyle($style);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#626a70'));
        $style->setFont($font, 20);
        $page->setStyle($style);
        $page->drawText(__("Cartlow"), $this->x + 280, $pageHight - 440, 'UTF-8');
        $page->drawText(__("Opposite dunes"), $this->x + 280, $pageHight - 460, 'UTF-8');
        $page->drawText(__("village, warehouse No.1 DIP 2"), $this->x + 280, $pageHight - 480, 'UTF-8');
        $page->drawText(__("Dubai"), $this->x + 280, $pageHight - 500, 'UTF-8');
 
        $style->setFont($font, 24);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#000000'));
        $page->setStyle($style);
        $page->drawText(__("Ph: "), $this->x + 280, $pageHight - 530, 'UTF-8');
        $page->drawText(__("+91 9876543210"), $this->x + 320, $pageHight - 530, 'UTF-8');

        $style->setFont($font, 20);
        $style->setFillColor(new \Zend_Pdf_Color_Html('#0a0b1d'));
        $page->setStyle($style);
        $page->drawText(__("Description:"), $this->x - 6, $pageHight - 570, 'UTF-8');
        $page->drawText(__("Iphone XR 128GB"), $this->x + 110, $pageHight - 570, 'UTF-8');
        $page->drawText(__("Instruction:"), $this->x - 6, $pageHight - 610, 'UTF-8');
        $page->drawText(__("None"), $this->x + 110, $pageHight - 610, 'UTF-8');

        $style->setFont($font, 22);
        $page->setStyle($style);
        $page->drawText(__("Airwaybill Number"), $this->x - 6, $pageHight - 765, 'UTF-8');

        // Only the text to draw is required
        $barcodeOptions = ['text' => '103074918'];

        // No required options
        $rendererOptions = [];

        $code = Barcode::factory(
            'code39',
            'image',
            $barcodeOptions,
            $rendererOptions
        )->draw();

        $path  =  $this->directoryList->getPath(DirectoryList::VAR_DIR);
        $imagePath = $path.'/barcode.png';
        $imagepng = "imagepng";
        $imagedestroy = "imagedestroy";
        $imagepng($code, $imagePath);
        $imagedestroy($code);
                    
        $image = \Zend_Pdf_Image::imageWithPath($imagePath);

        $y1 = $pageHight - 710 - $height;
        $y2 = $pageHight - 710;
        $x1 = 270;
        $x2 = $x1 + $width;
        
        //coordinates after transformation are rounded by Zend
        $page->drawImage(
            $image,
            $x1,
            $y1,
            $x2,
            $y2
        );

        $dir = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
        $dir->delete($imagePath);
        return $pdf;
    }
}


Thanks.