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; 2703352761SKirsten Roschanski protected $title; 2860e59de7SGerrit Uitslag protected $list = array(); 291c14c879SAndreas Gohr 301c14c879SAndreas Gohr /** 311c14c879SAndreas Gohr * Constructor. Sets the correct template 3203352761SKirsten Roschanski * 3303352761SKirsten Roschanski * @param string $title 341c14c879SAndreas Gohr */ 3503352761SKirsten Roschanski public function __construct($title=null) { 3602f9a447SGerrit Uitslag $this->tpl = $this->getExportConfig('template'); 3703352761SKirsten Roschanski $this->title = $title ? $title : ''; 381c14c879SAndreas Gohr } 391c14c879SAndreas Gohr 40ee19bac3SLuigi Micco /** 41ee19bac3SLuigi Micco * Register the events 42177a7d30SGerrit Uitslag * 43177a7d30SGerrit Uitslag * @param Doku_Event_Handler $controller 44ee19bac3SLuigi Micco */ 456be736bfSGerrit Uitslag public function register(Doku_Event_Handler $controller) { 46ee19bac3SLuigi Micco $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array()); 476be736bfSGerrit Uitslag $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array()); 48ee19bac3SLuigi Micco } 49ee19bac3SLuigi Micco 501c14c879SAndreas Gohr /** 511c14c879SAndreas Gohr * Do the HTML to PDF conversion work 52737417c6SKlap-in * 53737417c6SKlap-in * @param Doku_Event $event 54737417c6SKlap-in * @return bool 551c14c879SAndreas Gohr */ 5644e8e8fbSGerrit Uitslag public function convert(Doku_Event $event) { 57ee19bac3SLuigi Micco global $ACT; 58ee19bac3SLuigi Micco global $ID; 5927195d5bSMichael Große global $REV, $DATE_AT, $conf; 60ee19bac3SLuigi Micco 611ef68647SAndreas Gohr // our event? 62ad18f4e1SGerrit Uitslag if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false; 63ee19bac3SLuigi Micco 641ef68647SAndreas Gohr // check user's rights 651ef68647SAndreas Gohr if(auth_quickaclcheck($ID) < AUTH_READ) return false; 661ef68647SAndreas Gohr 67d63e7fe7SGerrit Uitslag if($data = $this->collectExportPages($event)) { 6803352761SKirsten Roschanski list($this->title, $this->list) = $data; 69d63e7fe7SGerrit Uitslag } else { 70d63e7fe7SGerrit Uitslag return false; 71d63e7fe7SGerrit Uitslag } 72d63e7fe7SGerrit Uitslag 73d63e7fe7SGerrit Uitslag // it's ours, no one else's 74d63e7fe7SGerrit Uitslag $event->preventDefault(); 75d63e7fe7SGerrit Uitslag 7627195d5bSMichael Große if ($ACT === 'export_pdf' && ($REV || $DATE_AT)) { 7727195d5bSMichael Große $tempFilename = tempnam($conf['tmpdir'], 'dw2pdf_'); 78f00df45eSMichael Große $generateNewPdf = true; 79*27d48100SMichael Große $isTempFile = true; 80f00df45eSMichael Große } else { 81a58f45f0SGerrit Uitslag // prepare cache and its dependencies 82a58f45f0SGerrit Uitslag $depends = array(); 8303352761SKirsten Roschanski $cache = $this->prepareCache($depends); 84f00df45eSMichael Große $tempFilename = $cache->cache; 8527195d5bSMichael Große $generateNewPdf = !$this->getConf('usecache') 8627195d5bSMichael Große || $this->getExportConfig('isDebug') 8727195d5bSMichael Große || !$cache->useCache($depends); 88*27d48100SMichael Große $isTempFile = false; 89f00df45eSMichael Große } 90d63e7fe7SGerrit Uitslag 91bd977188SGerrit Uitslag // hard work only when no cache available or needed for debugging 92f00df45eSMichael Große if($generateNewPdf) { 93e5f6c2cbSMichael Große // generating the pdf may take a long time for larger wikis / namespaces with many pages 94e5f6c2cbSMichael Große set_time_limit(0); 95b34cb34eSSzymon Olewniczak try { 96f00df45eSMichael Große $this->generatePDF($tempFilename); 97b34cb34eSSzymon Olewniczak } catch (Mpdf\MpdfException $e) { 98b34cb34eSSzymon Olewniczak //prevent act_export() 99b34cb34eSSzymon Olewniczak $ACT = 'show'; 100b34cb34eSSzymon Olewniczak msg($e->getMessage(), -1); 101b34cb34eSSzymon Olewniczak return false; 102b34cb34eSSzymon Olewniczak } 103b34cb34eSSzymon Olewniczak 104d63e7fe7SGerrit Uitslag } 105d63e7fe7SGerrit Uitslag 106d63e7fe7SGerrit Uitslag // deliver the file 107f00df45eSMichael Große $this->sendPDFFile($tempFilename); 108*27d48100SMichael Große if ($isTempFile) { 109*27d48100SMichael Große unlink($tempFilename); 110*27d48100SMichael Große } 111d63e7fe7SGerrit Uitslag return true; 112d63e7fe7SGerrit Uitslag } 113d63e7fe7SGerrit Uitslag 114d63e7fe7SGerrit Uitslag /** 115d63e7fe7SGerrit Uitslag * Obtain list of pages and title, based on url parameters 116d63e7fe7SGerrit Uitslag * 117d63e7fe7SGerrit Uitslag * @param Doku_Event $event 118d63e7fe7SGerrit Uitslag * @return string|bool 119d63e7fe7SGerrit Uitslag */ 120d63e7fe7SGerrit Uitslag protected function collectExportPages(Doku_Event $event) { 121d63e7fe7SGerrit Uitslag global $ACT; 122d63e7fe7SGerrit Uitslag global $ID; 123d63e7fe7SGerrit Uitslag global $INPUT; 124d63e7fe7SGerrit Uitslag global $conf; 125d63e7fe7SGerrit Uitslag 126d63e7fe7SGerrit Uitslag // list of one or multiple pages 127d63e7fe7SGerrit Uitslag $list = array(); 12828e636eaSGerrit Uitslag 12987c86ddaSAndreas Gohr if($ACT == 'export_pdf') { 130d63e7fe7SGerrit Uitslag $list[0] = $ID; 13103352761SKirsten Roschanski $this->title = $INPUT->str('pdftitle'); //DEPRECATED 13203352761SKirsten Roschanski $this->title = $INPUT->str('book_title', $this->title, true); 13303352761SKirsten Roschanski if(empty($this->title)) { 13403352761SKirsten Roschanski $this->title = p_get_first_heading($ID); 13515923cb9SGerrit Uitslag } 136ad18f4e1SGerrit Uitslag 137ad18f4e1SGerrit Uitslag } elseif($ACT == 'export_pdfns') { 138ad18f4e1SGerrit Uitslag //check input for title and ns 13903352761SKirsten Roschanski if(!$this->title = $INPUT->str('book_title')) { 14026be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needtitle'); 141ad18f4e1SGerrit Uitslag return false; 142ad18f4e1SGerrit Uitslag } 143177a7d30SGerrit Uitslag $pdfnamespace = cleanID($INPUT->str('book_ns')); 144ad18f4e1SGerrit Uitslag if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) { 14526be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needns'); 146ad18f4e1SGerrit Uitslag return false; 147ad18f4e1SGerrit Uitslag } 148ad18f4e1SGerrit Uitslag 14926be4eceSGerrit Uitslag //sort order 150177a7d30SGerrit Uitslag $order = $INPUT->str('book_order', 'natural', true); 151ad18f4e1SGerrit Uitslag $sortoptions = array('pagename', 'date', 'natural'); 152ad18f4e1SGerrit Uitslag if(!in_array($order, $sortoptions)) { 153ad18f4e1SGerrit Uitslag $order = 'natural'; 154ad18f4e1SGerrit Uitslag } 155ad18f4e1SGerrit Uitslag 15626be4eceSGerrit Uitslag //search depth 157177a7d30SGerrit Uitslag $depth = $INPUT->int('book_nsdepth', 0); 158ad18f4e1SGerrit Uitslag if($depth < 0) { 159ad18f4e1SGerrit Uitslag $depth = 0; 160ad18f4e1SGerrit Uitslag } 16126be4eceSGerrit Uitslag 162ad18f4e1SGerrit Uitslag //page search 163ad18f4e1SGerrit Uitslag $result = array(); 164ad18f4e1SGerrit Uitslag $opts = array('depth' => $depth); //recursive all levels 165ad18f4e1SGerrit Uitslag $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace)); 166ad18f4e1SGerrit Uitslag search($result, $conf['datadir'], 'search_allpages', $opts, $dir); 167ad18f4e1SGerrit Uitslag 16826be4eceSGerrit Uitslag //sorting 169ad18f4e1SGerrit Uitslag if(count($result) > 0) { 170ad18f4e1SGerrit Uitslag if($order == 'date') { 171ad18f4e1SGerrit Uitslag usort($result, array($this, '_datesort')); 172ad18f4e1SGerrit Uitslag } elseif($order == 'pagename') { 173ad18f4e1SGerrit Uitslag usort($result, array($this, '_pagenamesort')); 174ad18f4e1SGerrit Uitslag } 175ad18f4e1SGerrit Uitslag } 176ad18f4e1SGerrit Uitslag 177ad18f4e1SGerrit Uitslag foreach($result as $item) { 178d63e7fe7SGerrit Uitslag $list[] = $item['id']; 179ad18f4e1SGerrit Uitslag } 180ad18f4e1SGerrit Uitslag 181baa31dc5SGerrit Uitslag if ($pdfnamespace !== '') { 182baa31dc5SGerrit Uitslag if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { 183baa31dc5SGerrit Uitslag if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) { 184baa31dc5SGerrit Uitslag array_unshift($list,rtrim($pdfnamespace,':')); 185baa31dc5SGerrit Uitslag } 186baa31dc5SGerrit Uitslag } 187baa31dc5SGerrit Uitslag } 188baa31dc5SGerrit Uitslag 189737417c6SKlap-in } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 190b3eed6e3SGerrit Uitslag /** @deprecated April 2016 replaced by localStorage version of Bookcreator*/ 19126be4eceSGerrit Uitslag //is in Bookmanager of bookcreator plugin a title given? 19203352761SKirsten Roschanski $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED 19303352761SKirsten Roschanski $this->title = $INPUT->str('book_title', $this->title, true); 19403352761SKirsten Roschanski if(empty($this->title)) { 19526be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needtitle'); 196737417c6SKlap-in return false; 19726be4eceSGerrit Uitslag } else { 198d63e7fe7SGerrit Uitslag $list = explode("|", $_COOKIE['list-pagelist']); 19926be4eceSGerrit Uitslag } 200ad18f4e1SGerrit Uitslag 201b3eed6e3SGerrit Uitslag } elseif($INPUT->has('selection')) { 202b3eed6e3SGerrit Uitslag //handle Bookcreator requests based at localStorage 203b3eed6e3SGerrit Uitslag// if(!checkSecurityToken()) { 204b3eed6e3SGerrit Uitslag// http_status(403); 205b3eed6e3SGerrit Uitslag// print $this->getLang('empty'); 206b3eed6e3SGerrit Uitslag// exit(); 207b3eed6e3SGerrit Uitslag// } 208b3eed6e3SGerrit Uitslag 209b3eed6e3SGerrit Uitslag $json = new JSON(JSON_LOOSE_TYPE); 210b3eed6e3SGerrit Uitslag $list = $json->decode($INPUT->post->str('selection', '', true)); 211b3eed6e3SGerrit Uitslag if(!is_array($list) || empty($list)) { 212b3eed6e3SGerrit Uitslag http_status(400); 213b3eed6e3SGerrit Uitslag print $this->getLang('empty'); 214b3eed6e3SGerrit Uitslag exit(); 215b3eed6e3SGerrit Uitslag } 216b3eed6e3SGerrit Uitslag 21703352761SKirsten Roschanski $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED 21803352761SKirsten Roschanski $this->title = $INPUT->str('book_title', $this->title, true); 21903352761SKirsten Roschanski if(empty($this->title)) { 220b3eed6e3SGerrit Uitslag http_status(400); 221b3eed6e3SGerrit Uitslag print $this->getLang('needtitle'); 222b3eed6e3SGerrit Uitslag exit(); 223b3eed6e3SGerrit Uitslag } 224b3eed6e3SGerrit Uitslag 225737417c6SKlap-in } else { 22626be4eceSGerrit Uitslag //show empty bookcreator message 22726be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'empty'); 228737417c6SKlap-in return false; 229737417c6SKlap-in } 230737417c6SKlap-in 231719256adSGerrit Uitslag $list = array_map('cleanID', $list); 232c7138b3fSGerrit Uitslag 233c7138b3fSGerrit Uitslag $skippedpages = array(); 234c7138b3fSGerrit Uitslag foreach($list as $index => $pageid) { 235c7138b3fSGerrit Uitslag if(auth_quickaclcheck($pageid) < AUTH_READ) { 236c7138b3fSGerrit Uitslag $skippedpages[] = $pageid; 237c7138b3fSGerrit Uitslag unset($list[$index]); 238c7138b3fSGerrit Uitslag } 239c7138b3fSGerrit Uitslag } 240c7138b3fSGerrit Uitslag $list = array_filter($list); //removes also pages mentioned '0' 241c7138b3fSGerrit Uitslag 242c7138b3fSGerrit Uitslag //if selection contains forbidden pages throw (overridable) warning 243c7138b3fSGerrit Uitslag if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) { 244c7138b3fSGerrit Uitslag $msg = hsc(join(', ', $skippedpages)); 245c7138b3fSGerrit Uitslag if($INPUT->has('selection')) { 246c7138b3fSGerrit Uitslag http_status(400); 247c7138b3fSGerrit Uitslag print sprintf($this->getLang('forbidden'), $msg); 248c7138b3fSGerrit Uitslag exit(); 249c7138b3fSGerrit Uitslag } else { 250c7138b3fSGerrit Uitslag $this->showPageWithErrorMsg($event, 'forbidden', $msg); 251c7138b3fSGerrit Uitslag return false; 252c7138b3fSGerrit Uitslag } 253c7138b3fSGerrit Uitslag 254c7138b3fSGerrit Uitslag } 255c7138b3fSGerrit Uitslag 25603352761SKirsten Roschanski return array($this->title, $list); 257d63e7fe7SGerrit Uitslag } 258d63e7fe7SGerrit Uitslag 259a58f45f0SGerrit Uitslag /** 260e53f1ec0SSzymon Olewniczak * Get $meta['relations'] for the given page and revision 261e53f1ec0SSzymon Olewniczak * 262e53f1ec0SSzymon Olewniczak * @param $id 263e53f1ec0SSzymon Olewniczak * @param string $rev 264e53f1ec0SSzymon Olewniczak * @return mixed 265e53f1ec0SSzymon Olewniczak */ 266e53f1ec0SSzymon Olewniczak protected function getMetaRelation($id, $rev='') { 267e53f1ec0SSzymon Olewniczak //current revision 268e53f1ec0SSzymon Olewniczak if ($rev == '') return p_get_metadata($id, 'relation'); 269e53f1ec0SSzymon Olewniczak 270e53f1ec0SSzymon Olewniczak // get instructions 271e53f1ec0SSzymon Olewniczak $instructions = p_cached_instructions(wikiFN($id, $rev),false,$id); 272e53f1ec0SSzymon Olewniczak 273e53f1ec0SSzymon Olewniczak // set up the renderer 274e53f1ec0SSzymon Olewniczak $renderer = new Doku_Renderer_metadata(); 275e53f1ec0SSzymon Olewniczak 276e53f1ec0SSzymon Olewniczak // loop through the instructions 277e53f1ec0SSzymon Olewniczak foreach ($instructions as $instruction){ 278e53f1ec0SSzymon Olewniczak //execute only relation['media'] and relation['haspart'] functions 279e53f1ec0SSzymon Olewniczak if ($instruction[0] != 'locallink' && 280e53f1ec0SSzymon Olewniczak $instruction[0] != 'internallink' && 281e53f1ec0SSzymon Olewniczak $instruction[0] != 'externallink' && 282e53f1ec0SSzymon Olewniczak $instruction[0] != 'interwikilink' && 283e53f1ec0SSzymon Olewniczak $instruction[0] != 'windowssharelink' && 284e53f1ec0SSzymon Olewniczak $instruction[0] != 'emaillink' && 285e53f1ec0SSzymon Olewniczak $instruction[0] != 'internalmedia' && 286e53f1ec0SSzymon Olewniczak $instruction[0] != 'rss') continue; 287e53f1ec0SSzymon Olewniczak 288e53f1ec0SSzymon Olewniczak // execute the callback against the renderer 289e53f1ec0SSzymon Olewniczak call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]); 290e53f1ec0SSzymon Olewniczak } 291e53f1ec0SSzymon Olewniczak 292e53f1ec0SSzymon Olewniczak return $renderer->meta['relation']; 293e53f1ec0SSzymon Olewniczak } 294e53f1ec0SSzymon Olewniczak 295e53f1ec0SSzymon Olewniczak /** 296a58f45f0SGerrit Uitslag * Prepare cache 297a58f45f0SGerrit Uitslag * 298a58f45f0SGerrit Uitslag * @param array $depends (reference) array with dependencies 299a58f45f0SGerrit Uitslag * @return cache 300a58f45f0SGerrit Uitslag */ 30103352761SKirsten Roschanski protected function prepareCache(&$depends) { 302f00df45eSMichael Große global $REV; 303a58f45f0SGerrit Uitslag 304ee19bac3SLuigi Micco $cachekey = join(',', $this->list) 305f00df45eSMichael Große . $REV 306ee19bac3SLuigi Micco . $this->getExportConfig('template') 307ee19bac3SLuigi Micco . $this->getExportConfig('pagesize') 308ee19bac3SLuigi Micco . $this->getExportConfig('orientation') 309d83760efSGerrit Uitslag . $this->getExportConfig('font-size') 310ee19bac3SLuigi Micco . $this->getExportConfig('doublesided') 311ee19bac3SLuigi Micco . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0') 31203352761SKirsten Roschanski . $this->title; 313ee19bac3SLuigi Micco $cache = new cache($cachekey, '.dw2.pdf'); 314ee19bac3SLuigi Micco 315ee19bac3SLuigi Micco $dependencies = array(); 316ee19bac3SLuigi Micco foreach($this->list as $pageid) { 317f00df45eSMichael Große $relations = p_get_metadata($pageid, 'relation'); 318ee19bac3SLuigi Micco 319ee19bac3SLuigi Micco if(is_array($relations)) { 320ee19bac3SLuigi Micco if(array_key_exists('media', $relations) && is_array($relations['media'])) { 321ee19bac3SLuigi Micco foreach($relations['media'] as $mediaid => $exists) { 322ee19bac3SLuigi Micco if($exists) { 323f00df45eSMichael Große $dependencies[] = mediaFN($mediaid); 324ee19bac3SLuigi Micco } 325ee19bac3SLuigi Micco } 326ee19bac3SLuigi Micco } 327ee19bac3SLuigi Micco 328ee19bac3SLuigi Micco if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) { 329ee19bac3SLuigi Micco foreach($relations['haspart'] as $part_pageid => $exists) { 330ee19bac3SLuigi Micco if($exists) { 331ee19bac3SLuigi Micco $dependencies[] = wikiFN($part_pageid); 332ee19bac3SLuigi Micco } 333ee19bac3SLuigi Micco } 334ee19bac3SLuigi Micco } 335ee19bac3SLuigi Micco } 336ee19bac3SLuigi Micco 337ee19bac3SLuigi Micco $dependencies[] = metaFN($pageid, '.meta'); 338ee19bac3SLuigi Micco } 339ee19bac3SLuigi Micco 340ee19bac3SLuigi Micco $depends['files'] = array_map('wikiFN', $this->list); 341ee19bac3SLuigi Micco $depends['files'][] = __FILE__; 342ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/renderer.php'; 343ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php'; 344ee19bac3SLuigi Micco $depends['files'] = array_merge( 345ee19bac3SLuigi Micco $depends['files'], 346ee19bac3SLuigi Micco $dependencies, 347ee19bac3SLuigi Micco getConfigFiles('main') 348ee19bac3SLuigi Micco ); 349a58f45f0SGerrit Uitslag return $cache; 350ee19bac3SLuigi Micco } 351ee19bac3SLuigi Micco 352d63e7fe7SGerrit Uitslag /** 353d63e7fe7SGerrit Uitslag * Set error notification and reload page again 354d63e7fe7SGerrit Uitslag * 355d63e7fe7SGerrit Uitslag * @param Doku_Event $event 356d63e7fe7SGerrit Uitslag * @param string $msglangkey key of translation key 357c7138b3fSGerrit Uitslag * @param string $replacement 358d63e7fe7SGerrit Uitslag */ 359c7138b3fSGerrit Uitslag private function showPageWithErrorMsg(Doku_Event $event, $msglangkey, $replacement=null) { 360c7138b3fSGerrit Uitslag if(empty($replacement)) { 361c7138b3fSGerrit Uitslag $msg = $this->getLang($msglangkey); 362c7138b3fSGerrit Uitslag } else { 363c7138b3fSGerrit Uitslag $msg = sprintf($this->getLang($msglangkey), $replacement); 364c7138b3fSGerrit Uitslag } 365c7138b3fSGerrit Uitslag msg($msg, -1); 366d63e7fe7SGerrit Uitslag 367d63e7fe7SGerrit Uitslag $event->data = 'show'; 368d63e7fe7SGerrit Uitslag $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url 369d63e7fe7SGerrit Uitslag } 370d63e7fe7SGerrit Uitslag 371d63e7fe7SGerrit Uitslag /** 372e53f1ec0SSzymon Olewniczak * Returns the parsed Wikitext in dw2pdf for the given id and revision 373e53f1ec0SSzymon Olewniczak * 374e53f1ec0SSzymon Olewniczak * @param string $id page id 375e53f1ec0SSzymon Olewniczak * @param string|int $rev revision timestamp or empty string 376e53f1ec0SSzymon Olewniczak * @param string $date_at 377e53f1ec0SSzymon Olewniczak * @return null|string 378e53f1ec0SSzymon Olewniczak */ 379e53f1ec0SSzymon Olewniczak protected function p_wiki_dw2pdf($id, $rev = '', $date_at = '') { 380e53f1ec0SSzymon Olewniczak $file = wikiFN($id, $rev); 381e53f1ec0SSzymon Olewniczak 382e53f1ec0SSzymon Olewniczak if(!file_exists($file)) return ''; 383e53f1ec0SSzymon Olewniczak 384e53f1ec0SSzymon Olewniczak //ensure $id is in global $ID (needed for parsing) 385e53f1ec0SSzymon Olewniczak global $ID; 386e53f1ec0SSzymon Olewniczak $keep = $ID; 387e53f1ec0SSzymon Olewniczak $ID = $id; 388e53f1ec0SSzymon Olewniczak 389e53f1ec0SSzymon Olewniczak $ret = ''; 390e53f1ec0SSzymon Olewniczak 391e53f1ec0SSzymon Olewniczak if($rev || $date_at) { 392e53f1ec0SSzymon Olewniczak $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); //no caching on old revisions 393e53f1ec0SSzymon Olewniczak } else { 394e53f1ec0SSzymon Olewniczak $ret = p_cached_output($file, 'dw2pdf', $id); 395e53f1ec0SSzymon Olewniczak } 396e53f1ec0SSzymon Olewniczak 397e53f1ec0SSzymon Olewniczak //restore ID (just in case) 398e53f1ec0SSzymon Olewniczak $ID = $keep; 399e53f1ec0SSzymon Olewniczak 400e53f1ec0SSzymon Olewniczak return $ret; 401e53f1ec0SSzymon Olewniczak } 402e53f1ec0SSzymon Olewniczak 403e53f1ec0SSzymon Olewniczak /** 404d63e7fe7SGerrit Uitslag * Build a pdf from the html 405d63e7fe7SGerrit Uitslag * 406d63e7fe7SGerrit Uitslag * @param string $cachefile 407d63e7fe7SGerrit Uitslag */ 40803352761SKirsten Roschanski protected function generatePDF($cachefile) { 409e53f1ec0SSzymon Olewniczak global $ID, $REV, $INPUT, $DATE_AT, $ACT; 410e53f1ec0SSzymon Olewniczak 411e53f1ec0SSzymon Olewniczak if ($ACT == 'export_pdf') { //only one page is exported 412e53f1ec0SSzymon Olewniczak $rev = $REV; 413e53f1ec0SSzymon Olewniczak $date_at = $DATE_AT; 414e53f1ec0SSzymon Olewniczak } else { //we are exporting entre namespace, ommit revisions 415e53f1ec0SSzymon Olewniczak $rev = $date_at = ''; 416e53f1ec0SSzymon Olewniczak } 41787c86ddaSAndreas Gohr 41802f9a447SGerrit Uitslag //some shortcuts to export settings 41902f9a447SGerrit Uitslag $hasToC = $this->getExportConfig('hasToC'); 42002f9a447SGerrit Uitslag $levels = $this->getExportConfig('levels'); 42102f9a447SGerrit Uitslag $isDebug = $this->getExportConfig('isDebug'); 4226ea88a05SAndreas Gohr 4231ef68647SAndreas Gohr // initialize PDF library 424cde5a1b3SAndreas Gohr require_once(dirname(__FILE__) . "/DokuPDF.class.php"); 4256ea88a05SAndreas Gohr 4264870b378SLarsDW223 $mpdf = new DokuPDF($this->getExportConfig('pagesize'), 4274870b378SLarsDW223 $this->getExportConfig('orientation'), 4284870b378SLarsDW223 $this->getExportConfig('font-size')); 429ee19bac3SLuigi Micco 430d62df65bSAndreas Gohr // let mpdf fix local links 431d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 432d62df65bSAndreas Gohr $url = $self['scheme'] . '://' . $self['host']; 43302f9a447SGerrit Uitslag if($self['port']) { 43402f9a447SGerrit Uitslag $url .= ':' . $self['port']; 43502f9a447SGerrit Uitslag } 436d62df65bSAndreas Gohr $mpdf->setBasePath($url); 437d62df65bSAndreas Gohr 43856d13144SAndreas Gohr // Set the title 43903352761SKirsten Roschanski $mpdf->SetTitle($this->title); 44056d13144SAndreas Gohr 441d63e7fe7SGerrit Uitslag // some default document settings 442d63e7fe7SGerrit Uitslag //note: double-sided document, starts at an odd page (first page is a right-hand side page) 443213fdb75SGerrit Uitslag // single-side document has only odd pages 444213fdb75SGerrit Uitslag $mpdf->mirrorMargins = $this->getExportConfig('doublesided'); 445daa70883SAndreas Gohr $mpdf->setAutoTopMargin = 'stretch'; 446daa70883SAndreas Gohr $mpdf->setAutoBottomMargin = 'stretch'; 44702f9a447SGerrit Uitslag// $mpdf->pagenumSuffix = '/'; //prefix for {nbpg} 44802f9a447SGerrit Uitslag if($hasToC) { 44902f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC 45002f9a447SGerrit Uitslag $mpdf->h2toc = $levels; 45102f9a447SGerrit Uitslag } else { 45202f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'); 45302f9a447SGerrit Uitslag } 4542eedf77dSAndreas Gohr 45556d13144SAndreas Gohr // load the template 45603352761SKirsten Roschanski $template = $this->load_template(); 457ee19bac3SLuigi Micco 4581ef68647SAndreas Gohr // prepare HTML header styles 459a2c33768SGerrit Uitslag $html = ''; 46002f9a447SGerrit Uitslag if($isDebug) { 461a2c33768SGerrit Uitslag $html .= '<html><head>'; 462737417c6SKlap-in $html .= '<style type="text/css">'; 463a2c33768SGerrit Uitslag } 464db1aa1bfSKirsten Roschanski 465db1aa1bfSKirsten Roschanski $styles = '@page { size:auto; ' . $template['page'] . '}'; 466a2c33768SGerrit Uitslag $styles .= '@page :first {' . $template['first'] . '}'; 467254467c4SGerrit Uitslag 468254467c4SGerrit Uitslag $styles .= '@page landscape-page { size:landscape }'; 469254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-landscape { page:landscape-page }'; 470254467c4SGerrit Uitslag $styles .= '@page portrait-page { size:portrait }'; 471254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-portrait { page:portrait-page }'; 472db1aa1bfSKirsten Roschanski $styles .= $this->load_css(); 473254467c4SGerrit Uitslag 474a2c33768SGerrit Uitslag $mpdf->WriteHTML($styles, 1); 475a2c33768SGerrit Uitslag 47602f9a447SGerrit Uitslag if($isDebug) { 477a2c33768SGerrit Uitslag $html .= $styles; 4781ef68647SAndreas Gohr $html .= '</style>'; 4791ef68647SAndreas Gohr $html .= '</head><body>'; 480a2c33768SGerrit Uitslag } 481a2c33768SGerrit Uitslag 482a2c33768SGerrit Uitslag $body_start = $template['html']; 483a2c33768SGerrit Uitslag $body_start .= '<div class="dokuwiki">'; 4842eedf77dSAndreas Gohr 4851e45476bSmnapp // insert the cover page 486a2c33768SGerrit Uitslag $body_start .= $template['cover']; 487a2c33768SGerrit Uitslag 488a2c33768SGerrit Uitslag $mpdf->WriteHTML($body_start, 2, true, false); //start body html 48902f9a447SGerrit Uitslag if($isDebug) { 490a2c33768SGerrit Uitslag $html .= $body_start; 491a2c33768SGerrit Uitslag } 49202f9a447SGerrit Uitslag if($hasToC) { 49302f9a447SGerrit 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 49402f9a447SGerrit Uitslag // - first page of ToC starts always at odd page (so eventually an additional blank page is included before) 49502f9a447SGerrit Uitslag // - there is no page numbering at the pages of the ToC 49602f9a447SGerrit Uitslag $mpdf->TOCpagebreakByArray( 49702f9a447SGerrit Uitslag array( 498230b098dSGerrit Uitslag 'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>', 499230b098dSGerrit Uitslag 'toc-bookmarkText' => $this->getLang('tocheader'), 50002f9a447SGerrit Uitslag 'links' => true, 50102f9a447SGerrit Uitslag 'outdent' => '1em', 50202f9a447SGerrit Uitslag 'resetpagenum' => true, //start pagenumbering after ToC 50302f9a447SGerrit Uitslag 'pagenumstyle' => '1' 50402f9a447SGerrit Uitslag ) 50502f9a447SGerrit Uitslag ); 50602f9a447SGerrit Uitslag $html .= '<tocpagebreak>'; 50702f9a447SGerrit Uitslag } 50802f9a447SGerrit Uitslag 509c00eb13bSGerrit Uitslag // store original pageid 510c00eb13bSGerrit Uitslag $keep = $ID; 511c00eb13bSGerrit Uitslag 5121ef68647SAndreas Gohr // loop over all pages 513c7138b3fSGerrit Uitslag $counter = 0; 514c7138b3fSGerrit Uitslag $no_pages = count($this->list); 515c7138b3fSGerrit Uitslag foreach($this->list as $page) { 516c7138b3fSGerrit Uitslag $counter++; 517ee19bac3SLuigi Micco 518c00eb13bSGerrit Uitslag // set global pageid to the rendered page 519c00eb13bSGerrit Uitslag $ID = $page; 520c00eb13bSGerrit Uitslag 521e53f1ec0SSzymon Olewniczak //$pagehtml = p_cached_output($filename, 'dw2pdf', $page); 522e53f1ec0SSzymon Olewniczak $pagehtml = $this->p_wiki_dw2pdf($ID, $rev, $date_at); 523e53f1ec0SSzymon Olewniczak //file doesn't exists 524e53f1ec0SSzymon Olewniczak if($pagehtml == '') { 525e53f1ec0SSzymon Olewniczak continue; 526e53f1ec0SSzymon Olewniczak } 527719256adSGerrit Uitslag $pagehtml .= $this->page_depend_replacements($template['cite'], $page); 528c7138b3fSGerrit Uitslag if($counter < $no_pages) { 529a2c33768SGerrit Uitslag $pagehtml .= '<pagebreak />'; 530a2c33768SGerrit Uitslag } 531a2c33768SGerrit Uitslag 532a2c33768SGerrit Uitslag $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html 53302f9a447SGerrit Uitslag if($isDebug) { 534a2c33768SGerrit Uitslag $html .= $pagehtml; 5351ef68647SAndreas Gohr } 536ee19bac3SLuigi Micco } 537c00eb13bSGerrit Uitslag //restore ID 538c00eb13bSGerrit Uitslag $ID = $keep; 539ee19bac3SLuigi Micco 54033c15297SGerrit Uitslag // insert the back page 541a2c33768SGerrit Uitslag $body_end = $template['back']; 54233c15297SGerrit Uitslag 543a2c33768SGerrit Uitslag $body_end .= '</div>'; 544a2c33768SGerrit Uitslag 545d63e7fe7SGerrit Uitslag $mpdf->WriteHTML($body_end, 2, false, true); // finish body html 54602f9a447SGerrit Uitslag if($isDebug) { 547a2c33768SGerrit Uitslag $html .= $body_end; 548eeb17e15SAndreas Gohr $html .= '</body>'; 549eeb17e15SAndreas Gohr $html .= '</html>'; 550a2c33768SGerrit Uitslag } 551f765508eSGerrit Uitslag 552f765508eSGerrit Uitslag //Return html for debugging 55302f9a447SGerrit Uitslag if($isDebug) { 55402f9a447SGerrit Uitslag if($INPUT->str('debughtml', 'text', true) == 'html') { 55526be4eceSGerrit Uitslag echo $html; 556a2c33768SGerrit Uitslag } else { 557a2c33768SGerrit Uitslag header('Content-Type: text/plain; charset=utf-8'); 558a2c33768SGerrit Uitslag echo $html; 559a2c33768SGerrit Uitslag } 56026be4eceSGerrit Uitslag exit(); 56126be4eceSGerrit Uitslag }; 562f765508eSGerrit Uitslag 56387c86ddaSAndreas Gohr // write to cache file 564d63e7fe7SGerrit Uitslag $mpdf->Output($cachefile, 'F'); 56587c86ddaSAndreas Gohr } 56687c86ddaSAndreas Gohr 567d63e7fe7SGerrit Uitslag /** 568d63e7fe7SGerrit Uitslag * @param string $cachefile 569d63e7fe7SGerrit Uitslag */ 57003352761SKirsten Roschanski protected function sendPDFFile($cachefile) { 57187c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 572b853b723SAndreas Gohr header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 57387c86ddaSAndreas Gohr header('Pragma: public'); 574d63e7fe7SGerrit Uitslag http_conditionalRequest(filemtime($cachefile)); 57587c86ddaSAndreas Gohr 57603352761SKirsten Roschanski $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', ' '))); 57787c86ddaSAndreas Gohr if($this->getConf('output') == 'file') { 5789a3c8d9fSAndreas Gohr header('Content-Disposition: attachment; filename="' . $filename . '.pdf";'); 57987c86ddaSAndreas Gohr } else { 5809a3c8d9fSAndreas Gohr header('Content-Disposition: inline; filename="' . $filename . '.pdf";'); 58187c86ddaSAndreas Gohr } 582ee19bac3SLuigi Micco 583b3eed6e3SGerrit Uitslag //Bookcreator uses jQuery.fileDownload.js, which requires a cookie. 584b3eed6e3SGerrit Uitslag header('Set-Cookie: fileDownload=true; path=/'); 585b3eed6e3SGerrit Uitslag 586e993da11SGerrit Uitslag //try to send file, and exit if done 587d63e7fe7SGerrit Uitslag http_sendfile($cachefile); 58887c86ddaSAndreas Gohr 589d63e7fe7SGerrit Uitslag $fp = @fopen($cachefile, "rb"); 59087c86ddaSAndreas Gohr if($fp) { 591d63e7fe7SGerrit Uitslag http_rangeRequest($fp, filesize($cachefile), 'application/pdf'); 59287c86ddaSAndreas Gohr } else { 59387c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 59487c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 59587c86ddaSAndreas Gohr } 5961ef68647SAndreas Gohr exit(); 5971ef68647SAndreas Gohr } 5981ef68647SAndreas Gohr 5996be736bfSGerrit Uitslag /** 6002eedf77dSAndreas Gohr * Load the various template files and prepare the HTML/CSS for insertion 60144e8e8fbSGerrit Uitslag * 60244e8e8fbSGerrit Uitslag * @return array 6031ef68647SAndreas Gohr */ 60403352761SKirsten Roschanski protected function load_template() { 6051ef68647SAndreas Gohr global $ID; 6061ef68647SAndreas Gohr global $conf; 6071ef68647SAndreas Gohr 6082eedf77dSAndreas Gohr // this is what we'll return 6092eedf77dSAndreas Gohr $output = array( 6101e45476bSmnapp 'cover' => '', 6112eedf77dSAndreas Gohr 'html' => '', 6122eedf77dSAndreas Gohr 'page' => '', 6132eedf77dSAndreas Gohr 'first' => '', 6142eedf77dSAndreas Gohr 'cite' => '', 6152eedf77dSAndreas Gohr ); 6162eedf77dSAndreas Gohr 6172eedf77dSAndreas Gohr // prepare header/footer elements 6182eedf77dSAndreas Gohr $html = ''; 61933c15297SGerrit Uitslag foreach(array('header', 'footer') as $section) { 62033c15297SGerrit Uitslag foreach(array('', '_odd', '_even', '_first') as $order) { 62102f9a447SGerrit Uitslag $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html'; 62233c15297SGerrit Uitslag if(file_exists($file)) { 62333c15297SGerrit Uitslag $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF; 62433c15297SGerrit Uitslag $html .= file_get_contents($file) . DOKU_LF; 62533c15297SGerrit Uitslag $html .= '</htmlpage' . $section . '>' . DOKU_LF; 6262eedf77dSAndreas Gohr 6272eedf77dSAndreas Gohr // register the needed pseudo CSS 62833c15297SGerrit Uitslag if($order == '_first') { 62933c15297SGerrit Uitslag $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 63033c15297SGerrit Uitslag } elseif($order == '_even') { 63133c15297SGerrit Uitslag $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 63233c15297SGerrit Uitslag } elseif($order == '_odd') { 63333c15297SGerrit Uitslag $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 634daa70883SAndreas Gohr } else { 63533c15297SGerrit Uitslag $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 6362eedf77dSAndreas Gohr } 6372eedf77dSAndreas Gohr } 6382eedf77dSAndreas Gohr } 6392eedf77dSAndreas Gohr } 6402eedf77dSAndreas Gohr 6411ef68647SAndreas Gohr // prepare replacements 6421ef68647SAndreas Gohr $replace = array( 6431ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 64402f9a447SGerrit Uitslag '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / ' 64503352761SKirsten Roschanski '@TITLE@' => hsc($this->title), 6461ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 6471ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 6481ef68647SAndreas Gohr '@DATE@' => dformat(time()), 6495d6fbaeaSAndreas Gohr '@BASE@' => DOKU_BASE, 65002f9a447SGerrit Uitslag '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/' 6511ef68647SAndreas Gohr ); 6521ef68647SAndreas Gohr 6532eedf77dSAndreas Gohr // set HTML element 654a180c973SKlap-in $html = str_replace(array_keys($replace), array_values($replace), $html); 655a180c973SKlap-in //TODO For bookcreator $ID (= bookmanager page) makes no sense 656a180c973SKlap-in $output['html'] = $this->page_depend_replacements($html, $ID); 6571ef68647SAndreas Gohr 6581e45476bSmnapp // cover page 65902f9a447SGerrit Uitslag $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html'; 66033c15297SGerrit Uitslag if(file_exists($coverfile)) { 66133c15297SGerrit Uitslag $output['cover'] = file_get_contents($coverfile); 6621e45476bSmnapp $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']); 6639b071da5SMichael $output['cover'] = $this->page_depend_replacements($output['cover'], $ID); 6646e2ec302SGerrit Uitslag $output['cover'] .= '<pagebreak />'; 6651e45476bSmnapp } 6661e45476bSmnapp 66733c15297SGerrit Uitslag // cover page 66802f9a447SGerrit Uitslag $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html'; 66933c15297SGerrit Uitslag if(file_exists($backfile)) { 67033c15297SGerrit Uitslag $output['back'] = '<pagebreak />'; 67133c15297SGerrit Uitslag $output['back'] .= file_get_contents($backfile); 67233c15297SGerrit Uitslag $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']); 6739b071da5SMichael $output['back'] = $this->page_depend_replacements($output['back'], $ID); 67433c15297SGerrit Uitslag } 67533c15297SGerrit Uitslag 6762eedf77dSAndreas Gohr // citation box 67702f9a447SGerrit Uitslag $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html'; 67833c15297SGerrit Uitslag if(file_exists($citationfile)) { 67933c15297SGerrit Uitslag $output['cite'] = file_get_contents($citationfile); 6802eedf77dSAndreas Gohr $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 6812eedf77dSAndreas Gohr } 6821ef68647SAndreas Gohr 6832eedf77dSAndreas Gohr return $output; 6841ef68647SAndreas Gohr } 6851ef68647SAndreas Gohr 6861ef68647SAndreas Gohr /** 687a180c973SKlap-in * @param string $raw code with placeholders 688a180c973SKlap-in * @param string $id pageid 689a180c973SKlap-in * @return string 690a180c973SKlap-in */ 691a180c973SKlap-in protected function page_depend_replacements($raw, $id) { 692e53f1ec0SSzymon Olewniczak global $REV, $DATE_AT; 693a180c973SKlap-in 694a180c973SKlap-in // generate qr code for this page using google infographics api 695a180c973SKlap-in $qr_code = ''; 696a180c973SKlap-in if($this->getConf('qrcodesize')) { 697a180c973SKlap-in $url = urlencode(wl($id, '', '&', true)); 698a180c973SKlap-in $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' . 699a180c973SKlap-in $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />'; 700a180c973SKlap-in } 701a180c973SKlap-in // prepare replacements 702a180c973SKlap-in $replace['@ID@'] = $id; 703a180c973SKlap-in $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV))); 704e53f1ec0SSzymon Olewniczak 705e53f1ec0SSzymon Olewniczak $params = array(); 706e53f1ec0SSzymon Olewniczak if($DATE_AT) { 707e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 708e53f1ec0SSzymon Olewniczak } elseif($REV) { 709e53f1ec0SSzymon Olewniczak $params['rev'] = $REV; 710e53f1ec0SSzymon Olewniczak } 711e53f1ec0SSzymon Olewniczak $replace['@PAGEURL@'] = wl($id, $params, true, "&"); 712a180c973SKlap-in $replace['@QRCODE@'] = $qr_code; 713a180c973SKlap-in 714e3d68265SGerrit Uitslag $content = str_replace(array_keys($replace), array_values($replace), $raw); 715e3d68265SGerrit Uitslag 716e3d68265SGerrit Uitslag // @DATE(<date>[, <format>])@ 717e3d68265SGerrit Uitslag $content = preg_replace_callback( 718e3d68265SGerrit Uitslag '/@DATE\((.*?)(?:,\s*(.*?))?\)@/', 719e3d68265SGerrit Uitslag array($this, 'replacedate'), 720e3d68265SGerrit Uitslag $content 721e3d68265SGerrit Uitslag ); 722e3d68265SGerrit Uitslag 723e3d68265SGerrit Uitslag return $content; 724a180c973SKlap-in } 725a180c973SKlap-in 726e3d68265SGerrit Uitslag 727e3d68265SGerrit Uitslag /** 728e3d68265SGerrit Uitslag * (callback) Replace date by request datestring 729e3d68265SGerrit Uitslag * e.g. '%m(30-11-1975)' is replaced by '11' 730e3d68265SGerrit Uitslag * 731e3d68265SGerrit Uitslag * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern 732e3d68265SGerrit Uitslag * @return string 733e3d68265SGerrit Uitslag */ 734e3d68265SGerrit Uitslag function replacedate($match) { 735e3d68265SGerrit Uitslag global $conf; 736e3d68265SGerrit Uitslag //no 2nd argument for default date format 737e3d68265SGerrit Uitslag if($match[2] == null) { 738e3d68265SGerrit Uitslag $match[2] = $conf['dformat']; 739e3d68265SGerrit Uitslag } 740e3d68265SGerrit Uitslag return strftime($match[2], strtotime($match[1])); 741e3d68265SGerrit Uitslag } 742e3d68265SGerrit Uitslag 743a180c973SKlap-in /** 7441c14c879SAndreas Gohr * Load all the style sheets and apply the needed replacements 7451ef68647SAndreas Gohr */ 7461c14c879SAndreas Gohr protected function load_css() { 747737417c6SKlap-in global $conf; 7481c14c879SAndreas Gohr //reusue the CSS dispatcher functions without triggering the main function 7491c14c879SAndreas Gohr define('SIMPLE_TEST', 1); 7501c14c879SAndreas Gohr require_once(DOKU_INC . 'lib/exe/css.php'); 751ee19bac3SLuigi Micco 7521c14c879SAndreas Gohr // prepare CSS files 7531c14c879SAndreas Gohr $files = array_merge( 7541c14c879SAndreas Gohr array( 7551c14c879SAndreas Gohr DOKU_INC . 'lib/styles/screen.css' 7561c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7571c14c879SAndreas Gohr DOKU_INC . 'lib/styles/print.css' 7581c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7591c14c879SAndreas Gohr ), 7601c14c879SAndreas Gohr css_pluginstyles('all'), 76158e6409eSAndreas Gohr $this->css_pluginPDFstyles(), 7621c14c879SAndreas Gohr array( 7631c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.css' 7641c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7651c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' 7661c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 7671c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' 7681c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7691c14c879SAndreas Gohr ) 7701c14c879SAndreas Gohr ); 7711c14c879SAndreas Gohr $css = ''; 7721c14c879SAndreas Gohr foreach($files as $file => $location) { 77328e636eaSGerrit Uitslag $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 77428e636eaSGerrit Uitslag $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 7751c14c879SAndreas Gohr $css .= css_loadfile($file, $location); 7761ef68647SAndreas Gohr } 7771ef68647SAndreas Gohr 77828e636eaSGerrit Uitslag if(function_exists('css_parseless')) { 7791c14c879SAndreas Gohr // apply pattern replacements 78028e636eaSGerrit Uitslag $styleini = css_styleini($conf['template']); 78128e636eaSGerrit Uitslag $css = css_applystyle($css, $styleini['replacements']); 78228e636eaSGerrit Uitslag 78328e636eaSGerrit Uitslag // parse less 78428e636eaSGerrit Uitslag $css = css_parseless($css); 78528e636eaSGerrit Uitslag } else { 78628e636eaSGerrit Uitslag // @deprecated 2013-12-19: fix backward compatibility 7871c14c879SAndreas Gohr $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 78828e636eaSGerrit Uitslag } 7891ef68647SAndreas Gohr 7901c14c879SAndreas Gohr return $css; 791ee19bac3SLuigi Micco } 7921c14c879SAndreas Gohr 79358e6409eSAndreas Gohr /** 79458e6409eSAndreas Gohr * Returns a list of possible Plugin PDF Styles 79558e6409eSAndreas Gohr * 79658e6409eSAndreas Gohr * Checks for a pdf.css, falls back to print.css 79758e6409eSAndreas Gohr * 79858e6409eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 79958e6409eSAndreas Gohr */ 8006be736bfSGerrit Uitslag protected function css_pluginPDFstyles() { 80158e6409eSAndreas Gohr $list = array(); 80258e6409eSAndreas Gohr $plugins = plugin_list(); 803f54b51f7SAndreas Gohr 804f54b51f7SAndreas Gohr $usestyle = explode(',', $this->getConf('usestyles')); 80558e6409eSAndreas Gohr foreach($plugins as $p) { 806f54b51f7SAndreas Gohr if(in_array($p, $usestyle)) { 807f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/"; 808f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/"; 809f54b51f7SAndreas Gohr } 810f54b51f7SAndreas Gohr 81158e6409eSAndreas Gohr if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) { 81258e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/"; 81358e6409eSAndreas Gohr } else { 81458e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/"; 81558e6409eSAndreas Gohr } 81658e6409eSAndreas Gohr } 81758e6409eSAndreas Gohr return $list; 81858e6409eSAndreas Gohr } 819ad18f4e1SGerrit Uitslag 820ad18f4e1SGerrit Uitslag /** 82160e59de7SGerrit Uitslag * Returns array of pages which will be included in the exported pdf 82260e59de7SGerrit Uitslag * 82360e59de7SGerrit Uitslag * @return array 82460e59de7SGerrit Uitslag */ 82560e59de7SGerrit Uitslag public function getExportedPages() { 82660e59de7SGerrit Uitslag return $this->list; 82760e59de7SGerrit Uitslag } 82860e59de7SGerrit Uitslag 82960e59de7SGerrit Uitslag /** 830ad18f4e1SGerrit Uitslag * usort callback to sort by file lastmodified time 83144e8e8fbSGerrit Uitslag * 83244e8e8fbSGerrit Uitslag * @param array $a 83344e8e8fbSGerrit Uitslag * @param array $b 83444e8e8fbSGerrit Uitslag * @return int 835ad18f4e1SGerrit Uitslag */ 836ad18f4e1SGerrit Uitslag public function _datesort($a, $b) { 837ad18f4e1SGerrit Uitslag if($b['rev'] < $a['rev']) return -1; 838ad18f4e1SGerrit Uitslag if($b['rev'] > $a['rev']) return 1; 839ad18f4e1SGerrit Uitslag return strcmp($b['id'], $a['id']); 840ad18f4e1SGerrit Uitslag } 841ad18f4e1SGerrit Uitslag 842ad18f4e1SGerrit Uitslag /** 843ad18f4e1SGerrit Uitslag * usort callback to sort by page id 84444e8e8fbSGerrit Uitslag * @param array $a 84544e8e8fbSGerrit Uitslag * @param array $b 84644e8e8fbSGerrit Uitslag * @return int 847ad18f4e1SGerrit Uitslag */ 848ad18f4e1SGerrit Uitslag public function _pagenamesort($a, $b) { 849ad18f4e1SGerrit Uitslag if($a['id'] <= $b['id']) return -1; 850ad18f4e1SGerrit Uitslag if($a['id'] > $b['id']) return 1; 851ad18f4e1SGerrit Uitslag return 0; 852ad18f4e1SGerrit Uitslag } 85326be4eceSGerrit Uitslag 85426be4eceSGerrit Uitslag /** 85502f9a447SGerrit Uitslag * Return settings read from: 85602f9a447SGerrit Uitslag * 1. url parameters 85702f9a447SGerrit Uitslag * 2. plugin config 85802f9a447SGerrit Uitslag * 3. global config 85902f9a447SGerrit Uitslag * 86002f9a447SGerrit Uitslag * @return array 86102f9a447SGerrit Uitslag */ 86202f9a447SGerrit Uitslag protected function loadExportConfig() { 86302f9a447SGerrit Uitslag global $INPUT; 86402f9a447SGerrit Uitslag global $conf; 86502f9a447SGerrit Uitslag 86602f9a447SGerrit Uitslag $this->exportConfig = array(); 86702f9a447SGerrit Uitslag 86802f9a447SGerrit Uitslag // decide on the paper setup from param or config 86902f9a447SGerrit Uitslag $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true); 87002f9a447SGerrit Uitslag $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true); 87102f9a447SGerrit Uitslag 8724870b378SLarsDW223 // decide on the font-size from param or config 8734870b378SLarsDW223 $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true); 8744870b378SLarsDW223 875213fdb75SGerrit Uitslag $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided')); 876213fdb75SGerrit Uitslag $this->exportConfig['doublesided'] = $doublesided ? '1' : '0'; 877213fdb75SGerrit Uitslag 878213fdb75SGerrit Uitslag $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc')); 87902f9a447SGerrit Uitslag $levels = array(); 88002f9a447SGerrit Uitslag if($hasToC) { 88102f9a447SGerrit Uitslag $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true); 88202f9a447SGerrit Uitslag list($top_input, $max_input) = explode('-', $toclevels, 2); 88302f9a447SGerrit Uitslag list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2); 88402f9a447SGerrit Uitslag $bounds_input = array( 88502f9a447SGerrit Uitslag 'top' => array( 88602f9a447SGerrit Uitslag (int) $top_input, 88702f9a447SGerrit Uitslag (int) $top_conf 88802f9a447SGerrit Uitslag ), 88902f9a447SGerrit Uitslag 'max' => array( 89002f9a447SGerrit Uitslag (int) $max_input, 89102f9a447SGerrit Uitslag (int) $max_conf 89202f9a447SGerrit Uitslag ) 89302f9a447SGerrit Uitslag ); 89402f9a447SGerrit Uitslag $bounds = array( 89502f9a447SGerrit Uitslag 'top' => $conf['toptoclevel'], 89602f9a447SGerrit Uitslag 'max' => $conf['maxtoclevel'] 89702f9a447SGerrit Uitslag 89802f9a447SGerrit Uitslag ); 89902f9a447SGerrit Uitslag foreach($bounds_input as $bound => $values) { 90002f9a447SGerrit Uitslag foreach($values as $value) { 90102f9a447SGerrit Uitslag if($value > 0 && $value <= 5) { 90202f9a447SGerrit Uitslag //stop at valid value and store 90302f9a447SGerrit Uitslag $bounds[$bound] = $value; 90402f9a447SGerrit Uitslag break; 90502f9a447SGerrit Uitslag } 90602f9a447SGerrit Uitslag } 90702f9a447SGerrit Uitslag } 90802f9a447SGerrit Uitslag 90902f9a447SGerrit Uitslag if($bounds['max'] < $bounds['top']) { 91002f9a447SGerrit Uitslag $bounds['max'] = $bounds['top']; 91102f9a447SGerrit Uitslag } 91202f9a447SGerrit Uitslag 91302f9a447SGerrit Uitslag for($level = $bounds['top']; $level <= $bounds['max']; $level++) { 91402f9a447SGerrit Uitslag $levels["H$level"] = $level - 1; 91502f9a447SGerrit Uitslag } 91602f9a447SGerrit Uitslag } 91702f9a447SGerrit Uitslag $this->exportConfig['hasToC'] = $hasToC; 91802f9a447SGerrit Uitslag $this->exportConfig['levels'] = $levels; 91902f9a447SGerrit Uitslag 92002f9a447SGerrit Uitslag $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true); 92102f9a447SGerrit Uitslag 92202f9a447SGerrit Uitslag $tplconf = $this->getConf('template'); 92305d2b507SGerrit Uitslag $tpl = $INPUT->str('tpl', $tplconf, true); 92402f9a447SGerrit Uitslag if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) { 92502f9a447SGerrit Uitslag $tpl = $tplconf; 92602f9a447SGerrit Uitslag } 92702f9a447SGerrit Uitslag if(!$tpl){ 92802f9a447SGerrit Uitslag $tpl = 'default'; 92902f9a447SGerrit Uitslag } 93002f9a447SGerrit Uitslag $this->exportConfig['template'] = $tpl; 93102f9a447SGerrit Uitslag 93202f9a447SGerrit Uitslag $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml'); 93302f9a447SGerrit Uitslag } 93402f9a447SGerrit Uitslag 93502f9a447SGerrit Uitslag /** 93602f9a447SGerrit Uitslag * Returns requested config 93702f9a447SGerrit Uitslag * 93802f9a447SGerrit Uitslag * @param string $name 93902f9a447SGerrit Uitslag * @param mixed $notset 94002f9a447SGerrit Uitslag * @return mixed|bool 94102f9a447SGerrit Uitslag */ 94202f9a447SGerrit Uitslag public function getExportConfig($name, $notset = false) { 94302f9a447SGerrit Uitslag if ($this->exportConfig === null){ 94402f9a447SGerrit Uitslag $this->loadExportConfig(); 94502f9a447SGerrit Uitslag } 94602f9a447SGerrit Uitslag 94702f9a447SGerrit Uitslag if(isset($this->exportConfig[$name])){ 94802f9a447SGerrit Uitslag return $this->exportConfig[$name]; 94902f9a447SGerrit Uitslag }else{ 95002f9a447SGerrit Uitslag return $notset; 95102f9a447SGerrit Uitslag } 95202f9a447SGerrit Uitslag } 953d63e7fe7SGerrit Uitslag 954d63e7fe7SGerrit Uitslag /** 955d63e7fe7SGerrit Uitslag * Add 'export pdf'-button to pagetools 956d63e7fe7SGerrit Uitslag * 957d63e7fe7SGerrit Uitslag * @param Doku_Event $event 958d63e7fe7SGerrit Uitslag */ 95944e8e8fbSGerrit Uitslag public function addbutton(Doku_Event $event) { 960e53f1ec0SSzymon Olewniczak global $ID, $REV, $DATE_AT; 961d63e7fe7SGerrit Uitslag 962d63e7fe7SGerrit Uitslag if($this->getConf('showexportbutton') && $event->data['view'] == 'main') { 963d63e7fe7SGerrit Uitslag $params = array('do' => 'export_pdf'); 964e53f1ec0SSzymon Olewniczak if($DATE_AT) { 965e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 966e53f1ec0SSzymon Olewniczak } elseif($REV) { 967d63e7fe7SGerrit Uitslag $params['rev'] = $REV; 968d63e7fe7SGerrit Uitslag } 969d63e7fe7SGerrit Uitslag 970d63e7fe7SGerrit Uitslag // insert button at position before last (up to top) 971d63e7fe7SGerrit Uitslag $event->data['items'] = array_slice($event->data['items'], 0, -1, true) + 972d63e7fe7SGerrit Uitslag array('export_pdf' => 973d63e7fe7SGerrit Uitslag '<li>' 97472cadc31SChristian Paul . '<a href="' . wl($ID, $params) . '" class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">' 975d63e7fe7SGerrit Uitslag . '<span>' . $this->getLang('export_pdf_button') . '</span>' 976d63e7fe7SGerrit Uitslag . '</a>' 977d63e7fe7SGerrit Uitslag . '</li>' 978d63e7fe7SGerrit Uitslag ) + 979d63e7fe7SGerrit Uitslag array_slice($event->data['items'], -1, 1, true); 980d63e7fe7SGerrit Uitslag } 981d63e7fe7SGerrit Uitslag } 982ee19bac3SLuigi Micco} 983