View source
<?php
define('PDF_USING_MPDF_PDF_DEFAULT_FILENAME', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
function pdf_using_mpdf_help($path, $arg) {
if ($path == 'admin/help#pdf_using_mpdf') {
$html = '<p>' . t('This module is used for Conversion of an HTML page to PDF using mPDF PHP Library. This module allows you to generate the pdf documents of any node:<p><strong> PDF creation ( www.example.com/node/nid/pdf ) </strong></p><p> where nid is the node id of a particular node to render. </p> By creating your own CSS file and/or the node.tpl.php template files, it is possible to change the look of the output page to suit your taste. For a more fine-grained customization, it is possible to use a template file named: <p><strong> Drupal 7: node--[node-type|node-id].tpl.php </strong></p><p> located in the active theme directory. </p><p> Where node-type and node-id are Drupal node type (for example page, story, etc.) and node id (for example 10) respectively. </p><p><strong> API Function : pdf_using_mpdf_api() </strong></p> This API function is available to content developers that prefer to generate a pdf file of custom path. The function takes two parameters, first a rendered html content and an optional second parameter, name of the pdf file, for example pdf_using_mpdf_api($html) where $html is any html content. <p> You must install the following third-party tools to generate PDFs: </p><p> !default_link</p><p> Please follow the instructions in the README.txt files carefully. </p><p> Developed By : !osscube_link', array(
'!default_link' => l(t("mPDF"), "http://www.mpdf1.com"),
'!osscube_link' => l(t('OSSCube'), 'http://www.osscube.com/'),
)) . '</p>';
return $html;
}
}
function pdf_using_mpdf_permission() {
return array(
'generate pdf using mpdf' => array(
'title' => t('Generate PDF using mPDF'),
'description' => t('Permission for HTML to PDF conversion'),
),
'administer mpdf settings' => array(
'title' => t('Access PDF using mPDF settings'),
'description' => t('Permission for accessing mpdf settings'),
),
);
}
function pdf_using_mpdf_menu() {
$items['admin/config/user-interface/mpdf'] = array(
'title' => 'PDF using mPDF settings',
'description' => 'configuration of custom mPDF setting',
'page callback' => 'drupal_get_form',
'access callback' => 'user_access',
'access arguments' => array(
'administer mpdf settings',
),
'page arguments' => array(
'pdf_using_mpdf_config',
),
'weight' => -1,
'type' => MENU_NORMAL_ITEM,
'file' => 'pdf_using_mpdf.admin.inc',
);
$items['node/%node/pdf'] = array(
'title' => 'Generate PDF',
'page callback' => 'pdf_using_mpdf_generate_pdf',
'page arguments' => array(
1,
),
'access callback' => '_pdf_using_mpdf_attributes_access',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'pdf_using_mpdf.pages.inc',
);
return $items;
}
function _pdf_using_mpdf_attributes_access($node) {
if (user_access('generate pdf using mpdf')) {
if (variable_get('pdf_using_mpdf_type_' . $node->type) == 1) {
return TRUE;
}
else {
return FALSE;
}
}
return FALSE;
}
function _pdf_using_mpdf_generator($html, $filename = NULL) {
ini_set('Display_errors', 'On');
error_reporting(E_ALL);
$paper_size = array(
'4A0' => array(
'w' => 1682,
'h' => 2378,
),
'2A0' => array(
'w' => 1189,
'h' => 1682,
),
'A0' => array(
'w' => 841,
'h' => 1189,
),
'A1' => array(
'w' => 594,
'h' => 841,
),
'A2' => array(
'w' => 420,
'h' => 594,
),
'A3' => array(
'w' => 297,
'h' => 420,
),
'A4' => array(
'w' => 210,
'h' => 297,
),
'A5' => array(
'w' => 148,
'h' => 210,
),
'A6' => array(
'w' => 105,
'h' => 148,
),
'A7' => array(
'w' => 74,
'h' => 105,
),
'A8' => array(
'w' => 52,
'h' => 74,
),
'A9' => array(
'w' => 37,
'h' => 52,
),
'A10' => array(
'w' => 26,
'h' => 37,
),
'B0' => array(
'w' => 1000,
'h' => 1414,
),
'B1' => array(
'w' => 707,
'h' => 1000,
),
'B2' => array(
'w' => 500,
'h' => 707,
),
'B3' => array(
'w' => 353,
'h' => 500,
),
'B4' => array(
'w' => 250,
'h' => 353,
),
'B5' => array(
'w' => 176,
'h' => 250,
),
'B6' => array(
'w' => 125,
'h' => 176,
),
'B7' => array(
'w' => 88,
'h' => 125,
),
'B8' => array(
'w' => 62,
'h' => 88,
),
'B9' => array(
'w' => 44,
'h' => 62,
),
'B10' => array(
'w' => 31,
'h' => 44,
),
'C0' => array(
'w' => 917,
'h' => 1297,
),
'C1' => array(
'w' => 648,
'h' => 917,
),
'C2' => array(
'w' => 458,
'h' => 648,
),
'C3' => array(
'w' => 324,
'h' => 458,
),
'C4' => array(
'w' => 229,
'h' => 324,
),
'C5' => array(
'w' => 162,
'h' => 229,
),
'C6' => array(
'w' => 114,
'h' => 162,
),
'C7' => array(
'w' => 81,
'h' => 114,
),
'C8' => array(
'w' => 57,
'h' => 81,
),
'C9' => array(
'w' => 40,
'h' => 57,
),
'C10' => array(
'w' => 28,
'h' => 40,
),
'RA0' => array(
'w' => 860,
'h' => 1220,
),
'RA1' => array(
'w' => 610,
'h' => 860,
),
'RA2' => array(
'w' => 430,
'h' => 610,
),
'SRA0' => array(
'w' => 900,
'h' => 1280,
),
'SRA1' => array(
'w' => 640,
'h' => 900,
),
'SRA2' => array(
'w' => 450,
'h' => 640,
),
'Letter' => array(
'w' => 215.9,
'h' => 279.4,
),
'Legal' => array(
'w' => 215.9,
'h' => 355.6,
),
'Ledger' => array(
'w' => 279.4,
'h' => 431.8,
),
);
$root_path = drupal_get_path('module', 'pdf_using_mpdf');
$module_path = drupal_get_path('module', 'pdf_using_mpdf');
global $theme_path;
$page = variable_get('pdf_using_mpdf_pdf_page_size');
$font_size = variable_get('pdf_using_mpdf_pdf_font_size');
$font_style = variable_get('pdf_using_mpdf_pdf_default_font');
$margin_top = variable_get('pdf_using_mpdf_margin_top', 16);
$margin_right = variable_get('pdf_using_mpdf_margin_right', 15);
$margin_bottom = variable_get('pdf_using_mpdf_margin_bottom', 16);
$margin_left = variable_get('pdf_using_mpdf_margin_left', 15);
$margin_header = variable_get('pdf_using_mpdf_margin_header', 9);
$margin_footer = variable_get('pdf_using_mpdf_margin_footer', 9);
$mpdf = new mPDF('', array(
$paper_size[$page]['w'],
$paper_size[$page]['h'],
), $font_size, $font_style, $margin_left, $margin_right, $margin_top, $margin_bottom, $margin_header, $margin_footer);
$mpdf->dpi = (int) variable_get('pdf_using_mpdf_dpi', 96);
$mpdf->img_dpi = (int) variable_get('pdf_using_mpdf_img_dpi', 96);
$header = variable_get('pdf_using_mpdf_pdf_header');
if (isset($header) && $header != NULL) {
$header = token_replace($header);
$mpdf
->SetHTMLHeader($header);
}
$footer = variable_get('pdf_using_mpdf_pdf_footer');
if (isset($footer) && $footer != NULL) {
$footer = token_replace($footer);
$mpdf
->SetHTMLFooter($footer);
}
$watermark_option = variable_get('pdf_using_mpdf_watermark_option');
$watermark_opacity = variable_get('pdf_using_mpdf_watermark_opacity');
if ($watermark_option == 'text') {
$text = variable_get('pdf_using_mpdf_pdf_watermark_text');
if (isset($text) && $text != NULL) {
$mpdf
->SetWatermarkText($text, $watermark_opacity);
$mpdf->showWatermarkText = TRUE;
}
}
else {
$image_id = variable_get('pdf_using_mpdf_watermark_image');
if (isset($image_id) && $image_id != NULL) {
$file = file_load($image_id);
if (is_object($file)) {
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
file_usage_add($file, 'pdf_using_mpdf', 'image', $image_id);
$image_path = file_create_url($file->uri);
$mpdf
->SetWatermarkImage($image_path, $watermark_opacity, '', 'P');
$mpdf->showWatermarkImage = TRUE;
}
}
}
$title = variable_get('pdf_using_mpdf_pdf_set_title');
if (isset($title) && $title != NULL) {
$mpdf
->SetTitle($title);
}
$author = variable_get('pdf_using_mpdf_pdf_set_author');
if (isset($author) && $author != NULL) {
$mpdf
->SetAuthor($author);
}
$subject = variable_get('pdf_using_mpdf_pdf_set_subject');
if (isset($subject) && $subject != NULL) {
$mpdf
->SetSubject($subject);
}
$creator = variable_get('pdf_using_mpdf_pdf_set_creator');
if (isset($creator) && $creator != NULL) {
$mpdf
->SetCreator($creator);
}
$password = variable_get('pdf_using_mpdf_pdf_password');
if (isset($password) && $password != NULL) {
$mpdf
->SetProtection(array(
'print',
'copy',
), $password, $password);
}
$stylesheet = variable_get('pdf_using_mpdf_pdf_css_file');
$stylesheet_content = NULL;
if (isset($stylesheet) && $stylesheet != NULL) {
$css_file_module = file_scan_directory($module_path, '/.*\\.css$/');
$css_file_theme = file_scan_directory($theme_path, '/.*\\.css$/');
if (isset($css_file_module[$module_path . '/' . $stylesheet])) {
$stylesheet_content = file_get_contents($module_path . '/' . $stylesheet);
$mpdf
->WriteHTML($stylesheet_content, 1);
}
elseif (isset($css_file_theme[$theme_path . '/' . $stylesheet])) {
$stylesheet_content = file_get_contents($theme_path . '/' . $stylesheet);
$mpdf
->WriteHTML($stylesheet_content, 1);
}
else {
drupal_set_message(t('CSS style Sheet mentioned in PDF setting was not found.'), 'warning');
return TRUE;
}
}
$mpdf
->WriteHTML($html);
switch (variable_get('pdf_using_mpdf_pdf_save_option')) {
case 1:
$mpdf
->Output($filename . '.pdf', 'D');
exit;
break;
case 2:
$folder = pdf_using_mpdf_get_folder();
if (is_dir(drupal_realpath($folder))) {
if (!pdf_using_mpdf_is_writable(drupal_realpath($folder))) {
die('not writtable');
if (drupal_rmdir($folder)) {
drupal_mkdir($folder);
drupal_chmod($folder, 0777);
}
else {
drupal_set_message(t("Please ensure that public folder is writable."));
exit;
}
}
}
else {
if (pdf_using_mpdf_is_writable(drupal_realpath(file_build_uri('public://')))) {
drupal_mkdir($folder);
drupal_chmod($folder, 0777);
}
else {
drupal_set_message(t("Please ensure that public folder is writable."));
exit;
}
}
$path = $folder . '/' . $filename . '.pdf';
$mpdf
->Output($path, 'F');
drupal_goto($_SERVER['HTTP_REFERER']);
exit;
break;
case 0:
default:
$mpdf
->Output($filename . '.pdf', 'I');
exit;
break;
}
return TRUE;
}
function pdf_using_mpdf_api($html, $pdf_using_mpdf_pdf_filename = NULL) {
if (pdf_using_mpdf_library_exist() == TRUE) {
if ($pdf_using_mpdf_pdf_filename === NULL) {
$filename = explode(variable_get('pdf_using_mpdf_pdf_filename'), '[site:name]');
$pdf_using_mpdf_pdf_filename = token_replace($filename[0]);
}
_pdf_using_mpdf_generator($html, $pdf_using_mpdf_pdf_filename);
}
else {
drupal_set_message(t('No mPDF Library Found in "sites/all/libraries" or "!default_module_path". Please download PHP mPDF PDF generation tool from <a href="http://www.mpdf1.com/">mPDF1.com</a>', array(
'!default_module_path' => drupal_get_path('module', 'pdf_using_mpdf'),
)), 'warning');
}
}
function pdf_using_mpdf_library_exist() {
$tools = array();
$pattern = '/^mpdf.php$/';
$tools = array_keys(file_scan_directory(libraries_get_path('mpdf'), $pattern));
$tools = array_merge($tools, array_keys(file_scan_directory(drupal_get_path('module', 'pdf_using_mpdf'), $pattern)));
if (isset($tools[0])) {
require_once $tools[0];
return TRUE;
}
else {
return FALSE;
}
}
function pdf_using_mpdf_entity_info_alter(&$info) {
$info['node']['view modes'] += array(
'PDF' => array(
'label' => t('PDF'),
'custom settings' => FALSE,
),
);
}
function pdf_using_mpdf_preprocess_node(&$vars) {
if ($vars['view_mode'] == 'PDF') {
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__pdf';
}
}
function pdf_using_mpdf_node_view_alter(&$build) {
if ($build['#view_mode'] == 'PDF') {
unset($build['#contextual_links']);
}
}
function pdf_using_mpdf_is_writable($path) {
if ($path[strlen($path) - 1] == '/') {
return drupal_is_writable($path . uniqid(mt_rand()) . '.tmp');
}
elseif (is_dir($path)) {
return pdf_using_mpdf_is_writable($path . '/' . uniqid(mt_rand()) . '.tmp');
}
$rm = file_exists($path);
$f = @fopen($path, 'a');
if ($f === FALSE) {
return FALSE;
}
fclose($f);
if (!$rm) {
unlink($path);
}
return TRUE;
}
function pdf_using_mpdf_create_folder(stdClass $pdfmpdf = NULL) {
$folder = pdf_using_mpdf_get_folder($pdfmpdf);
$result = file_prepare_directory($folder, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
if (!$result) {
watchdog('file system', 'The directory %folder does not exist or is not writable.', array(
'%folder' => $folder,
), WATCHDOG_ERROR);
}
drupal_chmod($folder, 0777);
return $result;
}
function pdf_using_mpdf_get_folder(stdClass $pdfmpdf = NULL) {
$folder =& drupal_static(__FUNCTION__);
if (!isset($folder)) {
$folder = variable_get('pdf_using_mpdf', 'pdf_using_mpdf');
}
if (!empty($pdfmpdf->smid)) {
return file_build_uri($folder . '/' . $pdfmpdf->smid);
}
else {
return file_build_uri($folder);
}
}