xref: /plugin/dw2pdf/action.php (revision f2894c2f8e60927322cbdf164d7fe64b3565edb1)
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 <andi@splitbrain.org>
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        // let mpdf fix local links
43        $self = parse_url(DOKU_URL);
44        $url  = $self['scheme'].'://'.$self['host'];
45        if($self['port']) $url .= ':'.$port;
46        $mpdf->setBasePath($url);
47
48        // some default settings
49        $mpdf->mirrorMargins          = 1;  // Use different Odd/Even headers and footers and mirror margins
50        $mpdf->defaultheaderfontsize  = 8;  // in pts
51        $mpdf->defaultheaderfontstyle = ''; // blank, B, I, or BI
52        $mpdf->defaultheaderline      = 1;  // 1 to include line below header/above footer
53        $mpdf->defaultfooterfontsize  = 8;  // in pts
54        $mpdf->defaultfooterfontstyle = ''; // blank, B, I, or BI
55        $mpdf->defaultfooterline      = 1;  // 1 to include line below header/above footer
56
57        // prepare HTML header styles
58        $html  = '<html><head>';
59        $html .= '<style>';
60        $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css');
61        $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css');
62        $html .= '</style>';
63        $html .= '</head><body>';
64
65        // set headers/footers
66        $this->prepare_headers($mpdf);
67
68        // one or multiple pages?
69        $list = array();
70        if ( $ACT == 'export_pdf' ) {
71            $list[0] = $ID;
72        } elseif (isset($_COOKIE['list-pagelist'])) {
73            $list = explode("|", $_COOKIE['list-pagelist']);
74        }
75
76        // loop over all pages
77        $cnt = count($list);
78        for($n=0; $n<$cnt; $n++){
79            $page = $list[$n];
80
81            $html .= p_wiki_xhtml($page,$REV,false);
82            if($this->getConf('addcitation')){
83                $html .= $this->citation($page);
84            }
85            if ($n < ($cnt - 1)){
86                $html .= '<pagebreak />';
87            }
88        }
89
90        $this->arrangeHtml($html, $this->getConf("norender"));
91
92        $mpdf->WriteHTML($html);
93        $title = $_GET['pdfbook_title'];
94        if(!$title) $title = noNS($ID);
95        $output = 'I';
96        if($this->getConf('output') == 'file') $output = 'D';
97        $mpdf->Output(urlencode($title).'.pdf', $output);
98
99        exit();
100    }
101
102    /**
103     * Setup the page headers and footers
104     */
105    protected function prepare_headers(&$mpdf){
106        global $ID;
107        global $REV;
108        global $conf;
109
110        if($_GET['pdfbook_title']){
111            $title = $_GET['pdfbook_title'];
112        }else{
113            $title = p_get_first_heading($ID);
114        }
115        if(!$title) $title = noNS($ID);
116
117        // prepare replacements
118        $replace = array(
119                '@ID@'      => $ID,
120                '@PAGE@'    => '{PAGENO}',
121                '@PAGES@'   => '{nb}',
122                '@TITLE@'   => $title,
123                '@WIKI@'    => $conf['title'],
124                '@WIKIURL@' => DOKU_URL,
125                '@UPDATE@'  => dformat(filemtime(wikiFN($ID,$REV))),
126                '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"),
127                '@DATE@'    => dformat(time()),
128        );
129
130        // do the replacements
131        $fo = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd"));
132        $fe = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even"));
133        $ho = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd"));
134        $he = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even"));
135
136        // set the headers/footers
137        $mpdf->SetHeader($ho);
138        $mpdf->SetHeader($he, 'E');
139        $mpdf->SetFooter($fo);
140        $mpdf->SetFooter($fe, 'E');
141
142        // title
143        $mpdf->SetTitle($title);
144    }
145
146    /**
147     * Fix up the HTML a bit
148     *
149     * FIXME This is far from perfect and will modify things within code and
150     * nowiki blocks. It would probably be a good idea to use a real HTML
151     * parser or our own renderer instead of modifying the HTML at all.
152     */
153    protected function arrangeHtml(&$html, $norendertags = '' ) {
154        // add bookmark links
155        $bmlevel = $this->getConf('maxbookmarks');
156        if($bmlevel > 0) {
157            $html = preg_replace("/\<a name=(.+?)\>(.+?)\<\/a\>/s",'$2',$html);
158            for ($j = 1; $j<=$bmlevel; $j++) {
159                $html = preg_replace("/\<h".$j."\>(.+?)\<\/h".$j."\>/s",'<h'.$j.'>$1<bookmark content="$1" level="'.($j-1).'"/></h'.$j.'>',$html);
160            }
161        }
162
163        // insert a pagebreak for support of WRAP and PAGEBREAK plugins
164        $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html);
165        $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html);
166        $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html);
167
168        // Customized to strip all span tags so that the wiki <code> SQL would display properly
169        $norender = explode(',',$this->getConf('norender'));
170        $this->strip_only($html, $norender);
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