forked from wikimedia/mediawiki-extensions-Mpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MpdfAction.php
122 lines (111 loc) · 3.52 KB
/
MpdfAction.php
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
<?php
/**
* Handles the 'mpdf' action.
*/
class MpdfAction extends Action {
/**
* Return the name of the action this object responds to.
* @return string lowercase
*/
public function getName() {
return 'mpdf';
}
/**
* The main action entry point. Do all output for display and send it
* to the context output.
*/
public function show() {
global $wgMpdfSimpleOutput;
$title = $this->getTitle();
$output = $this->getOutput();
$request = $this->getRequest();
$titletext = $title->getPrefixedText();
$filename = str_replace( [ '\\', '/', ':', '*', '?', '"', '<', '>', "\n", "\r", "\0" ], '_', $titletext );
$article = new Article( $title );
Hooks::run( 'MpdfGetArticle', [ $title, &$article ] );
if ( $wgMpdfSimpleOutput ) {
$article->render();
ob_start();
$output->output();
$url = $title->getFullURL();
$footer = "<p><em>$url</em></p><h1>$titletext</h1>\n";
$html = $footer . ob_get_clean();
} else {
$output->setPrintable();
$article->view();
ob_start();
$output->output();
$html = ob_get_clean();
}
// Initialise PDF variables
$format = $request->getText( 'format' );
// If format=html in query-string, return html content directly
if ( $format == 'html' ) {
$output->disable();
header( "Content-Type: text/html" );
header( "Content-Disposition: attachment; filename=\"$filename.html\"" );
print $html;
} else {
// return pdf file
$mode = 'utf-8';
$format = 'A4';
$marginLeft = 15;
$marginRight = 15;
$marginTop = 16;
$marginBottom = 16;
$marginHeader = 9;
$marginFooter = 9;
$orientation = 'P';
$constr1 = explode( '<!--mpdf<constructor', $html, 2 );
if ( isset( $constr1[1] ) ) {
list( $constr2 ) = explode( '/>', $constr1[1], 1 );
$matches = [];
if ( preg_match( '/format\s*=\s*"(.*?)"/', $constr2, $matches ) ) {
$format = $matches[1];
}
if ( preg_match( '/margin-left\s*=\s*"?([0-9.]+)/', $constr2, $matches ) ) {
$marginLeft = (float)$matches[1];
}
if ( preg_match( '/margin-right\s*=\s*"?([0-9.]+)/', $constr2, $matches ) ) {
$marginRight = (float)$matches[1];
}
if ( preg_match( '/margin-top\s*=\s*"?([0-9.]+)/', $constr2, $matches ) ) {
$marginTop = (float)$matches[1];
}
if ( preg_match( '/margin-bottom\s*=\s*"?([0-9.]+)/', $constr2, $matches ) ) {
$marginBottom = (float)$matches[1];
}
if ( preg_match( '/margin-header\s*=\s*"?([0-9.]+)/', $constr2, $matches ) ) {
$marginHeader = (float)$matches[1];
}
if ( preg_match( '/margin-footer\s*=\s*"?([0-9.]+)/', $constr2, $matches ) ) {
$marginFooter = (float)$matches[1];
}
if ( preg_match( '/orientation\s*=\s*"(.*?)"/', $constr2, $matches ) ) {
$orientation = $matches[1];
}
}
$mpdf = new mPDF( $mode, $format, 0, '', $marginLeft, $marginRight, $marginTop, $marginBottom, $marginHeader, $marginFooter, $orientation );
// Suppress warning messages, because the mPDF library
// itself generates warnings (due to trying to add
// variables with a value of 'auto'), and if these get
// printed out, they can get into the PDF file and make
// it unreadable.
if ( function_exists( '\Wikimedia\suppressWarnings' ) ) {
// MW 1.31+
\Wikimedia\suppressWarnings();
} else {
wfSuppressWarnings();
}
$mpdf->WriteHTML( $html );
if ( function_exists( '\Wikimedia\restoreWarnings' ) ) {
// MW 1.31+
\Wikimedia\restoreWarnings();
} else {
wfRestoreWarnings();
}
$mpdf->Output( $filename . '.pdf', 'D' );
}
$output->disable();
}
}