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