xref: /plugin/dw2pdf/action.php (revision 5a91534f19a99fe8321f5041be58fcd198243f81)
1ee19bac3SLuigi Micco<?php
2ee19bac3SLuigi Micco /**
3ee19bac3SLuigi Micco  * dw2Pdf Plugin: Conversion from dokuwiki content to pdf.
4ee19bac3SLuigi Micco  *
5ee19bac3SLuigi Micco  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6ee19bac3SLuigi Micco  * @author     Luigi Micco <l.micco@tiscali.it>
75db42babSAndreas Gohr  * @author     Andreas Gohr <andi@splitbrain.org>
8ee19bac3SLuigi Micco  */
9ee19bac3SLuigi Micco
10ee19bac3SLuigi Micco// must be run within Dokuwiki
11ee19bac3SLuigi Miccoif (!defined('DOKU_INC')) die();
12ee19bac3SLuigi Miccoif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
13ee19bac3SLuigi Micco
141ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
15ee19bac3SLuigi Micco
161c14c879SAndreas Gohr    private $tpl;
171c14c879SAndreas Gohr
181c14c879SAndreas Gohr    /**
191c14c879SAndreas Gohr     * Constructor. Sets the correct template
201c14c879SAndreas Gohr     */
211c14c879SAndreas Gohr    function __construct(){
221c14c879SAndreas Gohr        $tpl;
231c14c879SAndreas Gohr        if(isset($_REQUEST['tpl'])){
241c14c879SAndreas Gohr            $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/','',$_REQUEST['tpl']));
251c14c879SAndreas Gohr        }
261c14c879SAndreas Gohr        if(!$tpl) $tpl = $this->getConf('template');
271c14c879SAndreas Gohr        if(!$tpl) $tpl = 'default';
281c14c879SAndreas Gohr        if(!is_dir(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl)) $tpl = 'default';
291c14c879SAndreas Gohr
301c14c879SAndreas Gohr        $this->tpl = $tpl;
311c14c879SAndreas Gohr    }
321c14c879SAndreas Gohr
33ee19bac3SLuigi Micco    /**
34ee19bac3SLuigi Micco     * Register the events
35ee19bac3SLuigi Micco     */
361ef68647SAndreas Gohr    function register(&$controller) {
37ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array());
38ee19bac3SLuigi Micco    }
39ee19bac3SLuigi Micco
401c14c879SAndreas Gohr    /**
411c14c879SAndreas Gohr     * Do the HTML to PDF conversion work
421c14c879SAndreas Gohr     */
431ef68647SAndreas Gohr    function convert(&$event, $param) {
44ee19bac3SLuigi Micco        global $ACT;
45ee19bac3SLuigi Micco        global $REV;
46ee19bac3SLuigi Micco        global $ID;
47ee19bac3SLuigi Micco        global $conf;
48ee19bac3SLuigi Micco
491ef68647SAndreas Gohr        // our event?
501ef68647SAndreas Gohr        if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false;
51ee19bac3SLuigi Micco
521ef68647SAndreas Gohr        // check user's rights
531ef68647SAndreas Gohr        if ( auth_quickaclcheck($ID) < AUTH_READ ) return false;
541ef68647SAndreas Gohr
551ef68647SAndreas Gohr        // it's ours, no one else's
56ee19bac3SLuigi Micco        $event->preventDefault();
57ee19bac3SLuigi Micco
5887c86ddaSAndreas Gohr        // one or multiple pages?
5987c86ddaSAndreas Gohr        $list = array();
6087c86ddaSAndreas Gohr        if ( $ACT == 'export_pdf' ) {
6187c86ddaSAndreas Gohr            $list[0] = $ID;
6287c86ddaSAndreas Gohr        } elseif (isset($_COOKIE['list-pagelist'])) {
6387c86ddaSAndreas Gohr            $list = explode("|", $_COOKIE['list-pagelist']);
6487c86ddaSAndreas Gohr        }
6587c86ddaSAndreas Gohr
6687c86ddaSAndreas Gohr        // prepare cache
671c14c879SAndreas Gohr        $cache = new cache(join(',',$list).$REV.$this->tpl,'.dw2.pdf');
6887c86ddaSAndreas Gohr        $depends['files']   = array_map('wikiFN',$list);
6987c86ddaSAndreas Gohr        $depends['files'][] = __FILE__;
7087c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/renderer.php';
7187c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php';
72df59f400SAndreas Gohr        $depends['files']   = array_merge($depends['files'], getConfigFiles('main'));
7387c86ddaSAndreas Gohr
74d01d2a42SAndreas Gohr        // hard work only when no cache available
7587c86ddaSAndreas Gohr        if(!$this->getConf('usecache') || !$cache->useCache($depends)){
761ef68647SAndreas Gohr            // initialize PDF library
77cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__)."/DokuPDF.class.php");
781ef68647SAndreas Gohr            $mpdf = new DokuPDF();
79ee19bac3SLuigi Micco
80d62df65bSAndreas Gohr            // let mpdf fix local links
81d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
82d62df65bSAndreas Gohr            $url  = $self['scheme'].'://'.$self['host'];
83d62df65bSAndreas Gohr            if($self['port']) $url .= ':'.$port;
84d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
85d62df65bSAndreas Gohr
8656d13144SAndreas Gohr            // Set the title
8756d13144SAndreas Gohr            $title = $_GET['pdfbook_title'];
8856d13144SAndreas Gohr            if(!$title) $title = p_get_first_heading($ID);
8956d13144SAndreas Gohr            $mpdf->SetTitle($title);
9056d13144SAndreas Gohr
911ef68647SAndreas Gohr            // some default settings
92daa70883SAndreas Gohr            $mpdf->mirrorMargins = 1;
93daa70883SAndreas Gohr            $mpdf->useOddEven    = 1;
94daa70883SAndreas Gohr            $mpdf->setAutoTopMargin = 'stretch';
95daa70883SAndreas Gohr            $mpdf->setAutoBottomMargin = 'stretch';
962eedf77dSAndreas Gohr
9756d13144SAndreas Gohr            // load the template
981c14c879SAndreas Gohr            $template = $this->load_template($title);
99ee19bac3SLuigi Micco
1001ef68647SAndreas Gohr            // prepare HTML header styles
101ee19bac3SLuigi Micco            $html  = '<html><head>';
1021ef68647SAndreas Gohr            $html .= '<style>';
1031c14c879SAndreas Gohr            $html .= $this->load_css();
104daa70883SAndreas Gohr            $html .= '@page { size:auto; '.$template['page'].'}';
1052eedf77dSAndreas Gohr            $html .= '@page :first {'.$template['first'].'}';
1062eedf77dSAndreas Gohr            $html .= $template['css'];
1071ef68647SAndreas Gohr            $html .= '</style>';
1081ef68647SAndreas Gohr            $html .= '</head><body>';
1092eedf77dSAndreas Gohr            $html .= $template['html'];
1101c14c879SAndreas Gohr            $html .= '<div class="dokuwiki">';
1112eedf77dSAndreas Gohr
1121ef68647SAndreas Gohr            // loop over all pages
1131ef68647SAndreas Gohr            $cnt = count($list);
1141ef68647SAndreas Gohr            for($n=0; $n<$cnt; $n++){
115ee19bac3SLuigi Micco                $page = $list[$n];
116ee19bac3SLuigi Micco
117a876b55bSAndreas Gohr                $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
1182eedf77dSAndreas Gohr                $html .= $template['cite'];
1191ef68647SAndreas Gohr                if ($n < ($cnt - 1)){
1201ef68647SAndreas Gohr                    $html .= '<pagebreak />';
1211ef68647SAndreas Gohr                }
122ee19bac3SLuigi Micco            }
123ee19bac3SLuigi Micco
1241c14c879SAndreas Gohr            $html .= '</div>';
125ee19bac3SLuigi Micco            $mpdf->WriteHTML($html);
126a06728a6SAndreas Gohr
12787c86ddaSAndreas Gohr            // write to cache file
12887c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
12987c86ddaSAndreas Gohr        }
13087c86ddaSAndreas Gohr
13187c86ddaSAndreas Gohr        // deliver the file
13287c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
133b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
13487c86ddaSAndreas Gohr        header('Pragma: public');
13587c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
13687c86ddaSAndreas Gohr
1379a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"','    ')));
13887c86ddaSAndreas Gohr        if($this->getConf('output') == 'file'){
1399a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="'.$filename.'.pdf";');
14087c86ddaSAndreas Gohr        }else{
1419a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="'.$filename.'.pdf";');
14287c86ddaSAndreas Gohr        }
143ee19bac3SLuigi Micco
14487c86ddaSAndreas Gohr        if (http_sendfile($cache->cache)) exit;
14587c86ddaSAndreas Gohr
14687c86ddaSAndreas Gohr        $fp = @fopen($cache->cache,"rb");
14787c86ddaSAndreas Gohr        if($fp){
14887c86ddaSAndreas Gohr            http_rangeRequest($fp,filesize($cache->cache),'application/pdf');
14987c86ddaSAndreas Gohr        }else{
15087c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
15187c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
15287c86ddaSAndreas Gohr        }
1531ef68647SAndreas Gohr        exit();
1541ef68647SAndreas Gohr    }
1551ef68647SAndreas Gohr
156d01d2a42SAndreas Gohr
157d01d2a42SAndreas Gohr    /**
1582eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
1591ef68647SAndreas Gohr     */
1601c14c879SAndreas Gohr    protected function load_template($title){
1611ef68647SAndreas Gohr        global $ID;
1621ef68647SAndreas Gohr        global $REV;
1631ef68647SAndreas Gohr        global $conf;
1641c14c879SAndreas Gohr        $tpl = $this->tpl;
1651ef68647SAndreas Gohr
1662eedf77dSAndreas Gohr        // this is what we'll return
1672eedf77dSAndreas Gohr        $output = array(
1682eedf77dSAndreas Gohr            'html'  => '',
1692eedf77dSAndreas Gohr            'css'   => '',
1702eedf77dSAndreas Gohr            'page'  => '',
1712eedf77dSAndreas Gohr            'first' => '',
1722eedf77dSAndreas Gohr            'cite'  => '',
1732eedf77dSAndreas Gohr        );
1742eedf77dSAndreas Gohr
1752eedf77dSAndreas Gohr        // prepare header/footer elements
1762eedf77dSAndreas Gohr        $html = '';
1772eedf77dSAndreas Gohr        foreach(array('header','footer') as $t){
1789e6e41f4SAndreas Gohr            foreach(array('','_odd','_even','_first') as $h){
1792eedf77dSAndreas Gohr                if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
1802eedf77dSAndreas Gohr                    $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
1812eedf77dSAndreas Gohr                    $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
1822eedf77dSAndreas Gohr                    $html .= '</htmlpage'.$t.'>'.DOKU_LF;
1832eedf77dSAndreas Gohr
1842eedf77dSAndreas Gohr                    // register the needed pseudo CSS
1859e6e41f4SAndreas Gohr                    if($h == '_first'){
1862eedf77dSAndreas Gohr                        $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
1872eedf77dSAndreas Gohr                    }elseif($h == '_even'){
1882eedf77dSAndreas Gohr                        $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
189daa70883SAndreas Gohr                    }elseif($h == '_odd'){
1902eedf77dSAndreas Gohr                        $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
191daa70883SAndreas Gohr                    }else{
192daa70883SAndreas Gohr                        $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
1932eedf77dSAndreas Gohr                    }
1942eedf77dSAndreas Gohr                }
1952eedf77dSAndreas Gohr            }
1962eedf77dSAndreas Gohr        }
1972eedf77dSAndreas Gohr
198eb7e2fbeSJohannes Fürwentsches        // generate qr code for this page using google infographics api
199eb7e2fbeSJohannes Fürwentsches        $qr_code = '';
200*5a91534fSAndreas Gohr        if ($this->getConf('qrcodesize')) {
201eb7e2fbeSJohannes Fürwentsches            $url = urlencode(wl($ID,'','&',true));
202*5a91534fSAndreas Gohr            $qr_code = '<img src="https://chart.googleapis.com/chart?chs='.
203*5a91534fSAndreas Gohr                       $this->getConf('qrcodesize').'&cht=qr&chl='.$url.'" />';
204eb7e2fbeSJohannes Fürwentsches        }
205d9ff9cfaSJohannes Fürwentsches
2061ef68647SAndreas Gohr        // prepare replacements
2071ef68647SAndreas Gohr        $replace = array(
2081ef68647SAndreas Gohr                '@ID@'      => $ID,
2091ef68647SAndreas Gohr                '@PAGE@'    => '{PAGENO}',
2101ef68647SAndreas Gohr                '@PAGES@'   => '{nb}',
2112eedf77dSAndreas Gohr                '@TITLE@'   => hsc($title),
2121ef68647SAndreas Gohr                '@WIKI@'    => $conf['title'],
2131ef68647SAndreas Gohr                '@WIKIURL@' => DOKU_URL,
2143ae7a8dfSAndreas Gohr                '@UPDATE@'  => dformat(filemtime(wikiFN($ID,$REV))),
2153ae7a8dfSAndreas Gohr                '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"),
2161ef68647SAndreas Gohr                '@DATE@'    => dformat(time()),
2175d6fbaeaSAndreas Gohr                '@BASE@'    => DOKU_BASE,
2185aa96d34SAndreas Gohr                '@TPLBASE@' => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$tpl.'/',
2195d6fbaeaSAndreas Gohr                '@TPLBASE@' => DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/',
220d9ff9cfaSJohannes Fürwentsches                '@QRCODE@'  => $qr_code,
2211ef68647SAndreas Gohr        );
2221ef68647SAndreas Gohr
2232eedf77dSAndreas Gohr        // set HTML element
2242eedf77dSAndreas Gohr        $output['html'] = str_replace(array_keys($replace), array_values($replace), $html);
2251ef68647SAndreas Gohr
2262eedf77dSAndreas Gohr        // citation box
2272eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
2282eedf77dSAndreas Gohr            $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
2292eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
2302eedf77dSAndreas Gohr        }
2311ef68647SAndreas Gohr
2322eedf77dSAndreas Gohr        // set custom styles
2332eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css')){
2342eedf77dSAndreas Gohr            $output['css'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css');
2352eedf77dSAndreas Gohr        }
2362eedf77dSAndreas Gohr
2372eedf77dSAndreas Gohr        return $output;
2381ef68647SAndreas Gohr    }
2391ef68647SAndreas Gohr
2401ef68647SAndreas Gohr    /**
2411c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
2421ef68647SAndreas Gohr     */
2431c14c879SAndreas Gohr    protected function load_css(){
2441c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
2451c14c879SAndreas Gohr        define('SIMPLE_TEST',1);
2461c14c879SAndreas Gohr        require_once(DOKU_INC.'lib/exe/css.php');
247ee19bac3SLuigi Micco
2481c14c879SAndreas Gohr        // prepare CSS files
2491c14c879SAndreas Gohr        $files = array_merge(
2501c14c879SAndreas Gohr                    array(
2511c14c879SAndreas Gohr                        DOKU_INC.'lib/styles/screen.css'
2521c14c879SAndreas Gohr                            => DOKU_BASE.'lib/styles/',
2531c14c879SAndreas Gohr                        DOKU_INC.'lib/styles/print.css'
2541c14c879SAndreas Gohr                            => DOKU_BASE.'lib/styles/',
2551c14c879SAndreas Gohr                    ),
2561c14c879SAndreas Gohr                    css_pluginstyles('all'),
25758e6409eSAndreas Gohr                    $this->css_pluginPDFstyles(),
2581c14c879SAndreas Gohr                    array(
2591c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/conf/style.css'
2601c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
2611c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css'
2621c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/',
2631c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/conf/style.local.css'
2641c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
2651c14c879SAndreas Gohr                    )
2661c14c879SAndreas Gohr                 );
2671c14c879SAndreas Gohr        $css = '';
2681c14c879SAndreas Gohr        foreach($files as $file => $location){
2691c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
2701ef68647SAndreas Gohr        }
2711ef68647SAndreas Gohr
2721c14c879SAndreas Gohr        // apply pattern replacements
2731c14c879SAndreas Gohr        $css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/');
2741ef68647SAndreas Gohr
2751c14c879SAndreas Gohr        return $css;
276ee19bac3SLuigi Micco    }
2771c14c879SAndreas Gohr
27858e6409eSAndreas Gohr
27958e6409eSAndreas Gohr    /**
28058e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
28158e6409eSAndreas Gohr     *
28258e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
28358e6409eSAndreas Gohr     *
28458e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
28558e6409eSAndreas Gohr     */
28658e6409eSAndreas Gohr    function css_pluginPDFstyles(){
28758e6409eSAndreas Gohr        global $lang;
28858e6409eSAndreas Gohr        $list = array();
28958e6409eSAndreas Gohr        $plugins = plugin_list();
290f54b51f7SAndreas Gohr
291f54b51f7SAndreas Gohr        $usestyle = explode(',',$this->getConf('usestyles'));
29258e6409eSAndreas Gohr        foreach ($plugins as $p){
293f54b51f7SAndreas Gohr            if(in_array($p,$usestyle)){
294f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
295f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
296f54b51f7SAndreas Gohr            }
297f54b51f7SAndreas Gohr
29858e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN."$p/pdf.css")){
29958e6409eSAndreas Gohr                $list[DOKU_PLUGIN."$p/pdf.css"] = DOKU_BASE."lib/plugins/$p/";
30058e6409eSAndreas Gohr            }else{
30158e6409eSAndreas Gohr                $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
30258e6409eSAndreas Gohr            }
30358e6409eSAndreas Gohr        }
30458e6409eSAndreas Gohr        return $list;
30558e6409eSAndreas Gohr    }
30658e6409eSAndreas Gohr
307ee19bac3SLuigi Micco}
308