[英]PHP - How to use mPDF to merge PDFs
我首先會說我可以用 mPDF 很好地生成 PDF,但對於我的生活,我無法讓它將現有的 PDF 與它剛剛生成的 PDF 合並。
我需要弄清楚的是如何將現有的 PDF 附加/添加到新生成的 PDF。 我已經嘗試使用 mPDF 方法來導入頁面,但我能得到的只是一個錯誤,如:
mPDF error: Cannot open '/downloads/test.pdf'.
上面的消息含糊不清,不清楚為什么它無法打開文件......這是我用來嘗試合並PDF的代碼:
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 的相對路徑。
我知道我可以使用類似 ghostscript 的東西,但我在客戶端的服務器上沒有這種類型的訪問權限。
讓我知道是否有人使用 mPDF 找到了解決方案,或者如果我是 SOL 並且需要找到另一個庫來進行 PDF 合並。 我真的在尋找解決方案或建議,而不僅僅是指向另一個庫的鏈接。 我已經用盡了我在谷歌或 mPDF 文檔中可以找到的描述我遇到的錯誤的內容。
編輯:將 mPDF 錯誤從http://example.com/pdf/example.pdf更改為“/downloads/test.pdf”。
EDIT_2:代碼已修復為采用相對路徑。
這是最終的工作代碼。 如果有人知道如何指定將 HTML 寫入 PDF 文檔的順序,將頁面作為最后一頁導入(自定義頁面大小不同於 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 - {所有文件中的所有頁面}:
$this = 擴展 mPDF 類的類...
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 7.4 的 mpdf 8.0.7。
Mfiles = array();
//Multiple pdf store in Mfiles array with full path.
array_push($Mfiles,'/assets/'.$pdfname.'.pdf');
$Mpath = 'Give file path with have you need to merge all pdf';
// after loop we start this code
if($Mfiles)
{
$filesTotal = sizeof($Mfiles);
$fileNumber = 1;
if (!file_exists($Mpath))
{
$handle = fopen($Mpath, 'w');
fclose($handle);
}
foreach ($Mfiles as $fileName)
{
if (file_exists($fileName))
{
$pagesInFile = $pdf->SetSourceFile($fileName);
for ($i = 1; $i <= $pagesInFile; $i++)
{
$tplId = $pdf->importPage($i);
$pdf->UseTemplate($tplId);
if (($fileNumber < $filesTotal) || ($i != $pagesInFile))
{
$pdf->WriteHTML('<pagebreak />');
}
}
}
$fileNumber++;
}
$pdf->Output($Mpath);
}
function mergePDFFiles(Array $filenames, $outFile) {
$mpdf = new mPDF();
if ($filenames) {
// print_r($filenames); die;
$filesTotal = sizeof($filenames);
$fileNumber = 1;
$mpdf->SetImportUse();
if (!file_exists($outFile)) {
$handle = fopen($outFile, 'w');
fclose($handle);
}
foreach ($filenames as $fileName) {
if (file_exists($fileName)) {
$pagesInFile = $mpdf->SetSourceFile($fileName);
//print_r($fileName); die;
for ($i = 1; $i <= $pagesInFile; $i++) {
$tplId = $mpdf->ImportPage($i);
$mpdf->UseTemplate($tplId);
if (($fileNumber < $filesTotal) || ($i != $pagesInFile)) {
$mpdf->WriteHTML('<pagebreak />');
}
}
}
$fileNumber++;
}
$mpdf->Output($outFile, 'D');
}
}
這是一個老問題。 我在將我的 PDF 合並為一個文件並在瀏覽器中顯示它后遇到了麻煩。 上面的兩個答案經過我的測試,沒有按預期工作。 第一個文件的第一頁顯示正確,第二個文件的第一頁跳了一個空白頁,被截斷了,即只顯示了N頁中的N-1頁。 最后一頁不見了。 這是我在爬入 mpdf.php 源代碼后所做的解決方法:
function mergePDFFiles(Array $filenames, $outFile, $title='', $author = '', $subject = '') {
$mpdf=new mPDF('c');
$mpdf->SetTitle($title);
$mpdf->SetAuthor($author);
$mpdf->SetSubject($subject);
if ($filenames) {
$filesTotal = sizeof($filenames);
$mpdf->SetImportUse();
for ($i = 0; $i<count($filenames);$i++) {
$curFile = $filenames[$i];
if (file_exists($curFile)){
$pageCount = $mpdf->SetSourceFile($curFile);
for ($p = 1; $p <= $pageCount; $p++) {
$tplId = $mpdf->ImportPage($p);
$wh = $mpdf->getTemplateSize($tplId);
if (($p==1)){
$mpdf->state = 0;
$mpdf->UseTemplate ($tplId);
}
else {
$mpdf->state = 1;
$mpdf->AddPage($wh['w']>$wh['h']?'L':'P');
$mpdf->UseTemplate($tplId);
}
}
}
}
}
$mpdf->Output();
unset($mpdf);
}
不使用$outFile
,因為生成的 PDF 旨在強制瀏覽器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.