将FPDI合并到现有的TCPDF scipt中,以将exisitng pdf附加到创建的pdf中

时间:2018-12-07 22:49:29

标签: php tcpdf fpdi

我有使用TCPDF创建pdf发票的代码,它运行良好。现在,我需要将另一个pdf的内容添加到最后一页。

我找到了使用FPDI的服务器示例,但是无法弄清楚如何将其合并到下面的代码中。

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Harlyn Enterprises');
$pdf->SetTitle('');
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, 10, 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);
// create first page
$pdf->AddPage();
$html = "html code for page 1 in here";
$pdf->writeHTML($html, true, false, true, false, '');
// create second page
$pdf->AddPage();
$html = "html code for page 2 in here";
$pdf->writeHTML($html, true, false, true, false, '');
$pdf[$site]->lastPage();
//Close and output PDF document
ob_clean();
$file = $_SERVER["filename.pdf";
$pdf->Output($file, 'F');

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我知道了。使用composer安装FPDI-TCPDF并使用以下代码:

require_once('tcpdf.php');
use setasign\Fpdi;
use setasign\fpdf;
require_once('vendor/setasign/fpdf/fpdf.php');
require_once('vendor/setasign/fpdi/src/autoload.php');
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
//set_time_limit(2);
//date_default_timezone_set('UTC');
//$start = microtime(true);

//$pdf = new Fpdi\TcpdfFpdi();
$pdf = new Fpdi\TcpdfFpdi('p', 'mm', 'A4');

if ($pdf instanceof \TCPDF) {
    $pdf->SetProtection(['print'], '', 'owner');
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
}

// create first page
$pdf->AddPage();
$html = "html code for page 1 in here";
$pdf->writeHTML($html, true, false, true, false, '');
// create second page
$pdf->AddPage();
$html = "html code for page 2 in here";
$pdf->writeHTML($html, true, false, true, false, '');


//Define files to add
$files = [
        'example_006.pdf',
];

// Attach each page of each pdf
foreach ($files as $file) {
    $pageCount = $pdf->setSourceFile($file);

    for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
        $pdf->AddPage();
        $pageId = $pdf->importPage($pageNo, '/MediaBox');
        $s = $pdf->useTemplate($pageId, 10, 10, 200);
    }
}

//Create PDF
$pdf[$site]->lastPage();
$pdf->Output('output.pdf','I');
?>