Background: After the PDF is generated, I want to update a field on the node with a count of the number of pages in the PDF.

I first tried this code:

<?php
function mymodule_views_post_execute(&$view) {
  if (
$view->name == 'myviewname' && $view->current_display == 'pdf_1' ) {
   
//update associated node's pages value
   
$numPages = $view->pdf->getNumPages();
   
$nodeid = $view->args[1];
    if (
is_numeric( $numPages ) && is_numeric( $nodeid ) ) {
//quicker to update db directly than via node api
     
db_query("UPDATE {field_data_field_pages} SET field_pages_value = " . $numPages . " WHERE entity_id = " . $nodeid);
    } else {
     
watchdog('mymodule','numPages or nodeid not numeric! ' . $nodeid . '---' . $numPages);
    }
  }
}
?>

But the problem if found is that $views->pdf->getNumPages() is zero at this point (as the view has only been executed and not rendered).

I'm now trying the same code in the function hook_views_post_render(&$view,&$output,&$cache) but it's not firing at all.

I've cleared the caches, and even tested with a simple function:

<?php
function mymodule_views_post_render(&$view,&$output,&$cache) {
  if (
$view->name == 'myview' ) {
   
watchdog('mymodule','in mymodule_views_post_render, name=myview, current_display=' . $view->current_display);
  }
}
?>

With this code, I get a log entry when viewing any other display of that view, eg. 'page', but nothing when viewing my pdf display.

Any ideas or possible workarounds I can try here?

Comments

Priority:Normal» Minor
Status:Active» Closed (cannot reproduce)

I'm sorry, I was mistaken, the hook is firing correctly for my pdf display.

Component:Code» Documentation
Category:bug» task
Priority:Minor» Normal
Status:Closed (cannot reproduce)» Needs review

Actually as this code works as designed, I'm changing the component to documentation, because this could be useful for someone.

My use case for this code is to use it in conjunction with Drupal Commerce. A user can download their PDF, or can create an order for a printed PDF.
Obviously the price for the printing changes depending on how many pages there are in the PDF.
The number of pages isn't known until the PDF is generated, so this code will update the node's page_count field with the number of pages from the PDF file.
This field can then be used to display the calculated cost to the user.

Hope this helps someone in the future.

My final code:

<?php
function mymodule_views_post_render(&$view,&$output,&$cache) {
  if (
$view->name == 'myview' && $view->current_display == 'pdf_1' ) {
   
//update associated node's pages value
   
$numPages = $view->pdf->getNumPages();
   
$nodeid = $view->args[1];
    if (
is_numeric( $numPages ) && is_numeric( $nodeid ) ) {
     
//quicker to update db directly than via node api
     
db_query("UPDATE {field_data_field_pages} SET field_pages_value = " . $numPages . " WHERE entity_id = " . $nodeid);
    } else {
     
watchdog('mymodule','numPages or nodeid not numeric! ' . $nodeid . '---' . $numPages);
    }
  }
}
?>

Looks good thanks for it. I'll just fix some code and CS.

Consider use this code, ezman code is good but have to be fixed with this CodeStandar and good use of the API function.

<?php
/**
* Implements hook_views_post_render().
*/
function mymodule_views_post_render(&$view, &$output, &$cache) {
  if (
$view->name === 'myview' && $view->current_display === 'pdf_1' ) {
   
// Update associated node's pages value.
   
$num_pages = $view->pdf->getNumPages();
   
$nodeid = $view->args[1];
    if (
is_numeric($num_pages) && is_numeric($nodeid)) {
     
// Quicker to update db directly than via node api.
     
db_update('field_data_field_pages')
        ->
fields(array(
         
'field_pages_value' => $num_pages
       
))
        ->
condition('entity_id', $nodeid)
        ->
execute();
     
// If you like more see the SQL uncomment this and comment the db_udpate.
      /*
      db_query("UPDATE {field_data_field_pages} SET field_pages_value = ':num_pages' WHERE entity_id = ':entity_id'", array(':num_pages' => $num_pages, ':entity_id' => $nodeid));
      */
   
}
    else {
     
watchdog('mymodule', 'numPages or nodeid not numeric! @nodeid---@num_pages', array('@nodeid' => $nodeid, '@num_pages' => $num_pages));
    }
  }
}
?>

Thanks for the tips and updated code, killua99!

Category:task» support
Status:Needs review» Active