問題描述
我首先會說我可以用 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);
}
}
這篇關于PHP - 如何使用 mPDF 合并 PDF的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!