forked from splitbrain/dokuwiki-plugin-dw2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.php
More file actions
307 lines (261 loc) · 10.5 KB
/
Copy pathaction.php
File metadata and controls
307 lines (261 loc) · 10.5 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
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
<?php
/**
* dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Luigi Micco <l.micco@tiscali.it>
* @author Andreas Gohr <andi@splitbrain.org>
*/
// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();
if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
class action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
private $tpl;
/**
* Constructor. Sets the correct template
*/
function __construct(){
$tpl;
if(isset($_REQUEST['tpl'])){
$tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/','',$_REQUEST['tpl']));
}
if(!$tpl) $tpl = $this->getConf('template');
if(!$tpl) $tpl = 'default';
if(!is_dir(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl)) $tpl = 'default';
$this->tpl = $tpl;
}
/**
* Register the events
*/
function register(&$controller) {
$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array());
}
/**
* Do the HTML to PDF conversion work
*/
function convert(&$event, $param) {
global $ACT;
global $REV;
global $ID;
global $conf;
// our event?
if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false;
// check user's rights
if ( auth_quickaclcheck($ID) < AUTH_READ ) return false;
// it's ours, no one else's
$event->preventDefault();
// one or multiple pages?
$list = array();
if ( $ACT == 'export_pdf' ) {
$list[0] = $ID;
} elseif (isset($_COOKIE['list-pagelist'])) {
$list = explode("|", $_COOKIE['list-pagelist']);
}
// prepare cache
$cache = new cache(join(',',$list).$REV.$this->tpl,'.dw2.pdf');
$depends['files'] = array_map('wikiFN',$list);
$depends['files'][] = __FILE__;
$depends['files'][] = dirname(__FILE__).'/renderer.php';
$depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php';
$depends['files'] = array_merge($depends['files'], getConfigFiles('main'));
// hard work only when no cache available
if(!$this->getConf('usecache') || !$cache->useCache($depends)){
// initialize PDF library
require_once(dirname(__FILE__)."/DokuPDF.class.php");
$mpdf = new DokuPDF();
// let mpdf fix local links
$self = parse_url(DOKU_URL);
$url = $self['scheme'].'://'.$self['host'];
if($self['port']) $url .= ':'.$port;
$mpdf->setBasePath($url);
// Set the title
$title = $_GET['pdfbook_title'];
if(!$title) $title = p_get_first_heading($ID);
$mpdf->SetTitle($title);
// some default settings
$mpdf->mirrorMargins = 1;
$mpdf->useOddEven = 1;
$mpdf->setAutoTopMargin = 'stretch';
$mpdf->setAutoBottomMargin = 'stretch';
// load the template
$template = $this->load_template($title);
// prepare HTML header styles
$html = '<html><head>';
$html .= '<style>';
$html .= $this->load_css();
$html .= '@page { size:auto; '.$template['page'].'}';
$html .= '@page :first {'.$template['first'].'}';
$html .= $template['css'];
$html .= '</style>';
$html .= '</head><body>';
$html .= $template['html'];
$html .= '<div class="dokuwiki">';
// loop over all pages
$cnt = count($list);
for($n=0; $n<$cnt; $n++){
$page = $list[$n];
$html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
$html .= $template['cite'];
if ($n < ($cnt - 1)){
$html .= '<pagebreak />';
}
}
$html .= '</div>';
$mpdf->WriteHTML($html);
// write to cache file
$mpdf->Output($cache->cache, 'F');
}
// deliver the file
header('Content-Type: application/pdf');
header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
header('Pragma: public');
http_conditionalRequest(filemtime($cache->cache));
$filename = rawurlencode(cleanID(strtr($title, ':/;"',' ')));
if($this->getConf('output') == 'file'){
header('Content-Disposition: attachment; filename="'.$filename.'.pdf";');
}else{
header('Content-Disposition: inline; filename="'.$filename.'.pdf";');
}
if (http_sendfile($cache->cache)) exit;
$fp = @fopen($cache->cache,"rb");
if($fp){
http_rangeRequest($fp,filesize($cache->cache),'application/pdf');
}else{
header("HTTP/1.0 500 Internal Server Error");
print "Could not read file - bad permissions?";
}
exit();
}
/**
* Load the various template files and prepare the HTML/CSS for insertion
*/
protected function load_template($title){
global $ID;
global $REV;
global $conf;
$tpl = $this->tpl;
// this is what we'll return
$output = array(
'html' => '',
'css' => '',
'page' => '',
'first' => '',
'cite' => '',
);
// prepare header/footer elements
$html = '';
foreach(array('header','footer') as $t){
foreach(array('','_odd','_even','_first') as $h){
if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
$html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
$html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
$html .= '</htmlpage'.$t.'>'.DOKU_LF;
// register the needed pseudo CSS
if($h == '_first'){
$output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
}elseif($h == '_even'){
$output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
}elseif($h == '_odd'){
$output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
}else{
$output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
}
}
}
}
// generate qr code for this page using google infographics api
$qr_code = '';
if ($this->getConf('qrcodesize')) {
$url = urlencode(wl($ID,'','&',true));
$qr_code = '<img src="https://chart.googleapis.com/chart?chs='.
$this->getConf('qrcodesize').'&cht=qr&chl='.$url.'" />';
}
// prepare replacements
$replace = array(
'@ID@' => $ID,
'@PAGE@' => '{PAGENO}',
'@PAGES@' => '{nb}',
'@TITLE@' => hsc($title),
'@WIKI@' => $conf['title'],
'@WIKIURL@' => DOKU_URL,
'@UPDATE@' => dformat(filemtime(wikiFN($ID,$REV))),
'@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"),
'@DATE@' => dformat(time()),
'@BASE@' => DOKU_BASE,
'@TPLBASE@' => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$tpl.'/',
'@TPLBASE@' => DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/',
'@QRCODE@' => $qr_code,
);
// set HTML element
$output['html'] = str_replace(array_keys($replace), array_values($replace), $html);
// citation box
if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
$output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
$output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
}
// set custom styles
if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css')){
$output['css'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css');
}
return $output;
}
/**
* Load all the style sheets and apply the needed replacements
*/
protected function load_css(){
//reusue the CSS dispatcher functions without triggering the main function
define('SIMPLE_TEST',1);
require_once(DOKU_INC.'lib/exe/css.php');
// prepare CSS files
$files = array_merge(
array(
DOKU_INC.'lib/styles/screen.css'
=> DOKU_BASE.'lib/styles/',
DOKU_INC.'lib/styles/print.css'
=> DOKU_BASE.'lib/styles/',
),
css_pluginstyles('all'),
$this->css_pluginPDFstyles(),
array(
DOKU_PLUGIN.'dw2pdf/conf/style.css'
=> DOKU_BASE.'lib/plugins/dw2pdf/conf/',
DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css'
=> DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/',
DOKU_PLUGIN.'dw2pdf/conf/style.local.css'
=> DOKU_BASE.'lib/plugins/dw2pdf/conf/',
)
);
$css = '';
foreach($files as $file => $location){
$css .= css_loadfile($file, $location);
}
// apply pattern replacements
$css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/');
return $css;
}
/**
* Returns a list of possible Plugin PDF Styles
*
* Checks for a pdf.css, falls back to print.css
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function css_pluginPDFstyles(){
global $lang;
$list = array();
$plugins = plugin_list();
$usestyle = explode(',',$this->getConf('usestyles'));
foreach ($plugins as $p){
if(in_array($p,$usestyle)){
$list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
$list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
}
if(file_exists(DOKU_PLUGIN."$p/pdf.css")){
$list[DOKU_PLUGIN."$p/pdf.css"] = DOKU_BASE."lib/plugins/$p/";
}else{
$list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
}
}
return $list;
}
}