【问题标题】:Add an External PDF file using TCPDF使用 TCPDF 添加外部 PDF 文件
【发布时间】:2017-09-14 12:19:46
【问题描述】:

我正在使用TCPDF 生成 PDF 文档。现在我有一个场景,我想在我的TCPDF 生成文件之间添加外部 PDF 文件。我尝试了很多,但我失败了。

这是我的代码

<?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
//============================================================+
?>

【问题讨论】:

    标签: php pdf tcpdf fpdi


    【解决方案1】:

    只需使用正确的类,不要覆盖 $pdf 变量!

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

    【讨论】:

    • 它告诉我Page Not Working 当我看到页面元素然后我识别它显示错误Failed to load resource: the server responded with a status of 500 (Internal Server Error)
    猜你喜欢
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2014-05-15
    • 2019-08-19
    • 2022-10-17
    • 1970-01-01
    相关资源
    最近更新 更多