Add an External PDF file using TCPDF

7.9k views Asked by At

I am using TCPDF for generating PDF Documents. Now I Have a Scenario in which i want to add and External PDF file between my TCPDF Generating File. I tried it very much but i failed.

Here is My Code

<?php

require_once('tcpdf_include.php');
require_once('../tcpdf.php');
require_once('fpdi2/src/autoload.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('PACRA');
$pdf->SetTitle('Client Undertaking');
$pdf->SetSubject('Client Undertaking');
$pdf->SetKeywords('Client Undertaking, PDF');
// set default header data
$pdf->setHeaderData('','', 'CLIENT ACCEPTANCE UNDERTAKING', '', array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->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');
    $pdf->setLanguageArray($l);
}
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 9, '', true);

// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();

// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

//$utf8text = file_get_contents('../LICENSE.TXT', true);

// Set some content to print
$html = <<<EOD

<p>Clauses 11-A-(r), 12-2-(j & I) & Annexure F & Annexure I </p>
<p>
<p>I, in connection with the Rating of <b>$opTitle</b>, hereby, undertake that I </p>
<p>I.   Or any of my Family Members do not have any Interest in it [Clause 12-2-(i)]</p>
<p>II.  Or any of my Family Members do not buy or sell, engage in any transaction or Own Securities of or of any entity related to it, other than holdings in diversified collective investment schemes [Clause 12-2-(I) & Annexure I:  3-ii-(a) & (b)]</p>
<p>III. Am not an Associate of any director, Substantial Shareholder or senior management officer of the customers of 
PACRA [Annexure F:  d-(i)]</p>
<p>IV.  Am not Substantial Shareholder of the customer being rated by PACRA [Annexure F:  d-(ii)]</p>
<p> V.  Am not a director of an entity being rated by PACRA [Annexure F:  d-(iii)]</p>
<p> VI. Have not had a recent employment (within the past six (6) months) or other Significant Business or Personal Relationship with it that may cause or may be perceived as causing a Conflict of Interest [Clause 11-(A)-(r)]<p>
<p> VII.    Do not have an immediate relation (i.e., a spouse, partner, parent, child, or sibling) who currently
 works for it [Annexure I:  3-ii-(d)]</p>
<p>VIII.    Do not have any other relationship with it directly or indirectly that may cause or may be perceived as causing Conflict of Interest [Annexure I:  3-ii-(e)]</p>
<p>IX.  If “Yes” to any of the above statements, shall not get involved in its rating process (Annexure I: 3-ii]</p>
<p>X.   Shall not join any PACRA Client (including this one), within one year of relieving from PACRA [Annexure I:  3-iv] </p>
<p>XI.  At any time during the rating process, if any of the above conditions become applicable, shall immediately make Disclosure to Team Leader and withdraw from the rating assignment<p>
<p> Capitalized terms used herein are defined in “Defined Terms” available on the website:  www.pacra.com </p>
EOD;



// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

$pdf = new FPDI();
$pdf->AddPage();
$pdf->AddFont('courier');
$pdf->Write(10, 'page 1 created by TCPDF');
$pages = $pdf->setSourceFile('middle.pdf');
for($i=0; $i<$pages; $i++)
{
     $pdf->AddPage();
     $tplIdx = $pdf->importPage($i+1);
     $pdf->useTemplate($tplIdx, 10, 10, 200);
}
$pdf->AddPage();
$pdf->Write(10, 'page 2 created by TCPDF');




$pdf->Output($fileName, 'I');

//$ordernumber=1;

//$pdf->Output('underTaking'.$ordernumber.'.pdf', 'F');

//============================================================+
// END OF FILE
//============================================================+
?>
1

There are 1 answers

1
Jan Slabon On

Just use the correct class and do not overwrite the $pdf variable!

<?php

require_once('tcpdf_include.php');
require_once('../tcpdf.php');
require_once('fpdi2/src/autoload.php');

// create new PDF document
$pdf = new \setasign\Fpdi\TcpdfFpdi(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('PACRA');
//
// [...] the other code
//
// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

$pdf->AddPage();
$pdf->AddFont('courier');
$pdf->Write(10, 'page 1 created by TCPDF');
$pages = $pdf->setSourceFile('middle.pdf');
for($i=0; $i<$pages; $i++)
{
     $pdf->AddPage();
     $tplIdx = $pdf->importPage($i+1);
     $pdf->useTemplate($tplIdx, 10, 10, 200);
}
$pdf->AddPage();
$pdf->Write(10, 'page 2 created by TCPDF');

$pdf->Output($fileName, 'I');