2

I am using one custom php library in magento 2 to convert html to pdf. Here is the code that i am using.

<?php

namespace Company\Module\Controller\Adminhtml\Quote;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList as DirectoryList;

use Company\Module\Helper\PdfCrowd;

class QuotePdf extends Action
{
    /**
     * @var \Magento\Framework\App\Response\Http\FileFactory
     */
    protected $fileFactory;
    protected $_mediaDirectory;
    protected $directory_list;

    protected $dompdfFactory;
    protected $layoutFactory;

    protected $authSession;
    //protected $quote_info;
    protected $userFactory;

    public function __construct(
        Context $context,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Backend\Model\Auth\Session $authSession,
        DirectoryList $directory_list,
        \Magento\User\Model\UserFactory $userFactory
    ) {
        parent::__construct($context);
        $this->fileFactory = $fileFactory;
        $this->authSession = $authSession;
        $this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
        $this->directory_list = $directory_list;
        $this->userFactory = $userFactory;
    }

    public function execute()
    {
        if($this->authSession->isLoggedIn()){

            $id = $this->getRequest()->getParam('id');

            $client = new Pdfcrowd("MYUSERNAME", "xxxxxxx-KEY");

            // set HTTP response headers
            $pdf = $client->convertHtml($this->getHtmlForPdf($id));

            header("Content-Type: application/pdf");
            header("Cache-Control: no-cache");
            header("Accept-Ranges: none");
            header("Content-Disposition: attachment; filename=commercialsale-".$id.".pdf");

            echo $pdf;
        }
    }
}

Here is my Helpler file

<?php

namespace Company\Module\Helper;

class PdfCrowd {
    //
    // Pdfcrowd constructor.
    // 
    // $username - your username at Pdfcrowd
    // $apikey  - your API key
    // $hostname - API hostname, defaults to pdfcrowd.com
    // 
    function __construct($username, $apikey, $hostname=null){
        if ($hostname)
            $this->hostname = $hostname;
        else
            $this->hostname = self::$api_host;
        $this->useSSL(false);
        $this->fields = array(
            'username' => $username,
            'key' => $apikey,
            'pdf_scaling_factor' => 1,
            'html_zoom' => 200);
        $this->proxy_name = null;
        $this->proxy_port = null;
        $this->proxy_username = "";
        $this->proxy_password = "";

        $this->user_agent = "pdfcrowd_php_client_".self::$client_version."_(http://pdfcrowd.com)";
    }
}
?>

And here is the error that i am getting...

Fatal error: Uncaught Error: Class 'Company\Module\Helper\PdfCrowd' not found in /home/patios/public_html/app/code/Company/Module/Controller/Adminhtml/Quote/QuotePdf.php:49 Stack trace: #0 /home/patios/public_html/vendor/magento/framework/App/Action/Action.php(107): Company\Module\Controller\Adminhtml\Quote\QuotePdf->execute() #1 /home/patios/public_html/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http)) 2 /home/patios/public_html/vendor/magento/framework/Interception/Interceptor.php(138): Company\Module\Controller\Adminhtml\Quote\QuotePdf\Interceptor->___callParent('dispatch', Array) 3 /home/patios/public_html/vendor/magento/framework/Interception/Interceptor.php(153): Company\Module\Controller\Adminhtml\Quote\QuotePdf\Interceptor->Magento\Framework\Interception{closure}(Object(Magento\Framework\App\Request\Http)) 4 /home/patios/public_html/generated/code/Company/Module/Controller/Adminhtml in /home/patios/public_html/app/code/Company/Module/Controller/Adminhtml/Quote/QuotePdf.php on line 49

Note: This code is working on my local but not on live server.

3
  • 2
    Check exact folder and class name for Company\Module\Helper\PdfCrowd
    – Sohel Rana
    Jun 8, 2018 at 13:41
  • Thanks to pointing me. Sorry actually there is capital word issue in my file name. Strange that this is working in my local.
    – Hardik
    Jun 8, 2018 at 13:53
  • yes, Linux is naming strict type. But other system like windows, mac not.
    – Sohel Rana
    Jun 8, 2018 at 13:55

2 Answers 2

1

Use below on your helper file:

class PdfCrowd extends \Magento\Framework\App\Helper\AbstractHelper{
}

instead of

class PdfCrowd{
}
0

Try this:

<?php

namespace Company\Module\Controller\Adminhtml\Quote;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Filesystem\DirectoryList as DirectoryList;

use Company\Module\Helper\PdfCrowd;

class QuotePdf extends Action
{
    /**
     * @var \Magento\Framework\App\Response\Http\FileFactory
     */
    protected $fileFactory;
    protected $_mediaDirectory;
    protected $directory_list;

    protected $dompdfFactory;
    protected $layoutFactory;

    protected $authSession;
    //protected $quote_info;
    protected $userFactory;
    protected $_pdfCrowd;
    public function __construct(
        Context $context,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Backend\Model\Auth\Session $authSession,
        PdfCrowd $pdfCrowd,
        DirectoryList $directory_list,
        \Magento\User\Model\UserFactory $userFactory
    ) {
        parent::__construct($context);
    $this->_pdfCrowd = $pdfCrowd;
        $this->fileFactory = $fileFactory;
        $this->authSession = $authSession;
        $this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
        $this->directory_list = $directory_list;
        $this->userFactory = $userFactory;
    }

    public function execute()
    {
        if($this->authSession->isLoggedIn()){

            $id = $this->getRequest()->getParam('id');

            $client = $this->_pdfCrowd->createPdf("MYUSERNAME", "xxxxxxx-KEY");

            // set HTTP response headers
            $pdf = $client->convertHtml($this->getHtmlForPdf($id));

            header("Content-Type: application/pdf");
            header("Cache-Control: no-cache");
            header("Accept-Ranges: none");
            header("Content-Disposition: attachment; filename=commercialsale-".$id.".pdf");

            echo $pdf;
        }
    }
}

I have replaced method name and called with dependency injection.

<?php

namespace Company\Module\Helper;

class PdfCrowd {
    //
    // Pdfcrowd constructor.
    // 
    // $username - your username at Pdfcrowd
    // $apikey  - your API key
    // $hostname - API hostname, defaults to pdfcrowd.com
    // 
    function createPdf($username, $apikey, $hostname=null){
        if ($hostname)
            $this->hostname = $hostname;
        else
            $this->hostname = self::$api_host;
        $this->useSSL(false);
        $this->fields = array(
            'username' => $username,
            'key' => $apikey,
            'pdf_scaling_factor' => 1,
            'html_zoom' => 200);
        $this->proxy_name = null;
        $this->proxy_port = null;
        $this->proxy_username = "";
        $this->proxy_password = "";

        $this->user_agent = "pdfcrowd_php_client_".self::$client_version."_(http://pdfcrowd.com)";
    }
}
?>
2
  • This error. Class Company\Module\Helper\PdfCrowd does not exist in [Company\Module\Controller\Adminhtml\Quote\QuotePdf\Interceptor]
    – Hardik
    Jun 8, 2018 at 13:38
  • please run this php bin/magento setup:di:compile then php bin/magento cache:flush
    – kunj
    Jun 8, 2018 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.