xref: /plugin/dw2pdf/action.php (revision 56d13144c7c763e9f55fea637c47d95854871cc2)
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
4687c86ddaSAndreas Gohr        // prepare cache
4787c86ddaSAndreas Gohr        $cache = new cache(join(',',$list).$REV,'.dw2.pdf');
4887c86ddaSAndreas Gohr        $depends['files']   = array_map('wikiFN',$list);
4987c86ddaSAndreas Gohr        $depends['files'][] = __FILE__;
5087c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/renderer.php';
5187c86ddaSAndreas Gohr        $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php';
5287c86ddaSAndreas Gohr
5387c86ddaSAndreas Gohr
5487c86ddaSAndreas Gohr        if(!$this->getConf('usecache') || !$cache->useCache($depends)){
551ef68647SAndreas Gohr            // initialize PDF library
56cde5a1b3SAndreas Gohr            require_once(dirname(__FILE__)."/DokuPDF.class.php");
571ef68647SAndreas Gohr            $mpdf = new DokuPDF();
58ee19bac3SLuigi Micco
59d62df65bSAndreas Gohr            // let mpdf fix local links
60d62df65bSAndreas Gohr            $self = parse_url(DOKU_URL);
61d62df65bSAndreas Gohr            $url  = $self['scheme'].'://'.$self['host'];
62d62df65bSAndreas Gohr            if($self['port']) $url .= ':'.$port;
63d62df65bSAndreas Gohr            $mpdf->setBasePath($url);
64d62df65bSAndreas Gohr
65*56d13144SAndreas Gohr            // Set the title
66*56d13144SAndreas Gohr            $title = $_GET['pdfbook_title'];
67*56d13144SAndreas Gohr            if(!$title) $title = p_get_first_heading($ID);
68*56d13144SAndreas Gohr            $mpdf->SetTitle($title);
69*56d13144SAndreas Gohr
701ef68647SAndreas Gohr            // some default settings
71ee19bac3SLuigi Micco            $mpdf->mirrorMargins          = 1;  // Use different Odd/Even headers and footers and mirror margins
722eedf77dSAndreas Gohr            $mdpf->useOddEven = 1;
732eedf77dSAndreas Gohr
74*56d13144SAndreas Gohr            // load the template
75*56d13144SAndreas Gohr            $template = $this->load_template('default',$title); //FIXME
76ee19bac3SLuigi Micco
771ef68647SAndreas Gohr            // prepare HTML header styles
78ee19bac3SLuigi Micco            $html  = '<html><head>';
791ef68647SAndreas Gohr            $html .= '<style>';
8083dae0dbSAndreas Gohr            $html .= file_get_contents(DOKU_INC.'lib/styles/screen.css');
8183dae0dbSAndreas Gohr            $html .= file_get_contents(DOKU_INC.'lib/styles/print.css');
821ef68647SAndreas Gohr            $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css');
831ef68647SAndreas Gohr            $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css');
842eedf77dSAndreas Gohr            $html .= '@page {'.$template['page'].'}';
852eedf77dSAndreas Gohr            $html .= '@page :first {'.$template['first'].'}';
862eedf77dSAndreas Gohr            $html .= '@page :last {'.$template['last'].'}';
872eedf77dSAndreas Gohr            $html .= $template['css'];
881ef68647SAndreas Gohr            $html .= '</style>';
891ef68647SAndreas Gohr            $html .= '</head><body>';
902eedf77dSAndreas Gohr            $html .= $template['html'];
912eedf77dSAndreas Gohr
921ef68647SAndreas Gohr            // loop over all pages
931ef68647SAndreas Gohr            $cnt = count($list);
941ef68647SAndreas Gohr            for($n=0; $n<$cnt; $n++){
95ee19bac3SLuigi Micco                $page = $list[$n];
96ee19bac3SLuigi Micco
97a876b55bSAndreas Gohr                $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
982eedf77dSAndreas Gohr                $html .= $template['cite'];
991ef68647SAndreas Gohr                if ($n < ($cnt - 1)){
1001ef68647SAndreas Gohr                    $html .= '<pagebreak />';
1011ef68647SAndreas Gohr                }
102ee19bac3SLuigi Micco            }
103ee19bac3SLuigi Micco
1041ef68647SAndreas Gohr            $this->arrangeHtml($html, $this->getConf("norender"));
105ee19bac3SLuigi Micco            $mpdf->WriteHTML($html);
106a06728a6SAndreas Gohr
10787c86ddaSAndreas Gohr            // write to cache file
10887c86ddaSAndreas Gohr            $mpdf->Output($cache->cache, 'F');
10987c86ddaSAndreas Gohr        }
11087c86ddaSAndreas Gohr
11187c86ddaSAndreas Gohr        // deliver the file
11287c86ddaSAndreas Gohr        header('Content-Type: application/pdf');
11387c86ddaSAndreas Gohr        header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
11487c86ddaSAndreas Gohr        header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
11587c86ddaSAndreas Gohr        header('Pragma: public');
11687c86ddaSAndreas Gohr        http_conditionalRequest(filemtime($cache->cache));
11787c86ddaSAndreas Gohr
11887c86ddaSAndreas Gohr        if($this->getConf('output') == 'file'){
119*56d13144SAndreas Gohr            header('Content-Disposition: attachment; filename="'.rawurlencode($title).'.pdf";');
12087c86ddaSAndreas Gohr        }else{
121*56d13144SAndreas Gohr            header('Content-Disposition: inline; filename="'.rawurlencode($title).'.pdf";');
12287c86ddaSAndreas Gohr        }
123ee19bac3SLuigi Micco
12487c86ddaSAndreas Gohr        if (http_sendfile($cache->cache)) exit;
12587c86ddaSAndreas Gohr
12687c86ddaSAndreas Gohr        $fp = @fopen($cache->cache,"rb");
12787c86ddaSAndreas Gohr        if($fp){
12887c86ddaSAndreas Gohr            http_rangeRequest($fp,filesize($cache->cache),'application/pdf');
12987c86ddaSAndreas Gohr        }else{
13087c86ddaSAndreas Gohr            header("HTTP/1.0 500 Internal Server Error");
13187c86ddaSAndreas Gohr            print "Could not read file - bad permissions?";
13287c86ddaSAndreas Gohr        }
1331ef68647SAndreas Gohr        exit();
1341ef68647SAndreas Gohr    }
1351ef68647SAndreas Gohr
1361ef68647SAndreas Gohr    /**
1372eedf77dSAndreas Gohr     * Load the various template files and prepare the HTML/CSS for insertion
1381ef68647SAndreas Gohr     */
139*56d13144SAndreas Gohr    protected function load_template($tpl,$title){
1401ef68647SAndreas Gohr        global $ID;
1411ef68647SAndreas Gohr        global $REV;
1421ef68647SAndreas Gohr        global $conf;
1431ef68647SAndreas Gohr
1442eedf77dSAndreas Gohr        // this is what we'll return
1452eedf77dSAndreas Gohr        $output = array(
1462eedf77dSAndreas Gohr            'html'  => '',
1472eedf77dSAndreas Gohr            'css'   => '',
1482eedf77dSAndreas Gohr            'page'  => '',
1492eedf77dSAndreas Gohr            'first' => '',
1502eedf77dSAndreas Gohr            'last'  => '',
1512eedf77dSAndreas Gohr            'cite'  => '',
1522eedf77dSAndreas Gohr        );
1532eedf77dSAndreas Gohr
1542eedf77dSAndreas Gohr        // prepare header/footer elements
1552eedf77dSAndreas Gohr        $html = '';
1562eedf77dSAndreas Gohr        foreach(array('header','footer') as $t){
1572eedf77dSAndreas Gohr            foreach(array('','_odd','_even','_first','_last') as $h){
1582eedf77dSAndreas Gohr                if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){
1592eedf77dSAndreas Gohr                    $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF;
1602eedf77dSAndreas Gohr                    $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF;
1612eedf77dSAndreas Gohr                    $html .= '</htmlpage'.$t.'>'.DOKU_LF;
1622eedf77dSAndreas Gohr
1632eedf77dSAndreas Gohr                    // register the needed pseudo CSS
1642eedf77dSAndreas Gohr                    if($h == '_last'){
1652eedf77dSAndreas Gohr                        $output['last'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
1662eedf77dSAndreas Gohr                    }elseif($h == '_first'){
1672eedf77dSAndreas Gohr                        $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF;
1682eedf77dSAndreas Gohr                    }elseif($h == '_even'){
1692eedf77dSAndreas Gohr                        $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
1702eedf77dSAndreas Gohr                    }else{
1712eedf77dSAndreas Gohr                        $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF;
1722eedf77dSAndreas Gohr                    }
1732eedf77dSAndreas Gohr                }
1742eedf77dSAndreas Gohr            }
1752eedf77dSAndreas Gohr        }
1762eedf77dSAndreas Gohr
1771ef68647SAndreas Gohr        // prepare replacements
1781ef68647SAndreas Gohr        $replace = array(
1791ef68647SAndreas Gohr                '@ID@'      => $ID,
1801ef68647SAndreas Gohr                '@PAGE@'    => '{PAGENO}',
1811ef68647SAndreas Gohr                '@PAGES@'   => '{nb}',
1822eedf77dSAndreas Gohr                '@TITLE@'   => hsc($title),
1831ef68647SAndreas Gohr                '@WIKI@'    => $conf['title'],
1841ef68647SAndreas Gohr                '@WIKIURL@' => DOKU_URL,
1853ae7a8dfSAndreas Gohr                '@UPDATE@'  => dformat(filemtime(wikiFN($ID,$REV))),
1863ae7a8dfSAndreas Gohr                '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"),
1871ef68647SAndreas Gohr                '@DATE@'    => dformat(time()),
1881ef68647SAndreas Gohr        );
1891ef68647SAndreas Gohr
1902eedf77dSAndreas Gohr        // set HTML element
1912eedf77dSAndreas Gohr        $output['html'] = str_replace(array_keys($replace), array_values($replace), $html);
1921ef68647SAndreas Gohr
1932eedf77dSAndreas Gohr        // citation box
1942eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){
1952eedf77dSAndreas Gohr            $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html');
1962eedf77dSAndreas Gohr            $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']);
1972eedf77dSAndreas Gohr        }
1981ef68647SAndreas Gohr
1992eedf77dSAndreas Gohr        // set custom styles
2002eedf77dSAndreas Gohr        if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css')){
2012eedf77dSAndreas Gohr            $output['css'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css');
2022eedf77dSAndreas Gohr        }
2032eedf77dSAndreas Gohr
2042eedf77dSAndreas Gohr        return $output;
2051ef68647SAndreas Gohr    }
2061ef68647SAndreas Gohr
2071ef68647SAndreas Gohr    /**
2081ef68647SAndreas Gohr     * Fix up the HTML a bit
2091ef68647SAndreas Gohr     *
210a876b55bSAndreas Gohr     * FIXME This is far from perfect and most of it should be moved to
211a876b55bSAndreas Gohr     * our own renderer instead of modifying the HTML at all.
2121ef68647SAndreas Gohr     */
2131ef68647SAndreas Gohr    protected function arrangeHtml(&$html, $norendertags = '' ) {
214ee19bac3SLuigi Micco
2151ef68647SAndreas Gohr        // insert a pagebreak for support of WRAP and PAGEBREAK plugins
2161ef68647SAndreas Gohr        $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html);
2171ef68647SAndreas Gohr        $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html);
2181ef68647SAndreas Gohr        $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html);
2191ef68647SAndreas Gohr
2201ef68647SAndreas Gohr    }
2211ef68647SAndreas Gohr
2221ef68647SAndreas Gohr
2231ef68647SAndreas Gohr    /**
2241ef68647SAndreas Gohr     * Strip unwanted tags
2251ef68647SAndreas Gohr     *
2261ef68647SAndreas Gohr     * @fixme could this be done by strip_tags?
2271ef68647SAndreas Gohr     * @author Jared Ong
2281ef68647SAndreas Gohr     */
2291ef68647SAndreas Gohr    protected function strip_only(&$str, $tags) {
230ee19bac3SLuigi Micco        if(!is_array($tags)) {
231ee19bac3SLuigi Micco            $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
232ee19bac3SLuigi Micco            if(end($tags) == '') array_pop($tags);
233ee19bac3SLuigi Micco        }
234ee19bac3SLuigi Micco        foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
235ee19bac3SLuigi Micco    }
236ee19bac3SLuigi Micco}
237