xref: /plugin/dw2pdf/action.php (revision 83dae0db7374bd2e1f9cbf3651c2bc55948ccb0d)
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_INC.'lib/styles/screen.css');
61        $html .= file_get_contents(DOKU_INC.'lib/styles/print.css');
62        $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css');
63        $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css');
64        $html .= '</style>';
65        $html .= '</head><body>';
66
67        // set headers/footers
68        $this->prepare_headers($mpdf);
69
70        // one or multiple pages?
71        $list = array();
72        if ( $ACT == 'export_pdf' ) {
73            $list[0] = $ID;
74        } elseif (isset($_COOKIE['list-pagelist'])) {
75            $list = explode("|", $_COOKIE['list-pagelist']);
76        }
77
78        // loop over all pages
79        $cnt = count($list);
80        for($n=0; $n<$cnt; $n++){
81            $page = $list[$n];
82
83            $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page);
84            if($this->getConf('addcitation')){
85                $html .= $this->citation($page);
86            }
87            if ($n < ($cnt - 1)){
88                $html .= '<pagebreak />';
89            }
90        }
91
92        $this->arrangeHtml($html, $this->getConf("norender"));
93        $mpdf->WriteHTML($html);
94
95        $title = $_GET['pdfbook_title'];
96        if(!$title) $title = noNS($ID);
97        $output = 'I';
98        if($this->getConf('output') == 'file') $output = 'D';
99        $mpdf->Output(urlencode($title).'.pdf', $output);
100
101        exit();
102    }
103
104    /**
105     * Setup the page headers and footers
106     */
107    protected function prepare_headers(&$mpdf){
108        global $ID;
109        global $REV;
110        global $conf;
111
112        if($_GET['pdfbook_title']){
113            $title = $_GET['pdfbook_title'];
114        }else{
115            $title = p_get_first_heading($ID);
116        }
117        if(!$title) $title = noNS($ID);
118
119        // prepare replacements
120        $replace = array(
121                '@ID@'      => $ID,
122                '@PAGE@'    => '{PAGENO}',
123                '@PAGES@'   => '{nb}',
124                '@TITLE@'   => $title,
125                '@WIKI@'    => $conf['title'],
126                '@WIKIURL@' => DOKU_URL,
127                '@UPDATE@'  => dformat(filemtime(wikiFN($ID,$REV))),
128                '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"),
129                '@DATE@'    => dformat(time()),
130        );
131
132        // do the replacements
133        $fo = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd"));
134        $fe = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even"));
135        $ho = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd"));
136        $he = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even"));
137
138        // set the headers/footers
139        $mpdf->SetHeader($ho);
140        $mpdf->SetHeader($he, 'E');
141        $mpdf->SetFooter($fo);
142        $mpdf->SetFooter($fe, 'E');
143
144        // title
145        $mpdf->SetTitle($title);
146    }
147
148    /**
149     * Fix up the HTML a bit
150     *
151     * FIXME This is far from perfect and most of it should be moved to
152     * our own renderer instead of modifying the HTML at all.
153     */
154    protected function arrangeHtml(&$html, $norendertags = '' ) {
155
156        // insert a pagebreak for support of WRAP and PAGEBREAK plugins
157        $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html);
158        $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html);
159        $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html);
160
161    }
162
163    /**
164     * Create the citation box
165     *
166     * @todo can we drop the inline style here?
167     */
168    protected function citation($page) {
169        global $conf;
170
171        $date = filemtime(wikiFN($page));
172        $html  = '';
173        $html .= "<br><br><div style='font-size: 80%; border: solid 0.5mm #DDDDDD;background-color: #EEEEEE; padding: 2mm; border-radius: 2mm 2mm; width: 100%;'>";
174        $html .= "From:<br>";
175        $html .= "<a href='".DOKU_URL."'>".DOKU_URL."</a>&nbsp;-&nbsp;"."<b>".$conf['title']."</b>";
176        $html .= "<br><br>Permanent link:<br>";
177        $html .= "<b><a href='".wl($page, false, true, "&")."'>".wl($page, false, true, "&")."</a></b>";
178        $html .= "<br><br>Last update: <b>".dformat($date)."</b><br>";
179        $html .= "</div>";
180        return $html;
181    }
182
183    /**
184     * Strip unwanted tags
185     *
186     * @fixme could this be done by strip_tags?
187     * @author Jared Ong
188     */
189    protected function strip_only(&$str, $tags) {
190        if(!is_array($tags)) {
191            $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
192            if(end($tags) == '') array_pop($tags);
193        }
194        foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str);
195    }
196}
197