PHP - How to use mPDF to merge PDF files

I will start by saying that I can create PDF files just fine with mPDF, but for life I cannot get it to combine an existing PDF file with a newly generated PDF file.

I need to figure out how to add / add an existing PDF to a newly created PDF file. I tried using mPDF methods to import pages, but all I can get is an error, for example:

mPDF error: Cannot open '/downloads/test.pdf'. 

The above message is ambiguous and unclear why it cannot open the file ... Here is the code that I use to try to combine PDF files:

  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 will be displayed by username. $ url is the HTML we write to the file during PDF generation. $ technical_drawing is the relative path to the PDF that we want to add / combine with the created PDF.

I understand that I can use something like ghostscript, but I do not have this type of access on the client server.

Let me know if anyone found a solution for this using mPDF, or if I am SOL and need to find another library for merging PDF files. I am really looking for solutions or suggestions, but not just links to another library. I have exhausted what I can find on Google or in the mPDF documentation that describes the error I have.

EDIT: mPDF error changed from http://example.com/pdf/example.pdf to '/downloads/test.pdf'.

EDIT_2: The code has been fixed to take a relative path.

Here is the final working code. A bonus if anyone knows how to specify the order in which the HTML document is written to PDF by importing the page as the last page (with a custom page size other than the HTML size).

  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; 
+6
source share
2 answers

I merge such PDF files - {all pages in all files}:

$ this = class that extends the mPDF class ...

 function mergePDFFiles(Array $filenames, $outFile) { if ($filenames) { $filesTotal = sizeof($filenames); $fileNumber = 1; $this->SetImportUse(); 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); $this->UseTemplate($tplId); if (($fileNumber < $filesTotal) || ($i != $pagesInFile)) { $this->WriteHTML('<pagebreak />'); } } } $fileNumber++; } $this->Output($outFile); } } 
+3
source
 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'); } } 
0
source

Source: https://habr.com/ru/post/976846/


All Articles