View source
<?php
define('PDF_USING_MPDF_PDF_DEFAULT_FILENAME', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
define('PDF_USING_MPDF_API_FILE', 'mpdf.php');
define('PDF_USING_MPDF_PDF_FONT_SIZE', 10);
define('PDF_USING_MPDF_PDF_PAGE_SIZE', 'A4');
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,
'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' => 'user_access',
'access arguments' => array(
'generate pdf using mpdf',
),
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'pdf_using_mpdf.pages.inc',
);
return $items;
}
function _pdf_using_mpdf_generator($html, $filename = NULL) {
$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');
$page = variable_get('pdf_using_mpdf_pdf_page_size', PDF_USING_MPDF_PDF_PAGE_SIZE);
$font_size = variable_get('pdf_using_mpdf_pdf_font_size', PDF_USING_MPDF_PDF_FONT_SIZE);
$margin_left = 15;
$margin_right = 15;
$margin_top = 16;
$margin_bottom = 16;
$margin_header = 9;
$margin_footer = 9;
$mpdf = new mPDF('', array(
$paper_size[$page]['w'],
$paper_size[$page]['h'],
), $font_size, '', $margin_left, $margin_right, $margin_top, $margin_bottom, $margin_header, $margin_footer);
$header_left = variable_get('pdf_using_mpdf_pdf_header_left');
$header_center = variable_get('pdf_using_mpdf_pdf_header_center');
$header_right = variable_get('pdf_using_mpdf_pdf_header_right');
if ($header_left != NULL || $header_center != NULL || $header_right != NULL) {
$header = array(
'odd' => array(
'L' => array(
'content' => $header_left,
'font-size' => 9,
'font-style' => 'B',
'font-family' => 'serif',
'color' => 'gray',
),
'C' => array(
'content' => $header_center,
'font-size' => 9,
'font-style' => 'B',
'font-family' => 'serif',
'color' => 'gray',
),
'R' => array(
'content' => $header_right,
'font-size' => 9,
'font-style' => 'B',
'font-family' => 'serif',
'color' => 'gray',
),
'line' => 1,
),
'even' => array(),
);
$mpdf
->SetHeader($header);
}
$footer_left = variable_get('pdf_using_mpdf_pdf_footer_left');
$footer_center = variable_get('pdf_using_mpdf_pdf_footer_center');
$footer_right = variable_get('pdf_using_mpdf_pdf_footer_right');
if ($footer_left != NULL || $footer_center != NULL || $footer_right != NULL) {
$footer = array(
'odd' => array(
'L' => array(
'content' => $footer_left,
'font-size' => 9,
'font-style' => 'B',
'font-family' => 'serif',
'color' => 'gray',
),
'C' => array(
'content' => $footer_center,
'font-size' => 9,
'font-style' => 'B',
'font-family' => 'serif',
'color' => 'gray',
),
'R' => array(
'content' => $footer_right,
'font-size' => 9,
'font-style' => 'B',
'font-family' => 'serif',
'color' => 'gray',
),
'line' => 1,
),
'even' => array(),
);
$mpdf
->SetFooter($footer);
}
$text = variable_get('pdf_using_mpdf_pdf_watermark_text');
if (isset($text) && $text != NULL) {
$mpdf
->SetWatermarkText($text, '0.1');
$mpdf->showWatermarkText = TRUE;
}
$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);
}
$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');
if (isset($stylesheet) && $stylesheet != NULL) {
$css_file = file_scan_directory($root_path, '/.*\\.css$/');
if (isset($css_file[$root_path . '/' . $stylesheet])) {
$stylesheet = file_get_contents($root_path . '/' . $stylesheet);
$mpdf
->WriteHTML($stylesheet, 1);
}
else {
drupal_set_message(t('CSS style Sheet mentioned in PDF setting was not found.'), 'warning');
return FALSE;
}
}
$mpdf
->WriteHTML($html);
if (variable_get('pdf_using_mpdf_pdf_save_option') == '0') {
$mpdf
->Output($filename . '.pdf', 'I');
exit;
}
else {
$mpdf
->Output($filename . '.pdf', 'D');
}
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', 'PDF_USING_MPDF_PDF_DEFAULT_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;
}
}