PHP - 如何使用 mPDF 合并 PDF
我首先会说我可以用 mPDF 很好地生成 PDF,但对于我的生活,我无法让它将现有的 PDF 与它刚刚生成的 PDF 合并.
I will start out by saying that I can generate PDFs just fine with mPDF, but for the life of me, I can't get it to merge an existing PDF with the PDF it just generated.
我需要弄清楚的是如何将现有的 PDF 附加/添加到新生成的 PDF.我已经尝试使用 mPDF 方法来导入页面,但我得到的只是一个错误,如:
What I need to figure out is how to append/add the existing PDF to the newly generated PDF. I've tried using the mPDF methods for importing pages, but all I can get is an error like:
mPDF error: Cannot open '/downloads/test.pdf'.
上述消息含糊不清,不清楚为什么它无法打开文件...这是我用来尝试合并 PDF 的代码:
The above message is ambiguous and unclear as to WHY it can't open the file... Here's the code I'm using to try and merge PDFs:
include_once("./pdf/mpdf/mpdf.php");
$output_file = $_GET['output_file'];
$url = $_GET['input_file'];
$technical_drawing = $_GET['tech_drawing'];
$html = file_get_contents($url);
$mpdf = new mPDF('utf-8','Letter','','',0,0,0,0,0,0,'P');
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile($technical_drawing);
$tplIdx = $mpdf->ImportPage($pagecount);
$mpdf->UseTemplate($tplIdx);
$mpdf->WriteHTML($html);
$mpdf->Output($output_file, 'D');
exit;
$output_file 将是显示给用户的文件名.$url 是我们在 PDF 生成期间写入文件的 HTML.$technical_drawing 是我们想要与生成的 PDF 附加/合并的 PDF 的相对路径.
$output_file will be the filename shown to the user. $url is the HTML we're writing into the file during PDF generation. $technical_drawing is a relative path to a PDF that we want to append/merge with the generated PDF.
我知道我可以使用诸如 ghostscript 之类的东西,但我在客户端的服务器上没有这种类型的访问权限.
I understand I could use something like ghostscript, but I don't have that type of access on the client's server.
让我知道是否有人使用 mPDF 找到了解决方案,或者如果我是 S.O.L.并且需要找到另一个库来进行 PDF 合并.我真的在寻找解决方案或建议,而不仅仅是指向另一个库的链接.我已经用尽了我在 Google 或 mPDF 文档中可以找到的描述我遇到的错误的内容.
Let me know if anyone has found a solution to this using mPDF or if I'm S.O.L. and need to find another library to do PDF merging. I am really looking for solutions or suggestions, but not just links to another library. I've exhausted what I can find in Google or in mPDF's documentation that describes the error I'm having.
将 mPDF 错误从 http://example.com/pdf/example.pdf 更改为 '/downloads/test.pdf'.
changed mPDF error from http://example.com/pdf/example.pdf to '/downloads/test.pdf'.
EDIT_2:已修复代码以采用相对路径.
EDIT_2: Code has been fixed to take relative path.
这是最终的工作代码.如果有人知道如何指定将 HTML 写入 PDF 文档的顺序,将页面作为最后一页导入(自定义页面大小不同于 HTML 的自定义页面大小),则奖励.
Here's final working code. Bonus if anyone knows how to specify the order of writing HTML to PDF document, importing page as last page (with custom page size different from that of the HTML).
include_once("./pdf/mpdf/mpdf.php");
$output_file = 'test-' . $_GET['output_file'];
$url = $_GET['input_file'];
$technical_drawing = $_GET['tech_drawing'];
$html = file_get_contents($url);
if(!file_exists($technical_drawing)) {
$mpdf = new mPDF('utf-8','Letter','','',0,0,0,0,0,0,'L');
} else {
$mpdf = new mPDF('utf-8','A3-L','','',0,0,0,0,0,0,'L');
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile($technical_drawing);
$import_page = $mpdf->ImportPage();
$mpdf->UseTemplate($import_page);
// Add Last page
$mpdf->AddPageByArray(array(
'orientation' => 'P',
'ohvalue' => 1,
'ehvalue' => -1,
'ofvalue' => -1,
'efvalue' => -1,
'newformat' => 'Letter'
));
}
$mpdf->WriteHTML($html);
$mpdf->Output($output_file, 'D');
exit;
推荐答案
我像这样合并 pdf - {所有文件中的所有页面}:
I Merge pdfs like this - {all pages in all files}:
$this = 扩展 mPDF 类的类...
$this = Class that extends mPDF class...
function mergePDFFiles(Array $filenames, $outFile)
{
if ($filenames) {
$filesTotal = sizeof($filenames);
$fileNumber = 1;
$this->SetImportUse(); // this line comment out if method doesnt exist
if (!file_exists($outFile)) {
$handle = fopen($outFile, 'w');
fclose($handle);
}
foreach ($filenames as $fileName) {
if (file_exists($fileName)) {
$pagesInFile = $this->SetSourceFile($fileName);
for ($i = 1; $i <= $pagesInFile; $i++) {
$tplId = $this->ImportPage($i); // in mPdf v8 should be 'importPage($i)'
$this->UseTemplate($tplId);
if (($fileNumber < $filesTotal) || ($i != $pagesInFile)) {
$this->WriteHTML('<pagebreak />');
}
}
}
$fileNumber++;
}
$this->Output($outFile);
}
}
相关文章