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 private $tpl; 17 18 /** 19 * Constructor. Sets the correct template 20 */ 21 function __construct(){ 22 $tpl = false; 23 if(isset($_REQUEST['tpl'])){ 24 $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/','',$_REQUEST['tpl'])); 25 } 26 if(!$tpl) $tpl = $this->getConf('template'); 27 if(!$tpl) $tpl = 'default'; 28 if(!is_dir(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl)) $tpl = 'default'; 29 30 $this->tpl = $tpl; 31 } 32 33 /** 34 * Register the events 35 */ 36 function register(Doku_Event_Handler $controller) { 37 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array()); 38 } 39 40 /** 41 * Do the HTML to PDF conversion work 42 * 43 * @param Doku_Event $event 44 * @param array $param 45 * @return bool 46 */ 47 function convert(&$event, $param) { 48 global $ACT; 49 global $REV; 50 global $ID; 51 52 // our event? 53 if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false; 54 55 // check user's rights 56 if ( auth_quickaclcheck($ID) < AUTH_READ ) return false; 57 58 // one or multiple pages? 59 $list = array(); 60 61 if($ACT == 'export_pdf') { 62 $list[0] = $ID; 63 $title = p_get_first_heading($ID); 64 } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 65 //is in Bookmanager of bookcreator plugin title given 66 if(!$title = $_GET['pdfbook_title']) { //TODO when title is changed, the cached file contains the old title 67 /** @var $bookcreator action_plugin_bookcreator */ 68 $bookcreator = plugin_load('action', 'bookcreator'); 69 msg($bookcreator->getLang('needtitle'), -1); 70 71 $event->data = 'show'; 72 $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url 73 return false; 74 } 75 $list = explode("|", $_COOKIE['list-pagelist']); 76 } else { 77 /** @var $bookcreator action_plugin_bookcreator */ 78 $bookcreator = plugin_load('action', 'bookcreator'); 79 msg($bookcreator->getLang('empty'), -1); 80 81 $event->data = 'show'; 82 $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url 83 return false; 84 } 85 86 // it's ours, no one else's 87 $event->preventDefault(); 88 89 // prepare cache 90 $cache = new cache(join(',',$list).$REV.$this->tpl,'.dw2.pdf'); 91 $depends['files'] = array_map('wikiFN',$list); 92 $depends['files'][] = __FILE__; 93 $depends['files'][] = dirname(__FILE__).'/renderer.php'; 94 $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php'; 95 $depends['files'] = array_merge($depends['files'], getConfigFiles('main')); 96 97 // hard work only when no cache available 98 if(!$this->getConf('usecache') || !$cache->useCache($depends)){ 99 // initialize PDF library 100 require_once(dirname(__FILE__)."/DokuPDF.class.php"); 101 $mpdf = new DokuPDF(); 102 103 // let mpdf fix local links 104 $self = parse_url(DOKU_URL); 105 $url = $self['scheme'].'://'.$self['host']; 106 if($self['port']) $url .= ':'.$self['port']; 107 $mpdf->setBasePath($url); 108 109 // Set the title 110 $mpdf->SetTitle($title); 111 112 // some default settings 113 $mpdf->mirrorMargins = 1; 114 $mpdf->useOddEven = 1; 115 $mpdf->setAutoTopMargin = 'stretch'; 116 $mpdf->setAutoBottomMargin = 'stretch'; 117 118 // load the template 119 $template = $this->load_template($title); 120 121 // prepare HTML header styles 122 $html = '<html><head>'; 123 $html .= '<style type="text/css">'; 124 $html .= $this->load_css(); 125 $html .= '@page { size:auto; '.$template['page'].'}'; 126 $html .= '@page :first {'.$template['first'].'}'; 127 $html .= '</style>'; 128 $html .= '</head><body>'; 129 $html .= $template['html']; 130 $html .= '<div class="dokuwiki">'; 131 132 // loop over all pages 133 $cnt = count($list); 134 for($n=0; $n<$cnt; $n++){ 135 $page = $list[$n]; 136 137 $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page); 138 $html .= $this->page_depend_replacements($template['cite'], cleanID($page)); 139 if ($n < ($cnt - 1)){ 140 $html .= '<pagebreak />'; 141 } 142 } 143 144 $html .= '</div>'; 145 $mpdf->WriteHTML($html); 146 147 // write to cache file 148 $mpdf->Output($cache->cache, 'F'); 149 } 150 151 // deliver the file 152 header('Content-Type: application/pdf'); 153 header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 154 header('Pragma: public'); 155 http_conditionalRequest(filemtime($cache->cache)); 156 157 $filename = rawurlencode(cleanID(strtr($title, ':/;"',' '))); 158 if($this->getConf('output') == 'file'){ 159 header('Content-Disposition: attachment; filename="'.$filename.'.pdf";'); 160 }else{ 161 header('Content-Disposition: inline; filename="'.$filename.'.pdf";'); 162 } 163 164 if (http_sendfile($cache->cache)) exit; 165 166 $fp = @fopen($cache->cache,"rb"); 167 if($fp){ 168 http_rangeRequest($fp,filesize($cache->cache),'application/pdf'); 169 }else{ 170 header("HTTP/1.0 500 Internal Server Error"); 171 print "Could not read file - bad permissions?"; 172 } 173 exit(); 174 } 175 176 177 /** 178 * Load the various template files and prepare the HTML/CSS for insertion 179 */ 180 protected function load_template($title){ 181 global $ID; 182 global $conf; 183 $tpl = $this->tpl; 184 185 // this is what we'll return 186 $output = array( 187 'html' => '', 188 'page' => '', 189 'first' => '', 190 'cite' => '', 191 ); 192 193 // prepare header/footer elements 194 $html = ''; 195 foreach(array('header','footer') as $t){ 196 foreach(array('','_odd','_even','_first') as $h){ 197 if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){ 198 $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF; 199 $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF; 200 $html .= '</htmlpage'.$t.'>'.DOKU_LF; 201 202 // register the needed pseudo CSS 203 if($h == '_first'){ 204 $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF; 205 }elseif($h == '_even'){ 206 $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF; 207 }elseif($h == '_odd'){ 208 $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF; 209 }else{ 210 $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF; 211 } 212 } 213 } 214 } 215 216 // prepare replacements 217 $replace = array( 218 '@PAGE@' => '{PAGENO}', 219 '@PAGES@' => '{nb}', 220 '@TITLE@' => hsc($title), 221 '@WIKI@' => $conf['title'], 222 '@WIKIURL@' => DOKU_URL, 223 '@DATE@' => dformat(time()), 224 '@BASE@' => DOKU_BASE, 225 '@TPLBASE@' => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$tpl.'/' 226 ); 227 228 // set HTML element 229 $html = str_replace(array_keys($replace), array_values($replace), $html); 230 //TODO For bookcreator $ID (= bookmanager page) makes no sense 231 $output['html'] = $this->page_depend_replacements($html, $ID); 232 233 // citation box 234 if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){ 235 $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html'); 236 $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 237 } 238 239 return $output; 240 } 241 242 /** 243 * @param string $raw code with placeholders 244 * @param string $id pageid 245 * @return string 246 */ 247 protected function page_depend_replacements($raw, $id){ 248 global $REV; 249 250 // generate qr code for this page using google infographics api 251 $qr_code = ''; 252 if ($this->getConf('qrcodesize')) { 253 $url = urlencode(wl($id,'','&',true)); 254 $qr_code = '<img src="https://chart.googleapis.com/chart?chs='. 255 $this->getConf('qrcodesize').'&cht=qr&chl='.$url.'" />'; 256 } 257 // prepare replacements 258 $replace['@ID@'] = $id; 259 $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV))); 260 $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev'=> $REV) : false, true, "&"); 261 $replace['@QRCODE@'] = $qr_code; 262 263 return str_replace(array_keys($replace), array_values($replace), $raw); 264 } 265 266 /** 267 * Load all the style sheets and apply the needed replacements 268 */ 269 protected function load_css(){ 270 global $conf; 271 //reusue the CSS dispatcher functions without triggering the main function 272 define('SIMPLE_TEST',1); 273 require_once(DOKU_INC.'lib/exe/css.php'); 274 275 // prepare CSS files 276 $files = array_merge( 277 array( 278 DOKU_INC.'lib/styles/screen.css' 279 => DOKU_BASE.'lib/styles/', 280 DOKU_INC.'lib/styles/print.css' 281 => DOKU_BASE.'lib/styles/', 282 ), 283 css_pluginstyles('all'), 284 $this->css_pluginPDFstyles(), 285 array( 286 DOKU_PLUGIN.'dw2pdf/conf/style.css' 287 => DOKU_BASE.'lib/plugins/dw2pdf/conf/', 288 DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css' 289 => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/', 290 DOKU_PLUGIN.'dw2pdf/conf/style.local.css' 291 => DOKU_BASE.'lib/plugins/dw2pdf/conf/', 292 ) 293 ); 294 $css = ''; 295 foreach($files as $file => $location){ 296 $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 297 $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 298 $css .= css_loadfile($file, $location); 299 } 300 301 if(function_exists('css_parseless')) { 302 // apply pattern replacements 303 $styleini = css_styleini($conf['template']); 304 $css = css_applystyle($css, $styleini['replacements']); 305 306 // parse less 307 $css = css_parseless($css); 308 } else { 309 // @deprecated 2013-12-19: fix backward compatibility 310 $css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 311 } 312 313 return $css; 314 } 315 316 317 /** 318 * Returns a list of possible Plugin PDF Styles 319 * 320 * Checks for a pdf.css, falls back to print.css 321 * 322 * @author Andreas Gohr <andi@splitbrain.org> 323 */ 324 function css_pluginPDFstyles(){ 325 $list = array(); 326 $plugins = plugin_list(); 327 328 $usestyle = explode(',',$this->getConf('usestyles')); 329 foreach ($plugins as $p){ 330 if(in_array($p,$usestyle)){ 331 $list[DOKU_PLUGIN."$p/screen.css"] = DOKU_BASE."lib/plugins/$p/"; 332 $list[DOKU_PLUGIN."$p/style.css"] = DOKU_BASE."lib/plugins/$p/"; 333 } 334 335 if(file_exists(DOKU_PLUGIN."$p/pdf.css")){ 336 $list[DOKU_PLUGIN."$p/pdf.css"] = DOKU_BASE."lib/plugins/$p/"; 337 }else{ 338 $list[DOKU_PLUGIN."$p/print.css"] = DOKU_BASE."lib/plugins/$p/"; 339 } 340 } 341 return $list; 342 } 343 344} 345