Last active
August 16, 2017 09:06
-
-
Save mootari/a016c25e65dc639088f7eaa46bfe4b04 to your computer and use it in GitHub Desktop.
Generating PDFs in Drupal 7 with a custom theme. (WkHtmlToPdf only extended to make getData() public.) ALL CAPS (like MY_MODULE, NODE_TYPE_A ...) are meant as placeholders.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_menu(). | |
*/ | |
function MY_MODULE_menu() { | |
$items = array(); | |
$items['MY_MODULE/%node'] = array( | |
'access callback' => 'MY_MODULE_access', | |
'access arguments' => array(1), | |
'page callback' => 'MY_MODULE_view', | |
'page arguments' => array(1), | |
'type' => MENU_CALLBACK, | |
'theme callback' => 'MY_MODULE_theme_callback' | |
); | |
return $items; | |
} | |
/** | |
* Access callback. | |
*/ | |
function MY_MODULE_access($node) { | |
$valid_types = array('NODE_TYPE_A', 'NODE_TYPE_B'); | |
return isset($node->type) && in_array($node->type, $valid_types) && node_access('view', $node); | |
} | |
/** | |
* Menu theme callback. | |
*/ | |
function MY_MODULE_theme_callback() { | |
return 'MY_FRONTEND_THEME'; | |
} | |
/** | |
* Implements hook_theme(). | |
*/ | |
function MY_MODULE_theme($existing, $type, $theme, $path) { | |
$hooks = array(); | |
$hooks['MY_MODULE_NODE_TYPE_A'] = array( | |
'variables' => array( | |
'node' => null, | |
'page_title' => '', | |
'footer_text' => '', | |
'VAR_A' => '', | |
'VAR_B' => '', | |
), | |
'template' => 'templates/MY-MODULE-NODE-TYPE-A' | |
); | |
$hooks['MY_MODULE_NODE_TYPE_B'] = array( | |
'variables' => array( | |
'node' => null, | |
'page_title' => '', | |
'footer_text' => '', | |
'VAR_C' => '', | |
'VAR_D' => '', | |
), | |
'template' => 'templates/MY-MODULE-NODE-TYPE-B' | |
); | |
return $hooks; | |
} | |
function template_preprocess_MY_MODULE_NODE_TYPE_A(&$vars) { | |
$vars = array( | |
'page_title' => 'MY TITLE', | |
'VAR_A' => 'foo', | |
'VAR_B' => 'bar', | |
) + $vars; | |
} | |
function template_preprocess_MY_MODULE_NODE_TYPE_B(&$vars) { | |
$vars = array( | |
'page_title' => 'MY TITLE', | |
'footer_text' => 'MY FOOTER', | |
'page_title' => 'MY TITLE', | |
'VAR_C' => 'foo', | |
'VAR_D' => 'bar', | |
) + $vars; | |
} | |
function MY_MODULE_view($node) { | |
$direct_output = variable_get('MY_MODULE:direct_output', false); | |
$disable_cache = variable_get('MY_MODULE:disable_cache', false); | |
$cid = "MY_MODULE:$node->type:$node->nid"; | |
if(!$disable_cache && ($cache = cache_get($cid))) { | |
$output = $cache->data; | |
} | |
else { | |
$theme = 'MY_MODULE_' . $node->type; | |
$html = theme($theme, array('node' => $node)); | |
$output = $direct_output ? $html : MY_MODULE_generate($html); | |
if(!$disable_cache) { | |
$ttl = ($output === false) | |
? variable_get('MY_MODULE:cache_error_ttl', 60 * 15) | |
: variable_get('MY_MODULE:cache_ttl', 60 * 60 * 6); | |
cache_set($cid, $output, 'cache', time() + $ttl); | |
} | |
} | |
if(!$direct_output) { | |
header('Pragma: public'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); | |
header('Content-Type: application/pdf'); | |
header('Content-Transfer-Encoding: binary'); | |
// @todo See http://php.net/manual/de/function.strlen.php#54906 for possible problems | |
// due to function overload. | |
header('Content-Length: ' . strlen($output)); | |
} | |
echo $output; | |
drupal_exit(); | |
} | |
function MY_MODULE_generate($html) { | |
$stylesheet = DRUPAL_ROOT . '/' . drupal_get_path('theme', 'MY_FRONTEND_THEME') . '/css/pdf.css'; | |
$options = array( | |
'no-outline', // Make Chrome not complain | |
'margin-top' => 0, | |
'margin-right' => 0, | |
'margin-bottom' => 0, | |
'margin-left' => 0, | |
'user-style-sheet' => $stylesheet | |
); | |
$options['binary'] = variable_get('MY_MODULE:binary', 'wkhtmltopdf'); | |
$options['tmpDir'] = file_directory_temp(); | |
$pdf = new \Drupal\MY_MODULE\WkHtmlToPdfPublic($options); | |
$pdf->addPage($html); | |
$output = $pdf->getData(); | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment