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(); 12 13/** 14 * Class action_plugin_dw2pdf 15 * 16 * Export hmtl content to pdf, for different url parameter configurations 17 * DokuPDF which extends mPDF is used for generating the pdf from html. 18 */ 19class action_plugin_dw2pdf extends DokuWiki_Action_Plugin { 20 /** 21 * Settings for current export, collected from url param, plugin config, global config 22 * 23 * @var array 24 */ 25 protected $exportConfig = null; 26 protected $tpl; 27 protected $title; 28 protected $list = array(); 29 30 /** 31 * Constructor. Sets the correct template 32 * 33 * @param string $title 34 */ 35 public function __construct($title=null) { 36 $this->tpl = $this->getExportConfig('template'); 37 $this->title = $title ? $title : ''; 38 } 39 40 /** 41 * Register the events 42 * 43 * @param Doku_Event_Handler $controller 44 */ 45 public function register(Doku_Event_Handler $controller) { 46 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array()); 47 $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array()); 48 } 49 50 /** 51 * Do the HTML to PDF conversion work 52 * 53 * @param Doku_Event $event 54 * @return bool 55 */ 56 public function convert(Doku_Event $event) { 57 global $ID; 58 59 // our event? 60 if(($event->data != 'export_pdfbook') && ($event->data != 'export_pdf') && ($event->data != 'export_pdfns')) return false; 61 62 // check user's rights 63 if(auth_quickaclcheck($ID) < AUTH_READ) return false; 64 65 if($data = $this->collectExportPages($event)) { 66 list($this->title, $this->list) = $data; 67 } else { 68 return false; 69 } 70 71 // it's ours, no one else's 72 $event->preventDefault(); 73 74 // prepare cache and its dependencies 75 $depends = array(); 76 $cache = $this->prepareCache($depends); 77 78 // hard work only when no cache available or needed for debugging 79 if(!$this->getConf('usecache') || $this->getExportConfig('isDebug') || !$cache->useCache($depends)) { 80 // generating the pdf may take a long time for larger wikis / namespaces with many pages 81 set_time_limit(0); 82 83 $this->generatePDF($cache->cache); 84 } 85 86 // deliver the file 87 $this->sendPDFFile($cache->cache); 88 return true; 89 } 90 91 92 /** 93 * Obtain list of pages and title, based on url parameters 94 * 95 * @param Doku_Event $event 96 * @return string|bool 97 */ 98 protected function collectExportPages(Doku_Event $event) { 99 global $ID; 100 global $INPUT; 101 global $conf; 102 103 // list of one or multiple pages 104 $list = array(); 105 106 if($event->data == 'export_pdf') { 107 $list[0] = $ID; 108 $this->title = $INPUT->str('pdftitle'); //DEPRECATED 109 $this->title = $INPUT->str('book_title', $this->title, true); 110 if(empty($this->title)) { 111 $this->title = p_get_first_heading($ID); 112 } 113 114 } elseif($event->data == 'export_pdfns') { 115 //check input for title and ns 116 if(!$this->title = $INPUT->str('book_title')) { 117 $this->showPageWithErrorMsg($event, 'needtitle'); 118 return false; 119 } 120 $pdfnamespace = cleanID($INPUT->str('book_ns')); 121 if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) { 122 $this->showPageWithErrorMsg($event, 'needns'); 123 return false; 124 } 125 126 //sort order 127 $order = $INPUT->str('book_order', 'natural', true); 128 $sortoptions = array('pagename', 'date', 'natural'); 129 if(!in_array($order, $sortoptions)) { 130 $order = 'natural'; 131 } 132 133 //search depth 134 $depth = $INPUT->int('book_nsdepth', 0); 135 if($depth < 0) { 136 $depth = 0; 137 } 138 139 //page search 140 $result = array(); 141 $opts = array('depth' => $depth); //recursive all levels 142 $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace)); 143 search($result, $conf['datadir'], 'search_allpages', $opts, $dir); 144 145 //sorting 146 if(count($result) > 0) { 147 if($order == 'date') { 148 usort($result, array($this, '_datesort')); 149 } elseif($order == 'pagename') { 150 usort($result, array($this, '_pagenamesort')); 151 } 152 } 153 154 foreach($result as $item) { 155 $list[] = $item['id']; 156 } 157 158 if ($pdfnamespace !== '') { 159 if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { 160 if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) { 161 array_unshift($list,rtrim($pdfnamespace,':')); 162 } 163 } 164 } 165 166 } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 167 /** @deprecated April 2016 replaced by localStorage version of Bookcreator*/ 168 //is in Bookmanager of bookcreator plugin a title given? 169 $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED 170 $this->title = $INPUT->str('book_title', $this->title, true); 171 if(empty($this->title)) { 172 $this->showPageWithErrorMsg($event, 'needtitle'); 173 return false; 174 } else { 175 $list = explode("|", $_COOKIE['list-pagelist']); 176 } 177 178 } elseif($INPUT->has('selection')) { 179 //handle Bookcreator requests based at localStorage 180// if(!checkSecurityToken()) { 181// http_status(403); 182// print $this->getLang('empty'); 183// exit(); 184// } 185 186 $json = new JSON(JSON_LOOSE_TYPE); 187 $list = $json->decode($INPUT->post->str('selection', '', true)); 188 if(!is_array($list) || empty($list)) { 189 http_status(400); 190 print $this->getLang('empty'); 191 exit(); 192 } 193 194 $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED 195 $this->title = $INPUT->str('book_title', $this->title, true); 196 if(empty($this->title)) { 197 http_status(400); 198 print $this->getLang('needtitle'); 199 exit(); 200 } 201 202 } else { 203 //show empty bookcreator message 204 $this->showPageWithErrorMsg($event, 'empty'); 205 return false; 206 } 207 208 $list = array_map('cleanID', $list); 209 210 $skippedpages = array(); 211 foreach($list as $index => $pageid) { 212 if(auth_quickaclcheck($pageid) < AUTH_READ) { 213 $skippedpages[] = $pageid; 214 unset($list[$index]); 215 } 216 } 217 $list = array_filter($list); //removes also pages mentioned '0' 218 219 //if selection contains forbidden pages throw (overridable) warning 220 if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) { 221 $msg = hsc(join(', ', $skippedpages)); 222 if($INPUT->has('selection')) { 223 http_status(400); 224 print sprintf($this->getLang('forbidden'), $msg); 225 exit(); 226 } else { 227 $this->showPageWithErrorMsg($event, 'forbidden', $msg); 228 return false; 229 } 230 231 } 232 233 return array($this->title, $list); 234 } 235 236 /** 237 * Prepare cache 238 * 239 * @param array $depends (reference) array with dependencies 240 * @return cache 241 */ 242 protected function prepareCache(&$depends) { 243 global $REV; 244 245 $cachekey = join(',', $this->list) 246 . $REV 247 . $this->getExportConfig('template') 248 . $this->getExportConfig('pagesize') 249 . $this->getExportConfig('orientation') 250 . $this->getExportConfig('font-size') 251 . $this->getExportConfig('doublesided') 252 . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0') 253 . $this->title; 254 $cache = new cache($cachekey, '.dw2.pdf'); 255 256 $dependencies = array(); 257 foreach($this->list as $pageid) { 258 $relations = p_get_metadata($pageid, 'relation'); 259 260 if(is_array($relations)) { 261 if(array_key_exists('media', $relations) && is_array($relations['media'])) { 262 foreach($relations['media'] as $mediaid => $exists) { 263 if($exists) { 264 $dependencies[] = mediaFN($mediaid); 265 } 266 } 267 } 268 269 if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) { 270 foreach($relations['haspart'] as $part_pageid => $exists) { 271 if($exists) { 272 $dependencies[] = wikiFN($part_pageid); 273 } 274 } 275 } 276 } 277 278 $dependencies[] = metaFN($pageid, '.meta'); 279 } 280 281 $depends['files'] = array_map('wikiFN', $this->list); 282 $depends['files'][] = __FILE__; 283 $depends['files'][] = dirname(__FILE__) . '/renderer.php'; 284 $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php'; 285 $depends['files'] = array_merge( 286 $depends['files'], 287 $dependencies, 288 getConfigFiles('main') 289 ); 290 return $cache; 291 } 292 293 /** 294 * Set error notification and reload page again 295 * 296 * @param Doku_Event $event 297 * @param string $msglangkey key of translation key 298 * @param string $replacement 299 */ 300 private function showPageWithErrorMsg(Doku_Event $event, $msglangkey, $replacement=null) { 301 if(empty($replacement)) { 302 $msg = $this->getLang($msglangkey); 303 } else { 304 $msg = sprintf($this->getLang($msglangkey), $replacement); 305 } 306 msg($msg, -1); 307 308 $event->data = 'show'; 309 $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url 310 } 311 312 /** 313 * Build a pdf from the html 314 * 315 * @param string $cachefile 316 */ 317 protected function generatePDF($cachefile) { 318 global $ID; 319 global $REV; 320 global $INPUT; 321 322 //some shortcuts to export settings 323 $hasToC = $this->getExportConfig('hasToC'); 324 $levels = $this->getExportConfig('levels'); 325 $isDebug = $this->getExportConfig('isDebug'); 326 327 // initialize PDF library 328 require_once(dirname(__FILE__) . "/DokuPDF.class.php"); 329 330 $mpdf = new DokuPDF($this->getExportConfig('pagesize'), 331 $this->getExportConfig('orientation'), 332 $this->getExportConfig('font-size')); 333 334 // let mpdf fix local links 335 $self = parse_url(DOKU_URL); 336 $url = $self['scheme'] . '://' . $self['host']; 337 if($self['port']) { 338 $url .= ':' . $self['port']; 339 } 340 $mpdf->setBasePath($url); 341 342 // Set the title 343 $mpdf->SetTitle($this->title); 344 345 // some default document settings 346 //note: double-sided document, starts at an odd page (first page is a right-hand side page) 347 // single-side document has only odd pages 348 $mpdf->mirrorMargins = $this->getExportConfig('doublesided'); 349 $mpdf->setAutoTopMargin = 'stretch'; 350 $mpdf->setAutoBottomMargin = 'stretch'; 351// $mpdf->pagenumSuffix = '/'; //prefix for {nbpg} 352 if($hasToC) { 353 $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC 354 $mpdf->h2toc = $levels; 355 } else { 356 $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'); 357 } 358 359 // load the template 360 $template = $this->load_template(); 361 362 // prepare HTML header styles 363 $html = ''; 364 if($isDebug) { 365 $html .= '<html><head>'; 366 $html .= '<style type="text/css">'; 367 } 368 369 $styles = '@page { size:auto; ' . $template['page'] . '}'; 370 $styles .= '@page :first {' . $template['first'] . '}'; 371 372 $styles .= '@page landscape-page { size:landscape }'; 373 $styles .= 'div.dw2pdf-landscape { page:landscape-page }'; 374 $styles .= '@page portrait-page { size:portrait }'; 375 $styles .= 'div.dw2pdf-portrait { page:portrait-page }'; 376 $styles .= $this->load_css(); 377 378 $mpdf->WriteHTML($styles, 1); 379 380 if($isDebug) { 381 $html .= $styles; 382 $html .= '</style>'; 383 $html .= '</head><body>'; 384 } 385 386 $body_start = $template['html']; 387 $body_start .= '<div class="dokuwiki">'; 388 389 // insert the cover page 390 $body_start .= $template['cover']; 391 392 $mpdf->WriteHTML($body_start, 2, true, false); //start body html 393 if($isDebug) { 394 $html .= $body_start; 395 } 396 if($hasToC) { 397 //Note: - for double-sided document the ToC is always on an even number of pages, so that the following content is on a correct odd/even page 398 // - first page of ToC starts always at odd page (so eventually an additional blank page is included before) 399 // - there is no page numbering at the pages of the ToC 400 $mpdf->TOCpagebreakByArray( 401 array( 402 'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>', 403 'toc-bookmarkText' => $this->getLang('tocheader'), 404 'links' => true, 405 'outdent' => '1em', 406 'resetpagenum' => true, //start pagenumbering after ToC 407 'pagenumstyle' => '1' 408 ) 409 ); 410 $html .= '<tocpagebreak>'; 411 } 412 413 // store original pageid 414 $keep = $ID; 415 416 // loop over all pages 417 $counter = 0; 418 $no_pages = count($this->list); 419 foreach($this->list as $page) { 420 $counter++; 421 $filename = wikiFN($page, $REV); 422 423 if(!file_exists($filename)) { 424 continue; 425 } 426 427 // set global pageid to the rendered page 428 $ID = $page; 429 430 $pagehtml = p_cached_output($filename, 'dw2pdf', $page); 431 $pagehtml .= $this->page_depend_replacements($template['cite'], $page); 432 if($counter < $no_pages) { 433 $pagehtml .= '<pagebreak />'; 434 } 435 436 $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html 437 if($isDebug) { 438 $html .= $pagehtml; 439 } 440 } 441 //restore ID 442 $ID = $keep; 443 444 // insert the back page 445 $body_end = $template['back']; 446 447 $body_end .= '</div>'; 448 449 $mpdf->WriteHTML($body_end, 2, false, true); // finish body html 450 if($isDebug) { 451 $html .= $body_end; 452 $html .= '</body>'; 453 $html .= '</html>'; 454 } 455 456 //Return html for debugging 457 if($isDebug) { 458 if($INPUT->str('debughtml', 'text', true) == 'html') { 459 echo $html; 460 } else { 461 header('Content-Type: text/plain; charset=utf-8'); 462 echo $html; 463 } 464 exit(); 465 }; 466 467 // write to cache file 468 $mpdf->Output($cachefile, 'F'); 469 } 470 471 /** 472 * @param string $cachefile 473 */ 474 protected function sendPDFFile($cachefile) { 475 header('Content-Type: application/pdf'); 476 header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 477 header('Pragma: public'); 478 http_conditionalRequest(filemtime($cachefile)); 479 480 $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', ' '))); 481 if($this->getConf('output') == 'file') { 482 header('Content-Disposition: attachment; filename="' . $filename . '.pdf";'); 483 } else { 484 header('Content-Disposition: inline; filename="' . $filename . '.pdf";'); 485 } 486 487 //Bookcreator uses jQuery.fileDownload.js, which requires a cookie. 488 header('Set-Cookie: fileDownload=true; path=/'); 489 490 //try to send file, and exit if done 491 http_sendfile($cachefile); 492 493 $fp = @fopen($cachefile, "rb"); 494 if($fp) { 495 http_rangeRequest($fp, filesize($cachefile), 'application/pdf'); 496 } else { 497 header("HTTP/1.0 500 Internal Server Error"); 498 print "Could not read file - bad permissions?"; 499 } 500 exit(); 501 } 502 503 /** 504 * Load the various template files and prepare the HTML/CSS for insertion 505 * 506 * @return array 507 */ 508 protected function load_template() { 509 global $ID; 510 global $conf; 511 512 // this is what we'll return 513 $output = array( 514 'cover' => '', 515 'html' => '', 516 'page' => '', 517 'first' => '', 518 'cite' => '', 519 ); 520 521 // prepare header/footer elements 522 $html = ''; 523 foreach(array('header', 'footer') as $section) { 524 foreach(array('', '_odd', '_even', '_first') as $order) { 525 $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html'; 526 if(file_exists($file)) { 527 $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF; 528 $html .= file_get_contents($file) . DOKU_LF; 529 $html .= '</htmlpage' . $section . '>' . DOKU_LF; 530 531 // register the needed pseudo CSS 532 if($order == '_first') { 533 $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 534 } elseif($order == '_even') { 535 $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 536 } elseif($order == '_odd') { 537 $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 538 } else { 539 $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 540 } 541 } 542 } 543 } 544 545 // prepare replacements 546 $replace = array( 547 '@PAGE@' => '{PAGENO}', 548 '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / ' 549 '@TITLE@' => hsc($this->title), 550 '@WIKI@' => $conf['title'], 551 '@WIKIURL@' => DOKU_URL, 552 '@DATE@' => dformat(time()), 553 '@BASE@' => DOKU_BASE, 554 '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/' 555 ); 556 557 // set HTML element 558 $html = str_replace(array_keys($replace), array_values($replace), $html); 559 //TODO For bookcreator $ID (= bookmanager page) makes no sense 560 $output['html'] = $this->page_depend_replacements($html, $ID); 561 562 // cover page 563 $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html'; 564 if(file_exists($coverfile)) { 565 $output['cover'] = file_get_contents($coverfile); 566 $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']); 567 $output['cover'] = $this->page_depend_replacements($output['cover'], $ID); 568 $output['cover'] .= '<pagebreak />'; 569 } 570 571 // cover page 572 $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html'; 573 if(file_exists($backfile)) { 574 $output['back'] = '<pagebreak />'; 575 $output['back'] .= file_get_contents($backfile); 576 $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']); 577 $output['back'] = $this->page_depend_replacements($output['back'], $ID); 578 } 579 580 // citation box 581 $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html'; 582 if(file_exists($citationfile)) { 583 $output['cite'] = file_get_contents($citationfile); 584 $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 585 } 586 587 return $output; 588 } 589 590 /** 591 * @param string $raw code with placeholders 592 * @param string $id pageid 593 * @return string 594 */ 595 protected function page_depend_replacements($raw, $id) { 596 global $REV; 597 598 // generate qr code for this page using google infographics api 599 $qr_code = ''; 600 if($this->getConf('qrcodesize')) { 601 $url = urlencode(wl($id, '', '&', true)); 602 $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' . 603 $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />'; 604 } 605 // prepare replacements 606 $replace['@ID@'] = $id; 607 $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV))); 608 $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&"); 609 $replace['@QRCODE@'] = $qr_code; 610 611 $content = str_replace(array_keys($replace), array_values($replace), $raw); 612 613 // @DATE(<date>[, <format>])@ 614 $content = preg_replace_callback( 615 '/@DATE\((.*?)(?:,\s*(.*?))?\)@/', 616 array($this, 'replacedate'), 617 $content 618 ); 619 620 return $content; 621 } 622 623 624 /** 625 * (callback) Replace date by request datestring 626 * e.g. '%m(30-11-1975)' is replaced by '11' 627 * 628 * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern 629 * @return string 630 */ 631 function replacedate($match) { 632 global $conf; 633 //no 2nd argument for default date format 634 if($match[2] == null) { 635 $match[2] = $conf['dformat']; 636 } 637 return strftime($match[2], strtotime($match[1])); 638 } 639 640 641 /** 642 * Load all the style sheets and apply the needed replacements 643 */ 644 protected function load_css() { 645 global $conf; 646 //reusue the CSS dispatcher functions without triggering the main function 647 define('SIMPLE_TEST', 1); 648 require_once(DOKU_INC . 'lib/exe/css.php'); 649 650 // prepare CSS files 651 $files = array_merge( 652 array( 653 DOKU_INC . 'lib/styles/screen.css' 654 => DOKU_BASE . 'lib/styles/', 655 DOKU_INC . 'lib/styles/print.css' 656 => DOKU_BASE . 'lib/styles/', 657 ), 658 css_pluginstyles('all'), 659 $this->css_pluginPDFstyles(), 660 array( 661 DOKU_PLUGIN . 'dw2pdf/conf/style.css' 662 => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 663 DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' 664 => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 665 DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' 666 => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 667 ) 668 ); 669 $css = ''; 670 foreach($files as $file => $location) { 671 $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 672 $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 673 $css .= css_loadfile($file, $location); 674 } 675 676 if(function_exists('css_parseless')) { 677 // apply pattern replacements 678 $styleini = css_styleini($conf['template']); 679 $css = css_applystyle($css, $styleini['replacements']); 680 681 // parse less 682 $css = css_parseless($css); 683 } else { 684 // @deprecated 2013-12-19: fix backward compatibility 685 $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 686 } 687 688 return $css; 689 } 690 691 /** 692 * Returns a list of possible Plugin PDF Styles 693 * 694 * Checks for a pdf.css, falls back to print.css 695 * 696 * @author Andreas Gohr <andi@splitbrain.org> 697 */ 698 protected function css_pluginPDFstyles() { 699 $list = array(); 700 $plugins = plugin_list(); 701 702 $usestyle = explode(',', $this->getConf('usestyles')); 703 foreach($plugins as $p) { 704 if(in_array($p, $usestyle)) { 705 $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/"; 706 $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/"; 707 } 708 709 if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) { 710 $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/"; 711 } else { 712 $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/"; 713 } 714 } 715 return $list; 716 } 717 718 /** 719 * Returns array of pages which will be included in the exported pdf 720 * 721 * @return array 722 */ 723 public function getExportedPages() { 724 return $this->list; 725 } 726 727 /** 728 * usort callback to sort by file lastmodified time 729 * 730 * @param array $a 731 * @param array $b 732 * @return int 733 */ 734 public function _datesort($a, $b) { 735 if($b['rev'] < $a['rev']) return -1; 736 if($b['rev'] > $a['rev']) return 1; 737 return strcmp($b['id'], $a['id']); 738 } 739 740 /** 741 * usort callback to sort by page id 742 * @param array $a 743 * @param array $b 744 * @return int 745 */ 746 public function _pagenamesort($a, $b) { 747 if($a['id'] <= $b['id']) return -1; 748 if($a['id'] > $b['id']) return 1; 749 return 0; 750 } 751 752 /** 753 * Return settings read from: 754 * 1. url parameters 755 * 2. plugin config 756 * 3. global config 757 * 758 * @return array 759 */ 760 protected function loadExportConfig() { 761 global $INPUT; 762 global $conf; 763 764 $this->exportConfig = array(); 765 766 // decide on the paper setup from param or config 767 $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true); 768 $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true); 769 770 // decide on the font-size from param or config 771 $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true); 772 773 $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided')); 774 $this->exportConfig['doublesided'] = $doublesided ? '1' : '0'; 775 776 $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc')); 777 $levels = array(); 778 if($hasToC) { 779 $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true); 780 list($top_input, $max_input) = explode('-', $toclevels, 2); 781 list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2); 782 $bounds_input = array( 783 'top' => array( 784 (int) $top_input, 785 (int) $top_conf 786 ), 787 'max' => array( 788 (int) $max_input, 789 (int) $max_conf 790 ) 791 ); 792 $bounds = array( 793 'top' => $conf['toptoclevel'], 794 'max' => $conf['maxtoclevel'] 795 796 ); 797 foreach($bounds_input as $bound => $values) { 798 foreach($values as $value) { 799 if($value > 0 && $value <= 5) { 800 //stop at valid value and store 801 $bounds[$bound] = $value; 802 break; 803 } 804 } 805 } 806 807 if($bounds['max'] < $bounds['top']) { 808 $bounds['max'] = $bounds['top']; 809 } 810 811 for($level = $bounds['top']; $level <= $bounds['max']; $level++) { 812 $levels["H$level"] = $level - 1; 813 } 814 } 815 $this->exportConfig['hasToC'] = $hasToC; 816 $this->exportConfig['levels'] = $levels; 817 818 $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true); 819 820 $tplconf = $this->getConf('template'); 821 $tpl = $INPUT->str('tpl', $tplconf, true); 822 if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) { 823 $tpl = $tplconf; 824 } 825 if(!$tpl){ 826 $tpl = 'default'; 827 } 828 $this->exportConfig['template'] = $tpl; 829 830 $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml'); 831 } 832 833 /** 834 * Returns requested config 835 * 836 * @param string $name 837 * @param mixed $notset 838 * @return mixed|bool 839 */ 840 public function getExportConfig($name, $notset = false) { 841 if ($this->exportConfig === null){ 842 $this->loadExportConfig(); 843 } 844 845 if(isset($this->exportConfig[$name])){ 846 return $this->exportConfig[$name]; 847 }else{ 848 return $notset; 849 } 850 } 851 852 /** 853 * Add 'export pdf'-button to pagetools 854 * 855 * @param Doku_Event $event 856 */ 857 public function addbutton(Doku_Event $event) { 858 global $ID, $REV; 859 860 if($this->getConf('showexportbutton') && $event->data['view'] == 'main') { 861 $params = array('do' => 'export_pdf'); 862 if($REV) { 863 $params['rev'] = $REV; 864 } 865 866 // insert button at position before last (up to top) 867 $event->data['items'] = array_slice($event->data['items'], 0, -1, true) + 868 array('export_pdf' => 869 '<li>' 870 . '<a href="' . wl($ID, $params) . '" class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">' 871 . '<span>' . $this->getLang('export_pdf_button') . '</span>' 872 . '</a>' 873 . '</li>' 874 ) + 875 array_slice($event->data['items'], -1, 1, true); 876 } 877 } 878} 879