1ee19bac3SLuigi Micco<?php 2ee19bac3SLuigi Micco/** 3ee19bac3SLuigi Micco * dw2Pdf Plugin: Conversion from dokuwiki content to pdf. 4ee19bac3SLuigi Micco * 5ee19bac3SLuigi Micco * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6ee19bac3SLuigi Micco * @author Luigi Micco <l.micco@tiscali.it> 75db42babSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8ee19bac3SLuigi Micco */ 9ee19bac3SLuigi Micco 10ee19bac3SLuigi Micco// must be run within Dokuwiki 11ee19bac3SLuigi Miccoif(!defined('DOKU_INC')) die(); 12ee19bac3SLuigi Micco 130639157eSGerrit Uitslag/** 140639157eSGerrit Uitslag * Class action_plugin_dw2pdf 150639157eSGerrit Uitslag * 160639157eSGerrit Uitslag * Export hmtl content to pdf, for different url parameter configurations 170639157eSGerrit Uitslag * DokuPDF which extends mPDF is used for generating the pdf from html. 180639157eSGerrit Uitslag */ 191ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin { 2002f9a447SGerrit Uitslag /** 2102f9a447SGerrit Uitslag * Settings for current export, collected from url param, plugin config, global config 2202f9a447SGerrit Uitslag * 2302f9a447SGerrit Uitslag * @var array 2402f9a447SGerrit Uitslag */ 25213fdb75SGerrit Uitslag protected $exportConfig = null; 2660e59de7SGerrit Uitslag protected $tpl; 2760e59de7SGerrit Uitslag protected $list = array(); 281c14c879SAndreas Gohr 291c14c879SAndreas Gohr /** 301c14c879SAndreas Gohr * Constructor. Sets the correct template 311c14c879SAndreas Gohr */ 326be736bfSGerrit Uitslag public function __construct() { 3302f9a447SGerrit Uitslag $this->tpl = $this->getExportConfig('template'); 341c14c879SAndreas Gohr } 351c14c879SAndreas Gohr 36ee19bac3SLuigi Micco /** 37ee19bac3SLuigi Micco * Register the events 38177a7d30SGerrit Uitslag * 39177a7d30SGerrit Uitslag * @param Doku_Event_Handler $controller 40ee19bac3SLuigi Micco */ 416be736bfSGerrit Uitslag public function register(Doku_Event_Handler $controller) { 42ee19bac3SLuigi Micco $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array()); 436be736bfSGerrit Uitslag $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array()); 44ee19bac3SLuigi Micco } 45ee19bac3SLuigi Micco 461c14c879SAndreas Gohr /** 471c14c879SAndreas Gohr * Do the HTML to PDF conversion work 48737417c6SKlap-in * 49737417c6SKlap-in * @param Doku_Event $event 50737417c6SKlap-in * @return bool 511c14c879SAndreas Gohr */ 5244e8e8fbSGerrit Uitslag public function convert(Doku_Event $event) { 53ee19bac3SLuigi Micco global $ACT; 54ee19bac3SLuigi Micco global $ID; 55ee19bac3SLuigi Micco 561ef68647SAndreas Gohr // our event? 57ad18f4e1SGerrit Uitslag if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false; 58ee19bac3SLuigi Micco 591ef68647SAndreas Gohr // check user's rights 601ef68647SAndreas Gohr if(auth_quickaclcheck($ID) < AUTH_READ) return false; 611ef68647SAndreas Gohr 62d63e7fe7SGerrit Uitslag if($data = $this->collectExportPages($event)) { 63d63e7fe7SGerrit Uitslag list($title, $this->list) = $data; 64d63e7fe7SGerrit Uitslag } else { 65d63e7fe7SGerrit Uitslag return false; 66d63e7fe7SGerrit Uitslag } 67d63e7fe7SGerrit Uitslag 68d63e7fe7SGerrit Uitslag // it's ours, no one else's 69d63e7fe7SGerrit Uitslag $event->preventDefault(); 70d63e7fe7SGerrit Uitslag 71a58f45f0SGerrit Uitslag // prepare cache and its dependencies 72a58f45f0SGerrit Uitslag $depends = array(); 73a58f45f0SGerrit Uitslag $cache = $this->prepareCache($title, $depends); 74d63e7fe7SGerrit Uitslag 75d63e7fe7SGerrit Uitslag // hard work only when no cache available 76d63e7fe7SGerrit Uitslag if(!$this->getConf('usecache') || !$cache->useCache($depends)) { 77d63e7fe7SGerrit Uitslag $this->generatePDF($cache->cache, $title); 78d63e7fe7SGerrit Uitslag } 79d63e7fe7SGerrit Uitslag 80d63e7fe7SGerrit Uitslag // deliver the file 81d63e7fe7SGerrit Uitslag $this->sendPDFFile($cache->cache, $title); 82d63e7fe7SGerrit Uitslag return true; 83d63e7fe7SGerrit Uitslag } 84d63e7fe7SGerrit Uitslag 85d63e7fe7SGerrit Uitslag 86d63e7fe7SGerrit Uitslag /** 87d63e7fe7SGerrit Uitslag * Obtain list of pages and title, based on url parameters 88d63e7fe7SGerrit Uitslag * 89d63e7fe7SGerrit Uitslag * @param Doku_Event $event 90d63e7fe7SGerrit Uitslag * @return string|bool 91d63e7fe7SGerrit Uitslag */ 92d63e7fe7SGerrit Uitslag protected function collectExportPages(Doku_Event $event) { 93d63e7fe7SGerrit Uitslag global $ACT; 94d63e7fe7SGerrit Uitslag global $ID; 95d63e7fe7SGerrit Uitslag global $INPUT; 96d63e7fe7SGerrit Uitslag global $conf; 97d63e7fe7SGerrit Uitslag 98d63e7fe7SGerrit Uitslag // list of one or multiple pages 99d63e7fe7SGerrit Uitslag $list = array(); 10028e636eaSGerrit Uitslag 10187c86ddaSAndreas Gohr if($ACT == 'export_pdf') { 102d63e7fe7SGerrit Uitslag $list[0] = $ID; 103177a7d30SGerrit Uitslag $title = $INPUT->str('pdftitle'); //DEPRECATED 104177a7d30SGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 105177a7d30SGerrit Uitslag if(empty($title)) { 106737417c6SKlap-in $title = p_get_first_heading($ID); 10715923cb9SGerrit Uitslag } 108ad18f4e1SGerrit Uitslag 109ad18f4e1SGerrit Uitslag } elseif($ACT == 'export_pdfns') { 110ad18f4e1SGerrit Uitslag //check input for title and ns 111177a7d30SGerrit Uitslag if(!$title = $INPUT->str('book_title')) { 11226be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needtitle'); 113ad18f4e1SGerrit Uitslag return false; 114ad18f4e1SGerrit Uitslag } 115177a7d30SGerrit Uitslag $pdfnamespace = cleanID($INPUT->str('book_ns')); 116ad18f4e1SGerrit Uitslag if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) { 11726be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needns'); 118ad18f4e1SGerrit Uitslag return false; 119ad18f4e1SGerrit Uitslag } 120ad18f4e1SGerrit Uitslag 12126be4eceSGerrit Uitslag //sort order 122177a7d30SGerrit Uitslag $order = $INPUT->str('book_order', 'natural', true); 123ad18f4e1SGerrit Uitslag $sortoptions = array('pagename', 'date', 'natural'); 124ad18f4e1SGerrit Uitslag if(!in_array($order, $sortoptions)) { 125ad18f4e1SGerrit Uitslag $order = 'natural'; 126ad18f4e1SGerrit Uitslag } 127ad18f4e1SGerrit Uitslag 12826be4eceSGerrit Uitslag //search depth 129177a7d30SGerrit Uitslag $depth = $INPUT->int('book_nsdepth', 0); 130ad18f4e1SGerrit Uitslag if($depth < 0) { 131ad18f4e1SGerrit Uitslag $depth = 0; 132ad18f4e1SGerrit Uitslag } 13326be4eceSGerrit Uitslag 134ad18f4e1SGerrit Uitslag //page search 135ad18f4e1SGerrit Uitslag $result = array(); 136ad18f4e1SGerrit Uitslag $opts = array('depth' => $depth); //recursive all levels 137ad18f4e1SGerrit Uitslag $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace)); 138ad18f4e1SGerrit Uitslag search($result, $conf['datadir'], 'search_allpages', $opts, $dir); 139ad18f4e1SGerrit Uitslag 14026be4eceSGerrit Uitslag //sorting 141ad18f4e1SGerrit Uitslag if(count($result) > 0) { 142ad18f4e1SGerrit Uitslag if($order == 'date') { 143ad18f4e1SGerrit Uitslag usort($result, array($this, '_datesort')); 144ad18f4e1SGerrit Uitslag } elseif($order == 'pagename') { 145ad18f4e1SGerrit Uitslag usort($result, array($this, '_pagenamesort')); 146ad18f4e1SGerrit Uitslag } 147ad18f4e1SGerrit Uitslag } 148ad18f4e1SGerrit Uitslag 149ad18f4e1SGerrit Uitslag foreach($result as $item) { 150d63e7fe7SGerrit Uitslag $list[] = $item['id']; 151ad18f4e1SGerrit Uitslag } 152ad18f4e1SGerrit Uitslag 153baa31dc5SGerrit Uitslag if ($pdfnamespace !== '') { 154baa31dc5SGerrit Uitslag if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { 155baa31dc5SGerrit Uitslag if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) { 156baa31dc5SGerrit Uitslag array_unshift($list,rtrim($pdfnamespace,':')); 157baa31dc5SGerrit Uitslag } 158baa31dc5SGerrit Uitslag } 159baa31dc5SGerrit Uitslag } 160baa31dc5SGerrit Uitslag 161737417c6SKlap-in } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 162b3eed6e3SGerrit Uitslag /** @deprecated April 2016 replaced by localStorage version of Bookcreator*/ 16326be4eceSGerrit Uitslag //is in Bookmanager of bookcreator plugin a title given? 164177a7d30SGerrit Uitslag $title = $INPUT->str('pdfbook_title'); //DEPRECATED 165177a7d30SGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 166177a7d30SGerrit Uitslag if(empty($title)) { 16726be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needtitle'); 168737417c6SKlap-in return false; 16926be4eceSGerrit Uitslag } else { 170d63e7fe7SGerrit Uitslag $list = explode("|", $_COOKIE['list-pagelist']); 17126be4eceSGerrit Uitslag } 172ad18f4e1SGerrit Uitslag 173b3eed6e3SGerrit Uitslag } elseif($INPUT->has('selection')) { 174b3eed6e3SGerrit Uitslag //handle Bookcreator requests based at localStorage 175b3eed6e3SGerrit Uitslag// if(!checkSecurityToken()) { 176b3eed6e3SGerrit Uitslag// http_status(403); 177b3eed6e3SGerrit Uitslag// print $this->getLang('empty'); 178b3eed6e3SGerrit Uitslag// exit(); 179b3eed6e3SGerrit Uitslag// } 180b3eed6e3SGerrit Uitslag 181b3eed6e3SGerrit Uitslag $json = new JSON(JSON_LOOSE_TYPE); 182b3eed6e3SGerrit Uitslag $list = $json->decode($INPUT->post->str('selection', '', true)); 183b3eed6e3SGerrit Uitslag if(!is_array($list) || empty($list)) { 184b3eed6e3SGerrit Uitslag http_status(400); 185b3eed6e3SGerrit Uitslag print $this->getLang('empty'); 186b3eed6e3SGerrit Uitslag exit(); 187b3eed6e3SGerrit Uitslag } 188b3eed6e3SGerrit Uitslag 189b3eed6e3SGerrit Uitslag $title = $INPUT->str('pdfbook_title'); //DEPRECATED 190b3eed6e3SGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 191b3eed6e3SGerrit Uitslag if(empty($title)) { 192b3eed6e3SGerrit Uitslag http_status(400); 193b3eed6e3SGerrit Uitslag print $this->getLang('needtitle'); 194b3eed6e3SGerrit Uitslag exit(); 195b3eed6e3SGerrit Uitslag } 196b3eed6e3SGerrit Uitslag 197737417c6SKlap-in } else { 19826be4eceSGerrit Uitslag //show empty bookcreator message 19926be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'empty'); 200737417c6SKlap-in return false; 201737417c6SKlap-in } 202737417c6SKlap-in 203719256adSGerrit Uitslag $list = array_map('cleanID', $list); 204*c7138b3fSGerrit Uitslag 205*c7138b3fSGerrit Uitslag $skippedpages = array(); 206*c7138b3fSGerrit Uitslag foreach($list as $index => $pageid) { 207*c7138b3fSGerrit Uitslag if(auth_quickaclcheck($pageid) < AUTH_READ) { 208*c7138b3fSGerrit Uitslag $skippedpages[] = $pageid; 209*c7138b3fSGerrit Uitslag unset($list[$index]); 210*c7138b3fSGerrit Uitslag } 211*c7138b3fSGerrit Uitslag } 212*c7138b3fSGerrit Uitslag $list = array_filter($list); //removes also pages mentioned '0' 213*c7138b3fSGerrit Uitslag 214*c7138b3fSGerrit Uitslag //if selection contains forbidden pages throw (overridable) warning 215*c7138b3fSGerrit Uitslag if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) { 216*c7138b3fSGerrit Uitslag $msg = hsc(join(', ', $skippedpages)); 217*c7138b3fSGerrit Uitslag if($INPUT->has('selection')) { 218*c7138b3fSGerrit Uitslag http_status(400); 219*c7138b3fSGerrit Uitslag print sprintf($this->getLang('forbidden'), $msg); 220*c7138b3fSGerrit Uitslag exit(); 221*c7138b3fSGerrit Uitslag } else { 222*c7138b3fSGerrit Uitslag $this->showPageWithErrorMsg($event, 'forbidden', $msg); 223*c7138b3fSGerrit Uitslag return false; 224*c7138b3fSGerrit Uitslag } 225*c7138b3fSGerrit Uitslag 226*c7138b3fSGerrit Uitslag } 227*c7138b3fSGerrit Uitslag 228d63e7fe7SGerrit Uitslag return array($title, $list); 229d63e7fe7SGerrit Uitslag } 230d63e7fe7SGerrit Uitslag 231a58f45f0SGerrit Uitslag /** 232a58f45f0SGerrit Uitslag * Prepare cache 233a58f45f0SGerrit Uitslag * 234a58f45f0SGerrit Uitslag * @param string $title 235a58f45f0SGerrit Uitslag * @param array $depends (reference) array with dependencies 236a58f45f0SGerrit Uitslag * @return cache 237a58f45f0SGerrit Uitslag */ 238a58f45f0SGerrit Uitslag protected function prepareCache($title, &$depends) { 239a58f45f0SGerrit Uitslag global $REV; 240a58f45f0SGerrit Uitslag 241ee19bac3SLuigi Micco $cachekey = join(',', $this->list) 242ee19bac3SLuigi Micco . $REV 243ee19bac3SLuigi Micco . $this->getExportConfig('template') 244ee19bac3SLuigi Micco . $this->getExportConfig('pagesize') 245ee19bac3SLuigi Micco . $this->getExportConfig('orientation') 246ee19bac3SLuigi Micco . $this->getExportConfig('doublesided') 247ee19bac3SLuigi Micco . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0') 248ee19bac3SLuigi Micco . $title; 249ee19bac3SLuigi Micco $cache = new cache($cachekey, '.dw2.pdf'); 250ee19bac3SLuigi Micco 251ee19bac3SLuigi Micco $dependencies = array(); 252ee19bac3SLuigi Micco foreach($this->list as $pageid) { 253ee19bac3SLuigi Micco $relations = p_get_metadata($pageid, 'relation'); 254ee19bac3SLuigi Micco 255ee19bac3SLuigi Micco if(is_array($relations)) { 256ee19bac3SLuigi Micco if(array_key_exists('media', $relations) && is_array($relations['media'])) { 257ee19bac3SLuigi Micco foreach($relations['media'] as $mediaid => $exists) { 258ee19bac3SLuigi Micco if($exists) { 259ee19bac3SLuigi Micco $dependencies[] = mediaFN($mediaid); 260ee19bac3SLuigi Micco } 261ee19bac3SLuigi Micco } 262ee19bac3SLuigi Micco } 263ee19bac3SLuigi Micco 264ee19bac3SLuigi Micco if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) { 265ee19bac3SLuigi Micco foreach($relations['haspart'] as $part_pageid => $exists) { 266ee19bac3SLuigi Micco if($exists) { 267ee19bac3SLuigi Micco $dependencies[] = wikiFN($part_pageid); 268ee19bac3SLuigi Micco } 269ee19bac3SLuigi Micco } 270ee19bac3SLuigi Micco } 271ee19bac3SLuigi Micco } 272ee19bac3SLuigi Micco 273ee19bac3SLuigi Micco $dependencies[] = metaFN($pageid, '.meta'); 274ee19bac3SLuigi Micco } 275ee19bac3SLuigi Micco 276ee19bac3SLuigi Micco $depends['files'] = array_map('wikiFN', $this->list); 277ee19bac3SLuigi Micco $depends['files'][] = __FILE__; 278ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/renderer.php'; 279ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php'; 280ee19bac3SLuigi Micco $depends['files'] = array_merge( 281ee19bac3SLuigi Micco $depends['files'], 282ee19bac3SLuigi Micco $dependencies, 283ee19bac3SLuigi Micco getConfigFiles('main') 284ee19bac3SLuigi Micco ); 285a58f45f0SGerrit Uitslag return $cache; 286ee19bac3SLuigi Micco } 287ee19bac3SLuigi Micco 288d63e7fe7SGerrit Uitslag /** 289d63e7fe7SGerrit Uitslag * Set error notification and reload page again 290d63e7fe7SGerrit Uitslag * 291d63e7fe7SGerrit Uitslag * @param Doku_Event $event 292d63e7fe7SGerrit Uitslag * @param string $msglangkey key of translation key 293*c7138b3fSGerrit Uitslag * @param string $replacement 294d63e7fe7SGerrit Uitslag */ 295*c7138b3fSGerrit Uitslag private function showPageWithErrorMsg(Doku_Event $event, $msglangkey, $replacement=null) { 296*c7138b3fSGerrit Uitslag if(empty($replacement)) { 297*c7138b3fSGerrit Uitslag $msg = $this->getLang($msglangkey); 298*c7138b3fSGerrit Uitslag } else { 299*c7138b3fSGerrit Uitslag $msg = sprintf($this->getLang($msglangkey), $replacement); 300*c7138b3fSGerrit Uitslag } 301*c7138b3fSGerrit Uitslag msg($msg, -1); 302d63e7fe7SGerrit Uitslag 303d63e7fe7SGerrit Uitslag $event->data = 'show'; 304d63e7fe7SGerrit Uitslag $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url 305d63e7fe7SGerrit Uitslag } 306d63e7fe7SGerrit Uitslag 307d63e7fe7SGerrit Uitslag /** 308d63e7fe7SGerrit Uitslag * Build a pdf from the html 309d63e7fe7SGerrit Uitslag * 310d63e7fe7SGerrit Uitslag * @param string $cachefile 311d63e7fe7SGerrit Uitslag * @param string $title 312d63e7fe7SGerrit Uitslag */ 313d63e7fe7SGerrit Uitslag protected function generatePDF($cachefile, $title) { 314d63e7fe7SGerrit Uitslag global $ID; 315d63e7fe7SGerrit Uitslag global $REV; 316d63e7fe7SGerrit Uitslag global $INPUT; 31787c86ddaSAndreas Gohr 31802f9a447SGerrit Uitslag //some shortcuts to export settings 31902f9a447SGerrit Uitslag $hasToC = $this->getExportConfig('hasToC'); 32002f9a447SGerrit Uitslag $levels = $this->getExportConfig('levels'); 32102f9a447SGerrit Uitslag $isDebug = $this->getExportConfig('isDebug'); 3226ea88a05SAndreas Gohr 3231ef68647SAndreas Gohr // initialize PDF library 324cde5a1b3SAndreas Gohr require_once(dirname(__FILE__) . "/DokuPDF.class.php"); 3256ea88a05SAndreas Gohr 32602f9a447SGerrit Uitslag $mpdf = new DokuPDF($this->getExportConfig('pagesize'), $this->getExportConfig('orientation')); 327ee19bac3SLuigi Micco 328d62df65bSAndreas Gohr // let mpdf fix local links 329d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 330d62df65bSAndreas Gohr $url = $self['scheme'] . '://' . $self['host']; 33102f9a447SGerrit Uitslag if($self['port']) { 33202f9a447SGerrit Uitslag $url .= ':' . $self['port']; 33302f9a447SGerrit Uitslag } 334d62df65bSAndreas Gohr $mpdf->setBasePath($url); 335d62df65bSAndreas Gohr 33656d13144SAndreas Gohr // Set the title 33756d13144SAndreas Gohr $mpdf->SetTitle($title); 33856d13144SAndreas Gohr 339d63e7fe7SGerrit Uitslag // some default document settings 340d63e7fe7SGerrit Uitslag //note: double-sided document, starts at an odd page (first page is a right-hand side page) 341213fdb75SGerrit Uitslag // single-side document has only odd pages 342213fdb75SGerrit Uitslag $mpdf->mirrorMargins = $this->getExportConfig('doublesided'); 343daa70883SAndreas Gohr $mpdf->setAutoTopMargin = 'stretch'; 344daa70883SAndreas Gohr $mpdf->setAutoBottomMargin = 'stretch'; 34502f9a447SGerrit Uitslag// $mpdf->pagenumSuffix = '/'; //prefix for {nbpg} 34602f9a447SGerrit Uitslag if($hasToC) { 34702f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC 34802f9a447SGerrit Uitslag $mpdf->h2toc = $levels; 34902f9a447SGerrit Uitslag } else { 35002f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'); 35102f9a447SGerrit Uitslag } 3522eedf77dSAndreas Gohr 35356d13144SAndreas Gohr // load the template 3541c14c879SAndreas Gohr $template = $this->load_template($title); 355ee19bac3SLuigi Micco 3561ef68647SAndreas Gohr // prepare HTML header styles 357a2c33768SGerrit Uitslag $html = ''; 35802f9a447SGerrit Uitslag if($isDebug) { 359a2c33768SGerrit Uitslag $html .= '<html><head>'; 360737417c6SKlap-in $html .= '<style type="text/css">'; 361a2c33768SGerrit Uitslag } 362a2c33768SGerrit Uitslag $styles = $this->load_css(); 363a2c33768SGerrit Uitslag $styles .= '@page { size:auto; ' . $template['page'] . '}'; 364a2c33768SGerrit Uitslag $styles .= '@page :first {' . $template['first'] . '}'; 365254467c4SGerrit Uitslag 366254467c4SGerrit Uitslag $styles .= '@page landscape-page { size:landscape }'; 367254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-landscape { page:landscape-page }'; 368254467c4SGerrit Uitslag $styles .= '@page portrait-page { size:portrait }'; 369254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-portrait { page:portrait-page }'; 370254467c4SGerrit Uitslag 371a2c33768SGerrit Uitslag $mpdf->WriteHTML($styles, 1); 372a2c33768SGerrit Uitslag 37302f9a447SGerrit Uitslag if($isDebug) { 374a2c33768SGerrit Uitslag $html .= $styles; 3751ef68647SAndreas Gohr $html .= '</style>'; 3761ef68647SAndreas Gohr $html .= '</head><body>'; 377a2c33768SGerrit Uitslag } 378a2c33768SGerrit Uitslag 379a2c33768SGerrit Uitslag $body_start = $template['html']; 380a2c33768SGerrit Uitslag $body_start .= '<div class="dokuwiki">'; 3812eedf77dSAndreas Gohr 3821e45476bSmnapp // insert the cover page 383a2c33768SGerrit Uitslag $body_start .= $template['cover']; 384a2c33768SGerrit Uitslag 385a2c33768SGerrit Uitslag $mpdf->WriteHTML($body_start, 2, true, false); //start body html 38602f9a447SGerrit Uitslag if($isDebug) { 387a2c33768SGerrit Uitslag $html .= $body_start; 388a2c33768SGerrit Uitslag } 38902f9a447SGerrit Uitslag if($hasToC) { 39002f9a447SGerrit Uitslag //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 39102f9a447SGerrit Uitslag // - first page of ToC starts always at odd page (so eventually an additional blank page is included before) 39202f9a447SGerrit Uitslag // - there is no page numbering at the pages of the ToC 39302f9a447SGerrit Uitslag $mpdf->TOCpagebreakByArray( 39402f9a447SGerrit Uitslag array( 395230b098dSGerrit Uitslag 'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>', 396230b098dSGerrit Uitslag 'toc-bookmarkText' => $this->getLang('tocheader'), 39702f9a447SGerrit Uitslag 'links' => true, 39802f9a447SGerrit Uitslag 'outdent' => '1em', 39902f9a447SGerrit Uitslag 'resetpagenum' => true, //start pagenumbering after ToC 40002f9a447SGerrit Uitslag 'pagenumstyle' => '1' 40102f9a447SGerrit Uitslag ) 40202f9a447SGerrit Uitslag ); 40302f9a447SGerrit Uitslag $html .= '<tocpagebreak>'; 40402f9a447SGerrit Uitslag } 40502f9a447SGerrit Uitslag 406c00eb13bSGerrit Uitslag // store original pageid 407c00eb13bSGerrit Uitslag $keep = $ID; 408c00eb13bSGerrit Uitslag 4091ef68647SAndreas Gohr // loop over all pages 410*c7138b3fSGerrit Uitslag $counter = 0; 411*c7138b3fSGerrit Uitslag $no_pages = count($this->list); 412*c7138b3fSGerrit Uitslag foreach($this->list as $page) { 413*c7138b3fSGerrit Uitslag $counter++; 414b3eed6e3SGerrit Uitslag $filename = wikiFN($page, $REV); 415b3eed6e3SGerrit Uitslag 416b3eed6e3SGerrit Uitslag if(!file_exists($filename)) { 417b3eed6e3SGerrit Uitslag continue; 418b3eed6e3SGerrit Uitslag } 419ee19bac3SLuigi Micco 420c00eb13bSGerrit Uitslag // set global pageid to the rendered page 421c00eb13bSGerrit Uitslag $ID = $page; 422c00eb13bSGerrit Uitslag 423b3eed6e3SGerrit Uitslag $pagehtml = p_cached_output($filename, 'dw2pdf', $page); 424719256adSGerrit Uitslag $pagehtml .= $this->page_depend_replacements($template['cite'], $page); 425*c7138b3fSGerrit Uitslag if($counter < $no_pages) { 426a2c33768SGerrit Uitslag $pagehtml .= '<pagebreak />'; 427a2c33768SGerrit Uitslag } 428a2c33768SGerrit Uitslag 429a2c33768SGerrit Uitslag $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html 43002f9a447SGerrit Uitslag if($isDebug) { 431a2c33768SGerrit Uitslag $html .= $pagehtml; 4321ef68647SAndreas Gohr } 433ee19bac3SLuigi Micco } 434c00eb13bSGerrit Uitslag //restore ID 435c00eb13bSGerrit Uitslag $ID = $keep; 436ee19bac3SLuigi Micco 43733c15297SGerrit Uitslag // insert the back page 438a2c33768SGerrit Uitslag $body_end = $template['back']; 43933c15297SGerrit Uitslag 440a2c33768SGerrit Uitslag $body_end .= '</div>'; 441a2c33768SGerrit Uitslag 442d63e7fe7SGerrit Uitslag $mpdf->WriteHTML($body_end, 2, false, true); // finish body html 44302f9a447SGerrit Uitslag if($isDebug) { 444a2c33768SGerrit Uitslag $html .= $body_end; 445eeb17e15SAndreas Gohr $html .= '</body>'; 446eeb17e15SAndreas Gohr $html .= '</html>'; 447a2c33768SGerrit Uitslag } 448f765508eSGerrit Uitslag 449f765508eSGerrit Uitslag //Return html for debugging 45002f9a447SGerrit Uitslag if($isDebug) { 45102f9a447SGerrit Uitslag if($INPUT->str('debughtml', 'text', true) == 'html') { 45226be4eceSGerrit Uitslag echo $html; 453a2c33768SGerrit Uitslag } else { 454a2c33768SGerrit Uitslag header('Content-Type: text/plain; charset=utf-8'); 455a2c33768SGerrit Uitslag echo $html; 456a2c33768SGerrit Uitslag } 45726be4eceSGerrit Uitslag exit(); 45826be4eceSGerrit Uitslag }; 459f765508eSGerrit Uitslag 46087c86ddaSAndreas Gohr // write to cache file 461d63e7fe7SGerrit Uitslag $mpdf->Output($cachefile, 'F'); 46287c86ddaSAndreas Gohr } 46387c86ddaSAndreas Gohr 464d63e7fe7SGerrit Uitslag /** 465d63e7fe7SGerrit Uitslag * @param string $cachefile 466d63e7fe7SGerrit Uitslag * @param string $title 467d63e7fe7SGerrit Uitslag */ 468d63e7fe7SGerrit Uitslag protected function sendPDFFile($cachefile, $title) { 46987c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 470b853b723SAndreas Gohr header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 47187c86ddaSAndreas Gohr header('Pragma: public'); 472d63e7fe7SGerrit Uitslag http_conditionalRequest(filemtime($cachefile)); 47387c86ddaSAndreas Gohr 4749a3c8d9fSAndreas Gohr $filename = rawurlencode(cleanID(strtr($title, ':/;"', ' '))); 47587c86ddaSAndreas Gohr if($this->getConf('output') == 'file') { 4769a3c8d9fSAndreas Gohr header('Content-Disposition: attachment; filename="' . $filename . '.pdf";'); 47787c86ddaSAndreas Gohr } else { 4789a3c8d9fSAndreas Gohr header('Content-Disposition: inline; filename="' . $filename . '.pdf";'); 47987c86ddaSAndreas Gohr } 480ee19bac3SLuigi Micco 481b3eed6e3SGerrit Uitslag //Bookcreator uses jQuery.fileDownload.js, which requires a cookie. 482b3eed6e3SGerrit Uitslag header('Set-Cookie: fileDownload=true; path=/'); 483b3eed6e3SGerrit Uitslag 484e993da11SGerrit Uitslag //try to send file, and exit if done 485d63e7fe7SGerrit Uitslag http_sendfile($cachefile); 48687c86ddaSAndreas Gohr 487d63e7fe7SGerrit Uitslag $fp = @fopen($cachefile, "rb"); 48887c86ddaSAndreas Gohr if($fp) { 489d63e7fe7SGerrit Uitslag http_rangeRequest($fp, filesize($cachefile), 'application/pdf'); 49087c86ddaSAndreas Gohr } else { 49187c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 49287c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 49387c86ddaSAndreas Gohr } 4941ef68647SAndreas Gohr exit(); 4951ef68647SAndreas Gohr } 4961ef68647SAndreas Gohr 4976be736bfSGerrit Uitslag /** 4982eedf77dSAndreas Gohr * Load the various template files and prepare the HTML/CSS for insertion 49944e8e8fbSGerrit Uitslag * 50044e8e8fbSGerrit Uitslag * @param string $title 50144e8e8fbSGerrit Uitslag * @return array 5021ef68647SAndreas Gohr */ 5031c14c879SAndreas Gohr protected function load_template($title) { 5041ef68647SAndreas Gohr global $ID; 5051ef68647SAndreas Gohr global $conf; 5061ef68647SAndreas Gohr 5072eedf77dSAndreas Gohr // this is what we'll return 5082eedf77dSAndreas Gohr $output = array( 5091e45476bSmnapp 'cover' => '', 5102eedf77dSAndreas Gohr 'html' => '', 5112eedf77dSAndreas Gohr 'page' => '', 5122eedf77dSAndreas Gohr 'first' => '', 5132eedf77dSAndreas Gohr 'cite' => '', 5142eedf77dSAndreas Gohr ); 5152eedf77dSAndreas Gohr 5162eedf77dSAndreas Gohr // prepare header/footer elements 5172eedf77dSAndreas Gohr $html = ''; 51833c15297SGerrit Uitslag foreach(array('header', 'footer') as $section) { 51933c15297SGerrit Uitslag foreach(array('', '_odd', '_even', '_first') as $order) { 52002f9a447SGerrit Uitslag $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html'; 52133c15297SGerrit Uitslag if(file_exists($file)) { 52233c15297SGerrit Uitslag $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF; 52333c15297SGerrit Uitslag $html .= file_get_contents($file) . DOKU_LF; 52433c15297SGerrit Uitslag $html .= '</htmlpage' . $section . '>' . DOKU_LF; 5252eedf77dSAndreas Gohr 5262eedf77dSAndreas Gohr // register the needed pseudo CSS 52733c15297SGerrit Uitslag if($order == '_first') { 52833c15297SGerrit Uitslag $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 52933c15297SGerrit Uitslag } elseif($order == '_even') { 53033c15297SGerrit Uitslag $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 53133c15297SGerrit Uitslag } elseif($order == '_odd') { 53233c15297SGerrit Uitslag $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 533daa70883SAndreas Gohr } else { 53433c15297SGerrit Uitslag $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 5352eedf77dSAndreas Gohr } 5362eedf77dSAndreas Gohr } 5372eedf77dSAndreas Gohr } 5382eedf77dSAndreas Gohr } 5392eedf77dSAndreas Gohr 5401ef68647SAndreas Gohr // prepare replacements 5411ef68647SAndreas Gohr $replace = array( 5421ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 54302f9a447SGerrit Uitslag '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / ' 5442eedf77dSAndreas Gohr '@TITLE@' => hsc($title), 5451ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 5461ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 5471ef68647SAndreas Gohr '@DATE@' => dformat(time()), 5485d6fbaeaSAndreas Gohr '@BASE@' => DOKU_BASE, 54902f9a447SGerrit Uitslag '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/' 5501ef68647SAndreas Gohr ); 5511ef68647SAndreas Gohr 5522eedf77dSAndreas Gohr // set HTML element 553a180c973SKlap-in $html = str_replace(array_keys($replace), array_values($replace), $html); 554a180c973SKlap-in //TODO For bookcreator $ID (= bookmanager page) makes no sense 555a180c973SKlap-in $output['html'] = $this->page_depend_replacements($html, $ID); 5561ef68647SAndreas Gohr 5571e45476bSmnapp // cover page 55802f9a447SGerrit Uitslag $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html'; 55933c15297SGerrit Uitslag if(file_exists($coverfile)) { 56033c15297SGerrit Uitslag $output['cover'] = file_get_contents($coverfile); 5611e45476bSmnapp $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']); 5629b071da5SMichael $output['cover'] = $this->page_depend_replacements($output['cover'], $ID); 5636e2ec302SGerrit Uitslag $output['cover'] .= '<pagebreak />'; 5641e45476bSmnapp } 5651e45476bSmnapp 56633c15297SGerrit Uitslag // cover page 56702f9a447SGerrit Uitslag $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html'; 56833c15297SGerrit Uitslag if(file_exists($backfile)) { 56933c15297SGerrit Uitslag $output['back'] = '<pagebreak />'; 57033c15297SGerrit Uitslag $output['back'] .= file_get_contents($backfile); 57133c15297SGerrit Uitslag $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']); 5729b071da5SMichael $output['back'] = $this->page_depend_replacements($output['back'], $ID); 57333c15297SGerrit Uitslag } 57433c15297SGerrit Uitslag 5752eedf77dSAndreas Gohr // citation box 57602f9a447SGerrit Uitslag $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html'; 57733c15297SGerrit Uitslag if(file_exists($citationfile)) { 57833c15297SGerrit Uitslag $output['cite'] = file_get_contents($citationfile); 5792eedf77dSAndreas Gohr $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 5802eedf77dSAndreas Gohr } 5811ef68647SAndreas Gohr 5822eedf77dSAndreas Gohr return $output; 5831ef68647SAndreas Gohr } 5841ef68647SAndreas Gohr 5851ef68647SAndreas Gohr /** 586a180c973SKlap-in * @param string $raw code with placeholders 587a180c973SKlap-in * @param string $id pageid 588a180c973SKlap-in * @return string 589a180c973SKlap-in */ 590a180c973SKlap-in protected function page_depend_replacements($raw, $id) { 591a180c973SKlap-in global $REV; 592a180c973SKlap-in 593a180c973SKlap-in // generate qr code for this page using google infographics api 594a180c973SKlap-in $qr_code = ''; 595a180c973SKlap-in if($this->getConf('qrcodesize')) { 596a180c973SKlap-in $url = urlencode(wl($id, '', '&', true)); 597a180c973SKlap-in $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' . 598a180c973SKlap-in $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />'; 599a180c973SKlap-in } 600a180c973SKlap-in // prepare replacements 601a180c973SKlap-in $replace['@ID@'] = $id; 602a180c973SKlap-in $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV))); 603a180c973SKlap-in $replace['@PAGEURL@'] = wl($id, ($REV) ? array('rev' => $REV) : false, true, "&"); 604a180c973SKlap-in $replace['@QRCODE@'] = $qr_code; 605a180c973SKlap-in 606e3d68265SGerrit Uitslag $content = str_replace(array_keys($replace), array_values($replace), $raw); 607e3d68265SGerrit Uitslag 608e3d68265SGerrit Uitslag // @DATE(<date>[, <format>])@ 609e3d68265SGerrit Uitslag $content = preg_replace_callback( 610e3d68265SGerrit Uitslag '/@DATE\((.*?)(?:,\s*(.*?))?\)@/', 611e3d68265SGerrit Uitslag array($this, 'replacedate'), 612e3d68265SGerrit Uitslag $content 613e3d68265SGerrit Uitslag ); 614e3d68265SGerrit Uitslag 615e3d68265SGerrit Uitslag return $content; 616a180c973SKlap-in } 617a180c973SKlap-in 618e3d68265SGerrit Uitslag 619e3d68265SGerrit Uitslag /** 620e3d68265SGerrit Uitslag * (callback) Replace date by request datestring 621e3d68265SGerrit Uitslag * e.g. '%m(30-11-1975)' is replaced by '11' 622e3d68265SGerrit Uitslag * 623e3d68265SGerrit Uitslag * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern 624e3d68265SGerrit Uitslag * @return string 625e3d68265SGerrit Uitslag */ 626e3d68265SGerrit Uitslag function replacedate($match) { 627e3d68265SGerrit Uitslag global $conf; 628e3d68265SGerrit Uitslag //no 2nd argument for default date format 629e3d68265SGerrit Uitslag if($match[2] == null) { 630e3d68265SGerrit Uitslag $match[2] = $conf['dformat']; 631e3d68265SGerrit Uitslag } 632e3d68265SGerrit Uitslag return strftime($match[2], strtotime($match[1])); 633e3d68265SGerrit Uitslag } 634e3d68265SGerrit Uitslag 635e3d68265SGerrit Uitslag 636a180c973SKlap-in /** 6371c14c879SAndreas Gohr * Load all the style sheets and apply the needed replacements 6381ef68647SAndreas Gohr */ 6391c14c879SAndreas Gohr protected function load_css() { 640737417c6SKlap-in global $conf; 6411c14c879SAndreas Gohr //reusue the CSS dispatcher functions without triggering the main function 6421c14c879SAndreas Gohr define('SIMPLE_TEST', 1); 6431c14c879SAndreas Gohr require_once(DOKU_INC . 'lib/exe/css.php'); 644ee19bac3SLuigi Micco 6451c14c879SAndreas Gohr // prepare CSS files 6461c14c879SAndreas Gohr $files = array_merge( 6471c14c879SAndreas Gohr array( 6481c14c879SAndreas Gohr DOKU_INC . 'lib/styles/screen.css' 6491c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 6501c14c879SAndreas Gohr DOKU_INC . 'lib/styles/print.css' 6511c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 6521c14c879SAndreas Gohr ), 6531c14c879SAndreas Gohr css_pluginstyles('all'), 65458e6409eSAndreas Gohr $this->css_pluginPDFstyles(), 6551c14c879SAndreas Gohr array( 6561c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.css' 6571c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 6581c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' 6591c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 6601c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' 6611c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 6621c14c879SAndreas Gohr ) 6631c14c879SAndreas Gohr ); 6641c14c879SAndreas Gohr $css = ''; 6651c14c879SAndreas Gohr foreach($files as $file => $location) { 66628e636eaSGerrit Uitslag $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 66728e636eaSGerrit Uitslag $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 6681c14c879SAndreas Gohr $css .= css_loadfile($file, $location); 6691ef68647SAndreas Gohr } 6701ef68647SAndreas Gohr 67128e636eaSGerrit Uitslag if(function_exists('css_parseless')) { 6721c14c879SAndreas Gohr // apply pattern replacements 67328e636eaSGerrit Uitslag $styleini = css_styleini($conf['template']); 67428e636eaSGerrit Uitslag $css = css_applystyle($css, $styleini['replacements']); 67528e636eaSGerrit Uitslag 67628e636eaSGerrit Uitslag // parse less 67728e636eaSGerrit Uitslag $css = css_parseless($css); 67828e636eaSGerrit Uitslag } else { 67928e636eaSGerrit Uitslag // @deprecated 2013-12-19: fix backward compatibility 6801c14c879SAndreas Gohr $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 68128e636eaSGerrit Uitslag } 6821ef68647SAndreas Gohr 6831c14c879SAndreas Gohr return $css; 684ee19bac3SLuigi Micco } 6851c14c879SAndreas Gohr 68658e6409eSAndreas Gohr /** 68758e6409eSAndreas Gohr * Returns a list of possible Plugin PDF Styles 68858e6409eSAndreas Gohr * 68958e6409eSAndreas Gohr * Checks for a pdf.css, falls back to print.css 69058e6409eSAndreas Gohr * 69158e6409eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 69258e6409eSAndreas Gohr */ 6936be736bfSGerrit Uitslag protected function css_pluginPDFstyles() { 69458e6409eSAndreas Gohr $list = array(); 69558e6409eSAndreas Gohr $plugins = plugin_list(); 696f54b51f7SAndreas Gohr 697f54b51f7SAndreas Gohr $usestyle = explode(',', $this->getConf('usestyles')); 69858e6409eSAndreas Gohr foreach($plugins as $p) { 699f54b51f7SAndreas Gohr if(in_array($p, $usestyle)) { 700f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/"; 701f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/"; 702f54b51f7SAndreas Gohr } 703f54b51f7SAndreas Gohr 70458e6409eSAndreas Gohr if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) { 70558e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/"; 70658e6409eSAndreas Gohr } else { 70758e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/"; 70858e6409eSAndreas Gohr } 70958e6409eSAndreas Gohr } 71058e6409eSAndreas Gohr return $list; 71158e6409eSAndreas Gohr } 712ad18f4e1SGerrit Uitslag 713ad18f4e1SGerrit Uitslag /** 71460e59de7SGerrit Uitslag * Returns array of pages which will be included in the exported pdf 71560e59de7SGerrit Uitslag * 71660e59de7SGerrit Uitslag * @return array 71760e59de7SGerrit Uitslag */ 71860e59de7SGerrit Uitslag public function getExportedPages() { 71960e59de7SGerrit Uitslag return $this->list; 72060e59de7SGerrit Uitslag } 72160e59de7SGerrit Uitslag 72260e59de7SGerrit Uitslag /** 723ad18f4e1SGerrit Uitslag * usort callback to sort by file lastmodified time 72444e8e8fbSGerrit Uitslag * 72544e8e8fbSGerrit Uitslag * @param array $a 72644e8e8fbSGerrit Uitslag * @param array $b 72744e8e8fbSGerrit Uitslag * @return int 728ad18f4e1SGerrit Uitslag */ 729ad18f4e1SGerrit Uitslag public function _datesort($a, $b) { 730ad18f4e1SGerrit Uitslag if($b['rev'] < $a['rev']) return -1; 731ad18f4e1SGerrit Uitslag if($b['rev'] > $a['rev']) return 1; 732ad18f4e1SGerrit Uitslag return strcmp($b['id'], $a['id']); 733ad18f4e1SGerrit Uitslag } 734ad18f4e1SGerrit Uitslag 735ad18f4e1SGerrit Uitslag /** 736ad18f4e1SGerrit Uitslag * usort callback to sort by page id 73744e8e8fbSGerrit Uitslag * @param array $a 73844e8e8fbSGerrit Uitslag * @param array $b 73944e8e8fbSGerrit Uitslag * @return int 740ad18f4e1SGerrit Uitslag */ 741ad18f4e1SGerrit Uitslag public function _pagenamesort($a, $b) { 742ad18f4e1SGerrit Uitslag if($a['id'] <= $b['id']) return -1; 743ad18f4e1SGerrit Uitslag if($a['id'] > $b['id']) return 1; 744ad18f4e1SGerrit Uitslag return 0; 745ad18f4e1SGerrit Uitslag } 74626be4eceSGerrit Uitslag 74726be4eceSGerrit Uitslag /** 74802f9a447SGerrit Uitslag * Return settings read from: 74902f9a447SGerrit Uitslag * 1. url parameters 75002f9a447SGerrit Uitslag * 2. plugin config 75102f9a447SGerrit Uitslag * 3. global config 75202f9a447SGerrit Uitslag * 75302f9a447SGerrit Uitslag * @return array 75402f9a447SGerrit Uitslag */ 75502f9a447SGerrit Uitslag protected function loadExportConfig() { 75602f9a447SGerrit Uitslag global $INPUT; 75702f9a447SGerrit Uitslag global $conf; 75802f9a447SGerrit Uitslag 75902f9a447SGerrit Uitslag $this->exportConfig = array(); 76002f9a447SGerrit Uitslag 76102f9a447SGerrit Uitslag // decide on the paper setup from param or config 76202f9a447SGerrit Uitslag $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true); 76302f9a447SGerrit Uitslag $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true); 76402f9a447SGerrit Uitslag 765213fdb75SGerrit Uitslag $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided')); 766213fdb75SGerrit Uitslag $this->exportConfig['doublesided'] = $doublesided ? '1' : '0'; 767213fdb75SGerrit Uitslag 768213fdb75SGerrit Uitslag $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc')); 76902f9a447SGerrit Uitslag $levels = array(); 77002f9a447SGerrit Uitslag if($hasToC) { 77102f9a447SGerrit Uitslag $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true); 77202f9a447SGerrit Uitslag list($top_input, $max_input) = explode('-', $toclevels, 2); 77302f9a447SGerrit Uitslag list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2); 77402f9a447SGerrit Uitslag $bounds_input = array( 77502f9a447SGerrit Uitslag 'top' => array( 77602f9a447SGerrit Uitslag (int) $top_input, 77702f9a447SGerrit Uitslag (int) $top_conf 77802f9a447SGerrit Uitslag ), 77902f9a447SGerrit Uitslag 'max' => array( 78002f9a447SGerrit Uitslag (int) $max_input, 78102f9a447SGerrit Uitslag (int) $max_conf 78202f9a447SGerrit Uitslag ) 78302f9a447SGerrit Uitslag ); 78402f9a447SGerrit Uitslag $bounds = array( 78502f9a447SGerrit Uitslag 'top' => $conf['toptoclevel'], 78602f9a447SGerrit Uitslag 'max' => $conf['maxtoclevel'] 78702f9a447SGerrit Uitslag 78802f9a447SGerrit Uitslag ); 78902f9a447SGerrit Uitslag foreach($bounds_input as $bound => $values) { 79002f9a447SGerrit Uitslag foreach($values as $value) { 79102f9a447SGerrit Uitslag if($value > 0 && $value <= 5) { 79202f9a447SGerrit Uitslag //stop at valid value and store 79302f9a447SGerrit Uitslag $bounds[$bound] = $value; 79402f9a447SGerrit Uitslag break; 79502f9a447SGerrit Uitslag } 79602f9a447SGerrit Uitslag } 79702f9a447SGerrit Uitslag } 79802f9a447SGerrit Uitslag 79902f9a447SGerrit Uitslag if($bounds['max'] < $bounds['top']) { 80002f9a447SGerrit Uitslag $bounds['max'] = $bounds['top']; 80102f9a447SGerrit Uitslag } 80202f9a447SGerrit Uitslag 80302f9a447SGerrit Uitslag for($level = $bounds['top']; $level <= $bounds['max']; $level++) { 80402f9a447SGerrit Uitslag $levels["H$level"] = $level - 1; 80502f9a447SGerrit Uitslag } 80602f9a447SGerrit Uitslag } 80702f9a447SGerrit Uitslag $this->exportConfig['hasToC'] = $hasToC; 80802f9a447SGerrit Uitslag $this->exportConfig['levels'] = $levels; 80902f9a447SGerrit Uitslag 81002f9a447SGerrit Uitslag $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true); 81102f9a447SGerrit Uitslag 81202f9a447SGerrit Uitslag $tplconf = $this->getConf('template'); 81305d2b507SGerrit Uitslag $tpl = $INPUT->str('tpl', $tplconf, true); 81402f9a447SGerrit Uitslag if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) { 81502f9a447SGerrit Uitslag $tpl = $tplconf; 81602f9a447SGerrit Uitslag } 81702f9a447SGerrit Uitslag if(!$tpl){ 81802f9a447SGerrit Uitslag $tpl = 'default'; 81902f9a447SGerrit Uitslag } 82002f9a447SGerrit Uitslag $this->exportConfig['template'] = $tpl; 82102f9a447SGerrit Uitslag 82202f9a447SGerrit Uitslag $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml'); 82302f9a447SGerrit Uitslag } 82402f9a447SGerrit Uitslag 82502f9a447SGerrit Uitslag /** 82602f9a447SGerrit Uitslag * Returns requested config 82702f9a447SGerrit Uitslag * 82802f9a447SGerrit Uitslag * @param string $name 82902f9a447SGerrit Uitslag * @param mixed $notset 83002f9a447SGerrit Uitslag * @return mixed|bool 83102f9a447SGerrit Uitslag */ 83202f9a447SGerrit Uitslag public function getExportConfig($name, $notset = false) { 83302f9a447SGerrit Uitslag if ($this->exportConfig === null){ 83402f9a447SGerrit Uitslag $this->loadExportConfig(); 83502f9a447SGerrit Uitslag } 83602f9a447SGerrit Uitslag 83702f9a447SGerrit Uitslag if(isset($this->exportConfig[$name])){ 83802f9a447SGerrit Uitslag return $this->exportConfig[$name]; 83902f9a447SGerrit Uitslag }else{ 84002f9a447SGerrit Uitslag return $notset; 84102f9a447SGerrit Uitslag } 84202f9a447SGerrit Uitslag } 843d63e7fe7SGerrit Uitslag 844d63e7fe7SGerrit Uitslag /** 845d63e7fe7SGerrit Uitslag * Add 'export pdf'-button to pagetools 846d63e7fe7SGerrit Uitslag * 847d63e7fe7SGerrit Uitslag * @param Doku_Event $event 848d63e7fe7SGerrit Uitslag */ 84944e8e8fbSGerrit Uitslag public function addbutton(Doku_Event $event) { 850d63e7fe7SGerrit Uitslag global $ID, $REV; 851d63e7fe7SGerrit Uitslag 852d63e7fe7SGerrit Uitslag if($this->getConf('showexportbutton') && $event->data['view'] == 'main') { 853d63e7fe7SGerrit Uitslag $params = array('do' => 'export_pdf'); 854d63e7fe7SGerrit Uitslag if($REV) { 855d63e7fe7SGerrit Uitslag $params['rev'] = $REV; 856d63e7fe7SGerrit Uitslag } 857d63e7fe7SGerrit Uitslag 858d63e7fe7SGerrit Uitslag // insert button at position before last (up to top) 859d63e7fe7SGerrit Uitslag $event->data['items'] = array_slice($event->data['items'], 0, -1, true) + 860d63e7fe7SGerrit Uitslag array('export_pdf' => 861d63e7fe7SGerrit Uitslag '<li>' 86272cadc31SChristian Paul . '<a href="' . wl($ID, $params) . '" class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">' 863d63e7fe7SGerrit Uitslag . '<span>' . $this->getLang('export_pdf_button') . '</span>' 864d63e7fe7SGerrit Uitslag . '</a>' 865d63e7fe7SGerrit Uitslag . '</li>' 866d63e7fe7SGerrit Uitslag ) + 867d63e7fe7SGerrit Uitslag array_slice($event->data['items'], -1, 1, true); 868d63e7fe7SGerrit Uitslag } 869d63e7fe7SGerrit Uitslag } 870ee19bac3SLuigi Micco} 871