使用MPDF将两个PDF文件合并为一个 [英] Merge two PDF files into single one using MPDF
本文介绍了使用MPDF将两个PDF文件合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用MPDF库生成pdf文件.我在根目录中创建了两个PDF文件,如下所示:
I am using MPDF library to generate pdf files .I have created two PDF files in my root directory as follows :
$invoice_nos = ['0'=>'ISE-00000014Y18','1'=>'ISE-00000005Y18'];
foreach ($invoice_nos as $key => $invoice_no) {
$html = 'Invoice No - '.$invoice_no;
$pdf_file_name = $invoice_no.'.pdf';
$pdf_file_path = ROOT . '/app/webroot/Service_Invoices/'. DS .$pdf_file_name ;
ob_start();
$mpdf = new \mPDF('utf-8', 'A4' ,'','',5,5,36,10,5,4);
$mpdf->WriteHTML($html,2);
ob_clean();
$mpdf->Output($pdf_file_name,'f');
}
现在,我想将这两个文件合并为具有不同页面的单个文件.我怎样才能做到这一点?我搜索了很多示例,但没有任何效果.
Now I want to merge these two files into a single file with different pages. How can I do this? I have searched many examples of it but nothing is working.
推荐答案
mPDF并不是合并PDF文件的最佳工具. GhostScript :
mPDF is not the best tool to merge PDF files. You'll be better off with GhostScript:
gs -dBATCH -dSAFER -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=combined.pdf invoice1.pdf invoice2.pdf
或者,将两个发票直接生成到一个文件:
Alternatively, generate both invoices directly to one file:
$invoice_nos = ['0' => 'ISE-00000014Y18', '1' => 'ISE-00000005Y18'];
$mpdf = new \mPDF('utf-8', 'A4', '', '', 5, 5, 36, 10, 5, 4);
foreach ($invoice_nos as $key => $invoice_no) {
$html = 'Invoice No - ' . $invoice_no;
$mpdf->WriteHTML($html, 2);
$mpdf->WriteHTML('<pagebreak>');
}
$pdf_file_name = $invoice_no . 'invoices.pdf';
$mpdf->Output($pdf_file_name, 'f');
这篇关于使用MPDF将两个PDF文件合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文