xref: /plugin/dw2pdf/action.php (revision 3ae7a8dfcb3e2931354e2d49a487b6e056bbd4c3)
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        // prepare replacements
112        $replace = array(
113                '@ID@'      => $ID,
114                '@PAGE@'    => '{PAGENO}',
115                '@PAGES@'   => '{nb}',
116                '@TITLE@'   => $title,
117                '@WIKI@'    => $conf['title'],
118                '@WIKIURL@' => DOKU_URL,
119                '@UPDATE@'  => dformat(filemtime(wikiFN($ID,$REV))),
120                '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"),
121                '@DATE@'    => dformat(time()),
122        );
123
124        // do the replacements
125        $fo = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd"));
126        $fe = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even"));
127        $ho = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd"));
128        $he = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even"));
129
130        // set the headers/footers
131        $mpdf->SetHeader($ho);
132        $mpdf->SetHeader($he, 'E');
133        $mpdf->SetFooter($fo);
134        $mpdf->SetFooter($fe, 'E');
135
136        // title
137        $mpdf->SetTitle($title);
138    }
139
140    /**
141     * Fix up the HTML a bit
142     *
143     * FIXME This is far from perfect and will modify things within code and
144     * nowiki blocks. It would probably be a good idea to use a real HTML
145     * parser or our own renderer instead of modifying the HTML at all.
146     */
147    protected function arrangeHtml(&$html, $norendertags = '' ) {
148        // add bookmark links
149        $bmlevel = $this->getConf('maxbookmarks');
150        if($bmlevel > 0) {
151            $html = preg_replace("/\<a name=(.+?)\>(.+?)\<\/a\>/s",'$2',$html);
152            for ($j = 1; $j<=$bmlevel; $j++) {
153                $html = preg_replace("/\<h".$j."\>(.+?)\<\/h".$j."\>/s",'<h'.$j.'>$1<bookmark content="$1" level="'.($j-1).'"/></h'.$j.'>',$html);
154            }
155        }
156
157        // insert a pagebreak for support of WRAP and PAGEBREAK plugins
158        $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html);
159        $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html);
160        $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html);
161
162        // Customized to strip all span tags so that the wiki <code> SQL would display properly
163        $norender = explode(',',$this->getConf('norender'));
164        $this->strip_only($html, $norender);
165        $this->strip_htmlencodedchars($html);
166
167        $html = str_replace('href="/','href="http://'.$_SERVER['HTTP_HOST'].'/',$html);
168
169    }
170
171    /**
172     * Create the citation box
173     *
174     * @todo can we drop the inline style here?
175     */
176    protected function citation($page) {
177        global $conf;
178
179        $date = filemtime(wikiFN($page));
180        $html  = '';
181        $html .= "<br><br><div style='font-size: 80%; border: solid 0.5mm #DDDDDD;background-color: #EEEEEE; padding: 2mm; border-radius: 2mm 2mm; width: 100%;'>";
182        $html .= "From:<br>";
183        $html .= "<a href='".DOKU_URL."'>".DOKU_URL."</a>&nbsp;-&nbsp;"."<b>".$conf['title']."</b>";
184        $html .= "<br><br>Permanent link:<br>";
185        $html .= "<b><a href='".wl($page, false, true, "&")."'>".wl($page, false, true, "&")."</a></b>";
186        $html .= "<br><br>Last update: <b>".dformat($date)."</b><br>";
187        $html .= "</div>";
188        return $html;
189    }
190
191    /**
192     * Strip unwanted tags
193     *
194     * @fixme could this be done by strip_tags?
195     * @author Jared Ong
196     */
197    protected function strip_only(&$str, $tags) {
198        if(!is_array($tags)) {
199            $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
200            if(end($tags) == '') array_pop($tags);
201        }
202        foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
203    }
204
205    /**
206     * Replace &#039; &quot; &gt; &lt; &amp;
207     *
208     * @fixme do we really need this? wouldn't this break things?
209     * @fixme and if we really need it, do correct numeric decoding
210     */
211    protected function strip_htmlencodedchars(&$str) {
212        $str = htmlspecialchars_decode($str);
213        $str = str_replace('&#039;', '\'', $str);
214    }
215}
216