I am looking to change the page size of the pdf output. I see that the default is A4. I'd like to change it to Letter. Is there a way to do this either in Entity Print or PHP WK HTML to PDF?

Comments

MrPeanut created an issue. See original summary.

benjy’s picture

This is possible via a little bit of code. I'm open to adding admin settings for the globally available options for wkhtmltopdf, we could even allow per entity type overrides without too much effort.

Here's an example of how you'd implement hook_entity_print_pdf_alter() to adjust the settings.

<?php
/**
 * Implements hook_entity_print_pdf_alter().
 */
function HOOK_entity_print_pdf_alter(WkHtmlToPdf $pdf, $entity_type, $entity) {
  $pdf->setOptions(array(
    'page-size' => 'A5',
  ));
}
?>
benjy’s picture

Also, all options are documented here: http://wkhtmltopdf.org/usage/wkhtmltopdf.txt

We should probably update the docs with a few examples like this.

benjy’s picture

Status: Active » Needs review

Settings to NR, will give this a few more days and then add this to the docs and mark as fixed.

mrpeanut’s picture

Here is what I have in my custom module:

/**
 * Implements hook_entity_print_pdf_alter().
 */
function mrpeanut_entity_print_pdf_alter(WkHtmlToPdf $pdf, $entity_type, $entity) {
  $pdf->setOptions(array(
    'page-size' => 'Letter',
  ));
}

I ran update.php and cleared cache but am still getting an A4 pdf.

benjy’s picture

Are you sure it's not working, Letter is only 5mm smaller than the default A4 so it could be hard to notice. See here for the sizes that are used: http://doc.qt.io/qt-4.8/qprinter.html#PaperSize-enum

Give A5 a try, that should be half the size of A4 and obvious for you to see.

mrpeanut’s picture

A5 is not working for me either. I even tried changing the orientation to landscape and still no change. Is mymodule_entity_print_pdf_alter the correct function? Is there a way I can test to see if that's being called? (The module itself is because I have other functions in it.)

benjy’s picture

Put a die in the hook, the PDF won't be generated and execution will halt if the code is firing. Few obvious things to check:

* Make sure you've spelt the module and hook name correctly.
* Make sure the module is enabled
* Make sure you clear the cache.

If all that fails, paste the code here for me to see it.

mrpeanut’s picture

The module is spelled correctly and enabled (I'm using other functions in the same module). I cleared cache. I put a die in the hook, and the PDF is still being generated. Here's my code (my module is mrpeanut.module):

function mrpeanut_entity_print_pdf_alter(WkHtmlToPdf $pdf, $entity_type, $entity) {
  $pdf->setOptions(array(
    'page-size' => 'A5',
  ));
  die();
}
benjy’s picture

Check entity_print.info, what version do you have?

mrpeanut’s picture

Wow, stupid mistake! I was originally on dev but had downloaded 1.3 when I was testing this. Sorry to waste your time! That works now.

benjy’s picture

Status: Needs review » Fixed

Great! I've added an example to the docs: https://www.drupal.org/node/2430561#overriding-page-size

nithinkolekar’s picture

Status: Fixed » Active

I am trying to change views pdf page size and orientation using hook_entity_print_views_pdf_alter with the following code but pdf is still printing in default a4, portrait .
Also tried placing die() before and inside if statement but pdf is generating instead of exiting.

function twc_pdfprint_print_views_pdf_alter(WkHtmlToPdf $pdf, $view) {
  //die();
  watchdog('hooktest', 'hook called for view %viewname', array('%viewname' => 'bank_challan'), WATCHDOG_INFO);

  if ($view->name === 'bank_challan') {
    $pdf->setOptions(array(
      'orientation' => 'Landscape',
      'page-size' => 'A5',
    ));
    die();
  }
}
benjy’s picture

twc_pdfprint_print_views_pdf_alter doesn't look right, if your module is twc_pdfprint then it should twc_pdfprint_entity_print_pdf_view_alter($pdf, $view)

I've just tagged 7.x-1.4 to prevent confusion as the hook wasn't in 7.x-1.3.

nithinkolekar’s picture

I tested by changing the function name (which was typo earlier) with updated 7.x-1.4 but it seems that hook in not called and watchdog message is not logging.

updated code

function twc_pdfprint_entity_print_views_pdf_alter(WkHtmlToPdf $pdf, $view) {
  //die();
  watchdog('hooktest', 'hook called for view %viewname', array('%viewname' => 'bank_challan'), WATCHDOG_INFO);

  if ($view->name === 'bank_challan') {
    $pdf->setOptions(array(
      'orientation' => 'Landscape',
      'page-size' => 'A5',
    ));
    die();
  }
}

in your comment above you are mentioning hook_entity_print_pdf_view_alter($pdf, $view) but in api file it is hook_entity_print_views_pdf_alter(WkHtmlToPdf $pdf, $view) which one is correct?

benjy’s picture

drupal_alter('entity_print_pdf_view', $pdf, $this->view);

entity_print_pdf_view_alter() is correct, i used view instead of views. I'll fix up the api file.

nithinkolekar’s picture

ok got it ... there is a small typo in hook name too.

// Allow other modules to alter the generated PDF object.
drupal_alter('entity_print_pdf_view', $pdf, $this->view);

hook_entity_print_view_pdf_alter

IMO hook_entity_print_views_pdf_alter is good which has the module name(entity_print_views) and we can easily identify it for pdf options.

benjy’s picture

Status: Active » Fixed

OK, i've added an additional hook for hook_entity_print_views_pdf_alter().

  • benjy committed 962f405 on 7.x-1.x
    Issue #2566573 by benjy, nithinkolekar make hook names consistent.
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

maxplus’s picture

Hi,
I'm also trying to change some settings for my views output but the view is not changing its output when I change some parameters.

This is my code in my theme template.php, am I doing something wrong?

function THEME_entity_print_views_pdf_alter(WkHtmlToPdf $pdf, $view) {
  $pdf->setOptions(array(
    'page-size' => 'A4',
    'margin-left' => '30mm',
    'margin-right' => '0mm',
    'margin-top' => '30mm',
    'margin-bottom' => '8mm',
  ));
}

Thanks!