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;