0

The Print module (https://www.drupal.org/project/print) allows users to generate a PDF of nodes, views and panels. It allows for the selection of a default page orientation (portrait or landscape), and it allows for administrators to override the default orientation in specific nodes.

However, I can't figure out how to override the default orientation when creating a PDF of a specific Views output. I have one View that needs to be printed in landscape format, the rest in portrait. Perhaps a PHP snippet in the header of the view?

Any help will be appreciated!

1 Answer 1

2

If you used MPDF as your library then add this function to your themes' template.php:

function MYTHEME_print_pdf_mpdf_alter(&$mpdf, $html, $meta) {
  //return;
  if(is_view_path()) {
    $paper_size = variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT);
    $format = $paper_size . "-L";
    // set document information
    $mpdf = new mPDF('UTF-8', $format);
    $mpdf->SetAuthor(strip_tags($meta['name']));
    $mpdf->SetCreator(variable_get('site_name', 'Drupal'));
  }
}

I could not find a way to alter the paper orientation after the object was instantiated, so I opted to just create it again (orientation is set in class creator).

You should then implement a function called is_view_path that returns true if it detect the printpdf path of the view, so that the code only runs for your specific view.

function is_view_path() {
  $path = current_path();
  return $path == 'VIEWPATH'; // just add your view path here
}

If you used TCPDF you should look at the function print_pdf_tcpdf_print_pdf_generate in print-pdf-tcpdf.page.inc at how to instantiate the $pdf object, and then implement an alter hook similarly to what I did above. I could not do it as for some reason I cannot get the tcpdf library to detect.

For DOMPDF I could not find an alter hook.

1
  • 1
    Thanks, J. Reynolds. I also noticed there is no alter hook for DOMPDF (which is the library I'm using). I followed the idea in #9 here drupal.org/node/608420 and added an alter hook for $page_orientation to print_pdf_dompdf.pages.inc. I guess it would make more sense to suggest a patch for an alter hook similar to the one in the mpdf file. Maybe I'll suggest that to the author when I have time.
    – argiepiano
    Nov 21, 2014 at 13:38

Your Answer

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

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