xref: /plugin/dw2pdf/action.php (revision daa7088361b7db1e21f6b03e34745e651eb26578)
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
16ee19bac3SLuigi Micco    /**
17ee19bac3SLuigi Micco     * Register the events
18ee19bac3SLuigi Micco     */
191ef68647SAndreas Gohr    function register(&$controller) {
20ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array());
21ee19bac3SLuigi Micco    }
22ee19bac3SLuigi Micco
231ef68647SAndreas Gohr    function convert(&$event, $param) {
24ee19bac3SLuigi Micco        global $ACT;
25ee19bac3SLuigi Micco        global $REV;
26ee19bac3SLuigi Micco        global $ID;
27ee19bac3SLuigi Micco        global $conf;
28ee19bac3SLuigi Micco
291ef68647SAndreas Gohr        // our event?
301ef68647SAndreas Gohr        if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false;
31ee19bac3SLuigi Micco
321ef68647SAndreas Gohr        // check user's rights
331ef68647SAndreas Gohr        if ( auth_quickaclcheck($ID) < AUTH_READ ) return false;
341ef68647SAndreas Gohr
351ef68647SAndreas Gohr        // it's ours, no one else's
36ee19bac3SLuigi Micco        $event->preventDefault();
37ee19bac3SLuigi Micco
3887c86ddaSAndreas Gohr        // one or multiple pages?
3987c86ddaSAndreas Gohr        $list = array();
4087c86ddaSAndreas Gohr        if ( $ACT == 'export_pdf' ) {
4187c86ddaSAndreas Gohr            $list[0] = $ID;
4287c86ddaSAndreas Gohr        } elseif (isset($_COOKIE['list-pagelist'])) {
4387c86ddaSAndreas Gohr            $list = explode("|", $_COOKIE['list-pagelist']);
4487c86ddaSAndreas Gohr        }
4587c86ddaSAndreas Gohr
46eee88cbeSAndreas Gohr        $tpl = $this->select_template();
47eee88cbeSAndreas Gohr
4887c86ddaSAndreas Gohr        // prepare cache
49eee88cbeSAndreas Gohr        $cache = new cache(join(',',$list).$REV.$tpl,'.dw2.pdf');
5087c86ddaSAndreas Gohr        $depends['files']   = array_map('wikiFN',$list);
5187c86ddaSAndreas Gohr        $depends['files'][] = __FILE__;
5287c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/renderer.php';
5387c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php';
5487c86ddaSAndreas Gohr
55d01d2a42SAndreas Gohr        // hard work only when no cache available
5687c86ddaSAndreas Gohr        if(!$this->getConf('usecache') || !$cache->useCache($depends)){
571ef68647SAndreas Gohr            // initialize PDF library
58cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__)."/DokuPDF.class.php");
591ef68647SAndreas Gohr            $mpdf = new DokuPDF();
60ee19bac3SLuigi Micco
61d62df65bSAndreas Gohr            // let mpdf fix local links
62d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
63d62df65bSAndreas Gohr            $url  = $self['scheme'].'://'.$self['host'];
64d62df65bSAndreas Gohr            if($self['port']) $url .= ':'.$port;
65d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
66d62df65bSAndreas Gohr
6756d13144SAndreas Gohr            // Set the title
6856d13144SAndreas Gohr            $title = $_GET['pdfbook_title'];
6956d13144SAndreas Gohr            if(!$title) $title = p_get_first_heading($ID);
7056d13144SAndreas Gohr            $mpdf->SetTitle($title);
7156d13144SAndreas Gohr
721ef68647SAndreas Gohr            // some default settings
73*daa70883SAndreas Gohr            $mpdf->mirrorMargins = 1;
74*daa70883SAndreas Gohr            $mpdf->useOddEven    = 1;
75*daa70883SAndreas Gohr            $mpdf->setAutoTopMargin = 'stretch';
76*daa70883SAndreas Gohr            $mpdf->setAutoBottomMargin = 'stretch';
772eedf77dSAndreas Gohr
7856d13144SAndreas Gohr            // load the template
79eee88cbeSAndreas Gohr            $template = $this->load_template($tpl, $title);
80ee19bac3SLuigi Micco
811ef68647SAndreas Gohr            // prepare HTML header styles
82ee19bac3SLuigi Micco            $html  = '<html><head>';
831ef68647SAndreas Gohr            $html .= '<style>';
8483dae0dbSAndreas Gohr            $html .= file_get_contents(DOKU_INC.'lib/styles/screen.css');
8583dae0dbSAndreas Gohr            $html .= file_get_contents(DOKU_INC.'lib/styles/print.css');
861ef68647SAndreas Gohr            $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css');
871ef68647SAndreas Gohr            $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css');
88*daa70883SAndreas Gohr            $html .= '@page { size:auto; '.$template['page'].'}';
892eedf77dSAndreas Gohr            $html .= '@page :first {'.$template['first'].'}';
902eedf77dSAndreas Gohr            $html .= '@page :last {'.$template['last'].'}';
912eedf77dSAndreas Gohr            $html .= $template['css'];
921ef68647SAndreas Gohr            $html .= '</style>';
931ef68647SAndreas Gohr            $html .= '</head><body>';
942eedf77dSAndreas Gohr            $html .= $template['html'];
952eedf77dSAndreas Gohr
961ef68647SAndreas Gohr            // loop over all pages
971ef68647SAndreas Gohr            $cnt = count($list);
981ef68647SAndreas Gohr            for($n=0; $n<$cnt; $n++){
99ee19bac3SLuigi Micco                $page = $list[$n];
100ee19bac3SLuigi Micco
101a876b55bSAndreas Gohr                $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
1022eedf77dSAndreas Gohr                $html .= $template['cite'];
1031ef68647SAndreas Gohr                if ($n < ($cnt - 1)){
1041ef68647SAndreas Gohr                    $html .= '<pagebreak />';
1051ef68647SAndreas Gohr                }
106ee19bac3SLuigi Micco            }
107ee19bac3SLuigi Micco
1081ef68647SAndreas Gohr            $this->arrangeHtml($html, $this->getConf("norender"));
109ee19bac3SLuigi Micco            $mpdf->WriteHTML($html);
110a06728a6SAndreas Gohr
11187c86ddaSAndreas Gohr            // write to cache file
11287c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
11387c86ddaSAndreas Gohr        }
11487c86ddaSAndreas Gohr
11587c86ddaSAndreas Gohr        // deliver the file
11687c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
11787c86ddaSAndreas Gohr        header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
11887c86ddaSAndreas Gohr        header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
11987c86ddaSAndreas Gohr        header('Pragma: public');
12087c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
12187c86ddaSAndreas Gohr
12287c86ddaSAndreas Gohr        if($this->getConf('output') == 'file'){
12356d13144SAndreas Gohr            header('Content-Disposition: attachment; filename="'.rawurlencode($title).'.pdf";');
12487c86ddaSAndreas Gohr        }else{
12556d13144SAndreas Gohr            header('Content-Disposition: inline; filename="'.rawurlencode($title).'.pdf";');
12687c86ddaSAndreas Gohr        }
127ee19bac3SLuigi Micco
12887c86ddaSAndreas Gohr        if (http_sendfile($cache->cache)) exit;
12987c86ddaSAndreas Gohr
13087c86ddaSAndreas Gohr        $fp = @fopen($cache->cache,"rb");
13187c86ddaSAndreas Gohr        if($fp){
13287c86ddaSAndreas Gohr            http_rangeRequest($fp,filesize($cache->cache),'application/pdf');
13387c86ddaSAndreas Gohr        }else{
13487c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
13587c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
13687c86ddaSAndreas Gohr        }
1371ef68647SAndreas Gohr        exit();
1381ef68647SAndreas Gohr    }
1391ef68647SAndreas Gohr
1401ef68647SAndreas Gohr    /**
141d01d2a42SAndreas Gohr     * Choose the correct template
142d01d2a42SAndreas Gohr     */
143d01d2a42SAndreas Gohr    protected function select_template(){
144d01d2a42SAndreas Gohr        $tpl;
145d01d2a42SAndreas Gohr        if(isset($_REQUEST['tpl'])){
146d01d2a42SAndreas Gohr            $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/','',$_REQUEST['tpl']));
147d01d2a42SAndreas Gohr        }
148d01d2a42SAndreas Gohr        if(!$tpl) $tpl = $this->getConf('template');
149d01d2a42SAndreas Gohr        if(!$tpl) $tpl = 'default';
150d01d2a42SAndreas Gohr        if(!is_dir(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl)) $tpl = 'default';
151d01d2a42SAndreas Gohr
152d01d2a42SAndreas Gohr        return $tpl;
153d01d2a42SAndreas Gohr    }
154d01d2a42SAndreas Gohr
155d01d2a42SAndreas Gohr    /**
1562eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
1571ef68647SAndreas Gohr     */
158eee88cbeSAndreas Gohr    protected function load_template($tpl, $title){
1591ef68647SAndreas Gohr        global $ID;
1601ef68647SAndreas Gohr        global $REV;
1611ef68647SAndreas Gohr        global $conf;
1621ef68647SAndreas Gohr
1632eedf77dSAndreas Gohr        // this is what we'll return
1642eedf77dSAndreas Gohr        $output = array(
1652eedf77dSAndreas Gohr            'html'  => '',
1662eedf77dSAndreas Gohr            'css'   => '',
1672eedf77dSAndreas Gohr            'page'  => '',
1682eedf77dSAndreas Gohr            'first' => '',
1692eedf77dSAndreas Gohr            'last'  => '',
1702eedf77dSAndreas Gohr            'cite'  => '',
171*daa70883SAndreas Gohr            'oe'    => 0
1722eedf77dSAndreas Gohr        );
1732eedf77dSAndreas Gohr
1742eedf77dSAndreas Gohr        // prepare header/footer elements
1752eedf77dSAndreas Gohr        $html = '';
1762eedf77dSAndreas Gohr        foreach(array('header','footer') as $t){
1772eedf77dSAndreas Gohr            foreach(array('','_odd','_even','_first','_last') as $h){
1782eedf77dSAndreas Gohr                if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
1792eedf77dSAndreas Gohr                    $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
1802eedf77dSAndreas Gohr                    $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
1812eedf77dSAndreas Gohr                    $html .= '</htmlpage'.$t.'>'.DOKU_LF;
1822eedf77dSAndreas Gohr
1832eedf77dSAndreas Gohr                    // register the needed pseudo CSS
1842eedf77dSAndreas Gohr                    if($h == '_last'){
1852eedf77dSAndreas Gohr                        $output['last'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
1862eedf77dSAndreas Gohr                    }elseif($h == '_first'){
1872eedf77dSAndreas Gohr                        $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
1882eedf77dSAndreas Gohr                    }elseif($h == '_even'){
1892eedf77dSAndreas Gohr                        $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
190*daa70883SAndreas Gohr                    }elseif($h == '_odd'){
1912eedf77dSAndreas Gohr                        $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
192*daa70883SAndreas Gohr                    }else{
193*daa70883SAndreas Gohr                        $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
1942eedf77dSAndreas Gohr                    }
1952eedf77dSAndreas Gohr                }
1962eedf77dSAndreas Gohr            }
1972eedf77dSAndreas Gohr        }
1982eedf77dSAndreas Gohr
1991ef68647SAndreas Gohr        // prepare replacements
2001ef68647SAndreas Gohr        $replace = array(
2011ef68647SAndreas Gohr                '@ID@'      => $ID,
2021ef68647SAndreas Gohr                '@PAGE@'    => '{PAGENO}',
2031ef68647SAndreas Gohr                '@PAGES@'   => '{nb}',
2042eedf77dSAndreas Gohr                '@TITLE@'   => hsc($title),
2051ef68647SAndreas Gohr                '@WIKI@'    => $conf['title'],
2061ef68647SAndreas Gohr                '@WIKIURL@' => DOKU_URL,
2073ae7a8dfSAndreas Gohr                '@UPDATE@'  => dformat(filemtime(wikiFN($ID,$REV))),
2083ae7a8dfSAndreas Gohr                '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"),
2091ef68647SAndreas Gohr                '@DATE@'    => dformat(time()),
2105d6fbaeaSAndreas Gohr                '@BASE@'    => DOKU_BASE,
2115d6fbaeaSAndreas Gohr                '@TPLBASE@' => DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/',
2121ef68647SAndreas Gohr        );
2131ef68647SAndreas Gohr
2142eedf77dSAndreas Gohr        // set HTML element
2152eedf77dSAndreas Gohr        $output['html'] = str_replace(array_keys($replace), array_values($replace), $html);
2161ef68647SAndreas Gohr
2172eedf77dSAndreas Gohr        // citation box
2182eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
2192eedf77dSAndreas Gohr            $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
2202eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
2212eedf77dSAndreas Gohr        }
2221ef68647SAndreas Gohr
2232eedf77dSAndreas Gohr        // set custom styles
2242eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css')){
2252eedf77dSAndreas Gohr            $output['css'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css');
2262eedf77dSAndreas Gohr        }
2272eedf77dSAndreas Gohr
2282eedf77dSAndreas Gohr        return $output;
2291ef68647SAndreas Gohr    }
2301ef68647SAndreas Gohr
2311ef68647SAndreas Gohr    /**
2321ef68647SAndreas Gohr     * Fix up the HTML a bit
2331ef68647SAndreas Gohr     *
234a876b55bSAndreas Gohr     * FIXME This is far from perfect and most of it should be moved to
235a876b55bSAndreas Gohr     * our own renderer instead of modifying the HTML at all.
2361ef68647SAndreas Gohr     */
2371ef68647SAndreas Gohr    protected function arrangeHtml(&$html, $norendertags = '' ) {
238ee19bac3SLuigi Micco
2391ef68647SAndreas Gohr        // insert a pagebreak for support of WRAP and PAGEBREAK plugins
2401ef68647SAndreas Gohr        $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html);
2411ef68647SAndreas Gohr        $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html);
2421ef68647SAndreas Gohr        $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html);
2431ef68647SAndreas Gohr
2441ef68647SAndreas Gohr    }
2451ef68647SAndreas Gohr
2461ef68647SAndreas Gohr
2471ef68647SAndreas Gohr    /**
2481ef68647SAndreas Gohr     * Strip unwanted tags
2491ef68647SAndreas Gohr     *
2501ef68647SAndreas Gohr     * @fixme could this be done by strip_tags?
2511ef68647SAndreas Gohr     * @author Jared Ong
2521ef68647SAndreas Gohr     */
2531ef68647SAndreas Gohr    protected function strip_only(&$str, $tags) {
254ee19bac3SLuigi Micco        if(!is_array($tags)) {
255ee19bac3SLuigi Micco            $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
256ee19bac3SLuigi Micco            if(end($tags) == '') array_pop($tags);
257ee19bac3SLuigi Micco        }
258ee19bac3SLuigi Micco        foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
259ee19bac3SLuigi Micco    }
260ee19bac3SLuigi Micco}
261