xref: /plugin/dw2pdf/action.php (revision 1ef68647990e8a746109cdd0e22aa1774399dc3c)
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>
7*1ef68647SAndreas 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
14ee19bac3SLuigi Miccorequire_once (DOKU_PLUGIN . 'action.php');
15ee19bac3SLuigi Micco
16*1ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin {
17ee19bac3SLuigi Micco
18ee19bac3SLuigi Micco    /**
19ee19bac3SLuigi Micco     * Register the events
20ee19bac3SLuigi Micco     */
21*1ef68647SAndreas Gohr    function register(&$controller) {
22ee19bac3SLuigi Micco        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array());
23ee19bac3SLuigi Micco    }
24ee19bac3SLuigi Micco
25*1ef68647SAndreas Gohr    function convert(&$event, $param) {
26ee19bac3SLuigi Micco        global $ACT;
27ee19bac3SLuigi Micco        global $REV;
28ee19bac3SLuigi Micco        global $ID;
29ee19bac3SLuigi Micco        global $conf;
30ee19bac3SLuigi Micco
31*1ef68647SAndreas Gohr        // our event?
32*1ef68647SAndreas Gohr        if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false;
33ee19bac3SLuigi Micco
34*1ef68647SAndreas Gohr        // check user's rights
35*1ef68647SAndreas Gohr        if ( auth_quickaclcheck($ID) < AUTH_READ ) return false;
36*1ef68647SAndreas Gohr
37*1ef68647SAndreas Gohr        // it's ours, no one else's
38ee19bac3SLuigi Micco        $event->preventDefault();
39ee19bac3SLuigi Micco
40*1ef68647SAndreas Gohr        // initialize PDF library
41*1ef68647SAndreas Gohr        require_once(dirname(__FILE__)."/DokuPDF.php");
42*1ef68647SAndreas Gohr        $mpdf = new DokuPDF();
43ee19bac3SLuigi Micco
44*1ef68647SAndreas Gohr        // some default settings
45ee19bac3SLuigi Micco        $mpdf->mirrorMargins          = 1;  // Use different Odd/Even headers and footers and mirror margins
46*1ef68647SAndreas Gohr        $mpdf->defaultheaderfontsize  = 8;  // in pts
47*1ef68647SAndreas Gohr        $mpdf->defaultheaderfontstyle = ''; // blank, B, I, or BI
48*1ef68647SAndreas Gohr        $mpdf->defaultheaderline      = 1;  // 1 to include line below header/above footer
49*1ef68647SAndreas Gohr        $mpdf->defaultfooterfontsize  = 8;  // in pts
50*1ef68647SAndreas Gohr        $mpdf->defaultfooterfontstyle = ''; // blank, B, I, or BI
51*1ef68647SAndreas Gohr        $mpdf->defaultfooterline      = 1;  // 1 to include line below header/above footer
52ee19bac3SLuigi Micco
53*1ef68647SAndreas Gohr        // prepare HTML header styles
54ee19bac3SLuigi Micco        $html  = '<html><head>';
55*1ef68647SAndreas Gohr        $html .= '<style>';
56*1ef68647SAndreas Gohr        $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css');
57*1ef68647SAndreas Gohr        $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css');
58*1ef68647SAndreas Gohr        $html .= '</style>';
59*1ef68647SAndreas Gohr        $html .= '</head><body>';
60ee19bac3SLuigi Micco
61*1ef68647SAndreas Gohr        // set headers/footers
62*1ef68647SAndreas Gohr        $this->prepare_headers($mpdf);
63ee19bac3SLuigi Micco
64*1ef68647SAndreas Gohr        // one or multiple pages?
65ee19bac3SLuigi Micco        $list = array();
66*1ef68647SAndreas Gohr        if ( $ACT == 'export_pdf' ) {
67ee19bac3SLuigi Micco            $list[0] = $ID;
68*1ef68647SAndreas Gohr        } elseif (isset($_COOKIE['list-pagelist'])) {
69ee19bac3SLuigi Micco            $list = explode("|", $_COOKIE['list-pagelist']);
70ee19bac3SLuigi Micco        }
71ee19bac3SLuigi Micco
72*1ef68647SAndreas Gohr        // loop over all pages
73*1ef68647SAndreas Gohr        $cnt = count($list);
74*1ef68647SAndreas Gohr        for($n=0; $n<$cnt; $n++){
75ee19bac3SLuigi Micco            $page = $list[$n];
76ee19bac3SLuigi Micco
77*1ef68647SAndreas Gohr            $html .= p_wiki_xhtml($page,$REV,false);
78*1ef68647SAndreas Gohr            if($this->getConf('addcitation')){
79*1ef68647SAndreas Gohr                $html .= $this->citation($page);
80ee19bac3SLuigi Micco            }
81*1ef68647SAndreas Gohr            if ($n < ($cnt - 1)){
82*1ef68647SAndreas Gohr                $html .= '<pagebreak />';
83*1ef68647SAndreas Gohr            }
84ee19bac3SLuigi Micco        }
85ee19bac3SLuigi Micco
86*1ef68647SAndreas Gohr        $this->arrangeHtml($html, $this->getConf("norender"));
87ee19bac3SLuigi Micco
88ee19bac3SLuigi Micco        $mpdf->WriteHTML($html);
89ee19bac3SLuigi Micco
90ee19bac3SLuigi Micco
91ee19bac3SLuigi Micco        $output = 'I';
92ee19bac3SLuigi Micco        if($this->getConf('output') == 'file') $output = 'D';
93ee19bac3SLuigi Micco        $mpdf->Output(urlencode($pdftitle).'.pdf', $output);
94ee19bac3SLuigi Micco
95*1ef68647SAndreas Gohr        exit();
96*1ef68647SAndreas Gohr    }
97*1ef68647SAndreas Gohr
98*1ef68647SAndreas Gohr    /**
99*1ef68647SAndreas Gohr     * Setup the page headers and footers
100*1ef68647SAndreas Gohr     */
101*1ef68647SAndreas Gohr    protected function prepare_headers(&$mpdf){
102*1ef68647SAndreas Gohr        global $ID;
103*1ef68647SAndreas Gohr        global $REV;
104*1ef68647SAndreas Gohr        global $conf;
105*1ef68647SAndreas Gohr
106*1ef68647SAndreas Gohr        if($_GET['pdfbook_title']){
107*1ef68647SAndreas Gohr            $title = $_GET['pdfbook_title'];
108*1ef68647SAndreas Gohr        }else{
109*1ef68647SAndreas Gohr            $title = p_get_first_heading($ID);
110*1ef68647SAndreas Gohr        }
111*1ef68647SAndreas Gohr        if(!$title) $title = noNS($ID);
112*1ef68647SAndreas Gohr
113*1ef68647SAndreas Gohr        $iddata = p_get_metadata($ID,'date');
114*1ef68647SAndreas Gohr
115*1ef68647SAndreas Gohr        // prepare replacements
116*1ef68647SAndreas Gohr        $replace = array(
117*1ef68647SAndreas Gohr                '@ID@'      => $ID,
118*1ef68647SAndreas Gohr                '@PAGE@'    => '{PAGENO}',
119*1ef68647SAndreas Gohr                '@PAGES@'   => '{nb}',
120*1ef68647SAndreas Gohr                '@TITLE@'   => $title,
121*1ef68647SAndreas Gohr                '@WIKI@'    => $conf['title'],
122*1ef68647SAndreas Gohr                '@WIKIURL@' => DOKU_URL,
123*1ef68647SAndreas Gohr                '@UPDATE@'  => dformat($iddata['modified']),
124*1ef68647SAndreas Gohr                '@PAGEURL@' => wl($ID,array('rev'=>$REV), true, "&"),
125*1ef68647SAndreas Gohr                '@DATE@'    => dformat(time()),
126*1ef68647SAndreas Gohr        );
127*1ef68647SAndreas Gohr
128*1ef68647SAndreas Gohr        // do the replacements
129*1ef68647SAndreas Gohr        $fo = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd"));
130*1ef68647SAndreas Gohr        $fe = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even"));
131*1ef68647SAndreas Gohr        $ho = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd"));
132*1ef68647SAndreas Gohr        $he = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even"));
133*1ef68647SAndreas Gohr
134*1ef68647SAndreas Gohr        // set the headers/footers
135*1ef68647SAndreas Gohr        $mpdf->SetHeader($ho);
136*1ef68647SAndreas Gohr        $mpdf->SetHeader($he, 'E');
137*1ef68647SAndreas Gohr        $mpdf->SetFooter($fo);
138*1ef68647SAndreas Gohr        $mpdf->SetFooter($fe, 'E');
139*1ef68647SAndreas Gohr
140*1ef68647SAndreas Gohr        // title
141*1ef68647SAndreas Gohr        $mpdf->SetTitle($title);
142*1ef68647SAndreas Gohr    }
143*1ef68647SAndreas Gohr
144*1ef68647SAndreas Gohr    /**
145*1ef68647SAndreas Gohr     * Fix up the HTML a bit
146*1ef68647SAndreas Gohr     *
147*1ef68647SAndreas Gohr     * FIXME This is far from perfect and will modify things within code and
148*1ef68647SAndreas Gohr     * nowiki blocks. It would probably be a good idea to use a real HTML
149*1ef68647SAndreas Gohr     * parser or our own renderer instead of modifying the HTML at all.
150*1ef68647SAndreas Gohr     */
151*1ef68647SAndreas Gohr    protected function arrangeHtml(&$html, $norendertags = '' ) {
152*1ef68647SAndreas Gohr        // add bookmark links
153*1ef68647SAndreas Gohr        $bmlevel = $this->getConf('maxbookmarks');
154*1ef68647SAndreas Gohr        if($bmlevel > 0) {
155*1ef68647SAndreas Gohr            $html = preg_replace("/\<a name=(.+?)\>(.+?)\<\/a\>/s",'$2',$html);
156*1ef68647SAndreas Gohr            for ($j = 1; $j<=$bmlevel; $j++) {
157*1ef68647SAndreas Gohr                $html = preg_replace("/\<h".$j."\>(.+?)\<\/h".$j."\>/s",'<h'.$j.'>$1<bookmark content="$1" level="'.($j-1).'"/></h'.$j.'>',$html);
158ee19bac3SLuigi Micco            }
159ee19bac3SLuigi Micco        }
160ee19bac3SLuigi Micco
161*1ef68647SAndreas Gohr        // insert a pagebreak for support of WRAP and PAGEBREAK plugins
162*1ef68647SAndreas Gohr        $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html);
163*1ef68647SAndreas Gohr        $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html);
164*1ef68647SAndreas Gohr        $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html);
165*1ef68647SAndreas Gohr
166*1ef68647SAndreas Gohr        // Customized to strip all span tags so that the wiki <code> SQL would display properly
167*1ef68647SAndreas Gohr        $norender = explode(',',$this->getConf('norender'));
168*1ef68647SAndreas Gohr        $this->strip_only($html, $norender);
169*1ef68647SAndreas Gohr#FIXME why??      $html = $this->strip_htmlencodedchars($html);
170*1ef68647SAndreas Gohr
171*1ef68647SAndreas Gohr        $html = str_replace('href="/','href="http://'.$_SERVER['HTTP_HOST'].'/',$html);
172*1ef68647SAndreas Gohr
173*1ef68647SAndreas Gohr    }
174*1ef68647SAndreas Gohr
175*1ef68647SAndreas Gohr    /**
176*1ef68647SAndreas Gohr     * Create the citation box
177*1ef68647SAndreas Gohr     *
178*1ef68647SAndreas Gohr     * @todo can we drop the inline style here?
179*1ef68647SAndreas Gohr     */
180*1ef68647SAndreas Gohr    protected function citation($page) {
181*1ef68647SAndreas Gohr        global $conf;
182*1ef68647SAndreas Gohr
183*1ef68647SAndreas Gohr        $date = filemtime(wikiFN($page));
184*1ef68647SAndreas Gohr        $html  = '';
185*1ef68647SAndreas 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%;'>";
186*1ef68647SAndreas Gohr        $html .= "From:<br>";
187*1ef68647SAndreas Gohr        $html .= "<a href='".DOKU_URL."'>".DOKU_URL."</a>&nbsp;-&nbsp;"."<b>".$conf['title']."</b>";
188*1ef68647SAndreas Gohr        $html .= "<br><br>Permanent link:<br>";
189*1ef68647SAndreas Gohr        $html .= "<b><a href='".wl($page, false, true, "&")."'>".wl($page, false, true, "&")."</a></b>";
190*1ef68647SAndreas Gohr        $html .= "<br><br>Last update: <b>".dformat($date)."</b><br>";
191*1ef68647SAndreas Gohr        $html .= "</div>";
192*1ef68647SAndreas Gohr        return $html;
193*1ef68647SAndreas Gohr    }
194*1ef68647SAndreas Gohr
195*1ef68647SAndreas Gohr    /**
196*1ef68647SAndreas Gohr     * Strip unwanted tags
197*1ef68647SAndreas Gohr     *
198*1ef68647SAndreas Gohr     * @fixme could this be done by strip_tags?
199*1ef68647SAndreas Gohr     * @author Jared Ong
200*1ef68647SAndreas Gohr     */
201*1ef68647SAndreas Gohr    protected function strip_only(&$str, $tags) {
202ee19bac3SLuigi Micco        if(!is_array($tags)) {
203ee19bac3SLuigi Micco            $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
204ee19bac3SLuigi Micco            if(end($tags) == '') array_pop($tags);
205ee19bac3SLuigi Micco        }
206ee19bac3SLuigi Micco        foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
207ee19bac3SLuigi Micco    }
208ee19bac3SLuigi Micco
209*1ef68647SAndreas Gohr    /**
210*1ef68647SAndreas Gohr     * Replace &#039; &quot; &gt; &lt; &amp;
211*1ef68647SAndreas Gohr     *
212*1ef68647SAndreas Gohr     * @fixme do we really need this? wouldn't this break things?
213*1ef68647SAndreas Gohr     */
214*1ef68647SAndreas Gohr    protected function strip_htmlencodedchars(&$str) {
215ee19bac3SLuigi Micco        $str = str_replace('&#039;', '\'', $str);
216ee19bac3SLuigi Micco        $str = str_replace('&quot;', '"', $str);
217ee19bac3SLuigi Micco        $str = str_replace('&gt;', '>', $str);
218ee19bac3SLuigi Micco        $str = str_replace('&lt;', '<', $str);
219ee19bac3SLuigi Micco        $str = str_replace('&amp;', '&', $str);
220ee19bac3SLuigi Micco    }
221ee19bac3SLuigi Micco}
222