forked from backdrop-contrib/printmpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printmpdf.module
479 lines (434 loc) · 14.8 KB
/
printmpdf.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<?php
/**
* @file
* Prints PDF for a given html node view.
*/
define('printmpdf_PDF_DEFAULT_FILENAME', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
/**
* Implements hook_config_info().
*/
function printmpdf_config_info() {
$prefixes['printmpdf.settings'] = array(
'label' => t('Print mpdf settings'),
'group' => t('Configuration'),
);
return $prefixes;
}
/**
* Implements hook_permission().
*/
function printmpdf_permission() {
return array(
'generate Print using mPDF' => array(
'title' => t('Generate Print using mPDF'),
'description' => t('Permission for HTML to PDF conversion'),
),
'administer mpdf settings' => array(
'title' => t('Access Print using mPDF settings'),
'description' => t('Permission for accessing mpdf settings'),
),
);
}
/**
* Implements hook_menu().
*/
function printmpdf_menu() {
$items['admin/config/user-interface/mpdf'] = array(
'title' => 'Print using mPDF settings',
'description' => 'configuration of custom mPDF setting',
'page callback' => 'backdrop_get_form',
'access callback' => 'user_access',
'access arguments' => array('administer mpdf settings'),
'page arguments' => array('printmpdf_config'),
'weight' => -1,
'type' => MENU_NORMAL_ITEM,
'file' => 'printmpdf.admin.inc',
);
$items['node/%node/pdf'] = array(
'title' => 'Generate PDF',
'page callback' => 'printmpdf_generate_pdf',
'page arguments' => array(1),
'access callback' => '_printmpdf_attributes_access',
'access arguments' => array(1),
// 'type' => MENU_LOCAL_TASK,
// 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'printmpdf.pages.inc',
);
return $items;
}
/**
* Check for generate PDF permission.
*
* @param string $node
* Node array for currnet node.
*
* @return bool
* TRUE if permision allow and FALSE if access denied.
*/
function _printmpdf_attributes_access($node) {
if (user_access('generate Print using mPDF')) {
if (config_get('printmpdf.settings','printmpdf_type_' . $node->type) == 1 ) {
return TRUE;
}
else {
return FALSE;
}
}
return FALSE; // TRUE or you can user user_access() permissions as well
}
/**
* Generate the PDF file using the mPDF library.
*
* @param string $html
* contents of the template already with the node data.
* @param string $filename
* name of the PDF file to be generated.
*/
function _printmpdf_generator($html, $filename = NULL) {
ini_set('Display_errors', 'On');
error_reporting(E_ALL);
// International Paper Sizes ( width x height).
$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 = backdrop_get_path('module', 'printmpdf');
$module_path = backdrop_get_path('module', 'printmpdf');
global $theme_path;
$page = config_get('printmpdf.settings','printmpdf_pdf_page_size');
$font_size = config_get('printmpdf.settings','printmpdf_pdf_font_size');
$font_style = config_get('printmpdf.settings','printmpdf_pdf_default_font');
// DEFAULT PDF margin Values.
$margin_top = config_get('printmpdf.settings','printmpdf_margin_top');
$margin_right = config_get('printmpdf.settings','printmpdf_margin_right');
$margin_bottom = config_get('printmpdf.settings','printmpdf_margin_bottom');
$margin_left = config_get('printmpdf.settings','printmpdf_margin_left');
$margin_header = config_get('printmpdf.settings','printmpdf_margin_header');
$margin_footer = config_get('printmpdf.settings','printmpdf_margin_footer');
// Creating Instance of mPDF Class Library.
/*
$mpdf = new \Mpdf\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 = new \Mpdf\Mpdf(['tempDir' => backdrop_realpath('public://').'/tmp/']);
// set document DPI
$mpdf->dpi = (int) config_get('printmpdf.settings','printmpdf_dpi');
// Set image DPI
$mpdf->img_dpi = (int) config_get('printmpdf.settings','printmpdf_img_dpi');
// Enabling header option if available.
$header = config_get('printmpdf.settings','printmpdf_pdf_header');
if (isset($header) && $header != NULL) {
$header = token_replace($header);
$mpdf->SetHTMLHeader($header);
}
// Enabling Footer option if available.
$footer = config_get('printmpdf.settings','printmpdf_pdf_footer');
if (isset($footer) && $footer != NULL) {
$footer = token_replace($footer);
$mpdf->SetHTMLFooter($footer);
}
// Setting Watermark Text to PDF.
$watermark_option = config_get('printmpdf.settings','printmpdf_watermark_option');
$watermark_opacity = config_get('printmpdf.settings','printmpdf_watermark_opacity');
// For watermark Text.
if ($watermark_option == 'text') {
$text = config_get('printmpdf.settings','printmpdf_pdf_watermark_text');
if (isset($text) && $text != NULL) {
$mpdf->SetWatermarkText($text, $watermark_opacity);
$mpdf->showWatermarkText = TRUE;
}
}
// For watermark Image.
else {
$image_id = config_get('printmpdf.settings','printmpdf_watermark_image');
if (isset($image_id) && $image_id != NULL) {
// Load the file via file.fid.
$file = file_load($image_id);
if (is_object($file)) {
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
file_usage_add($file, 'printmpdf', 'image', $image_id);
$image_path = file_create_url($file->uri);
$mpdf->SetWatermarkImage($image_path, $watermark_opacity, '', 'P');
$mpdf->showWatermarkImage = TRUE;
}
}
}
// Setting Title to PDF.
$title = config_get('printmpdf.settings','printmpdf_pdf_set_title');
if (isset($title) && $title != NULL) {
$mpdf->SetTitle($title);
}
// Setting Author to PDF.
$author = config_get('printmpdf.settings','printmpdf_pdf_set_author');
if (isset($author) && $author != NULL) {
$mpdf->SetAuthor($author);
}
// Setting Subject to PDF.
$subject = config_get('printmpdf.settings','printmpdf_pdf_set_subject');
if (isset($subject) && $subject != NULL) {
$mpdf->SetSubject($subject);
}
// Setting creator to PDF.
$creator = config_get('printmpdf.settings','printmpdf_pdf_set_creator');
if (isset($creator) && $creator != NULL) {
$mpdf->SetCreator($creator);
}
// Setting Password to PDF.
$password = config_get('printmpdf.settings','printmpdf_pdf_password');
if (isset($password) && $password != NULL) {
// Print and Copy is allowed.
$mpdf->SetProtection(array('print', 'copy'), $password, $password);
}
// Setting CSS stylesheet to PDF.
$stylesheet = config_get('printmpdf.settings','printmpdf_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$/');
// Check module directory
if (isset($css_file_module[$module_path . '/' . $stylesheet])) {
$stylesheet_content = file_get_contents($module_path . '/' . $stylesheet);
$mpdf->WriteHTML($stylesheet_content, 1);
}
// Check theme directory
elseif (isset($css_file_theme[$theme_path . '/' . $stylesheet])) {
$stylesheet_content = file_get_contents($theme_path . '/' . $stylesheet);
$mpdf->WriteHTML($stylesheet_content, 1);
}
else {
backdrop_set_message(t('CSS style Sheet mentioned in PDF setting was not found.'), 'warning');
return TRUE;
}
}
// Writing html content for pdf buffer.
$mpdf->WriteHTML($html);
// Generating PDF File.
switch(config_get('printmpdf.settings','printmpdf_pdf_save_option')) {
case 1:
// Dialog box for Download as PDF.
$mpdf->Output($filename . '.pdf', 'D');
exit;
break;
case 2:
$folder = printmpdf_get_folder();
if (is_dir(backdrop_realpath($folder)) ) {
if (!printmpdf_is_writable(backdrop_realpath($folder))) { die('not writtable');
if (backdrop_rmdir($folder)) {
backdrop_mkdir($folder);
backdrop_chmod($folder, 0777);
}
else {
backdrop_set_message(t("Please ensure that public folder is writable."));
exit;
}
}
} else {
if (printmpdf_is_writable(backdrop_realpath(file_build_uri('public://')))) {
backdrop_mkdir($folder);
backdrop_chmod($folder, 0777);
}
else {
backdrop_set_message(t("Please ensure that public folder is writable."));
exit;
}
}
// Save to server 'Public file system path'.
$path = $folder . '/' . $filename . '.pdf';
$mpdf->Output($path , 'F');
// backdrop_goto($_SERVER['HTTP_REFERER']);
// exit;
break;
case 0:
default:
// Open in same browser.
$mpdf->Output($filename . '.pdf', 'I');
exit;
break;
}
return TRUE;
}
/**
* API to generate a PDF file.
*
* @param string $html
* html is rendered HTML that will be converted into PDF.
*
* @param string $printmpdf_pdf_filename
* printmpdf_pdf_filename is Optional name of the PDF file.
*
* @return bool
* TRUE if PDF is successfully generated and FALSE if it isn't.
*/
function printmpdf_api($html, $printmpdf_pdf_filename = NULL) {
if (printmpdf_library_exist() == TRUE) { // always TRUE
if ($printmpdf_pdf_filename === NULL) {
$filename = explode(config_get('printmpdf.settings','printmpdf_pdf_filename'), '[site:name]');
$printmpdf_pdf_filename = token_replace($filename[0]);
}
_printmpdf_generator($html, $printmpdf_pdf_filename);
}
}
/**
* Function to check existence of mPDF library.
* changed, library now included in module distribution, so always return TRUE
* @return bool
* TRUE if mPDF library path exists.
*/
function printmpdf_library_exist() {
require_once BACKDROP_ROOT . '/' . backdrop_get_path('module', 'printmpdf') . '/vendor/autoload.php' ;
return TRUE;
}
/**
* Implements hook_entity_info_alter().
*/
function printmpdf_entity_info_alter(&$info) {
// Add the 'Print' view mode for nodes.
$info['node']['view modes'] += array(
'PDF' => array(
'label' => t('PDF'),
'custom settings' => FALSE,
),
);
}
/**
* Implements hook_preprocess_node().
*/
function printmpdf_preprocess_node(&$vars) {
if ($vars['view_mode'] == 'PDF') {
$vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__pdf';
}
}
/**
* Implements hook_node_view_alter().
*/
function printmpdf_node_view_alter(&$build) {
if ($build['#view_mode'] == 'PDF') {
// Remove contextual links
unset($build['#contextual_links']);
}
}
/*
* Function to check if a folder/file is writable
*/
function printmpdf_is_writable($path) {
if ($path[strlen($path)-1] == '/') {
return backdrop_is_writable($path . uniqid(mt_rand()) . '.tmp');
}
elseif (is_dir($path)) {
return printmpdf_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 to create printmpdf folder
*/
function printmpdf_create_folder(stdClass $pdfmpdf = NULL) {
$folder = printmpdf_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);
}
backdrop_chmod($folder, 0777);
return $result;
}
/**
* Get the folder for pdf files.
*/
function printmpdf_get_folder(stdClass $pdfmpdf = NULL) {
$folder = &backdrop_static(__FUNCTION__);
if ( !isset($folder) ) {
$folder = config_get('printmpdf.settings','printmpdf');
}
if ( !empty($pdfmpdf->smid) ) {
return file_build_uri($folder . '/' . $pdfmpdf->smid);
}
else {
return file_build_uri($folder);
}
}
/**
* Implements MODULE_NAME_field_extra_fields().
*/
function printmpdf_field_extra_fields() {
foreach (array('page','ichianalyse','rezept') as $node_type) {
$extra['node'][$node_type]['display']['printmpdf_icon'] = array(
'label' => t('printmpdf icon'),
'description' => t('The printmpdf icon'),
'weight' => 0,
);
}
return $extra;
}
/**
* Implements hook_node_view().
*/
function printmpdf_node_view($node, $view_mode, $langcode) {
// Only Display if visiblity is set
if (config_get('printmpdf.settings','printmpdf_type_' . $node->type) == 1 ) {
$icon_path = $GLOBALS['base_url']. '/' . backdrop_get_path('module', 'printmpdf') . "/file-icon-pdf-50px.png";
$node_pdf_url = $GLOBALS['base_url']. '/node/' . $node->nid . '/pdf';
$node->content['printmpdf_icon'] = array(
// '#markup' => '<a href="'. $node_pdf_url . '" target="_blank"><img src="' .$icon_path . '" ></a> Path=' . $icon_path . ' pdf path ' . $node_pdf_url ,
'#markup' => '<div class="printmpdfbutton"><a href="'. $node_pdf_url . '" target="_blank"><img src="' .$icon_path . '" ></a></div>',
);
}
}