-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfExport_body.php
More file actions
195 lines (168 loc) · 8.73 KB
/
PdfExport_body.php
File metadata and controls
195 lines (168 loc) · 8.73 KB
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
<?php
# This file has been modified by DumpyDooby.
if ( !defined( 'MEDIAWIKI' ) )
die();
# <Craig>
$PdfExportUseHtmlDoc = isset($PdfExportUseHtmlDoc) ? $PdfExportUseHtmlDoc : false;
# </Craig>
$wgPdfExportAttach = false; // set to true if you want output as an attachment
$wgPdfExportHttpsImages = false; // set to true if page is on a HTTPS server and contains images that are on the HTTPS server and also
// reachable with HTTP
class SpecialPdf extends SpecialPage {
var $title;
var $article;
var $html;
var $parserOptions;
var $bhtml;
public $iswindows;
function SpecialPdf() {
global $iswindows;
SpecialPage::SpecialPage( 'PdfPrint' );
$os = getenv ("SERVER_SOFTWARE");
$iswindows = strstr ($os, "Win32");
}
public function write1file ($text) {
// make a temporary directory with an unique name
// NOTE: If no PDF file is created and you get message "ERROR: No HTML files!",
// try using a temporary directory that is within web server space.
// For example (assuming the web server root directory is /var/www/html):
// $mytemp = "/var/html/www/tmp/f" .time(). "-" .rand() . ".html";
# <Craig>
$mytemp=tempnam(sys_get_temp_dir(), 'PdfExport');
# </Craig>
$article_f = fopen($mytemp,'w');
if($article_f === FALSE){
error_log("Failed opening temporary HTML file to \"$mytemp\" failed", 0);
return;
}
fwrite($article_f, $text);
# <Craig>
fseek($article_f, 0);
# </Craig>
fclose($article_f);
return $mytemp;
}
public function save1page ( $page ) {
global $wgUser;
global $wgParser;
global $wgScriptPath;
global $wgServer;
global $wgPdfExportHttpsImages;
$title = Title::newFromText( $page );
if( is_null( $title ) || !$title->userCanRead() )
return null;
$article = new Article ($title);
$parserOptions = ParserOptions::newFromUser( $wgUser );
$parserOptions->setEditSection( false );
$parserOptions->setTidy(true);
$wgParser->mShowToc = false;
$parserOutput = $wgParser->parse( $article->preSaveTransform( $article->getContent() ) ."\n\n", $title, $parserOptions );
$bhtml = $parserOutput->getText();
// Hack to thread the EUR sign correctly
$bhtml = str_replace(chr(0xE2) . chr(0x82) . chr(0xAC), chr(0xA4), $bhtml);
//$bhtml = utf8_decode($bhtml);
$bhtml = str_replace ($wgScriptPath, $wgServer . $wgScriptPath, $bhtml);
$bhtml = str_replace ('/w/',$wgServer . '/w/', $bhtml);
if ($wgPdfExportHttpsImages)
$bhtm = str_replace('img src=\"https:\/\/','img src=\"http:\/\/', $bhtml);
$html = "<html><head><title>" . utf8_decode($page) . "</title></head><body>" . $bhtml . "</body></html>";
return $this->write1file ($html);
}
function outputpdf ($pages, $landscape, $size) {
global $iswindows;
global $wgPdfExportAttach;
$returnStatus = 0;
$pagestring = "";
$pagefiles = array();
$foundone = false;
foreach ($pages as $pg) {
$f = $this->save1page ($pg);
if ($f == null) continue;
$foundone = true;
if ($iswindows) $pagestring .= "\"" . $f . "\" ";
else $pagestring .= $f . " ";
$pagefiles[] = $f;
}
if ($foundone == false) return;
putenv("HTMLDOC_NOCGI=1");
# <Craig>
global $PdfExportUseHtmlDoc;
if($PdfExportUseHtmlDoc == true && is_executable('htmldoc')){
# Run HTMLDOC to provide the PDF file to the user...
passthru("htmldoc -t pdf14 --charset iso-8859-15 --color --quiet --jpeg --size " . $size . " " . $landscape . "--webpage " . $pagestring, $returnStatus);
if($returnStatus == 1)
error_log("Generating PDF failed. Return status was:" . $returnStatus, 0);
}
else{
// USING TCPDF lib
$PdfContent = '';
foreach ($pagefiles as &$pagefile){
$PdfContent .= file_get_contents($pagefile);
#TODO should we put splitter <hr>
}
// force utf-8
$PdfContent = str_replace('<head>','<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>',$PdfContent);
$PdfContent = str_replace('</head>','<style type="text/css">body{padding:10px 10px 35px;}</style></head>',$PdfContent); // Insert styling to make room for header/footer.
$orientation = stristr($landscape,'landscape') ? 'landscape' : 'portrait';
//cleaning html
$config = array(
'indent' => true,
'output-xhtml' => true,
'wrap' => 200);
$tidy = new tidy();
$PdfContent = $tidy->repairString($PdfContent, $config ,'utf8');
include('TCPDF_Function.php');
OutputPdf('doc.pdf',$PdfContent);
}
# </Craig>
flush();
foreach ($pagefiles as $pgf) {
unlink ($pgf);
}
}
function execute( $par ) {
global $wgRequest;
global $wgOut;
wfLoadExtensionMessages ('PdfPrint');
$dopdf = false;
if ($wgRequest->wasPosted()) {
$pagel = $wgRequest->getText ('pagel');
$pages = array_filter( explode( "\n", $pagel ), 'wfFilterPage1' );
$size = $wgRequest->getText ('Size', 'Letter');
$orientations = $wgRequest->getVal ('orientation');
$orientation = ($orientations == 'landscape') ? " --landscape --browserwidth 1200 " : " --portrait ";
$dopdf = true;
}
else {
$page = isset( $par ) ? $par : $wgRequest->getText( 'page' );
if ($page != "") $dopdf = true;
$pages = array ($page);
$orientation = " --portrait ";
$size = "Letter";
}
if ($dopdf) {
$wgOut->setPrintable();
$wgOut->disable();
$this->outputpdf ($pages, $orientation, $size);
return;
}
$self = SpecialPage::getTitleFor( 'PdfPrint' );
$wgOut->addHtml( wfMsgExt( 'pdf_print_text', 'parse' ) );
$form = Xml::openElement( 'form', array( 'method' => 'post',
'action' => $self->getLocalUrl( 'action=submit' ) ) );
$form .= Xml::openElement( 'textarea', array( 'name' => 'pagel', 'cols' => 40, 'rows' => 10 ) );
$form .= Xml::closeElement( 'textarea' );
$form .= '<br />';
$form .= Xml::radioLabel(wfMsg ('pdf_portrait'), 'orientation' , 'portrait' , 'portrait', true);
$form .= Xml::radioLabel(wfMsg ('pdf_landscape'), 'orientation' , 'landscape' , 'landscape', false);
$form .= '<br />' . wfMsg('pdf_size');
$form .= Xml::listDropDown ('Size', wfMsg ('pdf_size_options'),'', wfMsg('pdf_size_default'));
$form .= Xml::submitButton( wfMsg( 'pdf_submit' ) );
$form .= Xml::closeElement( 'form' );
$wgOut->addHtml( $form );
}
}
function wfFilterPage1( $page ) {
return $page !== '' && $page !== null;
}
?>