xref: /plugin/dw2pdf/action.php (revision a876b55b2fc0600771960c8f25068b4355e94547)
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_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
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        $mpdf->WriteHTML($html);
92
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 most of it should be moved to
150     * our own renderer instead of modifying the HTML at all.
151     */
152    protected function arrangeHtml(&$html, $norendertags = '' ) {
153
154        // insert a pagebreak for support of WRAP and PAGEBREAK plugins
155        $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html);
156        $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html);
157        $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html);
158
159        // Customized to strip all span tags so that the wiki <code> SQL would display properly
160        $norender = explode(',',$this->getConf('norender'));
161        $this->strip_only($html, $norender);
162    }
163
164    /**
165     * Create the citation box
166     *
167     * @todo can we drop the inline style here?
168     */
169    protected function citation($page) {
170        global $conf;
171
172        $date = filemtime(wikiFN($page));
173        $html  = '';
174        $html .= "<br><br><div style='font-size: 80%; border: solid 0.5mm #DDDDDD;background-color: #EEEEEE; padding: 2mm; border-radius: 2mm 2mm; width: 100%;'>";
175        $html .= "From:<br>";
176        $html .= "<a href='".DOKU_URL."'>".DOKU_URL."</a>&nbsp;-&nbsp;"."<b>".$conf['title']."</b>";
177        $html .= "<br><br>Permanent link:<br>";
178        $html .= "<b><a href='".wl($page, false, true, "&")."'>".wl($page, false, true, "&")."</a></b>";
179        $html .= "<br><br>Last update: <b>".dformat($date)."</b><br>";
180        $html .= "</div>";
181        return $html;
182    }
183
184    /**
185     * Strip unwanted tags
186     *
187     * @fixme could this be done by strip_tags?
188     * @author Jared Ong
189     */
190    protected function strip_only(&$str, $tags) {
191        if(!is_array($tags)) {
192            $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
193            if(end($tags) == '') array_pop($tags);
194        }
195        foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
196    }
197}
198