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(); 299b288b2aSGerrit Uitslag protected $onetimefile = false; 301c14c879SAndreas Gohr 311c14c879SAndreas Gohr /** 321c14c879SAndreas Gohr * Constructor. Sets the correct template 3303352761SKirsten Roschanski * 3403352761SKirsten Roschanski * @param string $title 351c14c879SAndreas Gohr */ 3603352761SKirsten Roschanski public function __construct($title=null) { 3702f9a447SGerrit Uitslag $this->tpl = $this->getExportConfig('template'); 3803352761SKirsten Roschanski $this->title = $title ? $title : ''; 391c14c879SAndreas Gohr } 401c14c879SAndreas Gohr 41ee19bac3SLuigi Micco /** 429b288b2aSGerrit Uitslag * Delete cached files that were for one-time use 439b288b2aSGerrit Uitslag */ 449b288b2aSGerrit Uitslag public function __destruct() { 459b288b2aSGerrit Uitslag if($this->onetimefile) { 469b288b2aSGerrit Uitslag unlink($this->onetimefile); 479b288b2aSGerrit Uitslag } 489b288b2aSGerrit Uitslag } 499b288b2aSGerrit Uitslag 509b288b2aSGerrit Uitslag /** 51ee19bac3SLuigi Micco * Register the events 52177a7d30SGerrit Uitslag * 53177a7d30SGerrit Uitslag * @param Doku_Event_Handler $controller 54ee19bac3SLuigi Micco */ 556be736bfSGerrit Uitslag public function register(Doku_Event_Handler $controller) { 56ee19bac3SLuigi Micco $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array()); 576be736bfSGerrit Uitslag $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array()); 58026b594bSAndreas Gohr $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton', array()); 59ee19bac3SLuigi Micco } 60ee19bac3SLuigi Micco 611c14c879SAndreas Gohr /** 621c14c879SAndreas Gohr * Do the HTML to PDF conversion work 63737417c6SKlap-in * 64737417c6SKlap-in * @param Doku_Event $event 651c14c879SAndreas Gohr */ 6644e8e8fbSGerrit Uitslag public function convert(Doku_Event $event) { 672d9cd424SGerrit Uitslag global $ID, $REV, $DATE_AT; 682d9cd424SGerrit Uitslag global $conf, $INPUT; 69ee19bac3SLuigi Micco 701ef68647SAndreas Gohr // our event? 71d9c13ec7SGerrit Uitslag if(($event->data != 'export_pdfbook') && ($event->data != 'export_pdf') && ($event->data != 'export_pdfns')) return; 72ee19bac3SLuigi Micco 731ef68647SAndreas Gohr // check user's rights 74d9c13ec7SGerrit Uitslag if(auth_quickaclcheck($ID) < AUTH_READ) return; 751ef68647SAndreas Gohr 76d63e7fe7SGerrit Uitslag if($data = $this->collectExportPages($event)) { 7703352761SKirsten Roschanski list($this->title, $this->list) = $data; 78d63e7fe7SGerrit Uitslag } else { 79d9c13ec7SGerrit Uitslag return; 80d63e7fe7SGerrit Uitslag } 81d63e7fe7SGerrit Uitslag 822d9cd424SGerrit Uitslag if($event->data === 'export_pdf' && ($REV || $DATE_AT)) { 839b288b2aSGerrit Uitslag $cachefile = tempnam($conf['tmpdir'] . '/dwpdf', 'dw2pdf_'); 849b288b2aSGerrit Uitslag $this->onetimefile = $cachefile; 85f00df45eSMichael Große $generateNewPdf = true; 86f00df45eSMichael Große } else { 87a58f45f0SGerrit Uitslag // prepare cache and its dependencies 88a58f45f0SGerrit Uitslag $depends = array(); 8903352761SKirsten Roschanski $cache = $this->prepareCache($depends); 909b288b2aSGerrit Uitslag $cachefile = $cache->cache; 9127195d5bSMichael Große $generateNewPdf = !$this->getConf('usecache') 9227195d5bSMichael Große || $this->getExportConfig('isDebug') 9327195d5bSMichael Große || !$cache->useCache($depends); 94f00df45eSMichael Große } 95d63e7fe7SGerrit Uitslag 96bd977188SGerrit Uitslag // hard work only when no cache available or needed for debugging 97f00df45eSMichael Große if($generateNewPdf) { 98e5f6c2cbSMichael Große // generating the pdf may take a long time for larger wikis / namespaces with many pages 99e5f6c2cbSMichael Große set_time_limit(0); 100b34cb34eSSzymon Olewniczak try { 1019b288b2aSGerrit Uitslag $this->generatePDF($cachefile, $event); 102b34cb34eSSzymon Olewniczak } catch(Mpdf\MpdfException $e) { 1032d9cd424SGerrit Uitslag if($INPUT->has('selection')) { 1042d9cd424SGerrit Uitslag http_status(400); 1052d9cd424SGerrit Uitslag print $e->getMessage(); 1062d9cd424SGerrit Uitslag exit(); 1072d9cd424SGerrit Uitslag } else { 108b34cb34eSSzymon Olewniczak //prevent act_export() 1092d9cd424SGerrit Uitslag $event->data = 'show'; 110b34cb34eSSzymon Olewniczak msg($e->getMessage(), -1); 1112d9cd424SGerrit Uitslag $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url 112d9c13ec7SGerrit Uitslag return; 113b34cb34eSSzymon Olewniczak } 114d63e7fe7SGerrit Uitslag } 1152d9cd424SGerrit Uitslag } 1162d9cd424SGerrit Uitslag 1172d9cd424SGerrit Uitslag $event->preventDefault(); // after prevent, $event->data cannot be changed 118d63e7fe7SGerrit Uitslag 119d63e7fe7SGerrit Uitslag // deliver the file 1209b288b2aSGerrit Uitslag $this->sendPDFFile($cachefile); //exits 121d63e7fe7SGerrit Uitslag } 122d63e7fe7SGerrit Uitslag 123d63e7fe7SGerrit Uitslag /** 124d63e7fe7SGerrit Uitslag * Obtain list of pages and title, based on url parameters 125d63e7fe7SGerrit Uitslag * 126d63e7fe7SGerrit Uitslag * @param Doku_Event $event 127d63e7fe7SGerrit Uitslag * @return string|bool 128d63e7fe7SGerrit Uitslag */ 129d63e7fe7SGerrit Uitslag protected function collectExportPages(Doku_Event $event) { 13036a7917dSGerrit Uitslag global $ID, $REV; 131d63e7fe7SGerrit Uitslag global $INPUT; 132d63e7fe7SGerrit Uitslag global $conf; 133d63e7fe7SGerrit Uitslag 134d63e7fe7SGerrit Uitslag // list of one or multiple pages 135d63e7fe7SGerrit Uitslag $list = array(); 13628e636eaSGerrit Uitslag 1374b4cebc2SLarsDW223 if($event->data == 'export_pdf') { 138d63e7fe7SGerrit Uitslag $list[0] = $ID; 13903352761SKirsten Roschanski $this->title = $INPUT->str('pdftitle'); //DEPRECATED 14003352761SKirsten Roschanski $this->title = $INPUT->str('book_title', $this->title, true); 14103352761SKirsten Roschanski if(empty($this->title)) { 14203352761SKirsten Roschanski $this->title = p_get_first_heading($ID); 14315923cb9SGerrit Uitslag } 144ad18f4e1SGerrit Uitslag 14536a7917dSGerrit Uitslag $filename = wikiFN($ID, $REV); 14636a7917dSGerrit Uitslag if(!file_exists($filename)) { 14736a7917dSGerrit Uitslag $this->showPageWithErrorMsg($event, 'notexist'); 14836a7917dSGerrit Uitslag return false; 14936a7917dSGerrit Uitslag } 15036a7917dSGerrit Uitslag 1514b4cebc2SLarsDW223 } elseif($event->data == 'export_pdfns') { 152ad18f4e1SGerrit Uitslag //check input for title and ns 15303352761SKirsten Roschanski if(!$this->title = $INPUT->str('book_title')) { 15426be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needtitle'); 155ad18f4e1SGerrit Uitslag return false; 156ad18f4e1SGerrit Uitslag } 157177a7d30SGerrit Uitslag $pdfnamespace = cleanID($INPUT->str('book_ns')); 158ad18f4e1SGerrit Uitslag if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) { 15926be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needns'); 160ad18f4e1SGerrit Uitslag return false; 161ad18f4e1SGerrit Uitslag } 162ad18f4e1SGerrit Uitslag 16326be4eceSGerrit Uitslag //sort order 164177a7d30SGerrit Uitslag $order = $INPUT->str('book_order', 'natural', true); 165ad18f4e1SGerrit Uitslag $sortoptions = array('pagename', 'date', 'natural'); 166ad18f4e1SGerrit Uitslag if(!in_array($order, $sortoptions)) { 167ad18f4e1SGerrit Uitslag $order = 'natural'; 168ad18f4e1SGerrit Uitslag } 169ad18f4e1SGerrit Uitslag 17026be4eceSGerrit Uitslag //search depth 171177a7d30SGerrit Uitslag $depth = $INPUT->int('book_nsdepth', 0); 172ad18f4e1SGerrit Uitslag if($depth < 0) { 173ad18f4e1SGerrit Uitslag $depth = 0; 174ad18f4e1SGerrit Uitslag } 17526be4eceSGerrit Uitslag 176ad18f4e1SGerrit Uitslag //page search 177ad18f4e1SGerrit Uitslag $result = array(); 178ad18f4e1SGerrit Uitslag $opts = array('depth' => $depth); //recursive all levels 179ad18f4e1SGerrit Uitslag $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace)); 180ad18f4e1SGerrit Uitslag search($result, $conf['datadir'], 'search_allpages', $opts, $dir); 181ad18f4e1SGerrit Uitslag 18264541781SAnna Dabrowska // exclude ids 18364541781SAnna Dabrowska $excludes = $INPUT->arr('excludes'); 18464541781SAnna Dabrowska if (!empty($excludes)) { 185*d31b75d5SAnna Dabrowska $result = array_filter($result, function ($item) use ($excludes) { 186*d31b75d5SAnna Dabrowska return array_search($item['id'], $excludes) === false; 187*d31b75d5SAnna Dabrowska }); 18864541781SAnna Dabrowska } 18964541781SAnna Dabrowska 19026be4eceSGerrit Uitslag //sorting 191ad18f4e1SGerrit Uitslag if(count($result) > 0) { 192ad18f4e1SGerrit Uitslag if($order == 'date') { 193ad18f4e1SGerrit Uitslag usort($result, array($this, '_datesort')); 194ad18f4e1SGerrit Uitslag } elseif($order == 'pagename') { 195ad18f4e1SGerrit Uitslag usort($result, array($this, '_pagenamesort')); 196ad18f4e1SGerrit Uitslag } 197ad18f4e1SGerrit Uitslag } 198ad18f4e1SGerrit Uitslag 199ad18f4e1SGerrit Uitslag foreach($result as $item) { 200d63e7fe7SGerrit Uitslag $list[] = $item['id']; 201ad18f4e1SGerrit Uitslag } 202ad18f4e1SGerrit Uitslag 203baa31dc5SGerrit Uitslag if ($pdfnamespace !== '') { 204baa31dc5SGerrit Uitslag if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { 205baa31dc5SGerrit Uitslag if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) { 206baa31dc5SGerrit Uitslag array_unshift($list,rtrim($pdfnamespace,':')); 207baa31dc5SGerrit Uitslag } 208baa31dc5SGerrit Uitslag } 209baa31dc5SGerrit Uitslag } 210baa31dc5SGerrit Uitslag 211737417c6SKlap-in } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 212b3eed6e3SGerrit Uitslag /** @deprecated April 2016 replaced by localStorage version of Bookcreator*/ 21326be4eceSGerrit Uitslag //is in Bookmanager of bookcreator plugin a title given? 21403352761SKirsten Roschanski $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED 21503352761SKirsten Roschanski $this->title = $INPUT->str('book_title', $this->title, true); 21603352761SKirsten Roschanski if(empty($this->title)) { 21726be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needtitle'); 218737417c6SKlap-in return false; 21926be4eceSGerrit Uitslag } else { 220d63e7fe7SGerrit Uitslag $list = explode("|", $_COOKIE['list-pagelist']); 22126be4eceSGerrit Uitslag } 222ad18f4e1SGerrit Uitslag 223b3eed6e3SGerrit Uitslag } elseif($INPUT->has('selection')) { 224b3eed6e3SGerrit Uitslag //handle Bookcreator requests based at localStorage 225b3eed6e3SGerrit Uitslag// if(!checkSecurityToken()) { 226b3eed6e3SGerrit Uitslag// http_status(403); 227b3eed6e3SGerrit Uitslag// print $this->getLang('empty'); 228b3eed6e3SGerrit Uitslag// exit(); 229b3eed6e3SGerrit Uitslag// } 230b3eed6e3SGerrit Uitslag 231b3eed6e3SGerrit Uitslag $json = new JSON(JSON_LOOSE_TYPE); 232b3eed6e3SGerrit Uitslag $list = $json->decode($INPUT->post->str('selection', '', true)); 233b3eed6e3SGerrit Uitslag if(!is_array($list) || empty($list)) { 234b3eed6e3SGerrit Uitslag http_status(400); 235b3eed6e3SGerrit Uitslag print $this->getLang('empty'); 236b3eed6e3SGerrit Uitslag exit(); 237b3eed6e3SGerrit Uitslag } 238b3eed6e3SGerrit Uitslag 23903352761SKirsten Roschanski $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED 24003352761SKirsten Roschanski $this->title = $INPUT->str('book_title', $this->title, true); 24103352761SKirsten Roschanski if(empty($this->title)) { 242b3eed6e3SGerrit Uitslag http_status(400); 243b3eed6e3SGerrit Uitslag print $this->getLang('needtitle'); 244b3eed6e3SGerrit Uitslag exit(); 245b3eed6e3SGerrit Uitslag } 246b3eed6e3SGerrit Uitslag 247737417c6SKlap-in } else { 24826be4eceSGerrit Uitslag //show empty bookcreator message 24926be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'empty'); 250737417c6SKlap-in return false; 251737417c6SKlap-in } 252737417c6SKlap-in 253719256adSGerrit Uitslag $list = array_map('cleanID', $list); 254c7138b3fSGerrit Uitslag 255c7138b3fSGerrit Uitslag $skippedpages = array(); 256c7138b3fSGerrit Uitslag foreach($list as $index => $pageid) { 257c7138b3fSGerrit Uitslag if(auth_quickaclcheck($pageid) < AUTH_READ) { 258c7138b3fSGerrit Uitslag $skippedpages[] = $pageid; 259c7138b3fSGerrit Uitslag unset($list[$index]); 260c7138b3fSGerrit Uitslag } 261c7138b3fSGerrit Uitslag } 262c7138b3fSGerrit Uitslag $list = array_filter($list); //removes also pages mentioned '0' 263c7138b3fSGerrit Uitslag 264c7138b3fSGerrit Uitslag //if selection contains forbidden pages throw (overridable) warning 265c7138b3fSGerrit Uitslag if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) { 266c7138b3fSGerrit Uitslag $msg = hsc(join(', ', $skippedpages)); 267c7138b3fSGerrit Uitslag if($INPUT->has('selection')) { 268c7138b3fSGerrit Uitslag http_status(400); 269c7138b3fSGerrit Uitslag print sprintf($this->getLang('forbidden'), $msg); 270c7138b3fSGerrit Uitslag exit(); 271c7138b3fSGerrit Uitslag } else { 272c7138b3fSGerrit Uitslag $this->showPageWithErrorMsg($event, 'forbidden', $msg); 273c7138b3fSGerrit Uitslag return false; 274c7138b3fSGerrit Uitslag } 275c7138b3fSGerrit Uitslag 276c7138b3fSGerrit Uitslag } 277c7138b3fSGerrit Uitslag 27803352761SKirsten Roschanski return array($this->title, $list); 279d63e7fe7SGerrit Uitslag } 280d63e7fe7SGerrit Uitslag 281a58f45f0SGerrit Uitslag /** 282a58f45f0SGerrit Uitslag * Prepare cache 283a58f45f0SGerrit Uitslag * 284a58f45f0SGerrit Uitslag * @param array $depends (reference) array with dependencies 285a58f45f0SGerrit Uitslag * @return cache 286a58f45f0SGerrit Uitslag */ 28703352761SKirsten Roschanski protected function prepareCache(&$depends) { 288a58f45f0SGerrit Uitslag global $REV; 289a58f45f0SGerrit Uitslag 290ee19bac3SLuigi Micco $cachekey = join(',', $this->list) 291ee19bac3SLuigi Micco . $REV 292ee19bac3SLuigi Micco . $this->getExportConfig('template') 293ee19bac3SLuigi Micco . $this->getExportConfig('pagesize') 294ee19bac3SLuigi Micco . $this->getExportConfig('orientation') 295d83760efSGerrit Uitslag . $this->getExportConfig('font-size') 296ee19bac3SLuigi Micco . $this->getExportConfig('doublesided') 297ee19bac3SLuigi Micco . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0') 29803352761SKirsten Roschanski . $this->title; 299ee19bac3SLuigi Micco $cache = new cache($cachekey, '.dw2.pdf'); 300ee19bac3SLuigi Micco 301ee19bac3SLuigi Micco $dependencies = array(); 302ee19bac3SLuigi Micco foreach($this->list as $pageid) { 303ee19bac3SLuigi Micco $relations = p_get_metadata($pageid, 'relation'); 304ee19bac3SLuigi Micco 305ee19bac3SLuigi Micco if(is_array($relations)) { 306ee19bac3SLuigi Micco if(array_key_exists('media', $relations) && is_array($relations['media'])) { 307ee19bac3SLuigi Micco foreach($relations['media'] as $mediaid => $exists) { 308ee19bac3SLuigi Micco if($exists) { 309ee19bac3SLuigi Micco $dependencies[] = mediaFN($mediaid); 310ee19bac3SLuigi Micco } 311ee19bac3SLuigi Micco } 312ee19bac3SLuigi Micco } 313ee19bac3SLuigi Micco 314ee19bac3SLuigi Micco if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) { 315ee19bac3SLuigi Micco foreach($relations['haspart'] as $part_pageid => $exists) { 316ee19bac3SLuigi Micco if($exists) { 317ee19bac3SLuigi Micco $dependencies[] = wikiFN($part_pageid); 318ee19bac3SLuigi Micco } 319ee19bac3SLuigi Micco } 320ee19bac3SLuigi Micco } 321ee19bac3SLuigi Micco } 322ee19bac3SLuigi Micco 323ee19bac3SLuigi Micco $dependencies[] = metaFN($pageid, '.meta'); 324ee19bac3SLuigi Micco } 325ee19bac3SLuigi Micco 326ee19bac3SLuigi Micco $depends['files'] = array_map('wikiFN', $this->list); 327ee19bac3SLuigi Micco $depends['files'][] = __FILE__; 328ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/renderer.php'; 329ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php'; 330ee19bac3SLuigi Micco $depends['files'] = array_merge( 331ee19bac3SLuigi Micco $depends['files'], 332ee19bac3SLuigi Micco $dependencies, 333ee19bac3SLuigi Micco getConfigFiles('main') 334ee19bac3SLuigi Micco ); 335a58f45f0SGerrit Uitslag return $cache; 336ee19bac3SLuigi Micco } 337ee19bac3SLuigi Micco 338d63e7fe7SGerrit Uitslag /** 339d63e7fe7SGerrit Uitslag * Set error notification and reload page again 340d63e7fe7SGerrit Uitslag * 341d63e7fe7SGerrit Uitslag * @param Doku_Event $event 342d63e7fe7SGerrit Uitslag * @param string $msglangkey key of translation key 343c7138b3fSGerrit Uitslag * @param string $replacement 344d63e7fe7SGerrit Uitslag */ 345c7138b3fSGerrit Uitslag private function showPageWithErrorMsg(Doku_Event $event, $msglangkey, $replacement=null) { 346c7138b3fSGerrit Uitslag if(empty($replacement)) { 347c7138b3fSGerrit Uitslag $msg = $this->getLang($msglangkey); 348c7138b3fSGerrit Uitslag } else { 349c7138b3fSGerrit Uitslag $msg = sprintf($this->getLang($msglangkey), $replacement); 350c7138b3fSGerrit Uitslag } 351c7138b3fSGerrit Uitslag msg($msg, -1); 352d63e7fe7SGerrit Uitslag 353d63e7fe7SGerrit Uitslag $event->data = 'show'; 354d63e7fe7SGerrit Uitslag $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url 355d63e7fe7SGerrit Uitslag } 356d63e7fe7SGerrit Uitslag 357d63e7fe7SGerrit Uitslag /** 358e53f1ec0SSzymon Olewniczak * Returns the parsed Wikitext in dw2pdf for the given id and revision 359e53f1ec0SSzymon Olewniczak * 360e53f1ec0SSzymon Olewniczak * @param string $id page id 361e53f1ec0SSzymon Olewniczak * @param string|int $rev revision timestamp or empty string 362e53f1ec0SSzymon Olewniczak * @param string $date_at 363e53f1ec0SSzymon Olewniczak * @return null|string 364e53f1ec0SSzymon Olewniczak */ 365e53f1ec0SSzymon Olewniczak protected function p_wiki_dw2pdf($id, $rev = '', $date_at = '') { 366e53f1ec0SSzymon Olewniczak $file = wikiFN($id, $rev); 367e53f1ec0SSzymon Olewniczak 368e53f1ec0SSzymon Olewniczak if(!file_exists($file)) return ''; 369e53f1ec0SSzymon Olewniczak 370e53f1ec0SSzymon Olewniczak //ensure $id is in global $ID (needed for parsing) 371e53f1ec0SSzymon Olewniczak global $ID; 372e53f1ec0SSzymon Olewniczak $keep = $ID; 373e53f1ec0SSzymon Olewniczak $ID = $id; 374e53f1ec0SSzymon Olewniczak 375e53f1ec0SSzymon Olewniczak if($rev || $date_at) { 376e53f1ec0SSzymon Olewniczak $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); //no caching on old revisions 377e53f1ec0SSzymon Olewniczak } else { 378e53f1ec0SSzymon Olewniczak $ret = p_cached_output($file, 'dw2pdf', $id); 379e53f1ec0SSzymon Olewniczak } 380e53f1ec0SSzymon Olewniczak 381e53f1ec0SSzymon Olewniczak //restore ID (just in case) 382e53f1ec0SSzymon Olewniczak $ID = $keep; 383e53f1ec0SSzymon Olewniczak 384e53f1ec0SSzymon Olewniczak return $ret; 385e53f1ec0SSzymon Olewniczak } 386e53f1ec0SSzymon Olewniczak 387e53f1ec0SSzymon Olewniczak /** 388d63e7fe7SGerrit Uitslag * Build a pdf from the html 389d63e7fe7SGerrit Uitslag * 390d63e7fe7SGerrit Uitslag * @param string $cachefile 391d9c13ec7SGerrit Uitslag * @param Doku_Event $event 392d63e7fe7SGerrit Uitslag */ 3932d9cd424SGerrit Uitslag protected function generatePDF($cachefile, $event) { 3942d9cd424SGerrit Uitslag global $REV, $INPUT, $DATE_AT; 395e53f1ec0SSzymon Olewniczak 3962d9cd424SGerrit Uitslag if ($event->data == 'export_pdf') { //only one page is exported 397e53f1ec0SSzymon Olewniczak $rev = $REV; 398e53f1ec0SSzymon Olewniczak $date_at = $DATE_AT; 399e53f1ec0SSzymon Olewniczak } else { //we are exporting entre namespace, ommit revisions 400e53f1ec0SSzymon Olewniczak $rev = $date_at = ''; 401e53f1ec0SSzymon Olewniczak } 40287c86ddaSAndreas Gohr 40302f9a447SGerrit Uitslag //some shortcuts to export settings 40402f9a447SGerrit Uitslag $hasToC = $this->getExportConfig('hasToC'); 40502f9a447SGerrit Uitslag $levels = $this->getExportConfig('levels'); 40602f9a447SGerrit Uitslag $isDebug = $this->getExportConfig('isDebug'); 4076ea88a05SAndreas Gohr 4081ef68647SAndreas Gohr // initialize PDF library 409cde5a1b3SAndreas Gohr require_once(dirname(__FILE__) . "/DokuPDF.class.php"); 4106ea88a05SAndreas Gohr 4114870b378SLarsDW223 $mpdf = new DokuPDF($this->getExportConfig('pagesize'), 4124870b378SLarsDW223 $this->getExportConfig('orientation'), 4134870b378SLarsDW223 $this->getExportConfig('font-size')); 414ee19bac3SLuigi Micco 415d62df65bSAndreas Gohr // let mpdf fix local links 416d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 417d62df65bSAndreas Gohr $url = $self['scheme'] . '://' . $self['host']; 41802f9a447SGerrit Uitslag if($self['port']) { 41902f9a447SGerrit Uitslag $url .= ':' . $self['port']; 42002f9a447SGerrit Uitslag } 421d9c13ec7SGerrit Uitslag $mpdf->SetBasePath($url); 422d62df65bSAndreas Gohr 42356d13144SAndreas Gohr // Set the title 42403352761SKirsten Roschanski $mpdf->SetTitle($this->title); 42556d13144SAndreas Gohr 426d63e7fe7SGerrit Uitslag // some default document settings 427d63e7fe7SGerrit Uitslag //note: double-sided document, starts at an odd page (first page is a right-hand side page) 428213fdb75SGerrit Uitslag // single-side document has only odd pages 429213fdb75SGerrit Uitslag $mpdf->mirrorMargins = $this->getExportConfig('doublesided'); 430daa70883SAndreas Gohr $mpdf->setAutoTopMargin = 'stretch'; 431daa70883SAndreas Gohr $mpdf->setAutoBottomMargin = 'stretch'; 43202f9a447SGerrit Uitslag// $mpdf->pagenumSuffix = '/'; //prefix for {nbpg} 43302f9a447SGerrit Uitslag if($hasToC) { 43402f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC 43502f9a447SGerrit Uitslag $mpdf->h2toc = $levels; 43602f9a447SGerrit Uitslag } else { 43702f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'); 43802f9a447SGerrit Uitslag } 4392eedf77dSAndreas Gohr 44056d13144SAndreas Gohr // load the template 44103352761SKirsten Roschanski $template = $this->load_template(); 442ee19bac3SLuigi Micco 4431ef68647SAndreas Gohr // prepare HTML header styles 444a2c33768SGerrit Uitslag $html = ''; 44502f9a447SGerrit Uitslag if($isDebug) { 446a2c33768SGerrit Uitslag $html .= '<html><head>'; 447737417c6SKlap-in $html .= '<style type="text/css">'; 448a2c33768SGerrit Uitslag } 449db1aa1bfSKirsten Roschanski 450db1aa1bfSKirsten Roschanski $styles = '@page { size:auto; ' . $template['page'] . '}'; 451a2c33768SGerrit Uitslag $styles .= '@page :first {' . $template['first'] . '}'; 452254467c4SGerrit Uitslag 453254467c4SGerrit Uitslag $styles .= '@page landscape-page { size:landscape }'; 454254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-landscape { page:landscape-page }'; 455254467c4SGerrit Uitslag $styles .= '@page portrait-page { size:portrait }'; 456254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-portrait { page:portrait-page }'; 457db1aa1bfSKirsten Roschanski $styles .= $this->load_css(); 458254467c4SGerrit Uitslag 459a2c33768SGerrit Uitslag $mpdf->WriteHTML($styles, 1); 460a2c33768SGerrit Uitslag 46102f9a447SGerrit Uitslag if($isDebug) { 462a2c33768SGerrit Uitslag $html .= $styles; 4631ef68647SAndreas Gohr $html .= '</style>'; 4641ef68647SAndreas Gohr $html .= '</head><body>'; 465a2c33768SGerrit Uitslag } 466a2c33768SGerrit Uitslag 467a2c33768SGerrit Uitslag $body_start = $template['html']; 468a2c33768SGerrit Uitslag $body_start .= '<div class="dokuwiki">'; 4692eedf77dSAndreas Gohr 4701e45476bSmnapp // insert the cover page 471a2c33768SGerrit Uitslag $body_start .= $template['cover']; 472a2c33768SGerrit Uitslag 473a2c33768SGerrit Uitslag $mpdf->WriteHTML($body_start, 2, true, false); //start body html 47402f9a447SGerrit Uitslag if($isDebug) { 475a2c33768SGerrit Uitslag $html .= $body_start; 476a2c33768SGerrit Uitslag } 47702f9a447SGerrit Uitslag if($hasToC) { 47802f9a447SGerrit 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 47902f9a447SGerrit Uitslag // - first page of ToC starts always at odd page (so eventually an additional blank page is included before) 48002f9a447SGerrit Uitslag // - there is no page numbering at the pages of the ToC 48102f9a447SGerrit Uitslag $mpdf->TOCpagebreakByArray( 48202f9a447SGerrit Uitslag array( 483230b098dSGerrit Uitslag 'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>', 484230b098dSGerrit Uitslag 'toc-bookmarkText' => $this->getLang('tocheader'), 48502f9a447SGerrit Uitslag 'links' => true, 48602f9a447SGerrit Uitslag 'outdent' => '1em', 48702f9a447SGerrit Uitslag 'resetpagenum' => true, //start pagenumbering after ToC 48802f9a447SGerrit Uitslag 'pagenumstyle' => '1' 48902f9a447SGerrit Uitslag ) 49002f9a447SGerrit Uitslag ); 49102f9a447SGerrit Uitslag $html .= '<tocpagebreak>'; 49202f9a447SGerrit Uitslag } 49302f9a447SGerrit Uitslag 4941ef68647SAndreas Gohr // loop over all pages 495c7138b3fSGerrit Uitslag $counter = 0; 496c7138b3fSGerrit Uitslag $no_pages = count($this->list); 497c7138b3fSGerrit Uitslag foreach($this->list as $page) { 498c7138b3fSGerrit Uitslag $counter++; 499b3eed6e3SGerrit Uitslag 50079be256fSMichael Große $pagehtml = $this->p_wiki_dw2pdf($page, $rev, $date_at); 501e53f1ec0SSzymon Olewniczak //file doesn't exists 502e53f1ec0SSzymon Olewniczak if($pagehtml == '') { 503b3eed6e3SGerrit Uitslag continue; 504b3eed6e3SGerrit Uitslag } 505719256adSGerrit Uitslag $pagehtml .= $this->page_depend_replacements($template['cite'], $page); 506c7138b3fSGerrit Uitslag if($counter < $no_pages) { 507a2c33768SGerrit Uitslag $pagehtml .= '<pagebreak />'; 508a2c33768SGerrit Uitslag } 509a2c33768SGerrit Uitslag 510a2c33768SGerrit Uitslag $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html 51102f9a447SGerrit Uitslag if($isDebug) { 512a2c33768SGerrit Uitslag $html .= $pagehtml; 5131ef68647SAndreas Gohr } 514ee19bac3SLuigi Micco } 515ee19bac3SLuigi Micco 51633c15297SGerrit Uitslag // insert the back page 517a2c33768SGerrit Uitslag $body_end = $template['back']; 51833c15297SGerrit Uitslag 519a2c33768SGerrit Uitslag $body_end .= '</div>'; 520a2c33768SGerrit Uitslag 521d63e7fe7SGerrit Uitslag $mpdf->WriteHTML($body_end, 2, false, true); // finish body html 52202f9a447SGerrit Uitslag if($isDebug) { 523a2c33768SGerrit Uitslag $html .= $body_end; 524eeb17e15SAndreas Gohr $html .= '</body>'; 525eeb17e15SAndreas Gohr $html .= '</html>'; 526a2c33768SGerrit Uitslag } 527f765508eSGerrit Uitslag 528f765508eSGerrit Uitslag //Return html for debugging 52902f9a447SGerrit Uitslag if($isDebug) { 53002f9a447SGerrit Uitslag if($INPUT->str('debughtml', 'text', true) == 'html') { 53126be4eceSGerrit Uitslag echo $html; 532a2c33768SGerrit Uitslag } else { 533a2c33768SGerrit Uitslag header('Content-Type: text/plain; charset=utf-8'); 534a2c33768SGerrit Uitslag echo $html; 535a2c33768SGerrit Uitslag } 53626be4eceSGerrit Uitslag exit(); 53726be4eceSGerrit Uitslag }; 538f765508eSGerrit Uitslag 53987c86ddaSAndreas Gohr // write to cache file 540d63e7fe7SGerrit Uitslag $mpdf->Output($cachefile, 'F'); 54187c86ddaSAndreas Gohr } 54287c86ddaSAndreas Gohr 543d63e7fe7SGerrit Uitslag /** 544d63e7fe7SGerrit Uitslag * @param string $cachefile 545d63e7fe7SGerrit Uitslag */ 54603352761SKirsten Roschanski protected function sendPDFFile($cachefile) { 54787c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 548b853b723SAndreas Gohr header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 54987c86ddaSAndreas Gohr header('Pragma: public'); 550d63e7fe7SGerrit Uitslag http_conditionalRequest(filemtime($cachefile)); 551c0956f69SMichael Große global $INPUT; 552f7b24f48SMichael Große $outputTarget = $INPUT->str('outputTarget', $this->getConf('output')); 55387c86ddaSAndreas Gohr 55403352761SKirsten Roschanski $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', ' '))); 555c0956f69SMichael Große if($outputTarget === 'file') { 5569a3c8d9fSAndreas Gohr header('Content-Disposition: attachment; filename="' . $filename . '.pdf";'); 55787c86ddaSAndreas Gohr } else { 5589a3c8d9fSAndreas Gohr header('Content-Disposition: inline; filename="' . $filename . '.pdf";'); 55987c86ddaSAndreas Gohr } 560ee19bac3SLuigi Micco 561b3eed6e3SGerrit Uitslag //Bookcreator uses jQuery.fileDownload.js, which requires a cookie. 562b3eed6e3SGerrit Uitslag header('Set-Cookie: fileDownload=true; path=/'); 563b3eed6e3SGerrit Uitslag 564e993da11SGerrit Uitslag //try to send file, and exit if done 565d63e7fe7SGerrit Uitslag http_sendfile($cachefile); 56687c86ddaSAndreas Gohr 567d63e7fe7SGerrit Uitslag $fp = @fopen($cachefile, "rb"); 56887c86ddaSAndreas Gohr if($fp) { 569d63e7fe7SGerrit Uitslag http_rangeRequest($fp, filesize($cachefile), 'application/pdf'); 57087c86ddaSAndreas Gohr } else { 57187c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 57287c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 57387c86ddaSAndreas Gohr } 5741ef68647SAndreas Gohr exit(); 5751ef68647SAndreas Gohr } 5761ef68647SAndreas Gohr 5776be736bfSGerrit Uitslag /** 5782eedf77dSAndreas Gohr * Load the various template files and prepare the HTML/CSS for insertion 57944e8e8fbSGerrit Uitslag * 58044e8e8fbSGerrit Uitslag * @return array 5811ef68647SAndreas Gohr */ 58203352761SKirsten Roschanski protected function load_template() { 5831ef68647SAndreas Gohr global $ID; 5841ef68647SAndreas Gohr global $conf; 5851ef68647SAndreas Gohr 5862eedf77dSAndreas Gohr // this is what we'll return 5872eedf77dSAndreas Gohr $output = array( 5881e45476bSmnapp 'cover' => '', 5892eedf77dSAndreas Gohr 'html' => '', 5902eedf77dSAndreas Gohr 'page' => '', 5912eedf77dSAndreas Gohr 'first' => '', 5922eedf77dSAndreas Gohr 'cite' => '', 5932eedf77dSAndreas Gohr ); 5942eedf77dSAndreas Gohr 5952eedf77dSAndreas Gohr // prepare header/footer elements 5962eedf77dSAndreas Gohr $html = ''; 59733c15297SGerrit Uitslag foreach(array('header', 'footer') as $section) { 59833c15297SGerrit Uitslag foreach(array('', '_odd', '_even', '_first') as $order) { 59902f9a447SGerrit Uitslag $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html'; 60033c15297SGerrit Uitslag if(file_exists($file)) { 60133c15297SGerrit Uitslag $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF; 60233c15297SGerrit Uitslag $html .= file_get_contents($file) . DOKU_LF; 60333c15297SGerrit Uitslag $html .= '</htmlpage' . $section . '>' . DOKU_LF; 6042eedf77dSAndreas Gohr 6052eedf77dSAndreas Gohr // register the needed pseudo CSS 60633c15297SGerrit Uitslag if($order == '_first') { 60733c15297SGerrit Uitslag $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 60833c15297SGerrit Uitslag } elseif($order == '_even') { 60933c15297SGerrit Uitslag $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 61033c15297SGerrit Uitslag } elseif($order == '_odd') { 61133c15297SGerrit Uitslag $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 612daa70883SAndreas Gohr } else { 61333c15297SGerrit Uitslag $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 6142eedf77dSAndreas Gohr } 6152eedf77dSAndreas Gohr } 6162eedf77dSAndreas Gohr } 6172eedf77dSAndreas Gohr } 6182eedf77dSAndreas Gohr 6191ef68647SAndreas Gohr // prepare replacements 6201ef68647SAndreas Gohr $replace = array( 6211ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 62202f9a447SGerrit Uitslag '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / ' 62303352761SKirsten Roschanski '@TITLE@' => hsc($this->title), 6241ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 6251ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 6261ef68647SAndreas Gohr '@DATE@' => dformat(time()), 6275d6fbaeaSAndreas Gohr '@BASE@' => DOKU_BASE, 628893987a2SAndreas Gohr '@INC@' => DOKU_INC, 629893987a2SAndreas Gohr '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 630893987a2SAndreas Gohr '@TPLINC@' => DOKU_INC . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/' 6311ef68647SAndreas Gohr ); 6321ef68647SAndreas Gohr 6332eedf77dSAndreas Gohr // set HTML element 634a180c973SKlap-in $html = str_replace(array_keys($replace), array_values($replace), $html); 635a180c973SKlap-in //TODO For bookcreator $ID (= bookmanager page) makes no sense 636a180c973SKlap-in $output['html'] = $this->page_depend_replacements($html, $ID); 6371ef68647SAndreas Gohr 6381e45476bSmnapp // cover page 63902f9a447SGerrit Uitslag $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html'; 64033c15297SGerrit Uitslag if(file_exists($coverfile)) { 64133c15297SGerrit Uitslag $output['cover'] = file_get_contents($coverfile); 6421e45476bSmnapp $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']); 6439b071da5SMichael $output['cover'] = $this->page_depend_replacements($output['cover'], $ID); 6446e2ec302SGerrit Uitslag $output['cover'] .= '<pagebreak />'; 6451e45476bSmnapp } 6461e45476bSmnapp 64733c15297SGerrit Uitslag // cover page 64802f9a447SGerrit Uitslag $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html'; 64933c15297SGerrit Uitslag if(file_exists($backfile)) { 65033c15297SGerrit Uitslag $output['back'] = '<pagebreak />'; 65133c15297SGerrit Uitslag $output['back'] .= file_get_contents($backfile); 65233c15297SGerrit Uitslag $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']); 6539b071da5SMichael $output['back'] = $this->page_depend_replacements($output['back'], $ID); 65433c15297SGerrit Uitslag } 65533c15297SGerrit Uitslag 6562eedf77dSAndreas Gohr // citation box 65702f9a447SGerrit Uitslag $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html'; 65833c15297SGerrit Uitslag if(file_exists($citationfile)) { 65933c15297SGerrit Uitslag $output['cite'] = file_get_contents($citationfile); 6602eedf77dSAndreas Gohr $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 6612eedf77dSAndreas Gohr } 6621ef68647SAndreas Gohr 6632eedf77dSAndreas Gohr return $output; 6641ef68647SAndreas Gohr } 6651ef68647SAndreas Gohr 6661ef68647SAndreas Gohr /** 667a180c973SKlap-in * @param string $raw code with placeholders 668a180c973SKlap-in * @param string $id pageid 669a180c973SKlap-in * @return string 670a180c973SKlap-in */ 671a180c973SKlap-in protected function page_depend_replacements($raw, $id) { 672e53f1ec0SSzymon Olewniczak global $REV, $DATE_AT; 673a180c973SKlap-in 6749b2636c8SDiego Belmar // generate qr code for this page using quickchart.io (Google infographics api was deprecated in March 14, 2019) 675a180c973SKlap-in $qr_code = ''; 676a180c973SKlap-in if($this->getConf('qrcodesize')) { 677a180c973SKlap-in $url = urlencode(wl($id, '', '&', true)); 6789b2636c8SDiego Belmar $qr_code = '<img src="https://quickchart.io/qr?size=' . 6799b2636c8SDiego Belmar $this->getConf('qrcodesize') . '&text=' . $url . '&margin=1&ecLevel=Q" />'; 680a180c973SKlap-in } 681a180c973SKlap-in // prepare replacements 682a180c973SKlap-in $replace['@ID@'] = $id; 683a180c973SKlap-in $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV))); 684e53f1ec0SSzymon Olewniczak 685e53f1ec0SSzymon Olewniczak $params = array(); 686e53f1ec0SSzymon Olewniczak if($DATE_AT) { 687e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 688e53f1ec0SSzymon Olewniczak } elseif($REV) { 689e53f1ec0SSzymon Olewniczak $params['rev'] = $REV; 690e53f1ec0SSzymon Olewniczak } 691e53f1ec0SSzymon Olewniczak $replace['@PAGEURL@'] = wl($id, $params, true, "&"); 692a180c973SKlap-in $replace['@QRCODE@'] = $qr_code; 693a180c973SKlap-in 694e3d68265SGerrit Uitslag $content = str_replace(array_keys($replace), array_values($replace), $raw); 695e3d68265SGerrit Uitslag 696e3d68265SGerrit Uitslag // @DATE(<date>[, <format>])@ 697e3d68265SGerrit Uitslag $content = preg_replace_callback( 698e3d68265SGerrit Uitslag '/@DATE\((.*?)(?:,\s*(.*?))?\)@/', 699e3d68265SGerrit Uitslag array($this, 'replacedate'), 700e3d68265SGerrit Uitslag $content 701e3d68265SGerrit Uitslag ); 702e3d68265SGerrit Uitslag 703e3d68265SGerrit Uitslag return $content; 704a180c973SKlap-in } 705a180c973SKlap-in 706e3d68265SGerrit Uitslag 707e3d68265SGerrit Uitslag /** 708e3d68265SGerrit Uitslag * (callback) Replace date by request datestring 709e3d68265SGerrit Uitslag * e.g. '%m(30-11-1975)' is replaced by '11' 710e3d68265SGerrit Uitslag * 711e3d68265SGerrit Uitslag * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern 712e3d68265SGerrit Uitslag * @return string 713e3d68265SGerrit Uitslag */ 714e3d68265SGerrit Uitslag function replacedate($match) { 715e3d68265SGerrit Uitslag global $conf; 716e3d68265SGerrit Uitslag //no 2nd argument for default date format 717e3d68265SGerrit Uitslag if($match[2] == null) { 718e3d68265SGerrit Uitslag $match[2] = $conf['dformat']; 719e3d68265SGerrit Uitslag } 720e3d68265SGerrit Uitslag return strftime($match[2], strtotime($match[1])); 721e3d68265SGerrit Uitslag } 722e3d68265SGerrit Uitslag 723a180c973SKlap-in /** 7241c14c879SAndreas Gohr * Load all the style sheets and apply the needed replacements 7251ef68647SAndreas Gohr */ 7261c14c879SAndreas Gohr protected function load_css() { 727737417c6SKlap-in global $conf; 7281c14c879SAndreas Gohr //reusue the CSS dispatcher functions without triggering the main function 7291c14c879SAndreas Gohr define('SIMPLE_TEST', 1); 7301c14c879SAndreas Gohr require_once(DOKU_INC . 'lib/exe/css.php'); 731ee19bac3SLuigi Micco 7321c14c879SAndreas Gohr // prepare CSS files 7331c14c879SAndreas Gohr $files = array_merge( 7341c14c879SAndreas Gohr array( 7351c14c879SAndreas Gohr DOKU_INC . 'lib/styles/screen.css' 7361c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7371c14c879SAndreas Gohr DOKU_INC . 'lib/styles/print.css' 7381c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7391c14c879SAndreas Gohr ), 74058e6409eSAndreas Gohr $this->css_pluginPDFstyles(), 7411c14c879SAndreas Gohr array( 7421c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.css' 7431c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7441c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' 7451c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 7461c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' 7471c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7481c14c879SAndreas Gohr ) 7491c14c879SAndreas Gohr ); 7501c14c879SAndreas Gohr $css = ''; 7511c14c879SAndreas Gohr foreach($files as $file => $location) { 75228e636eaSGerrit Uitslag $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 75328e636eaSGerrit Uitslag $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 7541c14c879SAndreas Gohr $css .= css_loadfile($file, $location); 7551ef68647SAndreas Gohr } 7561ef68647SAndreas Gohr 75728e636eaSGerrit Uitslag if(function_exists('css_parseless')) { 7581c14c879SAndreas Gohr // apply pattern replacements 7597c6ca3bcSMichael Große if (function_exists('css_styleini')) { 7607c6ca3bcSMichael Große // compatiblity layer for pre-Greebo releases of DokuWiki 76128e636eaSGerrit Uitslag $styleini = css_styleini($conf['template']); 7627c6ca3bcSMichael Große } else { 7637c6ca3bcSMichael Große // Greebo functionality 7647c6ca3bcSMichael Große $styleUtils = new \dokuwiki\StyleUtils(); 7657c6ca3bcSMichael Große $styleini = $styleUtils->cssStyleini($conf['template']); 7667c6ca3bcSMichael Große } 76728e636eaSGerrit Uitslag $css = css_applystyle($css, $styleini['replacements']); 76828e636eaSGerrit Uitslag 76928e636eaSGerrit Uitslag // parse less 77028e636eaSGerrit Uitslag $css = css_parseless($css); 77128e636eaSGerrit Uitslag } else { 77228e636eaSGerrit Uitslag // @deprecated 2013-12-19: fix backward compatibility 7731c14c879SAndreas Gohr $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 77428e636eaSGerrit Uitslag } 7751ef68647SAndreas Gohr 7761c14c879SAndreas Gohr return $css; 777ee19bac3SLuigi Micco } 7781c14c879SAndreas Gohr 77958e6409eSAndreas Gohr /** 78058e6409eSAndreas Gohr * Returns a list of possible Plugin PDF Styles 78158e6409eSAndreas Gohr * 78258e6409eSAndreas Gohr * Checks for a pdf.css, falls back to print.css 78358e6409eSAndreas Gohr * 78458e6409eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 78558e6409eSAndreas Gohr */ 7866be736bfSGerrit Uitslag protected function css_pluginPDFstyles() { 78758e6409eSAndreas Gohr $list = array(); 78858e6409eSAndreas Gohr $plugins = plugin_list(); 789f54b51f7SAndreas Gohr 790f54b51f7SAndreas Gohr $usestyle = explode(',', $this->getConf('usestyles')); 79158e6409eSAndreas Gohr foreach($plugins as $p) { 792f54b51f7SAndreas Gohr if(in_array($p, $usestyle)) { 793f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/"; 7948003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/"; 7958003b493SGerrit Uitslag 796f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/"; 7978003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/"; 798f54b51f7SAndreas Gohr } 799f54b51f7SAndreas Gohr 8008003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/"; 8018003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/"; 8028003b493SGerrit Uitslag 803ab96d816SMichael Große if(file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) { 80458e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/"; 8058003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/"; 80658e6409eSAndreas Gohr } else { 80758e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/"; 8088003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/"; 80958e6409eSAndreas Gohr } 81058e6409eSAndreas Gohr } 81158e6409eSAndreas Gohr return $list; 81258e6409eSAndreas Gohr } 813ad18f4e1SGerrit Uitslag 814ad18f4e1SGerrit Uitslag /** 81560e59de7SGerrit Uitslag * Returns array of pages which will be included in the exported pdf 81660e59de7SGerrit Uitslag * 81760e59de7SGerrit Uitslag * @return array 81860e59de7SGerrit Uitslag */ 81960e59de7SGerrit Uitslag public function getExportedPages() { 82060e59de7SGerrit Uitslag return $this->list; 82160e59de7SGerrit Uitslag } 82260e59de7SGerrit Uitslag 82360e59de7SGerrit Uitslag /** 824ad18f4e1SGerrit Uitslag * usort callback to sort by file lastmodified time 82544e8e8fbSGerrit Uitslag * 82644e8e8fbSGerrit Uitslag * @param array $a 82744e8e8fbSGerrit Uitslag * @param array $b 82844e8e8fbSGerrit Uitslag * @return int 829ad18f4e1SGerrit Uitslag */ 830ad18f4e1SGerrit Uitslag public function _datesort($a, $b) { 831ad18f4e1SGerrit Uitslag if($b['rev'] < $a['rev']) return -1; 832ad18f4e1SGerrit Uitslag if($b['rev'] > $a['rev']) return 1; 833ad18f4e1SGerrit Uitslag return strcmp($b['id'], $a['id']); 834ad18f4e1SGerrit Uitslag } 835ad18f4e1SGerrit Uitslag 836ad18f4e1SGerrit Uitslag /** 837ad18f4e1SGerrit Uitslag * usort callback to sort by page id 83844e8e8fbSGerrit Uitslag * @param array $a 83944e8e8fbSGerrit Uitslag * @param array $b 84044e8e8fbSGerrit Uitslag * @return int 841ad18f4e1SGerrit Uitslag */ 842ad18f4e1SGerrit Uitslag public function _pagenamesort($a, $b) { 843eea6fd2cSMichael Große // do not sort numbers before namespace separators 844eea6fd2cSMichael Große $aID = str_replace(':', '/', $a['id']); 845eea6fd2cSMichael Große $bID = str_replace(':', '/', $b['id']); 846eea6fd2cSMichael Große if($aID <= $bID) return -1; 847eea6fd2cSMichael Große if($aID > $bID) return 1; 848ad18f4e1SGerrit Uitslag return 0; 849ad18f4e1SGerrit Uitslag } 85026be4eceSGerrit Uitslag 85126be4eceSGerrit Uitslag /** 85202f9a447SGerrit Uitslag * Return settings read from: 85302f9a447SGerrit Uitslag * 1. url parameters 85402f9a447SGerrit Uitslag * 2. plugin config 85502f9a447SGerrit Uitslag * 3. global config 85602f9a447SGerrit Uitslag * 85702f9a447SGerrit Uitslag * @return array 85802f9a447SGerrit Uitslag */ 85902f9a447SGerrit Uitslag protected function loadExportConfig() { 86002f9a447SGerrit Uitslag global $INPUT; 86102f9a447SGerrit Uitslag global $conf; 86202f9a447SGerrit Uitslag 86302f9a447SGerrit Uitslag $this->exportConfig = array(); 86402f9a447SGerrit Uitslag 86502f9a447SGerrit Uitslag // decide on the paper setup from param or config 86602f9a447SGerrit Uitslag $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true); 86702f9a447SGerrit Uitslag $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true); 86802f9a447SGerrit Uitslag 8694870b378SLarsDW223 // decide on the font-size from param or config 8704870b378SLarsDW223 $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true); 8714870b378SLarsDW223 872213fdb75SGerrit Uitslag $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided')); 873213fdb75SGerrit Uitslag $this->exportConfig['doublesided'] = $doublesided ? '1' : '0'; 874213fdb75SGerrit Uitslag 875213fdb75SGerrit Uitslag $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc')); 87602f9a447SGerrit Uitslag $levels = array(); 87702f9a447SGerrit Uitslag if($hasToC) { 87802f9a447SGerrit Uitslag $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true); 87902f9a447SGerrit Uitslag list($top_input, $max_input) = explode('-', $toclevels, 2); 88002f9a447SGerrit Uitslag list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2); 88102f9a447SGerrit Uitslag $bounds_input = array( 88202f9a447SGerrit Uitslag 'top' => array( 88302f9a447SGerrit Uitslag (int) $top_input, 88402f9a447SGerrit Uitslag (int) $top_conf 88502f9a447SGerrit Uitslag ), 88602f9a447SGerrit Uitslag 'max' => array( 88702f9a447SGerrit Uitslag (int) $max_input, 88802f9a447SGerrit Uitslag (int) $max_conf 88902f9a447SGerrit Uitslag ) 89002f9a447SGerrit Uitslag ); 89102f9a447SGerrit Uitslag $bounds = array( 89202f9a447SGerrit Uitslag 'top' => $conf['toptoclevel'], 89302f9a447SGerrit Uitslag 'max' => $conf['maxtoclevel'] 89402f9a447SGerrit Uitslag 89502f9a447SGerrit Uitslag ); 89602f9a447SGerrit Uitslag foreach($bounds_input as $bound => $values) { 89702f9a447SGerrit Uitslag foreach($values as $value) { 89802f9a447SGerrit Uitslag if($value > 0 && $value <= 5) { 89902f9a447SGerrit Uitslag //stop at valid value and store 90002f9a447SGerrit Uitslag $bounds[$bound] = $value; 90102f9a447SGerrit Uitslag break; 90202f9a447SGerrit Uitslag } 90302f9a447SGerrit Uitslag } 90402f9a447SGerrit Uitslag } 90502f9a447SGerrit Uitslag 90602f9a447SGerrit Uitslag if($bounds['max'] < $bounds['top']) { 90702f9a447SGerrit Uitslag $bounds['max'] = $bounds['top']; 90802f9a447SGerrit Uitslag } 90902f9a447SGerrit Uitslag 91002f9a447SGerrit Uitslag for($level = $bounds['top']; $level <= $bounds['max']; $level++) { 91102f9a447SGerrit Uitslag $levels["H$level"] = $level - 1; 91202f9a447SGerrit Uitslag } 91302f9a447SGerrit Uitslag } 91402f9a447SGerrit Uitslag $this->exportConfig['hasToC'] = $hasToC; 91502f9a447SGerrit Uitslag $this->exportConfig['levels'] = $levels; 91602f9a447SGerrit Uitslag 91702f9a447SGerrit Uitslag $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true); 91802f9a447SGerrit Uitslag 91902f9a447SGerrit Uitslag $tplconf = $this->getConf('template'); 92005d2b507SGerrit Uitslag $tpl = $INPUT->str('tpl', $tplconf, true); 92102f9a447SGerrit Uitslag if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) { 92202f9a447SGerrit Uitslag $tpl = $tplconf; 92302f9a447SGerrit Uitslag } 92402f9a447SGerrit Uitslag if(!$tpl){ 92502f9a447SGerrit Uitslag $tpl = 'default'; 92602f9a447SGerrit Uitslag } 92702f9a447SGerrit Uitslag $this->exportConfig['template'] = $tpl; 92802f9a447SGerrit Uitslag 92902f9a447SGerrit Uitslag $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml'); 93002f9a447SGerrit Uitslag } 93102f9a447SGerrit Uitslag 93202f9a447SGerrit Uitslag /** 93302f9a447SGerrit Uitslag * Returns requested config 93402f9a447SGerrit Uitslag * 93502f9a447SGerrit Uitslag * @param string $name 93602f9a447SGerrit Uitslag * @param mixed $notset 93702f9a447SGerrit Uitslag * @return mixed|bool 93802f9a447SGerrit Uitslag */ 93902f9a447SGerrit Uitslag public function getExportConfig($name, $notset = false) { 94002f9a447SGerrit Uitslag if ($this->exportConfig === null){ 94102f9a447SGerrit Uitslag $this->loadExportConfig(); 94202f9a447SGerrit Uitslag } 94302f9a447SGerrit Uitslag 94402f9a447SGerrit Uitslag if(isset($this->exportConfig[$name])){ 94502f9a447SGerrit Uitslag return $this->exportConfig[$name]; 94602f9a447SGerrit Uitslag }else{ 94702f9a447SGerrit Uitslag return $notset; 94802f9a447SGerrit Uitslag } 94902f9a447SGerrit Uitslag } 950d63e7fe7SGerrit Uitslag 951d63e7fe7SGerrit Uitslag /** 952d63e7fe7SGerrit Uitslag * Add 'export pdf'-button to pagetools 953d63e7fe7SGerrit Uitslag * 954d63e7fe7SGerrit Uitslag * @param Doku_Event $event 955d63e7fe7SGerrit Uitslag */ 95644e8e8fbSGerrit Uitslag public function addbutton(Doku_Event $event) { 957e53f1ec0SSzymon Olewniczak global $ID, $REV, $DATE_AT; 958d63e7fe7SGerrit Uitslag 959d63e7fe7SGerrit Uitslag if($this->getConf('showexportbutton') && $event->data['view'] == 'main') { 960d63e7fe7SGerrit Uitslag $params = array('do' => 'export_pdf'); 961e53f1ec0SSzymon Olewniczak if($DATE_AT) { 962e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 963e53f1ec0SSzymon Olewniczak } elseif($REV) { 964d63e7fe7SGerrit Uitslag $params['rev'] = $REV; 965d63e7fe7SGerrit Uitslag } 966d63e7fe7SGerrit Uitslag 967d63e7fe7SGerrit Uitslag // insert button at position before last (up to top) 968d63e7fe7SGerrit Uitslag $event->data['items'] = array_slice($event->data['items'], 0, -1, true) + 969d63e7fe7SGerrit Uitslag array('export_pdf' => 970d63e7fe7SGerrit Uitslag '<li>' 97172cadc31SChristian Paul . '<a href="' . wl($ID, $params) . '" class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">' 972d63e7fe7SGerrit Uitslag . '<span>' . $this->getLang('export_pdf_button') . '</span>' 973d63e7fe7SGerrit Uitslag . '</a>' 974d63e7fe7SGerrit Uitslag . '</li>' 975d63e7fe7SGerrit Uitslag ) + 976d63e7fe7SGerrit Uitslag array_slice($event->data['items'], -1, 1, true); 977d63e7fe7SGerrit Uitslag } 978d63e7fe7SGerrit Uitslag } 979026b594bSAndreas Gohr 980026b594bSAndreas Gohr /** 981026b594bSAndreas Gohr * Add 'export pdf' button to page tools, new SVG based mechanism 982026b594bSAndreas Gohr * 983026b594bSAndreas Gohr * @param Doku_Event $event 984026b594bSAndreas Gohr */ 985026b594bSAndreas Gohr public function addsvgbutton(Doku_Event $event) { 9864c493e44SGerrit Uitslag global $INFO; 9874c493e44SGerrit Uitslag if($event->data['view'] != 'page' || !$this->getConf('showexportbutton')) { 9884c493e44SGerrit Uitslag return; 9894c493e44SGerrit Uitslag } 9904c493e44SGerrit Uitslag 9914c493e44SGerrit Uitslag if(!$INFO['exists']) { 9924c493e44SGerrit Uitslag return; 9934c493e44SGerrit Uitslag } 9944c493e44SGerrit Uitslag 995026b594bSAndreas Gohr array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\dw2pdf\MenuItem()]); 996026b594bSAndreas Gohr } 997ee19bac3SLuigi Micco} 998