0

I am using a custom module which is including a php file from lib . But it is giving error in di:compile command as -

Fatal error: Cannot declare class PDFMerger, because the name is already in use in /home/xyz/web/mysite/public_html/lib/PDFMerger/PDFMerger.php on line 36

The code of file which is including it is -

<?php

namespace VendorName\ModuleName\Controller\Adminhtml\Barcode;

use Magento\Backend\App\Action;
require __DIR__ . '/../../../../../../../lib/PDFMerger/PDFMerger.php';
use PDFMerger;

class Printlabel extends Action
{
    protected $_helper;
    protected $_storeManager;
    protected $_pdfMerger;
    protected $response;
    protected $_dir;
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \HOM\Barcode\Helper\Data $helper,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        PDFMerger $pdfMerger,
        \Magento\Framework\App\Response\Http $response,
        \Magento\Framework\Filesystem\DirectoryList $dir
    )
    {
        parent::__construct($context);
        $this->_helper = $helper;
        $this->_storeManager = $storeManager;
        $this->_pdfMerger = $pdfMerger;
        $this->response = $response;
        $this->_dir = $dir;
    }

    public function execute()
    {
        $fileNames = array();
        $barcode_helper = $this->_helper;
        $order_ids = $this->getRequest()->getPost('selected', array());

        foreach ($order_ids as $order_id) {
            // generate label
            $label_data = $barcode_helper->printOrderLabelOnly($order_id);
            if (isset($label_data['tracking_code'])) {
                $fileNames[] = $label_data['tracking_code'] . '.pdf';
            }

            // if (isset($label_data['tracking_code'])) {
            //     $barcode_helper->download($label_data['tracking_code'] . '.pdf');
            // }
        }
        $mediaUrlDir = $this->_dir->getPath('media');
        $file_path = $mediaUrlDir . '/barcodes/';

        $pdf = $this->_pdfMerger;
        //$barcodeFilename = array();
        if (count($fileNames)) {
            foreach ($fileNames as $fileName) {
                if (file_exists($file_path . $fileName)) {
                    //$barcodeFilename[] = str_replace('.pdf', '', $fileName);
                    $pdf->addPDF($file_path . $fileName, 'all');
                }
            }
            //$barcodeFilenameFinal = implode('_', $barcodeFilename) . '.pdf';
            $barcodeFilenameFinal = 'barcodes_' . time() . '.pdf';
            $pdf->merge('file', $file_path . $barcodeFilenameFinal);

            $barcode_helper->download($barcodeFilenameFinal);
            //$zip_file_name = 'barcodes_' . time() . '.zip';
            //$file_path = $this->_helper->getBaseMediaUrl() . '/barcodes/';
            //$this->zipFilesDownload($fileNames,$zip_file_name,$file_path);
        }

        // redirect
        $redirectUrl = $this->_objectManager->create('Magento\Backend\Helper\Data')->getUrl('sales/order/');
        $this->response->setRedirect($redirectUrl)->sendResponse();
    }

    public function zipFilesDownload($file_names, $archive_file_name, $file_path)
    {
        $zip = new ZipArchive();

        if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE) !== TRUE) {
            return false;
        }

        foreach ($file_names as $files) {
            $zip->addFile($file_path . $files, $files);
        }
        $zip->close();

        header("Content-type: application/zip");
        header("Content-Disposition: attachment; filename=$archive_file_name");
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile("$archive_file_name");
        exit;
    }

    public function zipFilesDownload2($file_names, $archive_file_name, $file_path)
    {
        $zip = new ZipArchive();

        if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE) !== TRUE) {
            return false;
        }

        foreach ($file_names as $files) {
            $zip->addFile($file_path . $files, $files);
        }
        $zip->close();

        header("Content-type: application/zip");
        header("Content-Disposition: attachment; filename=$archive_file_name");
        header("Pragma: no-cache");
        header("Expires: 0");
        readfile("$archive_file_name");
    }
}

Please provide solution.

0

You may try the process described below.

Copy your PDFMerger/PDFMerger.php files into your Magento "lib/internal" folder in the root Magento installation directory and change the controller as follows.

<?php

namespace VendorName\ModuleName\Controller\Adminhtml\Barcode;

use Magento\Backend\App\Action;
//require __DIR__ . '/../../../../../../../lib/PDFMerger/PDFMerger.php';
require_once('lib/PDFMerger/PDFMerger.php');
//use PDFMerger;

class Printlabel extends Action
{

--------------------
--------------------
public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \HOM\Barcode\Helper\Data $helper,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
       // PDFMerger $pdfMerger,
        \Magento\Framework\App\Response\Http $response,
        \Magento\Framework\Filesystem\DirectoryList $dir
    )
    {
        parent::__construct($context);
        $this->_helper = $helper;
        $this->_storeManager = $storeManager;
        //$this->_pdfMerger = $pdfMerger;
        $this->response = $response;
        $this->_dir = $dir;
    } 


    public function execute()
    {
        $fileNames = array();
        $barcode_helper = $this->_helper;
        $order_ids = $this->getRequest()->getPost('selected', array());

        foreach ($order_ids as $order_id) {
            // generate label
            $label_data = $barcode_helper->printOrderLabelOnly($order_id);
            if (isset($label_data['tracking_code'])) {
                $fileNames[] = $label_data['tracking_code'] . '.pdf';
            }

            // if (isset($label_data['tracking_code'])) {
            //     $barcode_helper->download($label_data['tracking_code'] . '.pdf');
            // }
        }
        $mediaUrlDir = $this->_dir->getPath('media');
        $file_path = $mediaUrlDir . '/barcodes/';

        //$pdf = $this->_pdfMerger;
        $pdf = new \PDFMerger();
        //$barcodeFilename = array();
        if (count($fileNames)) {
            foreach ($fileNames as $fileName) {
                if (file_exists($file_path . $fileName)) {
                    //$barcodeFilename[] = str_replace('.pdf', '', $fileName);
                    $pdf->addPDF($file_path . $fileName, 'all');
                }
            }
            //$barcodeFilenameFinal = implode('_', $barcodeFilename) . '.pdf';
            $barcodeFilenameFinal = 'barcodes_' . time() . '.pdf';
            $pdf->merge('file', $file_path . $barcodeFilenameFinal);

            $barcode_helper->download($barcodeFilenameFinal);
            //$zip_file_name = 'barcodes_' . time() . '.zip';
            //$file_path = $this->_helper->getBaseMediaUrl() . '/barcodes/';
            //$this->zipFilesDownload($fileNames,$zip_file_name,$file_path);
        }

        // redirect
        $redirectUrl = $this->_objectManager->create('Magento\Backend\Helper\Data')->getUrl('sales/order/');
        $this->response->setRedirect($redirectUrl)->sendResponse();
    }

    ------------------
    ------------------

}
  • Nope, this is gives error in di:compile as Class lib\PDFMerger\PDFMerger does not exist Class HOM\Barcode\Controller\Adminhtml\Barcode\Printandship\Interceptor gen eration error: The requested class did not generate properly, because the ' generated' directory permission is read-only. If --- after running the 'bin /magento setup:di:compile' CLI command when the 'generated' directory permi ssion is set to write --- the requested class did not generate properly, th en you must add the generated class object to the signature of the related construct method, only. – chanchal May 3 at 9:52
  • I have given 777 permission to generated – chanchal May 3 at 9:57
0

Thanks, Its working for me. Its working with this code "$pdf = new \PDFMerger();" instead of '$pdf = $this->_pdfMerger';

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.