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