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 100639157eSGerrit Uitslag/** 110639157eSGerrit Uitslag * Class action_plugin_dw2pdf 120639157eSGerrit Uitslag * 132a127a1dSGerrit Uitslag * Export html content to pdf, for different url parameter configurations 140639157eSGerrit Uitslag * DokuPDF which extends mPDF is used for generating the pdf from html. 150639157eSGerrit Uitslag */ 16*0833b7cdSStephan Bauer 17*0833b7cdSStephan Baueruse dokuwiki\Cache\Cache; 18*0833b7cdSStephan Bauer 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; 262a127a1dSGerrit Uitslag /** @var string template name, to use templates from dw2pdf/tpl/<template name> */ 2760e59de7SGerrit Uitslag protected $tpl; 282a127a1dSGerrit Uitslag /** @var string title of exported pdf */ 2903352761SKirsten Roschanski protected $title; 306a7f9d6cSGerrit Uitslag /** @var array list of pages included in exported pdf */ 3160e59de7SGerrit Uitslag protected $list = array(); 322a127a1dSGerrit Uitslag /** @var bool|string path to temporary cachefile */ 339b288b2aSGerrit Uitslag protected $onetimefile = false; 349c76f78dSVincent GIRARD protected $currentBookChapter = 0; 351c14c879SAndreas Gohr 361c14c879SAndreas Gohr /** 371c14c879SAndreas Gohr * Constructor. Sets the correct template 381c14c879SAndreas Gohr */ 392a127a1dSGerrit Uitslag public function __construct() { 4002f9a447SGerrit Uitslag $this->tpl = $this->getExportConfig('template'); 411c14c879SAndreas Gohr } 421c14c879SAndreas Gohr 43ee19bac3SLuigi Micco /** 449b288b2aSGerrit Uitslag * Delete cached files that were for one-time use 459b288b2aSGerrit Uitslag */ 469b288b2aSGerrit Uitslag public function __destruct() { 479b288b2aSGerrit Uitslag if($this->onetimefile) { 489b288b2aSGerrit Uitslag unlink($this->onetimefile); 499b288b2aSGerrit Uitslag } 509b288b2aSGerrit Uitslag } 519b288b2aSGerrit Uitslag 529b288b2aSGerrit Uitslag /** 539c76f78dSVincent GIRARD * Return the value of currentBookChapter, which is the order of the file to be added in a book generation 549c76f78dSVincent GIRARD */ 559c76f78dSVincent GIRARD public function getCurrentBookChapter() 569c76f78dSVincent GIRARD { 579c76f78dSVincent GIRARD return $this->currentBookChapter; 589c76f78dSVincent GIRARD } 599c76f78dSVincent GIRARD 609c76f78dSVincent GIRARD /** 61ee19bac3SLuigi Micco * Register the events 62177a7d30SGerrit Uitslag * 63177a7d30SGerrit Uitslag * @param Doku_Event_Handler $controller 64ee19bac3SLuigi Micco */ 656be736bfSGerrit Uitslag public function register(Doku_Event_Handler $controller) { 66ee19bac3SLuigi Micco $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array()); 676be736bfSGerrit Uitslag $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array()); 68026b594bSAndreas Gohr $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton', array()); 69ee19bac3SLuigi Micco } 70ee19bac3SLuigi Micco 711c14c879SAndreas Gohr /** 721c14c879SAndreas Gohr * Do the HTML to PDF conversion work 73737417c6SKlap-in * 74737417c6SKlap-in * @param Doku_Event $event 751c14c879SAndreas Gohr */ 7644e8e8fbSGerrit Uitslag public function convert(Doku_Event $event) { 772a127a1dSGerrit Uitslag global $REV, $DATE_AT; 782d9cd424SGerrit Uitslag global $conf, $INPUT; 79ee19bac3SLuigi Micco 801ef68647SAndreas Gohr // our event? 812a127a1dSGerrit Uitslag $allowedEvents = ['export_pdfbook', 'export_pdf', 'export_pdfns']; 822a127a1dSGerrit Uitslag if(!in_array($event->data, $allowedEvents)) return; 83ee19bac3SLuigi Micco 842a127a1dSGerrit Uitslag try{ 852a127a1dSGerrit Uitslag //collect pages and check permissions 862a127a1dSGerrit Uitslag list($this->title, $this->list) = $this->collectExportablePages($event); 87d63e7fe7SGerrit Uitslag 882d9cd424SGerrit Uitslag if($event->data === 'export_pdf' && ($REV || $DATE_AT)) { 899b288b2aSGerrit Uitslag $cachefile = tempnam($conf['tmpdir'] . '/dwpdf', 'dw2pdf_'); 909b288b2aSGerrit Uitslag $this->onetimefile = $cachefile; 91f00df45eSMichael Große $generateNewPdf = true; 92f00df45eSMichael Große } else { 93a58f45f0SGerrit Uitslag // prepare cache and its dependencies 94a58f45f0SGerrit Uitslag $depends = array(); 9503352761SKirsten Roschanski $cache = $this->prepareCache($depends); 969b288b2aSGerrit Uitslag $cachefile = $cache->cache; 9727195d5bSMichael Große $generateNewPdf = !$this->getConf('usecache') 9827195d5bSMichael Große || $this->getExportConfig('isDebug') 9927195d5bSMichael Große || !$cache->useCache($depends); 100f00df45eSMichael Große } 101d63e7fe7SGerrit Uitslag 102bd977188SGerrit Uitslag // hard work only when no cache available or needed for debugging 103f00df45eSMichael Große if($generateNewPdf) { 104e5f6c2cbSMichael Große // generating the pdf may take a long time for larger wikis / namespaces with many pages 105e5f6c2cbSMichael Große set_time_limit(0); 1062a127a1dSGerrit Uitslag //may throw Mpdf\MpdfException as well 1079b288b2aSGerrit Uitslag $this->generatePDF($cachefile, $event); 1082a127a1dSGerrit Uitslag } 1092a127a1dSGerrit Uitslag } catch(Exception $e) { 1102d9cd424SGerrit Uitslag if($INPUT->has('selection')) { 1112d9cd424SGerrit Uitslag http_status(400); 1122d9cd424SGerrit Uitslag print $e->getMessage(); 1132d9cd424SGerrit Uitslag exit(); 1142d9cd424SGerrit Uitslag } else { 1152a127a1dSGerrit Uitslag //prevent Action/Export() 116b34cb34eSSzymon Olewniczak msg($e->getMessage(), -1); 1172a127a1dSGerrit Uitslag $event->data = 'redirect'; 118d9c13ec7SGerrit Uitslag return; 119b34cb34eSSzymon Olewniczak } 120d63e7fe7SGerrit Uitslag } 1212d9cd424SGerrit Uitslag $event->preventDefault(); // after prevent, $event->data cannot be changed 122d63e7fe7SGerrit Uitslag 123d63e7fe7SGerrit Uitslag // deliver the file 1249b288b2aSGerrit Uitslag $this->sendPDFFile($cachefile); //exits 125d63e7fe7SGerrit Uitslag } 126d63e7fe7SGerrit Uitslag 127d63e7fe7SGerrit Uitslag /** 1282a127a1dSGerrit Uitslag * Obtain list of pages and title, for different methods of exporting the pdf. 1292a127a1dSGerrit Uitslag * - Return a title and selection, throw otherwise an exception 1302a127a1dSGerrit Uitslag * - Check permisions 131d63e7fe7SGerrit Uitslag * 132d63e7fe7SGerrit Uitslag * @param Doku_Event $event 1337c79bc79SGerrit Uitslag * @return array|false 1342a127a1dSGerrit Uitslag * @throws Exception 135d63e7fe7SGerrit Uitslag */ 1362a127a1dSGerrit Uitslag protected function collectExportablePages(Doku_Event $event) { 13736a7917dSGerrit Uitslag global $ID, $REV; 138d63e7fe7SGerrit Uitslag global $INPUT; 1392a127a1dSGerrit Uitslag global $conf, $lang; 140d63e7fe7SGerrit Uitslag 141d63e7fe7SGerrit Uitslag // list of one or multiple pages 142d63e7fe7SGerrit Uitslag $list = array(); 14328e636eaSGerrit Uitslag 1444b4cebc2SLarsDW223 if($event->data == 'export_pdf') { 1452a127a1dSGerrit Uitslag if(auth_quickaclcheck($ID) < AUTH_READ) { // set more specific denied message 1462a127a1dSGerrit Uitslag throw new Exception($lang['accessdenied']); 1472a127a1dSGerrit Uitslag } 148d63e7fe7SGerrit Uitslag $list[0] = $ID; 1492a127a1dSGerrit Uitslag $title = $INPUT->str('pdftitle'); //DEPRECATED 1502a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 1512a127a1dSGerrit Uitslag if(empty($title)) { 1522a127a1dSGerrit Uitslag $title = p_get_first_heading($ID); 15315923cb9SGerrit Uitslag } 154ce8af5d0SHativ // use page name if title is still empty 1552a127a1dSGerrit Uitslag if(empty($title)) { 1562a127a1dSGerrit Uitslag $title = noNS($ID); 157ce8af5d0SHativ } 158ad18f4e1SGerrit Uitslag 15936a7917dSGerrit Uitslag $filename = wikiFN($ID, $REV); 16036a7917dSGerrit Uitslag if(!file_exists($filename)) { 1612a127a1dSGerrit Uitslag throw new Exception($this->getLang('notexist')); 16236a7917dSGerrit Uitslag } 16336a7917dSGerrit Uitslag 1644b4cebc2SLarsDW223 } elseif($event->data == 'export_pdfns') { 165ad18f4e1SGerrit Uitslag //check input for title and ns 1662a127a1dSGerrit Uitslag if(!$title = $INPUT->str('book_title')) { 1672a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 168ad18f4e1SGerrit Uitslag } 169177a7d30SGerrit Uitslag $pdfnamespace = cleanID($INPUT->str('book_ns')); 170ad18f4e1SGerrit Uitslag if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) { 1712a127a1dSGerrit Uitslag throw new Exception($this->getLang('needns')); 172ad18f4e1SGerrit Uitslag } 173ad18f4e1SGerrit Uitslag 17426be4eceSGerrit Uitslag //sort order 175177a7d30SGerrit Uitslag $order = $INPUT->str('book_order', 'natural', true); 176ad18f4e1SGerrit Uitslag $sortoptions = array('pagename', 'date', 'natural'); 177ad18f4e1SGerrit Uitslag if(!in_array($order, $sortoptions)) { 178ad18f4e1SGerrit Uitslag $order = 'natural'; 179ad18f4e1SGerrit Uitslag } 180ad18f4e1SGerrit Uitslag 18126be4eceSGerrit Uitslag //search depth 182177a7d30SGerrit Uitslag $depth = $INPUT->int('book_nsdepth', 0); 183ad18f4e1SGerrit Uitslag if($depth < 0) { 184ad18f4e1SGerrit Uitslag $depth = 0; 185ad18f4e1SGerrit Uitslag } 18626be4eceSGerrit Uitslag 187ad18f4e1SGerrit Uitslag //page search 188ad18f4e1SGerrit Uitslag $result = array(); 189ad18f4e1SGerrit Uitslag $opts = array('depth' => $depth); //recursive all levels 190ad18f4e1SGerrit Uitslag $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace)); 191ad18f4e1SGerrit Uitslag search($result, $conf['datadir'], 'search_allpages', $opts, $dir); 192ad18f4e1SGerrit Uitslag 19364541781SAnna Dabrowska // exclude ids 19464541781SAnna Dabrowska $excludes = $INPUT->arr('excludes'); 19564541781SAnna Dabrowska if (!empty($excludes)) { 196d31b75d5SAnna Dabrowska $result = array_filter($result, function ($item) use ($excludes) { 1978c25a9b9SGerrit Uitslag return !in_array($item['id'], $excludes); 1988c25a9b9SGerrit Uitslag }); 1998c25a9b9SGerrit Uitslag } 2008c25a9b9SGerrit Uitslag // exclude namespaces 2018c25a9b9SGerrit Uitslag $excludesns = $INPUT->arr('excludesns'); 2028c25a9b9SGerrit Uitslag if (!empty($excludesns)) { 2038c25a9b9SGerrit Uitslag $result = array_filter($result, function ($item) use ($excludesns) { 2048c25a9b9SGerrit Uitslag foreach ($excludesns as $ns) { 2058c25a9b9SGerrit Uitslag if (strpos($item['id'], $ns . ':') === 0) return false; 2068c25a9b9SGerrit Uitslag } 2078c25a9b9SGerrit Uitslag return true; 208d31b75d5SAnna Dabrowska }); 20964541781SAnna Dabrowska } 21064541781SAnna Dabrowska 21126be4eceSGerrit Uitslag //sorting 212ad18f4e1SGerrit Uitslag if(count($result) > 0) { 213ad18f4e1SGerrit Uitslag if($order == 'date') { 214ad18f4e1SGerrit Uitslag usort($result, array($this, '_datesort')); 21541e5d4e2SAndreas Gohr } elseif ($order == 'pagename' || $order == 'natural') { 216ad18f4e1SGerrit Uitslag usort($result, array($this, '_pagenamesort')); 217ad18f4e1SGerrit Uitslag } 218ad18f4e1SGerrit Uitslag } 219ad18f4e1SGerrit Uitslag 220ad18f4e1SGerrit Uitslag foreach($result as $item) { 221d63e7fe7SGerrit Uitslag $list[] = $item['id']; 222ad18f4e1SGerrit Uitslag } 223ad18f4e1SGerrit Uitslag 224baa31dc5SGerrit Uitslag if ($pdfnamespace !== '') { 225baa31dc5SGerrit Uitslag if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { 226baa31dc5SGerrit Uitslag if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) { 227baa31dc5SGerrit Uitslag array_unshift($list,rtrim($pdfnamespace,':')); 228baa31dc5SGerrit Uitslag } 229baa31dc5SGerrit Uitslag } 230baa31dc5SGerrit Uitslag } 231baa31dc5SGerrit Uitslag 232737417c6SKlap-in } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 233b3eed6e3SGerrit Uitslag /** @deprecated April 2016 replaced by localStorage version of Bookcreator*/ 23426be4eceSGerrit Uitslag //is in Bookmanager of bookcreator plugin a title given? 2352a127a1dSGerrit Uitslag $title = $INPUT->str('pdfbook_title'); //DEPRECATED 2362a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 2372a127a1dSGerrit Uitslag if(empty($title)) { 2382a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 23926be4eceSGerrit Uitslag } 240ad18f4e1SGerrit Uitslag 2412a127a1dSGerrit Uitslag $list = explode("|", $_COOKIE['list-pagelist']); 2422a127a1dSGerrit Uitslag 243b3eed6e3SGerrit Uitslag } elseif($INPUT->has('selection')) { 244b3eed6e3SGerrit Uitslag //handle Bookcreator requests based at localStorage 245b3eed6e3SGerrit Uitslag// if(!checkSecurityToken()) { 246b3eed6e3SGerrit Uitslag// http_status(403); 247b3eed6e3SGerrit Uitslag// print $this->getLang('empty'); 248b3eed6e3SGerrit Uitslag// exit(); 249b3eed6e3SGerrit Uitslag// } 250b3eed6e3SGerrit Uitslag 2517c79bc79SGerrit Uitslag $list = json_decode($INPUT->str('selection', '', true), true); 252b3eed6e3SGerrit Uitslag if (!is_array($list) || empty($list)) { 2532a127a1dSGerrit Uitslag throw new Exception($this->getLang('empty')); 254b3eed6e3SGerrit Uitslag } 255b3eed6e3SGerrit Uitslag 2562a127a1dSGerrit Uitslag $title = $INPUT->str('pdfbook_title'); //DEPRECATED 2572a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 2582a127a1dSGerrit Uitslag if (empty($title)) { 2592a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 2602a127a1dSGerrit Uitslag } 2612a127a1dSGerrit Uitslag 2622a127a1dSGerrit Uitslag } elseif($INPUT->has('savedselection')) { 2632a127a1dSGerrit Uitslag //export a saved selection of the Bookcreator Plugin 2642a127a1dSGerrit Uitslag if(plugin_isdisabled('bookcreator')) { 2652a127a1dSGerrit Uitslag throw new Exception($this->getLang('missingbookcreator')); 2662a127a1dSGerrit Uitslag } 2672a127a1dSGerrit Uitslag /** @var action_plugin_bookcreator_handleselection $SelectionHandling */ 2682a127a1dSGerrit Uitslag $SelectionHandling = plugin_load('action', 'bookcreator_handleselection'); 2692a127a1dSGerrit Uitslag $savedselection = $SelectionHandling->loadSavedSelection($INPUT->str('savedselection')); 2702a127a1dSGerrit Uitslag $title = $savedselection['title']; 2712a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 2722a127a1dSGerrit Uitslag $list = $savedselection['selection']; 2732a127a1dSGerrit Uitslag 2742a127a1dSGerrit Uitslag if(empty($title)) { 2752a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 276b3eed6e3SGerrit Uitslag } 277b3eed6e3SGerrit Uitslag 278737417c6SKlap-in } else { 27926be4eceSGerrit Uitslag //show empty bookcreator message 2802a127a1dSGerrit Uitslag throw new Exception($this->getLang('empty')); 281737417c6SKlap-in } 282737417c6SKlap-in 283719256adSGerrit Uitslag $list = array_map('cleanID', $list); 284c7138b3fSGerrit Uitslag 285c7138b3fSGerrit Uitslag $skippedpages = array(); 286c7138b3fSGerrit Uitslag foreach($list as $index => $pageid) { 287c7138b3fSGerrit Uitslag if(auth_quickaclcheck($pageid) < AUTH_READ) { 288c7138b3fSGerrit Uitslag $skippedpages[] = $pageid; 289c7138b3fSGerrit Uitslag unset($list[$index]); 290c7138b3fSGerrit Uitslag } 291c7138b3fSGerrit Uitslag } 2922a127a1dSGerrit Uitslag $list = array_filter($list, 'strlen'); //use of strlen() callback prevents removal of pagename '0' 293c7138b3fSGerrit Uitslag 294c7138b3fSGerrit Uitslag //if selection contains forbidden pages throw (overridable) warning 295c7138b3fSGerrit Uitslag if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) { 296c7138b3fSGerrit Uitslag $msg = hsc(join(', ', $skippedpages)); 2972a127a1dSGerrit Uitslag throw new Exception(sprintf($this->getLang('forbidden'), $msg)); 298c7138b3fSGerrit Uitslag } 299c7138b3fSGerrit Uitslag 3002a127a1dSGerrit Uitslag return array($title, $list); 301d63e7fe7SGerrit Uitslag } 302d63e7fe7SGerrit Uitslag 303a58f45f0SGerrit Uitslag /** 304a58f45f0SGerrit Uitslag * Prepare cache 305a58f45f0SGerrit Uitslag * 306a58f45f0SGerrit Uitslag * @param array $depends (reference) array with dependencies 307a58f45f0SGerrit Uitslag * @return cache 308a58f45f0SGerrit Uitslag */ 30903352761SKirsten Roschanski protected function prepareCache(&$depends) { 310a58f45f0SGerrit Uitslag global $REV; 311a58f45f0SGerrit Uitslag 312ee19bac3SLuigi Micco $cachekey = join(',', $this->list) 313ee19bac3SLuigi Micco . $REV 314ee19bac3SLuigi Micco . $this->getExportConfig('template') 315ee19bac3SLuigi Micco . $this->getExportConfig('pagesize') 316ee19bac3SLuigi Micco . $this->getExportConfig('orientation') 317d83760efSGerrit Uitslag . $this->getExportConfig('font-size') 318ee19bac3SLuigi Micco . $this->getExportConfig('doublesided') 3199c76f78dSVincent GIRARD . $this->getExportConfig('headernumber') 320ee19bac3SLuigi Micco . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0') 32103352761SKirsten Roschanski . $this->title; 322*0833b7cdSStephan Bauer $cache = new Cache($cachekey, '.dw2.pdf'); 323ee19bac3SLuigi Micco 324ee19bac3SLuigi Micco $dependencies = array(); 325ee19bac3SLuigi Micco foreach($this->list as $pageid) { 326ee19bac3SLuigi Micco $relations = p_get_metadata($pageid, 'relation'); 327ee19bac3SLuigi Micco 328ee19bac3SLuigi Micco if(is_array($relations)) { 329ee19bac3SLuigi Micco if(array_key_exists('media', $relations) && is_array($relations['media'])) { 330ee19bac3SLuigi Micco foreach($relations['media'] as $mediaid => $exists) { 331ee19bac3SLuigi Micco if($exists) { 332ee19bac3SLuigi Micco $dependencies[] = mediaFN($mediaid); 333ee19bac3SLuigi Micco } 334ee19bac3SLuigi Micco } 335ee19bac3SLuigi Micco } 336ee19bac3SLuigi Micco 337ee19bac3SLuigi Micco if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) { 338ee19bac3SLuigi Micco foreach($relations['haspart'] as $part_pageid => $exists) { 339ee19bac3SLuigi Micco if($exists) { 340ee19bac3SLuigi Micco $dependencies[] = wikiFN($part_pageid); 341ee19bac3SLuigi Micco } 342ee19bac3SLuigi Micco } 343ee19bac3SLuigi Micco } 344ee19bac3SLuigi Micco } 345ee19bac3SLuigi Micco 346ee19bac3SLuigi Micco $dependencies[] = metaFN($pageid, '.meta'); 347ee19bac3SLuigi Micco } 348ee19bac3SLuigi Micco 349ee19bac3SLuigi Micco $depends['files'] = array_map('wikiFN', $this->list); 350ee19bac3SLuigi Micco $depends['files'][] = __FILE__; 351ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/renderer.php'; 352ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php'; 353ee19bac3SLuigi Micco $depends['files'] = array_merge( 354ee19bac3SLuigi Micco $depends['files'], 355ee19bac3SLuigi Micco $dependencies, 356ee19bac3SLuigi Micco getConfigFiles('main') 357ee19bac3SLuigi Micco ); 358a58f45f0SGerrit Uitslag return $cache; 359ee19bac3SLuigi Micco } 360ee19bac3SLuigi Micco 361d63e7fe7SGerrit Uitslag /** 362e53f1ec0SSzymon Olewniczak * Returns the parsed Wikitext in dw2pdf for the given id and revision 363e53f1ec0SSzymon Olewniczak * 364e53f1ec0SSzymon Olewniczak * @param string $id page id 365e53f1ec0SSzymon Olewniczak * @param string|int $rev revision timestamp or empty string 366e53f1ec0SSzymon Olewniczak * @param string $date_at 367e53f1ec0SSzymon Olewniczak * @return null|string 368e53f1ec0SSzymon Olewniczak */ 369e53f1ec0SSzymon Olewniczak protected function p_wiki_dw2pdf($id, $rev = '', $date_at = '') { 370e53f1ec0SSzymon Olewniczak $file = wikiFN($id, $rev); 371e53f1ec0SSzymon Olewniczak 372e53f1ec0SSzymon Olewniczak if(!file_exists($file)) return ''; 373e53f1ec0SSzymon Olewniczak 374e53f1ec0SSzymon Olewniczak //ensure $id is in global $ID (needed for parsing) 375e53f1ec0SSzymon Olewniczak global $ID; 376e53f1ec0SSzymon Olewniczak $keep = $ID; 377e53f1ec0SSzymon Olewniczak $ID = $id; 378e53f1ec0SSzymon Olewniczak 379e53f1ec0SSzymon Olewniczak if($rev || $date_at) { 380e53f1ec0SSzymon Olewniczak $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); //no caching on old revisions 381e53f1ec0SSzymon Olewniczak } else { 382e53f1ec0SSzymon Olewniczak $ret = p_cached_output($file, 'dw2pdf', $id); 383e53f1ec0SSzymon Olewniczak } 384e53f1ec0SSzymon Olewniczak 385e53f1ec0SSzymon Olewniczak //restore ID (just in case) 386e53f1ec0SSzymon Olewniczak $ID = $keep; 387e53f1ec0SSzymon Olewniczak 388e53f1ec0SSzymon Olewniczak return $ret; 389e53f1ec0SSzymon Olewniczak } 390e53f1ec0SSzymon Olewniczak 391e53f1ec0SSzymon Olewniczak /** 392d63e7fe7SGerrit Uitslag * Build a pdf from the html 393d63e7fe7SGerrit Uitslag * 394d63e7fe7SGerrit Uitslag * @param string $cachefile 395d9c13ec7SGerrit Uitslag * @param Doku_Event $event 3967c79bc79SGerrit Uitslag * @throws \Mpdf\MpdfException 397d63e7fe7SGerrit Uitslag */ 3982d9cd424SGerrit Uitslag protected function generatePDF($cachefile, $event) { 3992d9cd424SGerrit Uitslag global $REV, $INPUT, $DATE_AT; 400e53f1ec0SSzymon Olewniczak 4012d9cd424SGerrit Uitslag if ($event->data == 'export_pdf') { //only one page is exported 402e53f1ec0SSzymon Olewniczak $rev = $REV; 403e53f1ec0SSzymon Olewniczak $date_at = $DATE_AT; 4042a127a1dSGerrit Uitslag } else { //we are exporting entire namespace, ommit revisions 405e53f1ec0SSzymon Olewniczak $rev = $date_at = ''; 406e53f1ec0SSzymon Olewniczak } 40787c86ddaSAndreas Gohr 40802f9a447SGerrit Uitslag //some shortcuts to export settings 40902f9a447SGerrit Uitslag $hasToC = $this->getExportConfig('hasToC'); 41002f9a447SGerrit Uitslag $levels = $this->getExportConfig('levels'); 41102f9a447SGerrit Uitslag $isDebug = $this->getExportConfig('isDebug'); 412b80f709fSNicolas $watermark = $this->getExportConfig('watermark'); 4136ea88a05SAndreas Gohr 4141ef68647SAndreas Gohr // initialize PDF library 415cde5a1b3SAndreas Gohr require_once(dirname(__FILE__) . "/DokuPDF.class.php"); 4166ea88a05SAndreas Gohr 4174870b378SLarsDW223 $mpdf = new DokuPDF($this->getExportConfig('pagesize'), 4184870b378SLarsDW223 $this->getExportConfig('orientation'), 4194870b378SLarsDW223 $this->getExportConfig('font-size')); 420ee19bac3SLuigi Micco 421d62df65bSAndreas Gohr // let mpdf fix local links 422d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 423d62df65bSAndreas Gohr $url = $self['scheme'] . '://' . $self['host']; 42421a55743SAnna Dabrowska if(!empty($self['port'])) { 42502f9a447SGerrit Uitslag $url .= ':' . $self['port']; 42602f9a447SGerrit Uitslag } 427d9c13ec7SGerrit Uitslag $mpdf->SetBasePath($url); 428d62df65bSAndreas Gohr 42956d13144SAndreas Gohr // Set the title 43003352761SKirsten Roschanski $mpdf->SetTitle($this->title); 43156d13144SAndreas Gohr 432d63e7fe7SGerrit Uitslag // some default document settings 433d63e7fe7SGerrit Uitslag //note: double-sided document, starts at an odd page (first page is a right-hand side page) 434213fdb75SGerrit Uitslag // single-side document has only odd pages 435213fdb75SGerrit Uitslag $mpdf->mirrorMargins = $this->getExportConfig('doublesided'); 436daa70883SAndreas Gohr $mpdf->setAutoTopMargin = 'stretch'; 437daa70883SAndreas Gohr $mpdf->setAutoBottomMargin = 'stretch'; 43802f9a447SGerrit Uitslag// $mpdf->pagenumSuffix = '/'; //prefix for {nbpg} 43902f9a447SGerrit Uitslag if($hasToC) { 44002f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC 44102f9a447SGerrit Uitslag $mpdf->h2toc = $levels; 44202f9a447SGerrit Uitslag } else { 44302f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'); 44402f9a447SGerrit Uitslag } 4452eedf77dSAndreas Gohr 446b80f709fSNicolas // Watermarker 447b80f709fSNicolas if($watermark) { 448b80f709fSNicolas $mpdf->SetWatermarkText($watermark); 449b80f709fSNicolas $mpdf->showWatermarkText = true; 450b80f709fSNicolas } 451b80f709fSNicolas 45256d13144SAndreas Gohr // load the template 45303352761SKirsten Roschanski $template = $this->load_template(); 454ee19bac3SLuigi Micco 4551ef68647SAndreas Gohr // prepare HTML header styles 456a2c33768SGerrit Uitslag $html = ''; 45702f9a447SGerrit Uitslag if($isDebug) { 458a2c33768SGerrit Uitslag $html .= '<html><head>'; 459737417c6SKlap-in $html .= '<style type="text/css">'; 460a2c33768SGerrit Uitslag } 461db1aa1bfSKirsten Roschanski 462db1aa1bfSKirsten Roschanski $styles = '@page { size:auto; ' . $template['page'] . '}'; 463a2c33768SGerrit Uitslag $styles .= '@page :first {' . $template['first'] . '}'; 464254467c4SGerrit Uitslag 465254467c4SGerrit Uitslag $styles .= '@page landscape-page { size:landscape }'; 466254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-landscape { page:landscape-page }'; 467254467c4SGerrit Uitslag $styles .= '@page portrait-page { size:portrait }'; 468254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-portrait { page:portrait-page }'; 469db1aa1bfSKirsten Roschanski $styles .= $this->load_css(); 470254467c4SGerrit Uitslag 471a2c33768SGerrit Uitslag $mpdf->WriteHTML($styles, 1); 472a2c33768SGerrit Uitslag 47302f9a447SGerrit Uitslag if($isDebug) { 474a2c33768SGerrit Uitslag $html .= $styles; 4751ef68647SAndreas Gohr $html .= '</style>'; 4761ef68647SAndreas Gohr $html .= '</head><body>'; 477a2c33768SGerrit Uitslag } 478a2c33768SGerrit Uitslag 479a2c33768SGerrit Uitslag $body_start = $template['html']; 480a2c33768SGerrit Uitslag $body_start .= '<div class="dokuwiki">'; 4812eedf77dSAndreas Gohr 4821e45476bSmnapp // insert the cover page 483a2c33768SGerrit Uitslag $body_start .= $template['cover']; 484a2c33768SGerrit Uitslag 485a2c33768SGerrit Uitslag $mpdf->WriteHTML($body_start, 2, true, false); //start body html 48602f9a447SGerrit Uitslag if($isDebug) { 487a2c33768SGerrit Uitslag $html .= $body_start; 488a2c33768SGerrit Uitslag } 48902f9a447SGerrit Uitslag if($hasToC) { 49002f9a447SGerrit 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 49102f9a447SGerrit Uitslag // - first page of ToC starts always at odd page (so eventually an additional blank page is included before) 49202f9a447SGerrit Uitslag // - there is no page numbering at the pages of the ToC 49302f9a447SGerrit Uitslag $mpdf->TOCpagebreakByArray( 49402f9a447SGerrit Uitslag array( 495230b098dSGerrit Uitslag 'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>', 496230b098dSGerrit Uitslag 'toc-bookmarkText' => $this->getLang('tocheader'), 49702f9a447SGerrit Uitslag 'links' => true, 49802f9a447SGerrit Uitslag 'outdent' => '1em', 49902f9a447SGerrit Uitslag 'resetpagenum' => true, //start pagenumbering after ToC 50002f9a447SGerrit Uitslag 'pagenumstyle' => '1' 50102f9a447SGerrit Uitslag ) 50202f9a447SGerrit Uitslag ); 50302f9a447SGerrit Uitslag $html .= '<tocpagebreak>'; 50402f9a447SGerrit Uitslag } 50502f9a447SGerrit Uitslag 5061ef68647SAndreas Gohr // loop over all pages 507c7138b3fSGerrit Uitslag $counter = 0; 508c7138b3fSGerrit Uitslag $no_pages = count($this->list); 509c7138b3fSGerrit Uitslag foreach($this->list as $page) { 5109c76f78dSVincent GIRARD $this->currentBookChapter = $counter; 511c7138b3fSGerrit Uitslag $counter++; 512b3eed6e3SGerrit Uitslag 51379be256fSMichael Große $pagehtml = $this->p_wiki_dw2pdf($page, $rev, $date_at); 514e53f1ec0SSzymon Olewniczak //file doesn't exists 515e53f1ec0SSzymon Olewniczak if($pagehtml == '') { 516b3eed6e3SGerrit Uitslag continue; 517b3eed6e3SGerrit Uitslag } 518719256adSGerrit Uitslag $pagehtml .= $this->page_depend_replacements($template['cite'], $page); 519c7138b3fSGerrit Uitslag if($counter < $no_pages) { 520a2c33768SGerrit Uitslag $pagehtml .= '<pagebreak />'; 521a2c33768SGerrit Uitslag } 522a2c33768SGerrit Uitslag 523a2c33768SGerrit Uitslag $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html 52402f9a447SGerrit Uitslag if($isDebug) { 525a2c33768SGerrit Uitslag $html .= $pagehtml; 5261ef68647SAndreas Gohr } 527ee19bac3SLuigi Micco } 528ee19bac3SLuigi Micco 52933c15297SGerrit Uitslag // insert the back page 530a2c33768SGerrit Uitslag $body_end = $template['back']; 53133c15297SGerrit Uitslag 532a2c33768SGerrit Uitslag $body_end .= '</div>'; 533a2c33768SGerrit Uitslag 534d63e7fe7SGerrit Uitslag $mpdf->WriteHTML($body_end, 2, false, true); // finish body html 53502f9a447SGerrit Uitslag if($isDebug) { 536a2c33768SGerrit Uitslag $html .= $body_end; 537eeb17e15SAndreas Gohr $html .= '</body>'; 538eeb17e15SAndreas Gohr $html .= '</html>'; 539a2c33768SGerrit Uitslag } 540f765508eSGerrit Uitslag 541f765508eSGerrit Uitslag //Return html for debugging 54202f9a447SGerrit Uitslag if($isDebug) { 54302f9a447SGerrit Uitslag if($INPUT->str('debughtml', 'text', true) == 'html') { 54426be4eceSGerrit Uitslag echo $html; 545a2c33768SGerrit Uitslag } else { 546a2c33768SGerrit Uitslag header('Content-Type: text/plain; charset=utf-8'); 547a2c33768SGerrit Uitslag echo $html; 548a2c33768SGerrit Uitslag } 54926be4eceSGerrit Uitslag exit(); 5507c79bc79SGerrit Uitslag } 551f765508eSGerrit Uitslag 55287c86ddaSAndreas Gohr // write to cache file 553d63e7fe7SGerrit Uitslag $mpdf->Output($cachefile, 'F'); 55487c86ddaSAndreas Gohr } 55587c86ddaSAndreas Gohr 556d63e7fe7SGerrit Uitslag /** 557d63e7fe7SGerrit Uitslag * @param string $cachefile 558d63e7fe7SGerrit Uitslag */ 55903352761SKirsten Roschanski protected function sendPDFFile($cachefile) { 56087c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 561b853b723SAndreas Gohr header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 56287c86ddaSAndreas Gohr header('Pragma: public'); 563d63e7fe7SGerrit Uitslag http_conditionalRequest(filemtime($cachefile)); 564c0956f69SMichael Große global $INPUT; 565f7b24f48SMichael Große $outputTarget = $INPUT->str('outputTarget', $this->getConf('output')); 56687c86ddaSAndreas Gohr 56703352761SKirsten Roschanski $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', ' '))); 568c0956f69SMichael Große if($outputTarget === 'file') { 5699a3c8d9fSAndreas Gohr header('Content-Disposition: attachment; filename="' . $filename . '.pdf";'); 57087c86ddaSAndreas Gohr } else { 5719a3c8d9fSAndreas Gohr header('Content-Disposition: inline; filename="' . $filename . '.pdf";'); 57287c86ddaSAndreas Gohr } 573ee19bac3SLuigi Micco 574b3eed6e3SGerrit Uitslag //Bookcreator uses jQuery.fileDownload.js, which requires a cookie. 575b3eed6e3SGerrit Uitslag header('Set-Cookie: fileDownload=true; path=/'); 576b3eed6e3SGerrit Uitslag 577e993da11SGerrit Uitslag //try to send file, and exit if done 578d63e7fe7SGerrit Uitslag http_sendfile($cachefile); 57987c86ddaSAndreas Gohr 580d63e7fe7SGerrit Uitslag $fp = @fopen($cachefile, "rb"); 58187c86ddaSAndreas Gohr if($fp) { 582d63e7fe7SGerrit Uitslag http_rangeRequest($fp, filesize($cachefile), 'application/pdf'); 58387c86ddaSAndreas Gohr } else { 58487c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 58587c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 58687c86ddaSAndreas Gohr } 5871ef68647SAndreas Gohr exit(); 5881ef68647SAndreas Gohr } 5891ef68647SAndreas Gohr 5906be736bfSGerrit Uitslag /** 5912eedf77dSAndreas Gohr * Load the various template files and prepare the HTML/CSS for insertion 59244e8e8fbSGerrit Uitslag * 59344e8e8fbSGerrit Uitslag * @return array 5941ef68647SAndreas Gohr */ 59503352761SKirsten Roschanski protected function load_template() { 5961ef68647SAndreas Gohr global $ID; 5971ef68647SAndreas Gohr global $conf; 5981ef68647SAndreas Gohr 5992eedf77dSAndreas Gohr // this is what we'll return 60021a55743SAnna Dabrowska $output = [ 6011e45476bSmnapp 'cover' => '', 60221a55743SAnna Dabrowska 'back' => '', 6032eedf77dSAndreas Gohr 'html' => '', 6042eedf77dSAndreas Gohr 'page' => '', 6052eedf77dSAndreas Gohr 'first' => '', 6062eedf77dSAndreas Gohr 'cite' => '', 60721a55743SAnna Dabrowska ]; 6082eedf77dSAndreas Gohr 6092eedf77dSAndreas Gohr // prepare header/footer elements 6102eedf77dSAndreas Gohr $html = ''; 61133c15297SGerrit Uitslag foreach(array('header', 'footer') as $section) { 61233c15297SGerrit Uitslag foreach(array('', '_odd', '_even', '_first') as $order) { 61302f9a447SGerrit Uitslag $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html'; 61433c15297SGerrit Uitslag if(file_exists($file)) { 61533c15297SGerrit Uitslag $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF; 61633c15297SGerrit Uitslag $html .= file_get_contents($file) . DOKU_LF; 61733c15297SGerrit Uitslag $html .= '</htmlpage' . $section . '>' . DOKU_LF; 6182eedf77dSAndreas Gohr 6192eedf77dSAndreas Gohr // register the needed pseudo CSS 62033c15297SGerrit Uitslag if($order == '_first') { 62133c15297SGerrit Uitslag $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 62233c15297SGerrit Uitslag } elseif($order == '_even') { 62333c15297SGerrit Uitslag $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 62433c15297SGerrit Uitslag } elseif($order == '_odd') { 62533c15297SGerrit Uitslag $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 626daa70883SAndreas Gohr } else { 62733c15297SGerrit Uitslag $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 6282eedf77dSAndreas Gohr } 6292eedf77dSAndreas Gohr } 6302eedf77dSAndreas Gohr } 6312eedf77dSAndreas Gohr } 6322eedf77dSAndreas Gohr 6331ef68647SAndreas Gohr // prepare replacements 6341ef68647SAndreas Gohr $replace = array( 6351ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 63602f9a447SGerrit Uitslag '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / ' 63703352761SKirsten Roschanski '@TITLE@' => hsc($this->title), 6381ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 6391ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 6401ef68647SAndreas Gohr '@DATE@' => dformat(time()), 6415d6fbaeaSAndreas Gohr '@BASE@' => DOKU_BASE, 642893987a2SAndreas Gohr '@INC@' => DOKU_INC, 643893987a2SAndreas Gohr '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 644893987a2SAndreas Gohr '@TPLINC@' => DOKU_INC . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/' 6451ef68647SAndreas Gohr ); 6461ef68647SAndreas Gohr 6472eedf77dSAndreas Gohr // set HTML element 648a180c973SKlap-in $html = str_replace(array_keys($replace), array_values($replace), $html); 649a180c973SKlap-in //TODO For bookcreator $ID (= bookmanager page) makes no sense 650a180c973SKlap-in $output['html'] = $this->page_depend_replacements($html, $ID); 6511ef68647SAndreas Gohr 6521e45476bSmnapp // cover page 65302f9a447SGerrit Uitslag $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html'; 65433c15297SGerrit Uitslag if(file_exists($coverfile)) { 65533c15297SGerrit Uitslag $output['cover'] = file_get_contents($coverfile); 6561e45476bSmnapp $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']); 6579b071da5SMichael $output['cover'] = $this->page_depend_replacements($output['cover'], $ID); 6586e2ec302SGerrit Uitslag $output['cover'] .= '<pagebreak />'; 6591e45476bSmnapp } 6601e45476bSmnapp 66133c15297SGerrit Uitslag // cover page 66202f9a447SGerrit Uitslag $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html'; 66333c15297SGerrit Uitslag if(file_exists($backfile)) { 66433c15297SGerrit Uitslag $output['back'] = '<pagebreak />'; 66533c15297SGerrit Uitslag $output['back'] .= file_get_contents($backfile); 66633c15297SGerrit Uitslag $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']); 6679b071da5SMichael $output['back'] = $this->page_depend_replacements($output['back'], $ID); 66833c15297SGerrit Uitslag } 66933c15297SGerrit Uitslag 6702eedf77dSAndreas Gohr // citation box 67102f9a447SGerrit Uitslag $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html'; 67233c15297SGerrit Uitslag if(file_exists($citationfile)) { 67333c15297SGerrit Uitslag $output['cite'] = file_get_contents($citationfile); 6742eedf77dSAndreas Gohr $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 6752eedf77dSAndreas Gohr } 6761ef68647SAndreas Gohr 6772eedf77dSAndreas Gohr return $output; 6781ef68647SAndreas Gohr } 6791ef68647SAndreas Gohr 6801ef68647SAndreas Gohr /** 681a180c973SKlap-in * @param string $raw code with placeholders 682a180c973SKlap-in * @param string $id pageid 683a180c973SKlap-in * @return string 684a180c973SKlap-in */ 685a180c973SKlap-in protected function page_depend_replacements($raw, $id) { 686e53f1ec0SSzymon Olewniczak global $REV, $DATE_AT; 687a180c973SKlap-in 6889b2636c8SDiego Belmar // generate qr code for this page using quickchart.io (Google infographics api was deprecated in March 14, 2019) 689a180c973SKlap-in $qr_code = ''; 690a180c973SKlap-in if($this->getConf('qrcodesize')) { 691a180c973SKlap-in $url = urlencode(wl($id, '', '&', true)); 6929b2636c8SDiego Belmar $qr_code = '<img src="https://quickchart.io/qr?size=' . 6939b2636c8SDiego Belmar $this->getConf('qrcodesize') . '&text=' . $url . '&margin=1&ecLevel=Q" />'; 694a180c973SKlap-in } 695a180c973SKlap-in // prepare replacements 696a180c973SKlap-in $replace['@ID@'] = $id; 697a180c973SKlap-in $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV))); 698e53f1ec0SSzymon Olewniczak 699e53f1ec0SSzymon Olewniczak $params = array(); 700e53f1ec0SSzymon Olewniczak if($DATE_AT) { 701e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 702e53f1ec0SSzymon Olewniczak } elseif($REV) { 703e53f1ec0SSzymon Olewniczak $params['rev'] = $REV; 704e53f1ec0SSzymon Olewniczak } 705e53f1ec0SSzymon Olewniczak $replace['@PAGEURL@'] = wl($id, $params, true, "&"); 706a180c973SKlap-in $replace['@QRCODE@'] = $qr_code; 707a180c973SKlap-in 708d824d23dSAnna Dabrowska $content = $raw; 709a12c41c3SAnna Dabrowska 710a12c41c3SAnna Dabrowska // let other plugins define their own replacements 711d824d23dSAnna Dabrowska $evdata = ['id' => $id, 'replace' => &$replace, 'content' => &$content]; 712a12c41c3SAnna Dabrowska $event = new Doku_Event('PLUGIN_DW2PDF_REPLACE', $evdata); 713d824d23dSAnna Dabrowska if ($event->advise_before()) { 714e3d68265SGerrit Uitslag $content = str_replace(array_keys($replace), array_values($replace), $raw); 715d824d23dSAnna Dabrowska } 716e3d68265SGerrit Uitslag 717a12c41c3SAnna Dabrowska // plugins may post-process HTML, e.g to clean up unused replacements 718a12c41c3SAnna Dabrowska $event->advise_after(); 719a12c41c3SAnna Dabrowska 720e3d68265SGerrit Uitslag // @DATE(<date>[, <format>])@ 721e3d68265SGerrit Uitslag $content = preg_replace_callback( 722e3d68265SGerrit Uitslag '/@DATE\((.*?)(?:,\s*(.*?))?\)@/', 723e3d68265SGerrit Uitslag array($this, 'replacedate'), 724e3d68265SGerrit Uitslag $content 725e3d68265SGerrit Uitslag ); 726e3d68265SGerrit Uitslag 727e3d68265SGerrit Uitslag return $content; 728a180c973SKlap-in } 729a180c973SKlap-in 730e3d68265SGerrit Uitslag 731e3d68265SGerrit Uitslag /** 732e3d68265SGerrit Uitslag * (callback) Replace date by request datestring 733e3d68265SGerrit Uitslag * e.g. '%m(30-11-1975)' is replaced by '11' 734e3d68265SGerrit Uitslag * 735e3d68265SGerrit Uitslag * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern 736e3d68265SGerrit Uitslag * @return string 737e3d68265SGerrit Uitslag */ 738e3d68265SGerrit Uitslag function replacedate($match) { 739e3d68265SGerrit Uitslag global $conf; 740e3d68265SGerrit Uitslag //no 2nd argument for default date format 741e3d68265SGerrit Uitslag if($match[2] == null) { 742e3d68265SGerrit Uitslag $match[2] = $conf['dformat']; 743e3d68265SGerrit Uitslag } 744e3d68265SGerrit Uitslag return strftime($match[2], strtotime($match[1])); 745e3d68265SGerrit Uitslag } 746e3d68265SGerrit Uitslag 747a180c973SKlap-in /** 7481c14c879SAndreas Gohr * Load all the style sheets and apply the needed replacements 7491ef68647SAndreas Gohr */ 7501c14c879SAndreas Gohr protected function load_css() { 751737417c6SKlap-in global $conf; 7527c79bc79SGerrit Uitslag //reuse the CSS dispatcher functions without triggering the main function 7531c14c879SAndreas Gohr define('SIMPLE_TEST', 1); 7541c14c879SAndreas Gohr require_once(DOKU_INC . 'lib/exe/css.php'); 755ee19bac3SLuigi Micco 7561c14c879SAndreas Gohr // prepare CSS files 7571c14c879SAndreas Gohr $files = array_merge( 7581c14c879SAndreas Gohr array( 7591c14c879SAndreas Gohr DOKU_INC . 'lib/styles/screen.css' 7601c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7611c14c879SAndreas Gohr DOKU_INC . 'lib/styles/print.css' 7621c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7631c14c879SAndreas Gohr ), 76458e6409eSAndreas Gohr $this->css_pluginPDFstyles(), 7651c14c879SAndreas Gohr array( 7661c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.css' 7671c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7681c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' 7691c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 7701c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' 7711c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7721c14c879SAndreas Gohr ) 7731c14c879SAndreas Gohr ); 7741c14c879SAndreas Gohr $css = ''; 7751c14c879SAndreas Gohr foreach($files as $file => $location) { 77628e636eaSGerrit Uitslag $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 77728e636eaSGerrit Uitslag $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 7781c14c879SAndreas Gohr $css .= css_loadfile($file, $location); 7791ef68647SAndreas Gohr } 7801ef68647SAndreas Gohr 78128e636eaSGerrit Uitslag if(function_exists('css_parseless')) { 7821c14c879SAndreas Gohr // apply pattern replacements 7837c6ca3bcSMichael Große if (function_exists('css_styleini')) { 7847c6ca3bcSMichael Große // compatiblity layer for pre-Greebo releases of DokuWiki 78528e636eaSGerrit Uitslag $styleini = css_styleini($conf['template']); 7867c6ca3bcSMichael Große } else { 7877c6ca3bcSMichael Große // Greebo functionality 7887c6ca3bcSMichael Große $styleUtils = new \dokuwiki\StyleUtils(); 789d0f0c534SGerrit Uitslag $styleini = $styleUtils->cssStyleini($conf['template']); // older versions need still the template 7907c6ca3bcSMichael Große } 79128e636eaSGerrit Uitslag $css = css_applystyle($css, $styleini['replacements']); 79228e636eaSGerrit Uitslag 79328e636eaSGerrit Uitslag // parse less 79428e636eaSGerrit Uitslag $css = css_parseless($css); 79528e636eaSGerrit Uitslag } else { 79628e636eaSGerrit Uitslag // @deprecated 2013-12-19: fix backward compatibility 7971c14c879SAndreas Gohr $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 79828e636eaSGerrit Uitslag } 7991ef68647SAndreas Gohr 8001c14c879SAndreas Gohr return $css; 801ee19bac3SLuigi Micco } 8021c14c879SAndreas Gohr 80358e6409eSAndreas Gohr /** 80458e6409eSAndreas Gohr * Returns a list of possible Plugin PDF Styles 80558e6409eSAndreas Gohr * 80658e6409eSAndreas Gohr * Checks for a pdf.css, falls back to print.css 80758e6409eSAndreas Gohr * 80858e6409eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 80958e6409eSAndreas Gohr */ 8106be736bfSGerrit Uitslag protected function css_pluginPDFstyles() { 81158e6409eSAndreas Gohr $list = array(); 81258e6409eSAndreas Gohr $plugins = plugin_list(); 813f54b51f7SAndreas Gohr 814f54b51f7SAndreas Gohr $usestyle = explode(',', $this->getConf('usestyles')); 81558e6409eSAndreas Gohr foreach($plugins as $p) { 816f54b51f7SAndreas Gohr if(in_array($p, $usestyle)) { 817f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/"; 8188003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/"; 8198003b493SGerrit Uitslag 820f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/"; 8218003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/"; 822f54b51f7SAndreas Gohr } 823f54b51f7SAndreas Gohr 8248003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/"; 8258003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/"; 8268003b493SGerrit Uitslag 827ab96d816SMichael Große if(file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) { 82858e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/"; 8298003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/"; 83058e6409eSAndreas Gohr } else { 83158e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/"; 8328003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/"; 83358e6409eSAndreas Gohr } 83458e6409eSAndreas Gohr } 8358b8ac6ecSAndreas Gohr 8368b8ac6ecSAndreas Gohr // template support 8378b8ac6ecSAndreas Gohr foreach (['pdf.css', 'pdf.less', 'css/pdf.css', 'css/pdf.less', 'styles/pdf.css', 'styles/pdf.less'] as $file) { 8388b8ac6ecSAndreas Gohr if (file_exists(tpl_incdir() . $file)) { 8398b8ac6ecSAndreas Gohr $list[tpl_incdir() . $file] = tpl_basedir() . $file; 8408b8ac6ecSAndreas Gohr } 8418b8ac6ecSAndreas Gohr } 8428b8ac6ecSAndreas Gohr 84358e6409eSAndreas Gohr return $list; 84458e6409eSAndreas Gohr } 845ad18f4e1SGerrit Uitslag 846ad18f4e1SGerrit Uitslag /** 84760e59de7SGerrit Uitslag * Returns array of pages which will be included in the exported pdf 84860e59de7SGerrit Uitslag * 84960e59de7SGerrit Uitslag * @return array 85060e59de7SGerrit Uitslag */ 85160e59de7SGerrit Uitslag public function getExportedPages() { 85260e59de7SGerrit Uitslag return $this->list; 85360e59de7SGerrit Uitslag } 85460e59de7SGerrit Uitslag 85560e59de7SGerrit Uitslag /** 856ad18f4e1SGerrit Uitslag * usort callback to sort by file lastmodified time 85744e8e8fbSGerrit Uitslag * 85844e8e8fbSGerrit Uitslag * @param array $a 85944e8e8fbSGerrit Uitslag * @param array $b 86044e8e8fbSGerrit Uitslag * @return int 861ad18f4e1SGerrit Uitslag */ 862ad18f4e1SGerrit Uitslag public function _datesort($a, $b) { 863ad18f4e1SGerrit Uitslag if($b['rev'] < $a['rev']) return -1; 864ad18f4e1SGerrit Uitslag if($b['rev'] > $a['rev']) return 1; 865ad18f4e1SGerrit Uitslag return strcmp($b['id'], $a['id']); 866ad18f4e1SGerrit Uitslag } 867ad18f4e1SGerrit Uitslag 868ad18f4e1SGerrit Uitslag /** 869ad18f4e1SGerrit Uitslag * usort callback to sort by page id 87044e8e8fbSGerrit Uitslag * @param array $a 87144e8e8fbSGerrit Uitslag * @param array $b 87244e8e8fbSGerrit Uitslag * @return int 873ad18f4e1SGerrit Uitslag */ 874ad18f4e1SGerrit Uitslag public function _pagenamesort($a, $b) { 87520025b90SAndreas Gohr global $conf; 87620025b90SAndreas Gohr 87720025b90SAndreas Gohr $partsA = explode(':', $a['id']); 87820025b90SAndreas Gohr $countA = count($partsA); 87920025b90SAndreas Gohr $partsB = explode(':', $b['id']); 88020025b90SAndreas Gohr $countB = count($partsB); 881e00f6071SAndreas Gohr $max = max($countA, $countB); 88220025b90SAndreas Gohr 88320025b90SAndreas Gohr 88420025b90SAndreas Gohr // compare namepsace by namespace 885e00f6071SAndreas Gohr for ($i = 0; $i < $max; $i++) { 886e00f6071SAndreas Gohr $partA = $partsA[$i] ?: null; 887e00f6071SAndreas Gohr $partB = $partsB[$i] ?: null; 88820025b90SAndreas Gohr 88920025b90SAndreas Gohr // have we reached the page level? 890e00f6071SAndreas Gohr if ($i === ($countA - 1) || $i === ($countB - 1)) { 89120025b90SAndreas Gohr // start page first 89220025b90SAndreas Gohr if ($partA == $conf['start']) return -1; 89320025b90SAndreas Gohr if ($partB == $conf['start']) return 1; 89420025b90SAndreas Gohr } 89520025b90SAndreas Gohr 896e00f6071SAndreas Gohr // prefer page over namespace 897e00f6071SAndreas Gohr if($partA === $partB) { 898e00f6071SAndreas Gohr if (!isset($partsA[$i + 1])) return -1; 899e00f6071SAndreas Gohr if (!isset($partsB[$i + 1])) return 1; 900e00f6071SAndreas Gohr continue; 901e00f6071SAndreas Gohr } 902e00f6071SAndreas Gohr 903e00f6071SAndreas Gohr 90420025b90SAndreas Gohr // simply compare 90541e5d4e2SAndreas Gohr return strnatcmp($partA, $partB); 90620025b90SAndreas Gohr } 90720025b90SAndreas Gohr 90841e5d4e2SAndreas Gohr return strnatcmp($a['id'], $b['id']); 909ad18f4e1SGerrit Uitslag } 91026be4eceSGerrit Uitslag 91126be4eceSGerrit Uitslag /** 9127c79bc79SGerrit Uitslag * Collects settings from: 91302f9a447SGerrit Uitslag * 1. url parameters 91402f9a447SGerrit Uitslag * 2. plugin config 91502f9a447SGerrit Uitslag * 3. global config 91602f9a447SGerrit Uitslag */ 91702f9a447SGerrit Uitslag protected function loadExportConfig() { 91802f9a447SGerrit Uitslag global $INPUT; 91902f9a447SGerrit Uitslag global $conf; 92002f9a447SGerrit Uitslag 92102f9a447SGerrit Uitslag $this->exportConfig = array(); 92202f9a447SGerrit Uitslag 92302f9a447SGerrit Uitslag // decide on the paper setup from param or config 92402f9a447SGerrit Uitslag $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true); 92502f9a447SGerrit Uitslag $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true); 92602f9a447SGerrit Uitslag 9274870b378SLarsDW223 // decide on the font-size from param or config 9284870b378SLarsDW223 $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true); 9294870b378SLarsDW223 930213fdb75SGerrit Uitslag $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided')); 931213fdb75SGerrit Uitslag $this->exportConfig['doublesided'] = $doublesided ? '1' : '0'; 932213fdb75SGerrit Uitslag 933b80f709fSNicolas $this->exportConfig['watermark'] = $INPUT->str('watermark', ''); 934b80f709fSNicolas 935213fdb75SGerrit Uitslag $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc')); 93602f9a447SGerrit Uitslag $levels = array(); 93702f9a447SGerrit Uitslag if($hasToC) { 93802f9a447SGerrit Uitslag $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true); 93921a55743SAnna Dabrowska list($top_input, $max_input) = array_pad(explode('-', $toclevels, 2), 2, ''); 94021a55743SAnna Dabrowska list($top_conf, $max_conf) = array_pad(explode('-', $this->getConf('toclevels'), 2), 2, ''); 94102f9a447SGerrit Uitslag $bounds_input = array( 94202f9a447SGerrit Uitslag 'top' => array( 94302f9a447SGerrit Uitslag (int) $top_input, 94402f9a447SGerrit Uitslag (int) $top_conf 94502f9a447SGerrit Uitslag ), 94602f9a447SGerrit Uitslag 'max' => array( 94702f9a447SGerrit Uitslag (int) $max_input, 94802f9a447SGerrit Uitslag (int) $max_conf 94902f9a447SGerrit Uitslag ) 95002f9a447SGerrit Uitslag ); 95102f9a447SGerrit Uitslag $bounds = array( 95202f9a447SGerrit Uitslag 'top' => $conf['toptoclevel'], 95302f9a447SGerrit Uitslag 'max' => $conf['maxtoclevel'] 95402f9a447SGerrit Uitslag 95502f9a447SGerrit Uitslag ); 95602f9a447SGerrit Uitslag foreach($bounds_input as $bound => $values) { 95702f9a447SGerrit Uitslag foreach($values as $value) { 95802f9a447SGerrit Uitslag if($value > 0 && $value <= 5) { 95902f9a447SGerrit Uitslag //stop at valid value and store 96002f9a447SGerrit Uitslag $bounds[$bound] = $value; 96102f9a447SGerrit Uitslag break; 96202f9a447SGerrit Uitslag } 96302f9a447SGerrit Uitslag } 96402f9a447SGerrit Uitslag } 96502f9a447SGerrit Uitslag 96602f9a447SGerrit Uitslag if($bounds['max'] < $bounds['top']) { 96702f9a447SGerrit Uitslag $bounds['max'] = $bounds['top']; 96802f9a447SGerrit Uitslag } 96902f9a447SGerrit Uitslag 97002f9a447SGerrit Uitslag for($level = $bounds['top']; $level <= $bounds['max']; $level++) { 97102f9a447SGerrit Uitslag $levels["H$level"] = $level - 1; 97202f9a447SGerrit Uitslag } 97302f9a447SGerrit Uitslag } 97402f9a447SGerrit Uitslag $this->exportConfig['hasToC'] = $hasToC; 97502f9a447SGerrit Uitslag $this->exportConfig['levels'] = $levels; 97602f9a447SGerrit Uitslag 97702f9a447SGerrit Uitslag $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true); 97802f9a447SGerrit Uitslag 97902f9a447SGerrit Uitslag $tplconf = $this->getConf('template'); 98005d2b507SGerrit Uitslag $tpl = $INPUT->str('tpl', $tplconf, true); 98102f9a447SGerrit Uitslag if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) { 98202f9a447SGerrit Uitslag $tpl = $tplconf; 98302f9a447SGerrit Uitslag } 98402f9a447SGerrit Uitslag if(!$tpl){ 98502f9a447SGerrit Uitslag $tpl = 'default'; 98602f9a447SGerrit Uitslag } 98702f9a447SGerrit Uitslag $this->exportConfig['template'] = $tpl; 98802f9a447SGerrit Uitslag 98902f9a447SGerrit Uitslag $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml'); 99002f9a447SGerrit Uitslag } 99102f9a447SGerrit Uitslag 99202f9a447SGerrit Uitslag /** 99302f9a447SGerrit Uitslag * Returns requested config 99402f9a447SGerrit Uitslag * 99502f9a447SGerrit Uitslag * @param string $name 99602f9a447SGerrit Uitslag * @param mixed $notset 99702f9a447SGerrit Uitslag * @return mixed|bool 99802f9a447SGerrit Uitslag */ 99902f9a447SGerrit Uitslag public function getExportConfig($name, $notset = false) { 100002f9a447SGerrit Uitslag if ($this->exportConfig === null){ 100102f9a447SGerrit Uitslag $this->loadExportConfig(); 100202f9a447SGerrit Uitslag } 100302f9a447SGerrit Uitslag 100402f9a447SGerrit Uitslag if(isset($this->exportConfig[$name])){ 100502f9a447SGerrit Uitslag return $this->exportConfig[$name]; 100602f9a447SGerrit Uitslag }else{ 100702f9a447SGerrit Uitslag return $notset; 100802f9a447SGerrit Uitslag } 100902f9a447SGerrit Uitslag } 1010d63e7fe7SGerrit Uitslag 1011d63e7fe7SGerrit Uitslag /** 1012d63e7fe7SGerrit Uitslag * Add 'export pdf'-button to pagetools 1013d63e7fe7SGerrit Uitslag * 1014d63e7fe7SGerrit Uitslag * @param Doku_Event $event 1015d63e7fe7SGerrit Uitslag */ 101644e8e8fbSGerrit Uitslag public function addbutton(Doku_Event $event) { 1017e53f1ec0SSzymon Olewniczak global $ID, $REV, $DATE_AT; 1018d63e7fe7SGerrit Uitslag 1019d63e7fe7SGerrit Uitslag if($this->getConf('showexportbutton') && $event->data['view'] == 'main') { 1020d63e7fe7SGerrit Uitslag $params = array('do' => 'export_pdf'); 1021e53f1ec0SSzymon Olewniczak if($DATE_AT) { 1022e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 1023e53f1ec0SSzymon Olewniczak } elseif($REV) { 1024d63e7fe7SGerrit Uitslag $params['rev'] = $REV; 1025d63e7fe7SGerrit Uitslag } 1026d63e7fe7SGerrit Uitslag 1027d63e7fe7SGerrit Uitslag // insert button at position before last (up to top) 1028d63e7fe7SGerrit Uitslag $event->data['items'] = array_slice($event->data['items'], 0, -1, true) + 1029d63e7fe7SGerrit Uitslag array('export_pdf' => 1030d63e7fe7SGerrit Uitslag '<li>' 103172cadc31SChristian Paul . '<a href="' . wl($ID, $params) . '" class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">' 1032d63e7fe7SGerrit Uitslag . '<span>' . $this->getLang('export_pdf_button') . '</span>' 1033d63e7fe7SGerrit Uitslag . '</a>' 1034d63e7fe7SGerrit Uitslag . '</li>' 1035d63e7fe7SGerrit Uitslag ) + 1036d63e7fe7SGerrit Uitslag array_slice($event->data['items'], -1, 1, true); 1037d63e7fe7SGerrit Uitslag } 1038d63e7fe7SGerrit Uitslag } 1039026b594bSAndreas Gohr 1040026b594bSAndreas Gohr /** 1041026b594bSAndreas Gohr * Add 'export pdf' button to page tools, new SVG based mechanism 1042026b594bSAndreas Gohr * 1043026b594bSAndreas Gohr * @param Doku_Event $event 1044026b594bSAndreas Gohr */ 1045026b594bSAndreas Gohr public function addsvgbutton(Doku_Event $event) { 10464c493e44SGerrit Uitslag global $INFO; 10474c493e44SGerrit Uitslag if($event->data['view'] != 'page' || !$this->getConf('showexportbutton')) { 10484c493e44SGerrit Uitslag return; 10494c493e44SGerrit Uitslag } 10504c493e44SGerrit Uitslag 10514c493e44SGerrit Uitslag if(!$INFO['exists']) { 10524c493e44SGerrit Uitslag return; 10534c493e44SGerrit Uitslag } 10544c493e44SGerrit Uitslag 1055026b594bSAndreas Gohr array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\dw2pdf\MenuItem()]); 1056026b594bSAndreas Gohr } 1057ee19bac3SLuigi Micco} 1058