Recently, I created an output template for the Print module, such that it dynamically generates a measurement sheet when a certain content type is parsed by its PDF functionality (using dompdf in this case).

Everything works great, but the client's requested file attachment fields for PDFs, such that existing PDFs are added to the end of dompdf's output. For instance, a PDF of a CAD drawing would be attached in Drupal, printpdf/$nid generates the template I've created, and the file locations given from the file upload fields are used to merge the files at those locations with the generated PDF.

dompdf apparently cannot merge PDF documents, so I'm thinking a workaround would be to generate the PDF and save to disk somewhere, run another PDF merger script, then present the merged output to the user.

How would one go about programmatically saving the output of Print.module's PDF generator to somewhere on the server, then running an external class on that saved output?

(Alternately, if you know of a way for dompdf to read PDFs and append them to the document, that'd work even better.)

1 Answer 1


I hacked print_pdf.pages.inc to merge PDFs after creation based on the existence of a filefield:

Starting at line 209, in function _print_pdf_dompdf();

    $node = $print['node']; 
    if ($node->field_pdf[0]) {
        $meas_pdf = $dompdf->output();
        file_put_contents($base_path . 'sites/default/files/pdf_temp.pdf', $meas_pdf);
        include $base_path . 'sites/all/libraries/PDFMerger/PDFMerger.php';
        $newpdf = new PDFMerger;
        $newpdf->addPDF($base_path . 'sites/default/files/pdf_temp.pdf', 'all');    
        foreach ($node->field_pdf as $attach) {
        $newpdf->addPDF($base_path . $attach['filepath'], 'all');
        $newpdf->merge('download', 'measurements.pdf');
    else {$dompdf->stream($filename, array('Attachment' => ($print_pdf_content_disposition == 2))); }

It'd be great to write a module that undefines Print.module's private _print_pdf_dompdf(); function and replaces it with my own (so I'm not hacking a contrib module); any idea how I'd go about that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.