xref: /plugin/dw2pdf/action.php (revision f765508ec270ca401de763caf93dc9e70b35a286)
1<?php
2 /**
3  * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
4  *
5  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6  * @author     Luigi Micco <l.micco@tiscali.it>
7  * @author     Andreas Gohr <andi@splitbrain.org>
8  */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13class action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
14
15    private $tpl;
16
17    /**
18     * Constructor. Sets the correct template
19     */
20    public function __construct(){
21        $tpl = false;
22        if(isset($_REQUEST['tpl'])){
23            $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/','',$_REQUEST['tpl']));
24        }
25        if(!$tpl) $tpl = $this->getConf('template');
26        if(!$tpl) $tpl = 'default';
27        if(!is_dir(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl)) $tpl = 'default';
28
29        $this->tpl = $tpl;
30    }
31
32    /**
33     * Register the events
34     */
35    public function register(Doku_Event_Handler $controller) {
36        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array());
37        $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array());
38    }
39
40    /**
41     * Do the HTML to PDF conversion work
42     *
43     * @param Doku_Event $event
44     * @param array      $param
45     * @return bool
46     */
47    public function convert(&$event, $param) {
48        global $ACT;
49        global $REV;
50        global $ID;
51        global $INPUT;
52
53        // our event?
54        if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false;
55
56        // check user's rights
57        if ( auth_quickaclcheck($ID) < AUTH_READ ) return false;
58
59        // one or multiple pages?
60        $list  = array();
61
62        if($ACT == 'export_pdf') {
63            $list[0] = $ID;
64            $title = p_get_first_heading($ID);
65        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
66            //is in Bookmanager of bookcreator plugin title given
67            if(!$title = $_GET['pdfbook_title']) {  //TODO when title is changed, the cached file contains the old title
68                /** @var $bookcreator action_plugin_bookcreator */
69                $bookcreator = plugin_load('action', 'bookcreator');
70                msg($bookcreator->getLang('needtitle'), -1);
71
72                $event->data               = 'show';
73                $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
74                return false;
75            }
76            $list = explode("|", $_COOKIE['list-pagelist']);
77        } else {
78            /** @var $bookcreator action_plugin_bookcreator */
79            $bookcreator = plugin_load('action', 'bookcreator');
80            msg($bookcreator->getLang('empty'), -1);
81
82            $event->data               = 'show';
83            $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
84            return false;
85        }
86
87        // it's ours, no one else's
88        $event->preventDefault();
89
90        // decide on the paper setup from param or config
91        $pagesize    = $INPUT->str('pagesize', $this->getConf('pagesize'), true);
92        $orientation = $INPUT->str('orientation', $this->getConf('orientation'), true);
93
94        // prepare cache
95        $cache = new cache(join(',',$list).$REV.$this->tpl.$pagesize.$orientation,'.dw2.pdf');
96        $depends['files']   = array_map('wikiFN',$list);
97        $depends['files'][] = __FILE__;
98        $depends['files'][] = dirname(__FILE__).'/renderer.php';
99        $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php';
100        $depends['files']   = array_merge($depends['files'], getConfigFiles('main'));
101
102        // hard work only when no cache available
103        if(!$this->getConf('usecache') || !$cache->useCache($depends)){
104            // initialize PDF library
105            require_once(dirname(__FILE__)."/DokuPDF.class.php");
106
107            $mpdf = new DokuPDF($pagesize, $orientation);
108
109            // let mpdf fix local links
110            $self = parse_url(DOKU_URL);
111            $url  = $self['scheme'].'://'.$self['host'];
112            if($self['port']) $url .= ':'.$self['port'];
113            $mpdf->setBasePath($url);
114
115            // Set the title
116            $mpdf->SetTitle($title);
117
118            // some default settings
119            $mpdf->mirrorMargins = 1;
120            $mpdf->useOddEven    = 1;
121            $mpdf->setAutoTopMargin = 'stretch';
122            $mpdf->setAutoBottomMargin = 'stretch';
123
124            // load the template
125            $template = $this->load_template($title);
126
127            // prepare HTML header styles
128            $html  = '<html><head>';
129            $html .= '<style type="text/css">';
130            $html .= $this->load_css();
131            $html .= '@page { size:auto; '.$template['page'].'}';
132            $html .= '@page :first {'.$template['first'].'}';
133            $html .= '</style>';
134            $html .= '</head><body>';
135            $html .= $template['html'];
136            $html .= '<div class="dokuwiki">';
137
138            // loop over all pages
139            $cnt = count($list);
140            for($n=0; $n<$cnt; $n++){
141                $page = $list[$n];
142
143                $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
144                $html .= $this->page_depend_replacements($template['cite'], cleanID($page));
145                if ($n < ($cnt - 1)){
146                    $html .= '<pagebreak />';
147                }
148            }
149
150            $html .= '</div>';
151
152            //Return html for debugging
153            if($_GET['debughtml'] == 'html') die($html);
154
155            $mpdf->WriteHTML($html);
156
157            // write to cache file
158            $mpdf->Output($cache->cache, 'F');
159        }
160
161        // deliver the file
162        header('Content-Type: application/pdf');
163        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
164        header('Pragma: public');
165        http_conditionalRequest(filemtime($cache->cache));
166
167        $filename = rawurlencode(cleanID(strtr($title, ':/;"','    ')));
168        if($this->getConf('output') == 'file'){
169            header('Content-Disposition: attachment; filename="'.$filename.'.pdf";');
170        }else{
171            header('Content-Disposition: inline; filename="'.$filename.'.pdf";');
172        }
173
174        //try to send file, and exit if done
175        http_sendfile($cache->cache);
176
177        $fp = @fopen($cache->cache,"rb");
178        if($fp){
179            http_rangeRequest($fp,filesize($cache->cache),'application/pdf');
180        }else{
181            header("HTTP/1.0 500 Internal Server Error");
182            print "Could not read file - bad permissions?";
183        }
184        exit();
185    }
186
187    /**
188     * Add 'export pdf'-button to pagetools
189     *
190     * @param Doku_Event $event
191     * @param mixed      $param not defined
192     */
193    public function addbutton(&$event, $param) {
194        global $ID, $REV, $conf;
195
196        if($this->getConf('showexportbutton') && $event->data['view'] == 'main') {
197            $params = array('do' => 'export_pdf');
198            if($REV) $params['rev'] = $REV;
199
200            switch($conf['template']) {
201                case 'dokuwiki':
202                case 'arago':
203                case 'wikilu': // a private template
204                    $event->data['items']['export_pdf'] =
205                        '<li>'
206                        .'<a href='.wl($ID, $params).'  class="action export_pdf" rel="nofollow" title="'.$this->getLang('export_pdf_button').'">'
207                        .'<span>'.$this->getLang('export_pdf_button').'</span>'
208                        .'</a>'
209                        .'</li>';
210                    break;
211            }
212        }
213    }
214
215    /**
216     * Load the various template files and prepare the HTML/CSS for insertion
217     */
218    protected function load_template($title){
219        global $ID;
220        global $conf;
221        $tpl = $this->tpl;
222
223        // this is what we'll return
224        $output = array(
225            'html'  => '',
226            'page'  => '',
227            'first' => '',
228            'cite'  => '',
229        );
230
231        // prepare header/footer elements
232        $html = '';
233        foreach(array('header','footer') as $t){
234            foreach(array('','_odd','_even','_first') as $h){
235                if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
236                    $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
237                    $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
238                    $html .= '</htmlpage'.$t.'>'.DOKU_LF;
239
240                    // register the needed pseudo CSS
241                    if($h == '_first'){
242                        $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
243                    }elseif($h == '_even'){
244                        $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
245                    }elseif($h == '_odd'){
246                        $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
247                    }else{
248                        $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
249                    }
250                }
251            }
252        }
253
254        // prepare replacements
255        $replace = array(
256                '@PAGE@'    => '{PAGENO}',
257                '@PAGES@'   => '{nb}',
258                '@TITLE@'   => hsc($title),
259                '@WIKI@'    => $conf['title'],
260                '@WIKIURL@' => DOKU_URL,
261                '@DATE@'    => dformat(time()),
262                '@BASE@'    => DOKU_BASE,
263                '@TPLBASE@' => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$tpl.'/'
264        );
265
266        // set HTML element
267        $html = str_replace(array_keys($replace), array_values($replace), $html);
268        //TODO For bookcreator $ID (= bookmanager page) makes no sense
269        $output['html'] = $this->page_depend_replacements($html, $ID);
270
271        // citation box
272        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
273            $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
274            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
275        }
276
277        return $output;
278    }
279
280    /**
281     * @param string $raw code with placeholders
282     * @param string $id  pageid
283     * @return string
284     */
285    protected function page_depend_replacements($raw, $id){
286        global $REV;
287
288        // generate qr code for this page using google infographics api
289        $qr_code = '';
290        if ($this->getConf('qrcodesize')) {
291            $url = urlencode(wl($id,'','&',true));
292            $qr_code = '<img src="https://chart.googleapis.com/chart?chs='.
293                $this->getConf('qrcodesize').'&cht=qr&chl='.$url.'" />';
294        }
295        // prepare replacements
296        $replace['@ID@']      = $id;
297        $replace['@UPDATE@']  = dformat(filemtime(wikiFN($id, $REV)));
298        $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev'=> $REV) : false, true, "&");
299        $replace['@QRCODE@']  = $qr_code;
300
301        return str_replace(array_keys($replace), array_values($replace), $raw);
302    }
303
304    /**
305     * Load all the style sheets and apply the needed replacements
306     */
307    protected function load_css(){
308        global $conf;
309        //reusue the CSS dispatcher functions without triggering the main function
310        define('SIMPLE_TEST',1);
311        require_once(DOKU_INC.'lib/exe/css.php');
312
313        // prepare CSS files
314        $files = array_merge(
315                    array(
316                        DOKU_INC.'lib/styles/screen.css'
317                            => DOKU_BASE.'lib/styles/',
318                        DOKU_INC.'lib/styles/print.css'
319                            => DOKU_BASE.'lib/styles/',
320                    ),
321                    css_pluginstyles('all'),
322                    $this->css_pluginPDFstyles(),
323                    array(
324                        DOKU_PLUGIN.'dw2pdf/conf/style.css'
325                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
326                        DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css'
327                            => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/',
328                        DOKU_PLUGIN.'dw2pdf/conf/style.local.css'
329                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
330                    )
331                 );
332        $css = '';
333        foreach($files as $file => $location){
334            $display = str_replace(fullpath(DOKU_INC), '', fullpath($file));
335            $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n";
336            $css .= css_loadfile($file, $location);
337        }
338
339        if(function_exists('css_parseless')) {
340            // apply pattern replacements
341            $styleini = css_styleini($conf['template']);
342            $css = css_applystyle($css, $styleini['replacements']);
343
344            // parse less
345            $css = css_parseless($css);
346        } else {
347            // @deprecated 2013-12-19: fix backward compatibility
348            $css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/');
349        }
350
351        return $css;
352    }
353
354    /**
355     * Returns a list of possible Plugin PDF Styles
356     *
357     * Checks for a pdf.css, falls back to print.css
358     *
359     * @author Andreas Gohr <andi@splitbrain.org>
360     */
361    protected function css_pluginPDFstyles(){
362        $list = array();
363        $plugins = plugin_list();
364
365        $usestyle = explode(',',$this->getConf('usestyles'));
366        foreach ($plugins as $p){
367            if(in_array($p,$usestyle)){
368                $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
369                $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
370            }
371
372            if(file_exists(DOKU_PLUGIN."$p/pdf.css")){
373                $list[DOKU_PLUGIN."$p/pdf.css"] = DOKU_BASE."lib/plugins/$p/";
374            }else{
375                $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
376            }
377        }
378        return $list;
379    }
380}
381