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