0

Followed these steps:

  1. Download TCPDF from https://github.com/tecnickcom/tcpdf

  2. Change the name of the file root directory after extraction to TCPDF

  3. Inside this directory, there is a tcpdf.php file, change the name to TCPDF

  4. Open the file and change the class name from tcpdf to TCPDF_TCPDF

  5. Copy the files into your Magento “lib” folder in the root Magento installation directory. i.e lib/TCPDF

    And can be called this class by $tcpdf = new TCPDF_TCPDF() from anywhere in Magento. Here is Source

But this is not working!! Getting Error like: Fatal error:

Class 'Vendor\Module\Controller\Adminhtml\Generatepdf\TCPDF_TCPDF' not found in E:\Xampp\htdocs\magento\app\code\Vendor\Module\Controller\Adminhtml\Generatepdf\CreatePdf.php on line 35

Installation with composer works fine and I get PDF generated! But I have to send this complete package and cannot ask client to run composer command for this module.I think autoloader is not getting aware of this library. How can I integrate it?

This is my class:

<?php

namespace Vendor\Module\Controller\Adminhtml\Generatepdf;

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


class CreatePdf extends \Magento\Backend\App\Action  {

protected $_dir;
protected $customerName;
protected $order;
public function __construct(
    Context $context,
    Order $order,
    DirectoryList $dir

)
{
    $this->order = $order;
    $this->_dir = $dir;

    parent::__construct($context);
}

public function execute() {

     $tcpdf = new TCPDF_TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    //$tcpdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $tcpdf->SetCreator(PDF_CREATOR);

    $tcpdf->SetTitle('Title');

    $tcpdf->setPrintHeader(false);

    $tcpdf->setPrintFooter(false);


    // set default monospaced font

    $tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);


    // set margins

    $tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

    $tcpdf->SetHeaderMargin(PDF_MARGIN_HEADER);

    $tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);


    // set auto page breaks

    $tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);


    // set image scale factor

    $tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);


    // set some language-dependent strings (optional)

    if (@file_exists(dirname(__FILE__) . '/lang/eng.php')) {

        require_once(dirname(__FILE__) . '/lang/eng.php');

        $tcpdf->setLanguageArray($l);

    }

    // ---------------------------------------------------------

    // set default font subsetting mode

    $tcpdf->setFontSubsetting(true);

    //your htmls here

    $html=$this->getHtml();

    // set some language dependent data:

    $lg = Array();

    $lg['a_meta_charset'] = 'UTF-8';


    $tcpdf->setLanguageArray($lg);


    // set font

    //dejavusans & freesans For Indian Rupees symbol

    $tcpdf->SetFont('freesans', '', 12);

    // remove default header/footer

    //$tcpdf->setPrintHeader(false);

    $tcpdf->setPrintFooter(false);


    $tcpdf->AddPage();


    $tcpdf->writeHTML($html, true, false, true, false, '');


    $tcpdf->lastPage();

    //$tcpdf->Output('report_per_route.pdf', 'I');

    //$this->logger->debug('report_per_route');
    $baseurl = $this->getDirPath();
    $filename = $baseurl .'/export/'. 'Sample_pdf'. time().'.pdf';
    $tcpdf->Output($filename, 'I');
}

}
?>

1 Answer 1

5

Here is the successfully tested way to do this:

  1. Download TCPDF from https://github.com/tecnickcom/tcpdf

  2. Change the name of the file root directory after extraction to TCPDF

  3. Inside this directory, there is a tcpdf.php file, change the name to TCPDF

  4. Open the file and change the class name from tcpdf to TCPDF_TCPDF

  5. Copy the files into your Magento “lib/internal” folder in the root Magento installation directory.

=>So it should look like this: lib/internal/TCPDF

Now, write below statement in the class where you want to make tcpdf object:

require_once('lib/internal/TCPDF/TCPDF.php');

And use this class as:

$tcpdf = new \TCPDF_TCPDF();

Example Code is:

<?php

namespace Vendor\Module\Controller\Adminhtml\Generatepdf;

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

require_once('lib/internal/TCPDF/TCPDF.php');

class CreatePdf extends \Magento\Backend\App\Action  {

protected $_dir;
protected $customerName;
protected $order;
public function __construct(
Context $context,
Order $order,
DirectoryList $dir

)
{
$this->order = $order;
$this->_dir = $dir;

parent::__construct($context);
}

public function execute() {

$tcpdf = new \TCPDF_TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$tcpdf->SetCreator(PDF_CREATOR);

$tcpdf->SetTitle('Title');

$tcpdf->setPrintHeader(false);

$tcpdf->setPrintFooter(false);


// set default monospaced font

$tcpdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);


// set margins

$tcpdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

$tcpdf->SetHeaderMargin(PDF_MARGIN_HEADER);

$tcpdf->SetFooterMargin(PDF_MARGIN_FOOTER);


// set auto page breaks

$tcpdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);


// set image scale factor

$tcpdf->setImageScale(PDF_IMAGE_SCALE_RATIO);


// set some language-dependent strings (optional)

if (@file_exists(dirname(__FILE__) . '/lang/eng.php')) {

    require_once(dirname(__FILE__) . '/lang/eng.php');

    $tcpdf->setLanguageArray($l);

}

// ---------------------------------------------------------

// set default font subsetting mode

$tcpdf->setFontSubsetting(true);

//your htmls here

$html=$this->getHtml();

// set some language dependent data:

$lg = Array();

$lg['a_meta_charset'] = 'UTF-8';


$tcpdf->setLanguageArray($lg);


// set font

//dejavusans & freesans For Indian Rupees symbol

$tcpdf->SetFont('freesans', '', 12);

// remove default header/footer

//$tcpdf->setPrintHeader(false);

$tcpdf->setPrintFooter(false);


$tcpdf->AddPage();


$tcpdf->writeHTML($html, true, false, true, false, '');


$tcpdf->lastPage();

//$tcpdf->Output('report_per_route.pdf', 'I');

//$this->logger->debug('report_per_route');
$baseurl = $this->getDirPath();
$filename = $baseurl .'/export/'. 'Sample_pdf'. time().'.pdf';
$tcpdf->Output($filename, 'I');
}

}
?>

Happy coding :)

Your Answer

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

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