0

I have added TCPDF as follows:

  1. Downloaded TCPDF library from https://github.com/tecnickcom/tcpdf and place at magento2.0/lib/internal/TCPDF
  2. Renamed tcpdf.php to TCPDF.php
  3. Opened TCPDF.php and change class name to class TCPDF_TCPDF
  4. Created Controller

magento2.0/app/code/Namespace/Module_Name/Controller/Generatepdf/createpdf.php

<? php

namespace Namespace\Module_Name\Controller\Generatepdf;

use TCPDF_TCPDF;

use Magento\Framework\App\Filesystem\DirectoryList;

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

public function execute() {
$baseDir = DirectoryList::MEDIA;

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

// set document information

    $tcpdf->SetCreator(PDF_CREATOR);

    $tcpdf->SetAuthor('Nicola Asuni');

    $tcpdf->SetTitle('TCPDF Example 001');

    $tcpdf->SetSubject('TCPDF Tutorial');

    $tcpdf->SetKeywords('TCPDF, PDF, example, test, guide');


// set default header data

    $Header_title = 'Sample PDF';

    $tcpdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE . $Header_title, PDF_HEADER_STRING, array(0, 64, 255), array(0, 64, 128));

    $tcpdf->setFooterData(array(0, 64, 0), array(0, 64, 128));


// set header and footer fonts

    $tcpdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));

    $tcpdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));


// 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 = **For PDF file Write Your HTML Code here** ;

// 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');

    $filename = $baseurl . '/Sample_pdf'. time().'.pdf';

    $tcpdf->Output($filename, 'F');




}

}


? >

I want to understand how to add my code in above class. My required PDF should contain fileds like Order Number, Customer Name, Item Name, Item Quanity. I am getting these values in my custom template file from Order collection.

Thanks in advance !!

1

try tp Add your data like this after setting your languages in the above code

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

// set font
$pdf->SetFont('times', 'BI', 12);

// add a page
$pdf->AddPage();

// set some text to print
$txt = <<<EOD
TCPDF Example 003

 Custom page header and footer are defined by extending the TCPDF class     and overriding the Header() and Footer() methods.
 EOD;

 // print a block of text using Write()
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);

 // ---------------------------------------------------------
| improve this answer | |
0
  • add code in composer.json

{ "name": "xyz/module-xyz", "description": "XYZ", "require": { "php": "~5.5.0|~5.6.0|~7.0.0|~7.1.0|~7.2.0", "tecnickcom/tcpdf ":"*" }, "type": "magento2-module", "version": "1.0.0", "license": [ "OSL-3.0", "AFL-3.0" ], "authors": [ { "name": "xyz", "email": "biz@xyz.com", "homepage": "https://example.com/" } ], "autoload": { "files": [ "registration.php" ], "psr-4": { "xyz\Xyz\": "" } } }

| improve this answer | |
  • after run command composer require tecnickcom/tcpdf – mahesh gohil Nov 22 '19 at 5:42

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.