【问题标题】:incorporating FPDI in to an exisiting TCPDF scipt to append exisitng pdf to the created pdf将 FPDI 合并到现有的 TCPDF 脚本中,以将现有的 pdf 附加到创建的 pdf 中
【发布时间】:2018-12-07 22:49:28
【问题描述】:

我有这段代码使用 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');

任何帮助将不胜感激。

【问题讨论】:

    标签: php tcpdf fpdi


    【解决方案1】:

    我想通了。使用 composer 安装 FPDI-TCPD 并使用以下代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-13
      • 2012-01-01
      • 1970-01-01
      • 2018-04-04
      • 2013-08-08
      • 2016-01-29
      • 1970-01-01
      • 2021-11-20
      相关资源
      最近更新 更多