xref: /plugin/dw2pdf/action.php (revision 737417c69f280f598d9db0ccda4ef70cdf721e26)
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(){
22*737417c6SKlap-in        $tpl = false;
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
42*737417c6SKlap-in     *
43*737417c6SKlap-in     * @param Doku_Event $event
44*737417c6SKlap-in     * @param array      $param
45*737417c6SKlap-in     * @return bool
461c14c879SAndreas Gohr     */
471ef68647SAndreas Gohr    function convert(&$event, $param) {
48ee19bac3SLuigi Micco        global $ACT;
49ee19bac3SLuigi Micco        global $REV;
50ee19bac3SLuigi Micco        global $ID;
51ee19bac3SLuigi Micco
521ef68647SAndreas Gohr        // our event?
531ef68647SAndreas Gohr        if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false;
54ee19bac3SLuigi Micco
551ef68647SAndreas Gohr        // check user's rights
561ef68647SAndreas Gohr        if ( auth_quickaclcheck($ID) < AUTH_READ ) return false;
571ef68647SAndreas Gohr
5887c86ddaSAndreas Gohr        // one or multiple pages?
5987c86ddaSAndreas Gohr        $list  = array();
6087c86ddaSAndreas Gohr        if($ACT == 'export_pdf') {
6187c86ddaSAndreas Gohr            $list[0] = $ID;
62*737417c6SKlap-in            $title = p_get_first_heading($ID);
63*737417c6SKlap-in        } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) {
64*737417c6SKlap-in            //is in Bookmanager of bookcreator plugin title given
65*737417c6SKlap-in            if(!$title = $_GET['pdfbook_title']) {  //TODO when title is changed, the cached file contains the old title
66*737417c6SKlap-in                /** @var $bookcreator action_plugin_bookcreator */
67*737417c6SKlap-in                $bookcreator =& plugin_load('action', 'bookcreator');
68*737417c6SKlap-in                msg($bookcreator->getLang('needtitle'), -1);
69*737417c6SKlap-in
70*737417c6SKlap-in                $event->data               = 'show';
71*737417c6SKlap-in                $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
72*737417c6SKlap-in                return false;
7387c86ddaSAndreas Gohr            }
74*737417c6SKlap-in            $list = explode("|", $_COOKIE['list-pagelist']);
75*737417c6SKlap-in        } else {
76*737417c6SKlap-in            /** @var $bookcreator action_plugin_bookcreator */
77*737417c6SKlap-in            $bookcreator =& plugin_load('action', 'bookcreator');
78*737417c6SKlap-in            msg($bookcreator->getLang('empty'), -1);
79*737417c6SKlap-in
80*737417c6SKlap-in            $event->data               = 'show';
81*737417c6SKlap-in            $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url
82*737417c6SKlap-in            return false;
83*737417c6SKlap-in        }
84*737417c6SKlap-in
85*737417c6SKlap-in        // it's ours, no one else's
86*737417c6SKlap-in        $event->preventDefault();
8787c86ddaSAndreas Gohr
8887c86ddaSAndreas Gohr        // prepare cache
891c14c879SAndreas Gohr        $cache = new cache(join(',',$list).$REV.$this->tpl,'.dw2.pdf');
9087c86ddaSAndreas Gohr        $depends['files']   = array_map('wikiFN',$list);
9187c86ddaSAndreas Gohr        $depends['files'][] = __FILE__;
9287c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/renderer.php';
9387c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php';
94df59f400SAndreas Gohr        $depends['files']   = array_merge($depends['files'], getConfigFiles('main'));
9587c86ddaSAndreas Gohr
96d01d2a42SAndreas Gohr        // hard work only when no cache available
9787c86ddaSAndreas Gohr        if(!$this->getConf('usecache') || !$cache->useCache($depends)){
981ef68647SAndreas Gohr            // initialize PDF library
99cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__)."/DokuPDF.class.php");
1001ef68647SAndreas Gohr            $mpdf = new DokuPDF();
101ee19bac3SLuigi Micco
102d62df65bSAndreas Gohr            // let mpdf fix local links
103d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
104d62df65bSAndreas Gohr            $url  = $self['scheme'].'://'.$self['host'];
105*737417c6SKlap-in            if($self['port']) $url .= ':'.$self['port'];
106d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
107d62df65bSAndreas Gohr
10856d13144SAndreas Gohr            // Set the title
10956d13144SAndreas Gohr            $mpdf->SetTitle($title);
11056d13144SAndreas Gohr
1111ef68647SAndreas Gohr            // some default settings
112daa70883SAndreas Gohr            $mpdf->mirrorMargins = 1;
113daa70883SAndreas Gohr            $mpdf->useOddEven    = 1;
114daa70883SAndreas Gohr            $mpdf->setAutoTopMargin = 'stretch';
115daa70883SAndreas Gohr            $mpdf->setAutoBottomMargin = 'stretch';
1162eedf77dSAndreas Gohr
11756d13144SAndreas Gohr            // load the template
1181c14c879SAndreas Gohr            $template = $this->load_template($title);
119ee19bac3SLuigi Micco
1201ef68647SAndreas Gohr            // prepare HTML header styles
121ee19bac3SLuigi Micco            $html  = '<html><head>';
122*737417c6SKlap-in            $html .= '<style type="text/css">';
1231c14c879SAndreas Gohr            $html .= $this->load_css();
124daa70883SAndreas Gohr            $html .= '@page { size:auto; '.$template['page'].'}';
1252eedf77dSAndreas Gohr            $html .= '@page :first {'.$template['first'].'}';
1262eedf77dSAndreas Gohr            $html .= $template['css'];
1271ef68647SAndreas Gohr            $html .= '</style>';
1281ef68647SAndreas Gohr            $html .= '</head><body>';
1292eedf77dSAndreas Gohr            $html .= $template['html'];
1301c14c879SAndreas Gohr            $html .= '<div class="dokuwiki">';
1312eedf77dSAndreas Gohr
1321ef68647SAndreas Gohr            // loop over all pages
1331ef68647SAndreas Gohr            $cnt = count($list);
1341ef68647SAndreas Gohr            for($n=0; $n<$cnt; $n++){
135ee19bac3SLuigi Micco                $page = $list[$n];
136ee19bac3SLuigi Micco
137a876b55bSAndreas Gohr                $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
1382eedf77dSAndreas Gohr                $html .= $template['cite'];
1391ef68647SAndreas Gohr                if ($n < ($cnt - 1)){
1401ef68647SAndreas Gohr                    $html .= '<pagebreak />';
1411ef68647SAndreas Gohr                }
142ee19bac3SLuigi Micco            }
143ee19bac3SLuigi Micco
1441c14c879SAndreas Gohr            $html .= '</div>';
145ee19bac3SLuigi Micco            $mpdf->WriteHTML($html);
146a06728a6SAndreas Gohr
14787c86ddaSAndreas Gohr            // write to cache file
14887c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
14987c86ddaSAndreas Gohr        }
15087c86ddaSAndreas Gohr
15187c86ddaSAndreas Gohr        // deliver the file
15287c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
153b853b723SAndreas Gohr        header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
15487c86ddaSAndreas Gohr        header('Pragma: public');
15587c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
15687c86ddaSAndreas Gohr
1579a3c8d9fSAndreas Gohr        $filename = rawurlencode(cleanID(strtr($title, ':/;"','    ')));
15887c86ddaSAndreas Gohr        if($this->getConf('output') == 'file'){
1599a3c8d9fSAndreas Gohr            header('Content-Disposition: attachment; filename="'.$filename.'.pdf";');
16087c86ddaSAndreas Gohr        }else{
1619a3c8d9fSAndreas Gohr            header('Content-Disposition: inline; filename="'.$filename.'.pdf";');
16287c86ddaSAndreas Gohr        }
163ee19bac3SLuigi Micco
16487c86ddaSAndreas Gohr        if (http_sendfile($cache->cache)) exit;
16587c86ddaSAndreas Gohr
16687c86ddaSAndreas Gohr        $fp = @fopen($cache->cache,"rb");
16787c86ddaSAndreas Gohr        if($fp){
16887c86ddaSAndreas Gohr            http_rangeRequest($fp,filesize($cache->cache),'application/pdf');
16987c86ddaSAndreas Gohr        }else{
17087c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
17187c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
17287c86ddaSAndreas Gohr        }
1731ef68647SAndreas Gohr        exit();
1741ef68647SAndreas Gohr    }
1751ef68647SAndreas Gohr
176d01d2a42SAndreas Gohr
177d01d2a42SAndreas Gohr    /**
1782eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
1791ef68647SAndreas Gohr     */
1801c14c879SAndreas Gohr    protected function load_template($title){
1811ef68647SAndreas Gohr        global $ID;
1821ef68647SAndreas Gohr        global $REV;
1831ef68647SAndreas Gohr        global $conf;
1841c14c879SAndreas Gohr        $tpl = $this->tpl;
1851ef68647SAndreas Gohr
1862eedf77dSAndreas Gohr        // this is what we'll return
1872eedf77dSAndreas Gohr        $output = array(
1882eedf77dSAndreas Gohr            'html'  => '',
1892eedf77dSAndreas Gohr            'css'   => '',
1902eedf77dSAndreas Gohr            'page'  => '',
1912eedf77dSAndreas Gohr            'first' => '',
1922eedf77dSAndreas Gohr            'cite'  => '',
1932eedf77dSAndreas Gohr        );
1942eedf77dSAndreas Gohr
1952eedf77dSAndreas Gohr        // prepare header/footer elements
1962eedf77dSAndreas Gohr        $html = '';
1972eedf77dSAndreas Gohr        foreach(array('header','footer') as $t){
1989e6e41f4SAndreas Gohr            foreach(array('','_odd','_even','_first') as $h){
1992eedf77dSAndreas Gohr                if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
2002eedf77dSAndreas Gohr                    $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
2012eedf77dSAndreas Gohr                    $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
2022eedf77dSAndreas Gohr                    $html .= '</htmlpage'.$t.'>'.DOKU_LF;
2032eedf77dSAndreas Gohr
2042eedf77dSAndreas Gohr                    // register the needed pseudo CSS
2059e6e41f4SAndreas Gohr                    if($h == '_first'){
2062eedf77dSAndreas Gohr                        $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
2072eedf77dSAndreas Gohr                    }elseif($h == '_even'){
2082eedf77dSAndreas Gohr                        $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
209daa70883SAndreas Gohr                    }elseif($h == '_odd'){
2102eedf77dSAndreas Gohr                        $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
211daa70883SAndreas Gohr                    }else{
212daa70883SAndreas Gohr                        $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
2132eedf77dSAndreas Gohr                    }
2142eedf77dSAndreas Gohr                }
2152eedf77dSAndreas Gohr            }
2162eedf77dSAndreas Gohr        }
2172eedf77dSAndreas Gohr
218eb7e2fbeSJohannes Fürwentsches        // generate qr code for this page using google infographics api
219eb7e2fbeSJohannes Fürwentsches        $qr_code = '';
2205a91534fSAndreas Gohr        if ($this->getConf('qrcodesize')) {
221eb7e2fbeSJohannes Fürwentsches            $url = urlencode(wl($ID,'','&',true));
2225a91534fSAndreas Gohr            $qr_code = '<img src="https://chart.googleapis.com/chart?chs='.
2235a91534fSAndreas Gohr                       $this->getConf('qrcodesize').'&cht=qr&chl='.$url.'" />';
224eb7e2fbeSJohannes Fürwentsches        }
225d9ff9cfaSJohannes Fürwentsches
2261ef68647SAndreas Gohr        // prepare replacements
2271ef68647SAndreas Gohr        $replace = array(
2281ef68647SAndreas Gohr                '@ID@'      => $ID,
2291ef68647SAndreas Gohr                '@PAGE@'    => '{PAGENO}',
2301ef68647SAndreas Gohr                '@PAGES@'   => '{nb}',
2312eedf77dSAndreas Gohr                '@TITLE@'   => hsc($title),
2321ef68647SAndreas Gohr                '@WIKI@'    => $conf['title'],
2331ef68647SAndreas Gohr                '@WIKIURL@' => DOKU_URL,
2343ae7a8dfSAndreas Gohr                '@UPDATE@'  => dformat(filemtime(wikiFN($ID,$REV))),
2353ae7a8dfSAndreas Gohr                '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"),
2361ef68647SAndreas Gohr                '@DATE@'    => dformat(time()),
2375d6fbaeaSAndreas Gohr                '@BASE@'    => DOKU_BASE,
2385aa96d34SAndreas Gohr                '@TPLBASE@' => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$tpl.'/',
239d9ff9cfaSJohannes Fürwentsches                '@QRCODE@'  => $qr_code,
2401ef68647SAndreas Gohr        );
2411ef68647SAndreas Gohr
2422eedf77dSAndreas Gohr        // set HTML element
2432eedf77dSAndreas Gohr        $output['html'] = str_replace(array_keys($replace), array_values($replace), $html);
2441ef68647SAndreas Gohr
2452eedf77dSAndreas Gohr        // citation box
2462eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
2472eedf77dSAndreas Gohr            $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
2482eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
2492eedf77dSAndreas Gohr        }
2501ef68647SAndreas Gohr
2512eedf77dSAndreas Gohr        // set custom styles
2522eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css')){
2532eedf77dSAndreas Gohr            $output['css'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css');
2542eedf77dSAndreas Gohr        }
2552eedf77dSAndreas Gohr
2562eedf77dSAndreas Gohr        return $output;
2571ef68647SAndreas Gohr    }
2581ef68647SAndreas Gohr
2591ef68647SAndreas Gohr    /**
2601c14c879SAndreas Gohr     * Load all the style sheets and apply the needed replacements
2611ef68647SAndreas Gohr     */
2621c14c879SAndreas Gohr    protected function load_css(){
263*737417c6SKlap-in        global $conf;
2641c14c879SAndreas Gohr        //reusue the CSS dispatcher functions without triggering the main function
2651c14c879SAndreas Gohr        define('SIMPLE_TEST',1);
2661c14c879SAndreas Gohr        require_once(DOKU_INC.'lib/exe/css.php');
267ee19bac3SLuigi Micco
2681c14c879SAndreas Gohr        // prepare CSS files
2691c14c879SAndreas Gohr        $files = array_merge(
2701c14c879SAndreas Gohr                    array(
2711c14c879SAndreas Gohr                        DOKU_INC.'lib/styles/screen.css'
2721c14c879SAndreas Gohr                            => DOKU_BASE.'lib/styles/',
2731c14c879SAndreas Gohr                        DOKU_INC.'lib/styles/print.css'
2741c14c879SAndreas Gohr                            => DOKU_BASE.'lib/styles/',
2751c14c879SAndreas Gohr                    ),
2761c14c879SAndreas Gohr                    css_pluginstyles('all'),
27758e6409eSAndreas Gohr                    $this->css_pluginPDFstyles(),
2781c14c879SAndreas Gohr                    array(
2791c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/conf/style.css'
2801c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
2811c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css'
2821c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/',
2831c14c879SAndreas Gohr                        DOKU_PLUGIN.'dw2pdf/conf/style.local.css'
2841c14c879SAndreas Gohr                            => DOKU_BASE.'lib/plugins/dw2pdf/conf/',
2851c14c879SAndreas Gohr                    )
2861c14c879SAndreas Gohr                 );
2871c14c879SAndreas Gohr        $css = '';
2881c14c879SAndreas Gohr        foreach($files as $file => $location){
2891c14c879SAndreas Gohr            $css .= css_loadfile($file, $location);
2901ef68647SAndreas Gohr        }
2911ef68647SAndreas Gohr
2921c14c879SAndreas Gohr        // apply pattern replacements
2931c14c879SAndreas Gohr        $css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/');
2941ef68647SAndreas Gohr
2951c14c879SAndreas Gohr        return $css;
296ee19bac3SLuigi Micco    }
2971c14c879SAndreas Gohr
29858e6409eSAndreas Gohr
29958e6409eSAndreas Gohr    /**
30058e6409eSAndreas Gohr     * Returns a list of possible Plugin PDF Styles
30158e6409eSAndreas Gohr     *
30258e6409eSAndreas Gohr     * Checks for a pdf.css, falls back to print.css
30358e6409eSAndreas Gohr     *
30458e6409eSAndreas Gohr     * @author Andreas Gohr <andi@splitbrain.org>
30558e6409eSAndreas Gohr     */
30658e6409eSAndreas Gohr    function css_pluginPDFstyles(){
30758e6409eSAndreas Gohr        $list = array();
30858e6409eSAndreas Gohr        $plugins = plugin_list();
309f54b51f7SAndreas Gohr
310f54b51f7SAndreas Gohr        $usestyle = explode(',',$this->getConf('usestyles'));
31158e6409eSAndreas Gohr        foreach ($plugins as $p){
312f54b51f7SAndreas Gohr            if(in_array($p,$usestyle)){
313f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/";
314f54b51f7SAndreas Gohr                $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/";
315f54b51f7SAndreas Gohr            }
316f54b51f7SAndreas Gohr
31758e6409eSAndreas Gohr            if(file_exists(DOKU_PLUGIN."$p/pdf.css")){
31858e6409eSAndreas Gohr                $list[DOKU_PLUGIN."$p/pdf.css"] = DOKU_BASE."lib/plugins/$p/";
31958e6409eSAndreas Gohr            }else{
32058e6409eSAndreas Gohr                $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/";
32158e6409eSAndreas Gohr            }
32258e6409eSAndreas Gohr        }
32358e6409eSAndreas Gohr        return $list;
32458e6409eSAndreas Gohr    }
32558e6409eSAndreas Gohr
326ee19bac3SLuigi Micco}
327