xref: /plugin/dw2pdf/action.php (revision cde5a1b34dcb92562dc23bfddd21e50150d05e67)
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>
71ef68647SAndreas Gohr  * @author     Andreas Gohr <gohr@cosmocode.de>
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
381ef68647SAndreas Gohr        // initialize PDF library
39*cde5a1b3SAndreas Gohr        require_once(dirname(__FILE__)."/DokuPDF.class.php");
401ef68647SAndreas Gohr        $mpdf = new DokuPDF();
41ee19bac3SLuigi Micco
421ef68647SAndreas Gohr        // some default settings
43ee19bac3SLuigi Micco        $mpdf->mirrorMargins          = 1;  // Use different Odd/Even headers and footers and mirror margins
441ef68647SAndreas Gohr        $mpdf->defaultheaderfontsize  = 8;  // in pts
451ef68647SAndreas Gohr        $mpdf->defaultheaderfontstyle = ''; // blank, B, I, or BI
461ef68647SAndreas Gohr        $mpdf->defaultheaderline      = 1;  // 1 to include line below header/above footer
471ef68647SAndreas Gohr        $mpdf->defaultfooterfontsize  = 8;  // in pts
481ef68647SAndreas Gohr        $mpdf->defaultfooterfontstyle = ''; // blank, B, I, or BI
491ef68647SAndreas Gohr        $mpdf->defaultfooterline      = 1;  // 1 to include line below header/above footer
50ee19bac3SLuigi Micco
511ef68647SAndreas Gohr        // prepare HTML header styles
52ee19bac3SLuigi Micco        $html  = '<html><head>';
531ef68647SAndreas Gohr        $html .= '<style>';
541ef68647SAndreas Gohr        $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css');
551ef68647SAndreas Gohr        $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css');
561ef68647SAndreas Gohr        $html .= '</style>';
571ef68647SAndreas Gohr        $html .= '</head><body>';
58ee19bac3SLuigi Micco
591ef68647SAndreas Gohr        // set headers/footers
601ef68647SAndreas Gohr        $this->prepare_headers($mpdf);
61ee19bac3SLuigi Micco
621ef68647SAndreas Gohr        // one or multiple pages?
63ee19bac3SLuigi Micco        $list = array();
641ef68647SAndreas Gohr        if ( $ACT == 'export_pdf' ) {
65ee19bac3SLuigi Micco            $list[0] = $ID;
661ef68647SAndreas Gohr        } elseif (isset($_COOKIE['list-pagelist'])) {
67ee19bac3SLuigi Micco            $list = explode("|", $_COOKIE['list-pagelist']);
68ee19bac3SLuigi Micco        }
69ee19bac3SLuigi Micco
701ef68647SAndreas Gohr        // loop over all pages
711ef68647SAndreas Gohr        $cnt = count($list);
721ef68647SAndreas Gohr        for($n=0; $n<$cnt; $n++){
73ee19bac3SLuigi Micco            $page = $list[$n];
74ee19bac3SLuigi Micco
751ef68647SAndreas Gohr            $html .= p_wiki_xhtml($page,$REV,false);
761ef68647SAndreas Gohr            if($this->getConf('addcitation')){
771ef68647SAndreas Gohr                $html .= $this->citation($page);
78ee19bac3SLuigi Micco            }
791ef68647SAndreas Gohr            if ($n < ($cnt - 1)){
801ef68647SAndreas Gohr                $html .= '<pagebreak />';
811ef68647SAndreas Gohr            }
82ee19bac3SLuigi Micco        }
83ee19bac3SLuigi Micco
841ef68647SAndreas Gohr        $this->arrangeHtml($html, $this->getConf("norender"));
85ee19bac3SLuigi Micco
86ee19bac3SLuigi Micco        $mpdf->WriteHTML($html);
87ee19bac3SLuigi Micco
88*cde5a1b3SAndreas Gohr        $title = $_GET['pdfbook_title'];
89*cde5a1b3SAndreas Gohr        if(!$title) $title = noNS($ID);
90ee19bac3SLuigi Micco        $output = 'I';
91ee19bac3SLuigi Micco        if($this->getConf('output') == 'file') $output = 'D';
92*cde5a1b3SAndreas Gohr        $mpdf->Output(urlencode($title).'.pdf', $output);
93ee19bac3SLuigi Micco
941ef68647SAndreas Gohr        exit();
951ef68647SAndreas Gohr    }
961ef68647SAndreas Gohr
971ef68647SAndreas Gohr    /**
981ef68647SAndreas Gohr     * Setup the page headers and footers
991ef68647SAndreas Gohr     */
1001ef68647SAndreas Gohr    protected function prepare_headers(&$mpdf){
1011ef68647SAndreas Gohr        global $ID;
1021ef68647SAndreas Gohr        global $REV;
1031ef68647SAndreas Gohr        global $conf;
1041ef68647SAndreas Gohr
1051ef68647SAndreas Gohr        if($_GET['pdfbook_title']){
1061ef68647SAndreas Gohr            $title = $_GET['pdfbook_title'];
1071ef68647SAndreas Gohr        }else{
1081ef68647SAndreas Gohr            $title = p_get_first_heading($ID);
1091ef68647SAndreas Gohr        }
1101ef68647SAndreas Gohr        if(!$title) $title = noNS($ID);
1111ef68647SAndreas Gohr
1121ef68647SAndreas Gohr        $iddata = p_get_metadata($ID,'date');
1131ef68647SAndreas Gohr
1141ef68647SAndreas Gohr        // prepare replacements
1151ef68647SAndreas Gohr        $replace = array(
1161ef68647SAndreas Gohr                '@ID@'      => $ID,
1171ef68647SAndreas Gohr                '@PAGE@'    => '{PAGENO}',
1181ef68647SAndreas Gohr                '@PAGES@'   => '{nb}',
1191ef68647SAndreas Gohr                '@TITLE@'   => $title,
1201ef68647SAndreas Gohr                '@WIKI@'    => $conf['title'],
1211ef68647SAndreas Gohr                '@WIKIURL@' => DOKU_URL,
1221ef68647SAndreas Gohr                '@UPDATE@'  => dformat($iddata['modified']),
1231ef68647SAndreas Gohr                '@PAGEURL@' => wl($ID,array('rev'=>$REV), true, "&"),
1241ef68647SAndreas Gohr                '@DATE@'    => dformat(time()),
1251ef68647SAndreas Gohr        );
1261ef68647SAndreas Gohr
1271ef68647SAndreas Gohr        // do the replacements
1281ef68647SAndreas Gohr        $fo = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd"));
1291ef68647SAndreas Gohr        $fe = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even"));
1301ef68647SAndreas Gohr        $ho = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd"));
1311ef68647SAndreas Gohr        $he = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even"));
1321ef68647SAndreas Gohr
1331ef68647SAndreas Gohr        // set the headers/footers
1341ef68647SAndreas Gohr        $mpdf->SetHeader($ho);
1351ef68647SAndreas Gohr        $mpdf->SetHeader($he, 'E');
1361ef68647SAndreas Gohr        $mpdf->SetFooter($fo);
1371ef68647SAndreas Gohr        $mpdf->SetFooter($fe, 'E');
1381ef68647SAndreas Gohr
1391ef68647SAndreas Gohr        // title
1401ef68647SAndreas Gohr        $mpdf->SetTitle($title);
1411ef68647SAndreas Gohr    }
1421ef68647SAndreas Gohr
1431ef68647SAndreas Gohr    /**
1441ef68647SAndreas Gohr     * Fix up the HTML a bit
1451ef68647SAndreas Gohr     *
1461ef68647SAndreas Gohr     * FIXME This is far from perfect and will modify things within code and
1471ef68647SAndreas Gohr     * nowiki blocks. It would probably be a good idea to use a real HTML
1481ef68647SAndreas Gohr     * parser or our own renderer instead of modifying the HTML at all.
1491ef68647SAndreas Gohr     */
1501ef68647SAndreas Gohr    protected function arrangeHtml(&$html, $norendertags = '' ) {
1511ef68647SAndreas Gohr        // add bookmark links
1521ef68647SAndreas Gohr        $bmlevel = $this->getConf('maxbookmarks');
1531ef68647SAndreas Gohr        if($bmlevel > 0) {
1541ef68647SAndreas Gohr            $html = preg_replace("/\<a name=(.+?)\>(.+?)\<\/a\>/s",'$2',$html);
1551ef68647SAndreas Gohr            for ($j = 1; $j<=$bmlevel; $j++) {
1561ef68647SAndreas Gohr                $html = preg_replace("/\<h".$j."\>(.+?)\<\/h".$j."\>/s",'<h'.$j.'>$1<bookmark content="$1" level="'.($j-1).'"/></h'.$j.'>',$html);
157ee19bac3SLuigi Micco            }
158ee19bac3SLuigi Micco        }
159ee19bac3SLuigi Micco
1601ef68647SAndreas Gohr        // insert a pagebreak for support of WRAP and PAGEBREAK plugins
1611ef68647SAndreas Gohr        $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html);
1621ef68647SAndreas Gohr        $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html);
1631ef68647SAndreas Gohr        $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html);
1641ef68647SAndreas Gohr
1651ef68647SAndreas Gohr        // Customized to strip all span tags so that the wiki <code> SQL would display properly
1661ef68647SAndreas Gohr        $norender = explode(',',$this->getConf('norender'));
1671ef68647SAndreas Gohr        $this->strip_only($html, $norender);
168*cde5a1b3SAndreas Gohr        $this->strip_htmlencodedchars($html);
1691ef68647SAndreas Gohr
1701ef68647SAndreas Gohr        $html = str_replace('href="/','href="http://'.$_SERVER['HTTP_HOST'].'/',$html);
1711ef68647SAndreas Gohr
1721ef68647SAndreas Gohr    }
1731ef68647SAndreas Gohr
1741ef68647SAndreas Gohr    /**
1751ef68647SAndreas Gohr     * Create the citation box
1761ef68647SAndreas Gohr     *
1771ef68647SAndreas Gohr     * @todo can we drop the inline style here?
1781ef68647SAndreas Gohr     */
1791ef68647SAndreas Gohr    protected function citation($page) {
1801ef68647SAndreas Gohr        global $conf;
1811ef68647SAndreas Gohr
1821ef68647SAndreas Gohr        $date = filemtime(wikiFN($page));
1831ef68647SAndreas Gohr        $html  = '';
1841ef68647SAndreas Gohr        $html .= "<br><br><div style='font-size: 80%; border: solid 0.5mm #DDDDDD;background-color: #EEEEEE; padding: 2mm; border-radius: 2mm 2mm; width: 100%;'>";
1851ef68647SAndreas Gohr        $html .= "From:<br>";
1861ef68647SAndreas Gohr        $html .= "<a href='".DOKU_URL."'>".DOKU_URL."</a>&nbsp;-&nbsp;"."<b>".$conf['title']."</b>";
1871ef68647SAndreas Gohr        $html .= "<br><br>Permanent link:<br>";
1881ef68647SAndreas Gohr        $html .= "<b><a href='".wl($page, false, true, "&")."'>".wl($page, false, true, "&")."</a></b>";
1891ef68647SAndreas Gohr        $html .= "<br><br>Last update: <b>".dformat($date)."</b><br>";
1901ef68647SAndreas Gohr        $html .= "</div>";
1911ef68647SAndreas Gohr        return $html;
1921ef68647SAndreas Gohr    }
1931ef68647SAndreas Gohr
1941ef68647SAndreas Gohr    /**
1951ef68647SAndreas Gohr     * Strip unwanted tags
1961ef68647SAndreas Gohr     *
1971ef68647SAndreas Gohr     * @fixme could this be done by strip_tags?
1981ef68647SAndreas Gohr     * @author Jared Ong
1991ef68647SAndreas Gohr     */
2001ef68647SAndreas Gohr    protected function strip_only(&$str, $tags) {
201ee19bac3SLuigi Micco        if(!is_array($tags)) {
202ee19bac3SLuigi Micco            $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
203ee19bac3SLuigi Micco            if(end($tags) == '') array_pop($tags);
204ee19bac3SLuigi Micco        }
205ee19bac3SLuigi Micco        foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
206ee19bac3SLuigi Micco    }
207ee19bac3SLuigi Micco
2081ef68647SAndreas Gohr    /**
2091ef68647SAndreas Gohr     * Replace &#039; &quot; &gt; &lt; &amp;
2101ef68647SAndreas Gohr     *
2111ef68647SAndreas Gohr     * @fixme do we really need this? wouldn't this break things?
2121ef68647SAndreas Gohr     */
2131ef68647SAndreas Gohr    protected function strip_htmlencodedchars(&$str) {
214ee19bac3SLuigi Micco        $str = str_replace('&#039;', '\'', $str);
215ee19bac3SLuigi Micco        $str = str_replace('&quot;', '"', $str);
216ee19bac3SLuigi Micco        $str = str_replace('&gt;', '>', $str);
217ee19bac3SLuigi Micco        $str = str_replace('&lt;', '<', $str);
218ee19bac3SLuigi Micco        $str = str_replace('&amp;', '&', $str);
219ee19bac3SLuigi Micco    }
220ee19bac3SLuigi Micco}
221