Multitask that print module!

September 30
blog author

Appno Blogger

Appnovation Coop

Print (http://drupal.org/project/print) is a very useful third party module which allows you to create print friendly or pdf versions of your drupal site nodes. However, the default design only allows you to do one page at a time (eg calling 'printpdf/*node ID*'). In order to combine multiple nodes in a single printpdf call you need to modify the module code a bit. This blog will show you how to combine multiple pages in a single printpdf call. The example I am showing here is for using tcpdf. If you are using dompdf or something else you can still apply the similar idea.

Step 1

You need to first understand how the printpdf function works. By default, printpdf will take a single node id, parse that node and then format its content into pdf output. Since the function only accepts one node id by default, your first change will involve parsing the first input parameter. You can easily do so by applying a split function to the input parameter and form an array out of it.

  • Go to module folder and navigate to "print/print_pdf/". Locate the print_pdf.pages.inc file and then open the file to edit it.
  • Find "the print_pdf_controller" function
  • Under "$args = func_get_args();" add the line: "$a2 = split(',', $args[0]);"
  • Modify "$path = implode('/', $args);" to "$pah = implode('/', array($a2[0]));"

Step 2

  • Locate the function call to "_print_pdf_tcpdf" (in case you are using tcpdf) inside the print_pdf_controller function.
  • Modify the line "_print_pdf_tcpdf($print, $html, $pdf_filename);" to "_print_pdf_tcpdf($print, $html, $pdf_filename, $a2);"

Step 3

Time to get inside the "_print_pdf_tcpdf" function. Find the function inside print_pdf_pages.inc file.

  • Change the function call from "function _print_pdf_tcpdf($print, $html, $filename);" to "function _print_pdf_tcpdf($print, $html, $filename, $a2);"
  • Add a loop just before the line "$pdf = theme('print_pdf_tcpdf_header', $pdf, $html, $font);"
  • For each of the node id passed by the $a2 parameter (let's call it $nid), add the following 4 lines of code inside the loop. $p = print_controller($nid, NULL, PRINT_PDF_FORMAT); //$p would be the raw html output $n = $p['node']; $h = theme('print_page', $p, PRINT_PDF_FORMAT, $n); $h = drupal_final_markup($h);

Step 4

Now $h will be the fully themed output of the current node id. All you need to do is inserting this $h content into the $html content between the body tags. Now, save your work and test run the printpdf call with something like "printpdf/1,2,3". This will now print page 1, 2 and 3 in just one printpdf call. The original function "printpdf/1" for printing page 1 still works.