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