Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento 2: generate pdf with google generated qr code

02 Dec 2021 » magento2, php

add action url


<?php
/**
 * Td\StoreSeller Ui Component Action.
 */
namespace Td\StoreSeller\Ui\Component\Listing\Grid\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\StoreManagerInterface as StoreManager;

class Action extends Column
{
    /** Url path */
    const ROW_EDIT_URL = 'storeseller/grid/addrow';
    
    /** print Url path */
    const PRINT_URL = 'storeseller/grid/printrow';
    
    /** @var UrlInterface */
    protected $_urlBuilder;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var string
     */
    private $_editUrl;

    /**
     * @var string
     */
    private $_printUrl;

    /**
     * @param ContextInterface   $context
     * @param UiComponentFactory $uiComponentFactory
     * @param UrlInterface       $urlBuilder
     * @param array              $components
     * @param array              $data
     * @param string             $editUrl
     * @param string             $printUrl
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = [],
        $editUrl = self::ROW_EDIT_URL,
        $printUrl = self::PRINT_URL
    ) {
        $this->_urlBuilder = $urlBuilder;
        $this->_editUrl = $editUrl;
        $this->_printUrl = $printUrl;
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->getStoreManager();
    }

    /**
     * Prepare Data Source.
     *
     * @param array $dataSource
     *
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as &$item) {
                $name = $this->getData('name');
                if (isset($item['entity_id'])) {
                    $item[$name]['edit'] = [
                        'href' => $this->_urlBuilder->getUrl(
                            $this->_editUrl,
                            ['id' => $item['entity_id']]
                        ),
                        'label' => __('Edit'),
                    ];


                    $item['url'] = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB) . "evaluanos?seller_id=" . $item['entity_id'];
                    
                    $item[$name]['print'] = [
                        'href' => $this->_urlBuilder->getUrl(
                            $this->_printUrl,
                            [
                                'seller_name' => $item['seller_name'],
                                'data' => base64_encode("https://chart.googleapis.com/chart?cht=qr&chs=177x177&chl=".$item['url'])
                            ]
                        ),
                        'label' => __('Descargar QR'),
                    ];
                }
            }
        }
        
        return $dataSource;
    }

    private function getStoreManager()
     {
         if ($this->storeManager === null) {
             $this->storeManager = \Magento\Framework\App\ObjectManager::getInstance()
                 ->get(\Magento\Store\Model\StoreManagerInterface::class);
         }
         return $this->storeManager;
     }
}


Add controller for generate and download pdf

<?php
/**
 * Td_StoreSeller List Controller.
 */
namespace Td\StoreSeller\Controller\Adminhtml\Grid;

use Magento\Framework\App\Filesystem\DirectoryList;

class PrintRow extends \Magento\Backend\App\Action
{
    /**
     * @var \Magento\Framework\Registry
     */
    private $coreRegistry;

    /**
     * @var \Td\StoreSeller\Model\GridFactory
     */
    private $gridFactory;

    /**
     * @var \Magento\Framework\Filesystem
     */
    protected $filesystem;

    /**
     * @var \Magento\Framework\App\Response\Http\FileFactory
     */
    protected $_fileFactory;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\Registry $coreRegistry,
     * @param \Td\StoreSeller\GridFactory $gridFactory
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Td\StoreSeller\Model\GridFactory $gridFactory,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory
    ) {
        parent::__construct($context);
        $this->coreRegistry = $coreRegistry;
        $this->gridFactory = $gridFactory;
        $this->filesystem = $filesystem;
        $this->_fileFactory = $fileFactory;
    }

    /**
     * Mapped Grid List page.
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    {
        $imgUrl = base64_decode($this->getRequest()->getParam('data'));
        $sellerName = $this->getRequest()->getParam('seller_name');
        
        /** @var \Magento\Framework\Filesystem\Directory\Write $directory */
        $directory = $this->filesystem->getDirectoryWrite(
            DirectoryList::TMP
        );
        $directory->create();
        $image = @imagecreatefromstring(file_get_contents($imgUrl));
        if (!$image) {
            return false;
        }

        $xSize = imagesx($image);
        $ySize = imagesy($image);
        $page = new \Zend_Pdf_Page($xSize, $ySize);
        
        imageinterlace($image, 0);
        $tmpFileName = $directory->getAbsolutePath(
            'qr.png'
        );
        imagepng($image, $tmpFileName);
        $pdfImage = \Zend_Pdf_Image::imageWithPath($tmpFileName);
        $page->drawImage($pdfImage, 0, 0, $xSize, $ySize);
        $directory->delete($directory->getRelativePath($tmpFileName));
        if (is_resource($image)) {
            imagedestroy($image);
        }
        
        if (!$page) {
            $this->messageManager->addError(__('We don\'t recognize or support the file extension in this QR.'));
        }
        $pdf = new \Zend_Pdf();
        $pdf->pages[] = $page;
        $pdfContent = $pdf->render();

        return $this->_fileFactory->create(
            $sellerName.'.pdf',
            $pdfContent,
            DirectoryList::VAR_DIR,
            'application/pdf'
        );
    }

    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Td_StoreSeller::add_row');
    }
}