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.

tru.d
3251 gold badge2 silver badges17 bronze badges
asked Jun 8, 2018 at 13:09
3
  • 2
    Check exact folder and class name for Company\Module\Helper\PdfCrowd Commented 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. Commented Jun 8, 2018 at 13:53
  • yes, Linux is naming strict type. But other system like windows, mac not. Commented 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{
}
answered Jun 8, 2018 at 14:24
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)";
 }
}
?>
answered Jun 8, 2018 at 13:18
2
  • This error. Class Company\Module\Helper\PdfCrowd does not exist in [Company\Module\Controller\Adminhtml\Quote\QuotePdf\Interceptor] Commented Jun 8, 2018 at 13:38
  • please run this php bin/magento setup:di:compile then php bin/magento cache:flush Commented Jun 8, 2018 at 14:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.