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 */ 161ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin { 1702f9a447SGerrit Uitslag /** 1802f9a447SGerrit Uitslag * Settings for current export, collected from url param, plugin config, global config 1902f9a447SGerrit Uitslag * 2002f9a447SGerrit Uitslag * @var array 2102f9a447SGerrit Uitslag */ 22213fdb75SGerrit Uitslag protected $exportConfig = null; 232a127a1dSGerrit Uitslag /** @var string template name, to use templates from dw2pdf/tpl/<template name> */ 2460e59de7SGerrit Uitslag protected $tpl; 252a127a1dSGerrit Uitslag /** @var string title of exported pdf */ 2603352761SKirsten Roschanski protected $title; 276a7f9d6cSGerrit Uitslag /** @var array list of pages included in exported pdf */ 2860e59de7SGerrit Uitslag protected $list = array(); 292a127a1dSGerrit Uitslag /** @var bool|string path to temporary cachefile */ 309b288b2aSGerrit Uitslag protected $onetimefile = false; 311c14c879SAndreas Gohr 321c14c879SAndreas Gohr /** 331c14c879SAndreas Gohr * Constructor. Sets the correct template 341c14c879SAndreas Gohr */ 352a127a1dSGerrit Uitslag public function __construct() { 3602f9a447SGerrit Uitslag $this->tpl = $this->getExportConfig('template'); 371c14c879SAndreas Gohr } 381c14c879SAndreas Gohr 39ee19bac3SLuigi Micco /** 409b288b2aSGerrit Uitslag * Delete cached files that were for one-time use 419b288b2aSGerrit Uitslag */ 429b288b2aSGerrit Uitslag public function __destruct() { 439b288b2aSGerrit Uitslag if($this->onetimefile) { 449b288b2aSGerrit Uitslag unlink($this->onetimefile); 459b288b2aSGerrit Uitslag } 469b288b2aSGerrit Uitslag } 479b288b2aSGerrit Uitslag 489b288b2aSGerrit Uitslag /** 49ee19bac3SLuigi Micco * Register the events 50177a7d30SGerrit Uitslag * 51177a7d30SGerrit Uitslag * @param Doku_Event_Handler $controller 52ee19bac3SLuigi Micco */ 536be736bfSGerrit Uitslag public function register(Doku_Event_Handler $controller) { 54ee19bac3SLuigi Micco $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array()); 556be736bfSGerrit Uitslag $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array()); 56026b594bSAndreas Gohr $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton', array()); 57ee19bac3SLuigi Micco } 58ee19bac3SLuigi Micco 591c14c879SAndreas Gohr /** 601c14c879SAndreas Gohr * Do the HTML to PDF conversion work 61737417c6SKlap-in * 62737417c6SKlap-in * @param Doku_Event $event 631c14c879SAndreas Gohr */ 6444e8e8fbSGerrit Uitslag public function convert(Doku_Event $event) { 652a127a1dSGerrit Uitslag global $REV, $DATE_AT; 662d9cd424SGerrit Uitslag global $conf, $INPUT; 67ee19bac3SLuigi Micco 681ef68647SAndreas Gohr // our event? 692a127a1dSGerrit Uitslag $allowedEvents = ['export_pdfbook', 'export_pdf', 'export_pdfns']; 702a127a1dSGerrit Uitslag if(!in_array($event->data, $allowedEvents)) return; 71ee19bac3SLuigi Micco 722a127a1dSGerrit Uitslag try{ 732a127a1dSGerrit Uitslag //collect pages and check permissions 742a127a1dSGerrit Uitslag list($this->title, $this->list) = $this->collectExportablePages($event); 75d63e7fe7SGerrit Uitslag 762d9cd424SGerrit Uitslag if($event->data === 'export_pdf' && ($REV || $DATE_AT)) { 779b288b2aSGerrit Uitslag $cachefile = tempnam($conf['tmpdir'] . '/dwpdf', 'dw2pdf_'); 789b288b2aSGerrit Uitslag $this->onetimefile = $cachefile; 79f00df45eSMichael Große $generateNewPdf = true; 80f00df45eSMichael Große } else { 81a58f45f0SGerrit Uitslag // prepare cache and its dependencies 82a58f45f0SGerrit Uitslag $depends = array(); 8303352761SKirsten Roschanski $cache = $this->prepareCache($depends); 849b288b2aSGerrit Uitslag $cachefile = $cache->cache; 8527195d5bSMichael Große $generateNewPdf = !$this->getConf('usecache') 8627195d5bSMichael Große || $this->getExportConfig('isDebug') 8727195d5bSMichael Große || !$cache->useCache($depends); 88f00df45eSMichael Große } 89d63e7fe7SGerrit Uitslag 90bd977188SGerrit Uitslag // hard work only when no cache available or needed for debugging 91f00df45eSMichael Große if($generateNewPdf) { 92e5f6c2cbSMichael Große // generating the pdf may take a long time for larger wikis / namespaces with many pages 93e5f6c2cbSMichael Große set_time_limit(0); 942a127a1dSGerrit Uitslag //may throw Mpdf\MpdfException as well 959b288b2aSGerrit Uitslag $this->generatePDF($cachefile, $event); 962a127a1dSGerrit Uitslag } 972a127a1dSGerrit Uitslag } catch(Exception $e) { 982d9cd424SGerrit Uitslag if($INPUT->has('selection')) { 992d9cd424SGerrit Uitslag http_status(400); 1002d9cd424SGerrit Uitslag print $e->getMessage(); 1012d9cd424SGerrit Uitslag exit(); 1022d9cd424SGerrit Uitslag } else { 1032a127a1dSGerrit Uitslag //prevent Action/Export() 104b34cb34eSSzymon Olewniczak msg($e->getMessage(), -1); 1052a127a1dSGerrit Uitslag $event->data = 'redirect'; 106d9c13ec7SGerrit Uitslag return; 107b34cb34eSSzymon Olewniczak } 108d63e7fe7SGerrit Uitslag } 1092d9cd424SGerrit Uitslag $event->preventDefault(); // after prevent, $event->data cannot be changed 110d63e7fe7SGerrit Uitslag 111d63e7fe7SGerrit Uitslag // deliver the file 1129b288b2aSGerrit Uitslag $this->sendPDFFile($cachefile); //exits 113d63e7fe7SGerrit Uitslag } 114d63e7fe7SGerrit Uitslag 115d63e7fe7SGerrit Uitslag /** 1162a127a1dSGerrit Uitslag * Obtain list of pages and title, for different methods of exporting the pdf. 1172a127a1dSGerrit Uitslag * - Return a title and selection, throw otherwise an exception 1182a127a1dSGerrit Uitslag * - Check permisions 119d63e7fe7SGerrit Uitslag * 120d63e7fe7SGerrit Uitslag * @param Doku_Event $event 1217c79bc79SGerrit Uitslag * @return array|false 1222a127a1dSGerrit Uitslag * @throws Exception 123d63e7fe7SGerrit Uitslag */ 1242a127a1dSGerrit Uitslag protected function collectExportablePages(Doku_Event $event) { 12536a7917dSGerrit Uitslag global $ID, $REV; 126d63e7fe7SGerrit Uitslag global $INPUT; 1272a127a1dSGerrit Uitslag global $conf, $lang; 128d63e7fe7SGerrit Uitslag 129d63e7fe7SGerrit Uitslag // list of one or multiple pages 130d63e7fe7SGerrit Uitslag $list = array(); 13128e636eaSGerrit Uitslag 1324b4cebc2SLarsDW223 if($event->data == 'export_pdf') { 1332a127a1dSGerrit Uitslag if(auth_quickaclcheck($ID) < AUTH_READ) { // set more specific denied message 1342a127a1dSGerrit Uitslag throw new Exception($lang['accessdenied']); 1352a127a1dSGerrit Uitslag } 136d63e7fe7SGerrit Uitslag $list[0] = $ID; 1372a127a1dSGerrit Uitslag $title = $INPUT->str('pdftitle'); //DEPRECATED 1382a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 1392a127a1dSGerrit Uitslag if(empty($title)) { 1402a127a1dSGerrit Uitslag $title = p_get_first_heading($ID); 14115923cb9SGerrit Uitslag } 142ce8af5d0SHativ // use page name if title is still empty 1432a127a1dSGerrit Uitslag if(empty($title)) { 1442a127a1dSGerrit Uitslag $title = noNS($ID); 145ce8af5d0SHativ } 146ad18f4e1SGerrit Uitslag 14736a7917dSGerrit Uitslag $filename = wikiFN($ID, $REV); 14836a7917dSGerrit Uitslag if(!file_exists($filename)) { 1492a127a1dSGerrit Uitslag throw new Exception($this->getLang('notexist')); 15036a7917dSGerrit Uitslag } 15136a7917dSGerrit Uitslag 1524b4cebc2SLarsDW223 } elseif($event->data == 'export_pdfns') { 153ad18f4e1SGerrit Uitslag //check input for title and ns 1542a127a1dSGerrit Uitslag if(!$title = $INPUT->str('book_title')) { 1552a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 156ad18f4e1SGerrit Uitslag } 157177a7d30SGerrit Uitslag $pdfnamespace = cleanID($INPUT->str('book_ns')); 158ad18f4e1SGerrit Uitslag if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) { 1592a127a1dSGerrit Uitslag throw new Exception($this->getLang('needns')); 160ad18f4e1SGerrit Uitslag } 161ad18f4e1SGerrit Uitslag 16226be4eceSGerrit Uitslag //sort order 163177a7d30SGerrit Uitslag $order = $INPUT->str('book_order', 'natural', true); 164ad18f4e1SGerrit Uitslag $sortoptions = array('pagename', 'date', 'natural'); 165ad18f4e1SGerrit Uitslag if(!in_array($order, $sortoptions)) { 166ad18f4e1SGerrit Uitslag $order = 'natural'; 167ad18f4e1SGerrit Uitslag } 168ad18f4e1SGerrit Uitslag 16926be4eceSGerrit Uitslag //search depth 170177a7d30SGerrit Uitslag $depth = $INPUT->int('book_nsdepth', 0); 171ad18f4e1SGerrit Uitslag if($depth < 0) { 172ad18f4e1SGerrit Uitslag $depth = 0; 173ad18f4e1SGerrit Uitslag } 17426be4eceSGerrit Uitslag 175ad18f4e1SGerrit Uitslag //page search 176ad18f4e1SGerrit Uitslag $result = array(); 177ad18f4e1SGerrit Uitslag $opts = array('depth' => $depth); //recursive all levels 178ad18f4e1SGerrit Uitslag $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace)); 179ad18f4e1SGerrit Uitslag search($result, $conf['datadir'], 'search_allpages', $opts, $dir); 180ad18f4e1SGerrit Uitslag 18164541781SAnna Dabrowska // exclude ids 18264541781SAnna Dabrowska $excludes = $INPUT->arr('excludes'); 18364541781SAnna Dabrowska if (!empty($excludes)) { 184d31b75d5SAnna Dabrowska $result = array_filter($result, function ($item) use ($excludes) { 185*8c25a9b9SGerrit Uitslag return !in_array($item['id'], $excludes); 186*8c25a9b9SGerrit Uitslag }); 187*8c25a9b9SGerrit Uitslag } 188*8c25a9b9SGerrit Uitslag // exclude namespaces 189*8c25a9b9SGerrit Uitslag $excludesns = $INPUT->arr('excludesns'); 190*8c25a9b9SGerrit Uitslag if (!empty($excludesns)) { 191*8c25a9b9SGerrit Uitslag $result = array_filter($result, function ($item) use ($excludesns) { 192*8c25a9b9SGerrit Uitslag foreach ($excludesns as $ns) { 193*8c25a9b9SGerrit Uitslag if (strpos($item['id'], $ns . ':') === 0) return false; 194*8c25a9b9SGerrit Uitslag } 195*8c25a9b9SGerrit Uitslag return true; 196d31b75d5SAnna Dabrowska }); 19764541781SAnna Dabrowska } 19864541781SAnna Dabrowska 19926be4eceSGerrit Uitslag //sorting 200ad18f4e1SGerrit Uitslag if(count($result) > 0) { 201ad18f4e1SGerrit Uitslag if($order == 'date') { 202ad18f4e1SGerrit Uitslag usort($result, array($this, '_datesort')); 20341e5d4e2SAndreas Gohr } elseif ($order == 'pagename' || $order == 'natural') { 204ad18f4e1SGerrit Uitslag usort($result, array($this, '_pagenamesort')); 205ad18f4e1SGerrit Uitslag } 206ad18f4e1SGerrit Uitslag } 207ad18f4e1SGerrit Uitslag 208ad18f4e1SGerrit Uitslag foreach($result as $item) { 209d63e7fe7SGerrit Uitslag $list[] = $item['id']; 210ad18f4e1SGerrit Uitslag } 211ad18f4e1SGerrit Uitslag 212baa31dc5SGerrit Uitslag if ($pdfnamespace !== '') { 213baa31dc5SGerrit Uitslag if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { 214baa31dc5SGerrit Uitslag if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) { 215baa31dc5SGerrit Uitslag array_unshift($list,rtrim($pdfnamespace,':')); 216baa31dc5SGerrit Uitslag } 217baa31dc5SGerrit Uitslag } 218baa31dc5SGerrit Uitslag } 219baa31dc5SGerrit Uitslag 220737417c6SKlap-in } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 221b3eed6e3SGerrit Uitslag /** @deprecated April 2016 replaced by localStorage version of Bookcreator*/ 22226be4eceSGerrit Uitslag //is in Bookmanager of bookcreator plugin a title given? 2232a127a1dSGerrit Uitslag $title = $INPUT->str('pdfbook_title'); //DEPRECATED 2242a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 2252a127a1dSGerrit Uitslag if(empty($title)) { 2262a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 22726be4eceSGerrit Uitslag } 228ad18f4e1SGerrit Uitslag 2292a127a1dSGerrit Uitslag $list = explode("|", $_COOKIE['list-pagelist']); 2302a127a1dSGerrit Uitslag 231b3eed6e3SGerrit Uitslag } elseif($INPUT->has('selection')) { 232b3eed6e3SGerrit Uitslag //handle Bookcreator requests based at localStorage 233b3eed6e3SGerrit Uitslag// if(!checkSecurityToken()) { 234b3eed6e3SGerrit Uitslag// http_status(403); 235b3eed6e3SGerrit Uitslag// print $this->getLang('empty'); 236b3eed6e3SGerrit Uitslag// exit(); 237b3eed6e3SGerrit Uitslag// } 238b3eed6e3SGerrit Uitslag 2397c79bc79SGerrit Uitslag $list = json_decode($INPUT->str('selection', '', true), true); 240b3eed6e3SGerrit Uitslag if (!is_array($list) || empty($list)) { 2412a127a1dSGerrit Uitslag throw new Exception($this->getLang('empty')); 242b3eed6e3SGerrit Uitslag } 243b3eed6e3SGerrit Uitslag 2442a127a1dSGerrit Uitslag $title = $INPUT->str('pdfbook_title'); //DEPRECATED 2452a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 2462a127a1dSGerrit Uitslag if (empty($title)) { 2472a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 2482a127a1dSGerrit Uitslag } 2492a127a1dSGerrit Uitslag 2502a127a1dSGerrit Uitslag } elseif($INPUT->has('savedselection')) { 2512a127a1dSGerrit Uitslag //export a saved selection of the Bookcreator Plugin 2522a127a1dSGerrit Uitslag if(plugin_isdisabled('bookcreator')) { 2532a127a1dSGerrit Uitslag throw new Exception($this->getLang('missingbookcreator')); 2542a127a1dSGerrit Uitslag } 2552a127a1dSGerrit Uitslag /** @var action_plugin_bookcreator_handleselection $SelectionHandling */ 2562a127a1dSGerrit Uitslag $SelectionHandling = plugin_load('action', 'bookcreator_handleselection'); 2572a127a1dSGerrit Uitslag $savedselection = $SelectionHandling->loadSavedSelection($INPUT->str('savedselection')); 2582a127a1dSGerrit Uitslag $title = $savedselection['title']; 2592a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 2602a127a1dSGerrit Uitslag $list = $savedselection['selection']; 2612a127a1dSGerrit Uitslag 2622a127a1dSGerrit Uitslag if(empty($title)) { 2632a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 264b3eed6e3SGerrit Uitslag } 265b3eed6e3SGerrit Uitslag 266737417c6SKlap-in } else { 26726be4eceSGerrit Uitslag //show empty bookcreator message 2682a127a1dSGerrit Uitslag throw new Exception($this->getLang('empty')); 269737417c6SKlap-in } 270737417c6SKlap-in 271719256adSGerrit Uitslag $list = array_map('cleanID', $list); 272c7138b3fSGerrit Uitslag 273c7138b3fSGerrit Uitslag $skippedpages = array(); 274c7138b3fSGerrit Uitslag foreach($list as $index => $pageid) { 275c7138b3fSGerrit Uitslag if(auth_quickaclcheck($pageid) < AUTH_READ) { 276c7138b3fSGerrit Uitslag $skippedpages[] = $pageid; 277c7138b3fSGerrit Uitslag unset($list[$index]); 278c7138b3fSGerrit Uitslag } 279c7138b3fSGerrit Uitslag } 2802a127a1dSGerrit Uitslag $list = array_filter($list, 'strlen'); //use of strlen() callback prevents removal of pagename '0' 281c7138b3fSGerrit Uitslag 282c7138b3fSGerrit Uitslag //if selection contains forbidden pages throw (overridable) warning 283c7138b3fSGerrit Uitslag if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) { 284c7138b3fSGerrit Uitslag $msg = hsc(join(', ', $skippedpages)); 2852a127a1dSGerrit Uitslag throw new Exception(sprintf($this->getLang('forbidden'), $msg)); 286c7138b3fSGerrit Uitslag } 287c7138b3fSGerrit Uitslag 2882a127a1dSGerrit Uitslag return array($title, $list); 289d63e7fe7SGerrit Uitslag } 290d63e7fe7SGerrit Uitslag 291a58f45f0SGerrit Uitslag /** 292a58f45f0SGerrit Uitslag * Prepare cache 293a58f45f0SGerrit Uitslag * 294a58f45f0SGerrit Uitslag * @param array $depends (reference) array with dependencies 295a58f45f0SGerrit Uitslag * @return cache 296a58f45f0SGerrit Uitslag */ 29703352761SKirsten Roschanski protected function prepareCache(&$depends) { 298a58f45f0SGerrit Uitslag global $REV; 299a58f45f0SGerrit Uitslag 300ee19bac3SLuigi Micco $cachekey = join(',', $this->list) 301ee19bac3SLuigi Micco . $REV 302ee19bac3SLuigi Micco . $this->getExportConfig('template') 303ee19bac3SLuigi Micco . $this->getExportConfig('pagesize') 304ee19bac3SLuigi Micco . $this->getExportConfig('orientation') 305d83760efSGerrit Uitslag . $this->getExportConfig('font-size') 306ee19bac3SLuigi Micco . $this->getExportConfig('doublesided') 307ee19bac3SLuigi Micco . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0') 30803352761SKirsten Roschanski . $this->title; 309ee19bac3SLuigi Micco $cache = new cache($cachekey, '.dw2.pdf'); 310ee19bac3SLuigi Micco 311ee19bac3SLuigi Micco $dependencies = array(); 312ee19bac3SLuigi Micco foreach($this->list as $pageid) { 313ee19bac3SLuigi Micco $relations = p_get_metadata($pageid, 'relation'); 314ee19bac3SLuigi Micco 315ee19bac3SLuigi Micco if(is_array($relations)) { 316ee19bac3SLuigi Micco if(array_key_exists('media', $relations) && is_array($relations['media'])) { 317ee19bac3SLuigi Micco foreach($relations['media'] as $mediaid => $exists) { 318ee19bac3SLuigi Micco if($exists) { 319ee19bac3SLuigi Micco $dependencies[] = mediaFN($mediaid); 320ee19bac3SLuigi Micco } 321ee19bac3SLuigi Micco } 322ee19bac3SLuigi Micco } 323ee19bac3SLuigi Micco 324ee19bac3SLuigi Micco if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) { 325ee19bac3SLuigi Micco foreach($relations['haspart'] as $part_pageid => $exists) { 326ee19bac3SLuigi Micco if($exists) { 327ee19bac3SLuigi Micco $dependencies[] = wikiFN($part_pageid); 328ee19bac3SLuigi Micco } 329ee19bac3SLuigi Micco } 330ee19bac3SLuigi Micco } 331ee19bac3SLuigi Micco } 332ee19bac3SLuigi Micco 333ee19bac3SLuigi Micco $dependencies[] = metaFN($pageid, '.meta'); 334ee19bac3SLuigi Micco } 335ee19bac3SLuigi Micco 336ee19bac3SLuigi Micco $depends['files'] = array_map('wikiFN', $this->list); 337ee19bac3SLuigi Micco $depends['files'][] = __FILE__; 338ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/renderer.php'; 339ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php'; 340ee19bac3SLuigi Micco $depends['files'] = array_merge( 341ee19bac3SLuigi Micco $depends['files'], 342ee19bac3SLuigi Micco $dependencies, 343ee19bac3SLuigi Micco getConfigFiles('main') 344ee19bac3SLuigi Micco ); 345a58f45f0SGerrit Uitslag return $cache; 346ee19bac3SLuigi Micco } 347ee19bac3SLuigi Micco 348d63e7fe7SGerrit Uitslag /** 349e53f1ec0SSzymon Olewniczak * Returns the parsed Wikitext in dw2pdf for the given id and revision 350e53f1ec0SSzymon Olewniczak * 351e53f1ec0SSzymon Olewniczak * @param string $id page id 352e53f1ec0SSzymon Olewniczak * @param string|int $rev revision timestamp or empty string 353e53f1ec0SSzymon Olewniczak * @param string $date_at 354e53f1ec0SSzymon Olewniczak * @return null|string 355e53f1ec0SSzymon Olewniczak */ 356e53f1ec0SSzymon Olewniczak protected function p_wiki_dw2pdf($id, $rev = '', $date_at = '') { 357e53f1ec0SSzymon Olewniczak $file = wikiFN($id, $rev); 358e53f1ec0SSzymon Olewniczak 359e53f1ec0SSzymon Olewniczak if(!file_exists($file)) return ''; 360e53f1ec0SSzymon Olewniczak 361e53f1ec0SSzymon Olewniczak //ensure $id is in global $ID (needed for parsing) 362e53f1ec0SSzymon Olewniczak global $ID; 363e53f1ec0SSzymon Olewniczak $keep = $ID; 364e53f1ec0SSzymon Olewniczak $ID = $id; 365e53f1ec0SSzymon Olewniczak 366e53f1ec0SSzymon Olewniczak if($rev || $date_at) { 367e53f1ec0SSzymon Olewniczak $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); //no caching on old revisions 368e53f1ec0SSzymon Olewniczak } else { 369e53f1ec0SSzymon Olewniczak $ret = p_cached_output($file, 'dw2pdf', $id); 370e53f1ec0SSzymon Olewniczak } 371e53f1ec0SSzymon Olewniczak 372e53f1ec0SSzymon Olewniczak //restore ID (just in case) 373e53f1ec0SSzymon Olewniczak $ID = $keep; 374e53f1ec0SSzymon Olewniczak 375e53f1ec0SSzymon Olewniczak return $ret; 376e53f1ec0SSzymon Olewniczak } 377e53f1ec0SSzymon Olewniczak 378e53f1ec0SSzymon Olewniczak /** 379d63e7fe7SGerrit Uitslag * Build a pdf from the html 380d63e7fe7SGerrit Uitslag * 381d63e7fe7SGerrit Uitslag * @param string $cachefile 382d9c13ec7SGerrit Uitslag * @param Doku_Event $event 3837c79bc79SGerrit Uitslag * @throws \Mpdf\MpdfException 384d63e7fe7SGerrit Uitslag */ 3852d9cd424SGerrit Uitslag protected function generatePDF($cachefile, $event) { 3862d9cd424SGerrit Uitslag global $REV, $INPUT, $DATE_AT; 387e53f1ec0SSzymon Olewniczak 3882d9cd424SGerrit Uitslag if ($event->data == 'export_pdf') { //only one page is exported 389e53f1ec0SSzymon Olewniczak $rev = $REV; 390e53f1ec0SSzymon Olewniczak $date_at = $DATE_AT; 3912a127a1dSGerrit Uitslag } else { //we are exporting entire namespace, ommit revisions 392e53f1ec0SSzymon Olewniczak $rev = $date_at = ''; 393e53f1ec0SSzymon Olewniczak } 39487c86ddaSAndreas Gohr 39502f9a447SGerrit Uitslag //some shortcuts to export settings 39602f9a447SGerrit Uitslag $hasToC = $this->getExportConfig('hasToC'); 39702f9a447SGerrit Uitslag $levels = $this->getExportConfig('levels'); 39802f9a447SGerrit Uitslag $isDebug = $this->getExportConfig('isDebug'); 399b80f709fSNicolas $watermark = $this->getExportConfig('watermark'); 4006ea88a05SAndreas Gohr 4011ef68647SAndreas Gohr // initialize PDF library 402cde5a1b3SAndreas Gohr require_once(dirname(__FILE__) . "/DokuPDF.class.php"); 4036ea88a05SAndreas Gohr 4044870b378SLarsDW223 $mpdf = new DokuPDF($this->getExportConfig('pagesize'), 4054870b378SLarsDW223 $this->getExportConfig('orientation'), 4064870b378SLarsDW223 $this->getExportConfig('font-size')); 407ee19bac3SLuigi Micco 408d62df65bSAndreas Gohr // let mpdf fix local links 409d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 410d62df65bSAndreas Gohr $url = $self['scheme'] . '://' . $self['host']; 41121a55743SAnna Dabrowska if(!empty($self['port'])) { 41202f9a447SGerrit Uitslag $url .= ':' . $self['port']; 41302f9a447SGerrit Uitslag } 414d9c13ec7SGerrit Uitslag $mpdf->SetBasePath($url); 415d62df65bSAndreas Gohr 41656d13144SAndreas Gohr // Set the title 41703352761SKirsten Roschanski $mpdf->SetTitle($this->title); 41856d13144SAndreas Gohr 419d63e7fe7SGerrit Uitslag // some default document settings 420d63e7fe7SGerrit Uitslag //note: double-sided document, starts at an odd page (first page is a right-hand side page) 421213fdb75SGerrit Uitslag // single-side document has only odd pages 422213fdb75SGerrit Uitslag $mpdf->mirrorMargins = $this->getExportConfig('doublesided'); 423daa70883SAndreas Gohr $mpdf->setAutoTopMargin = 'stretch'; 424daa70883SAndreas Gohr $mpdf->setAutoBottomMargin = 'stretch'; 42502f9a447SGerrit Uitslag// $mpdf->pagenumSuffix = '/'; //prefix for {nbpg} 42602f9a447SGerrit Uitslag if($hasToC) { 42702f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC 42802f9a447SGerrit Uitslag $mpdf->h2toc = $levels; 42902f9a447SGerrit Uitslag } else { 43002f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'); 43102f9a447SGerrit Uitslag } 4322eedf77dSAndreas Gohr 433b80f709fSNicolas // Watermarker 434b80f709fSNicolas if($watermark) { 435b80f709fSNicolas $mpdf->SetWatermarkText($watermark); 436b80f709fSNicolas $mpdf->showWatermarkText = true; 437b80f709fSNicolas } 438b80f709fSNicolas 43956d13144SAndreas Gohr // load the template 44003352761SKirsten Roschanski $template = $this->load_template(); 441ee19bac3SLuigi Micco 4421ef68647SAndreas Gohr // prepare HTML header styles 443a2c33768SGerrit Uitslag $html = ''; 44402f9a447SGerrit Uitslag if($isDebug) { 445a2c33768SGerrit Uitslag $html .= '<html><head>'; 446737417c6SKlap-in $html .= '<style type="text/css">'; 447a2c33768SGerrit Uitslag } 448db1aa1bfSKirsten Roschanski 449db1aa1bfSKirsten Roschanski $styles = '@page { size:auto; ' . $template['page'] . '}'; 450a2c33768SGerrit Uitslag $styles .= '@page :first {' . $template['first'] . '}'; 451254467c4SGerrit Uitslag 452254467c4SGerrit Uitslag $styles .= '@page landscape-page { size:landscape }'; 453254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-landscape { page:landscape-page }'; 454254467c4SGerrit Uitslag $styles .= '@page portrait-page { size:portrait }'; 455254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-portrait { page:portrait-page }'; 456db1aa1bfSKirsten Roschanski $styles .= $this->load_css(); 457254467c4SGerrit Uitslag 458a2c33768SGerrit Uitslag $mpdf->WriteHTML($styles, 1); 459a2c33768SGerrit Uitslag 46002f9a447SGerrit Uitslag if($isDebug) { 461a2c33768SGerrit Uitslag $html .= $styles; 4621ef68647SAndreas Gohr $html .= '</style>'; 4631ef68647SAndreas Gohr $html .= '</head><body>'; 464a2c33768SGerrit Uitslag } 465a2c33768SGerrit Uitslag 466a2c33768SGerrit Uitslag $body_start = $template['html']; 467a2c33768SGerrit Uitslag $body_start .= '<div class="dokuwiki">'; 4682eedf77dSAndreas Gohr 4691e45476bSmnapp // insert the cover page 470a2c33768SGerrit Uitslag $body_start .= $template['cover']; 471a2c33768SGerrit Uitslag 472a2c33768SGerrit Uitslag $mpdf->WriteHTML($body_start, 2, true, false); //start body html 47302f9a447SGerrit Uitslag if($isDebug) { 474a2c33768SGerrit Uitslag $html .= $body_start; 475a2c33768SGerrit Uitslag } 47602f9a447SGerrit Uitslag if($hasToC) { 47702f9a447SGerrit 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 47802f9a447SGerrit Uitslag // - first page of ToC starts always at odd page (so eventually an additional blank page is included before) 47902f9a447SGerrit Uitslag // - there is no page numbering at the pages of the ToC 48002f9a447SGerrit Uitslag $mpdf->TOCpagebreakByArray( 48102f9a447SGerrit Uitslag array( 482230b098dSGerrit Uitslag 'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>', 483230b098dSGerrit Uitslag 'toc-bookmarkText' => $this->getLang('tocheader'), 48402f9a447SGerrit Uitslag 'links' => true, 48502f9a447SGerrit Uitslag 'outdent' => '1em', 48602f9a447SGerrit Uitslag 'resetpagenum' => true, //start pagenumbering after ToC 48702f9a447SGerrit Uitslag 'pagenumstyle' => '1' 48802f9a447SGerrit Uitslag ) 48902f9a447SGerrit Uitslag ); 49002f9a447SGerrit Uitslag $html .= '<tocpagebreak>'; 49102f9a447SGerrit Uitslag } 49202f9a447SGerrit Uitslag 4931ef68647SAndreas Gohr // loop over all pages 494c7138b3fSGerrit Uitslag $counter = 0; 495c7138b3fSGerrit Uitslag $no_pages = count($this->list); 496c7138b3fSGerrit Uitslag foreach($this->list as $page) { 497c7138b3fSGerrit Uitslag $counter++; 498b3eed6e3SGerrit Uitslag 49979be256fSMichael Große $pagehtml = $this->p_wiki_dw2pdf($page, $rev, $date_at); 500e53f1ec0SSzymon Olewniczak //file doesn't exists 501e53f1ec0SSzymon Olewniczak if($pagehtml == '') { 502b3eed6e3SGerrit Uitslag continue; 503b3eed6e3SGerrit Uitslag } 504719256adSGerrit Uitslag $pagehtml .= $this->page_depend_replacements($template['cite'], $page); 505c7138b3fSGerrit Uitslag if($counter < $no_pages) { 506a2c33768SGerrit Uitslag $pagehtml .= '<pagebreak />'; 507a2c33768SGerrit Uitslag } 508a2c33768SGerrit Uitslag 509a2c33768SGerrit Uitslag $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html 51002f9a447SGerrit Uitslag if($isDebug) { 511a2c33768SGerrit Uitslag $html .= $pagehtml; 5121ef68647SAndreas Gohr } 513ee19bac3SLuigi Micco } 514ee19bac3SLuigi Micco 51533c15297SGerrit Uitslag // insert the back page 516a2c33768SGerrit Uitslag $body_end = $template['back']; 51733c15297SGerrit Uitslag 518a2c33768SGerrit Uitslag $body_end .= '</div>'; 519a2c33768SGerrit Uitslag 520d63e7fe7SGerrit Uitslag $mpdf->WriteHTML($body_end, 2, false, true); // finish body html 52102f9a447SGerrit Uitslag if($isDebug) { 522a2c33768SGerrit Uitslag $html .= $body_end; 523eeb17e15SAndreas Gohr $html .= '</body>'; 524eeb17e15SAndreas Gohr $html .= '</html>'; 525a2c33768SGerrit Uitslag } 526f765508eSGerrit Uitslag 527f765508eSGerrit Uitslag //Return html for debugging 52802f9a447SGerrit Uitslag if($isDebug) { 52902f9a447SGerrit Uitslag if($INPUT->str('debughtml', 'text', true) == 'html') { 53026be4eceSGerrit Uitslag echo $html; 531a2c33768SGerrit Uitslag } else { 532a2c33768SGerrit Uitslag header('Content-Type: text/plain; charset=utf-8'); 533a2c33768SGerrit Uitslag echo $html; 534a2c33768SGerrit Uitslag } 53526be4eceSGerrit Uitslag exit(); 5367c79bc79SGerrit Uitslag } 537f765508eSGerrit Uitslag 53887c86ddaSAndreas Gohr // write to cache file 539d63e7fe7SGerrit Uitslag $mpdf->Output($cachefile, 'F'); 54087c86ddaSAndreas Gohr } 54187c86ddaSAndreas Gohr 542d63e7fe7SGerrit Uitslag /** 543d63e7fe7SGerrit Uitslag * @param string $cachefile 544d63e7fe7SGerrit Uitslag */ 54503352761SKirsten Roschanski protected function sendPDFFile($cachefile) { 54687c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 547b853b723SAndreas Gohr header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 54887c86ddaSAndreas Gohr header('Pragma: public'); 549d63e7fe7SGerrit Uitslag http_conditionalRequest(filemtime($cachefile)); 550c0956f69SMichael Große global $INPUT; 551f7b24f48SMichael Große $outputTarget = $INPUT->str('outputTarget', $this->getConf('output')); 55287c86ddaSAndreas Gohr 55303352761SKirsten Roschanski $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', ' '))); 554c0956f69SMichael Große if($outputTarget === 'file') { 5559a3c8d9fSAndreas Gohr header('Content-Disposition: attachment; filename="' . $filename . '.pdf";'); 55687c86ddaSAndreas Gohr } else { 5579a3c8d9fSAndreas Gohr header('Content-Disposition: inline; filename="' . $filename . '.pdf";'); 55887c86ddaSAndreas Gohr } 559ee19bac3SLuigi Micco 560b3eed6e3SGerrit Uitslag //Bookcreator uses jQuery.fileDownload.js, which requires a cookie. 561b3eed6e3SGerrit Uitslag header('Set-Cookie: fileDownload=true; path=/'); 562b3eed6e3SGerrit Uitslag 563e993da11SGerrit Uitslag //try to send file, and exit if done 564d63e7fe7SGerrit Uitslag http_sendfile($cachefile); 56587c86ddaSAndreas Gohr 566d63e7fe7SGerrit Uitslag $fp = @fopen($cachefile, "rb"); 56787c86ddaSAndreas Gohr if($fp) { 568d63e7fe7SGerrit Uitslag http_rangeRequest($fp, filesize($cachefile), 'application/pdf'); 56987c86ddaSAndreas Gohr } else { 57087c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 57187c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 57287c86ddaSAndreas Gohr } 5731ef68647SAndreas Gohr exit(); 5741ef68647SAndreas Gohr } 5751ef68647SAndreas Gohr 5766be736bfSGerrit Uitslag /** 5772eedf77dSAndreas Gohr * Load the various template files and prepare the HTML/CSS for insertion 57844e8e8fbSGerrit Uitslag * 57944e8e8fbSGerrit Uitslag * @return array 5801ef68647SAndreas Gohr */ 58103352761SKirsten Roschanski protected function load_template() { 5821ef68647SAndreas Gohr global $ID; 5831ef68647SAndreas Gohr global $conf; 5841ef68647SAndreas Gohr 5852eedf77dSAndreas Gohr // this is what we'll return 58621a55743SAnna Dabrowska $output = [ 5871e45476bSmnapp 'cover' => '', 58821a55743SAnna Dabrowska 'back' => '', 5892eedf77dSAndreas Gohr 'html' => '', 5902eedf77dSAndreas Gohr 'page' => '', 5912eedf77dSAndreas Gohr 'first' => '', 5922eedf77dSAndreas Gohr 'cite' => '', 59321a55743SAnna Dabrowska ]; 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 694d824d23dSAnna Dabrowska $content = $raw; 695a12c41c3SAnna Dabrowska 696a12c41c3SAnna Dabrowska // let other plugins define their own replacements 697d824d23dSAnna Dabrowska $evdata = ['id' => $id, 'replace' => &$replace, 'content' => &$content]; 698a12c41c3SAnna Dabrowska $event = new Doku_Event('PLUGIN_DW2PDF_REPLACE', $evdata); 699d824d23dSAnna Dabrowska if ($event->advise_before()) { 700e3d68265SGerrit Uitslag $content = str_replace(array_keys($replace), array_values($replace), $raw); 701d824d23dSAnna Dabrowska } 702e3d68265SGerrit Uitslag 703a12c41c3SAnna Dabrowska // plugins may post-process HTML, e.g to clean up unused replacements 704a12c41c3SAnna Dabrowska $event->advise_after(); 705a12c41c3SAnna Dabrowska 706e3d68265SGerrit Uitslag // @DATE(<date>[, <format>])@ 707e3d68265SGerrit Uitslag $content = preg_replace_callback( 708e3d68265SGerrit Uitslag '/@DATE\((.*?)(?:,\s*(.*?))?\)@/', 709e3d68265SGerrit Uitslag array($this, 'replacedate'), 710e3d68265SGerrit Uitslag $content 711e3d68265SGerrit Uitslag ); 712e3d68265SGerrit Uitslag 713e3d68265SGerrit Uitslag return $content; 714a180c973SKlap-in } 715a180c973SKlap-in 716e3d68265SGerrit Uitslag 717e3d68265SGerrit Uitslag /** 718e3d68265SGerrit Uitslag * (callback) Replace date by request datestring 719e3d68265SGerrit Uitslag * e.g. '%m(30-11-1975)' is replaced by '11' 720e3d68265SGerrit Uitslag * 721e3d68265SGerrit Uitslag * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern 722e3d68265SGerrit Uitslag * @return string 723e3d68265SGerrit Uitslag */ 724e3d68265SGerrit Uitslag function replacedate($match) { 725e3d68265SGerrit Uitslag global $conf; 726e3d68265SGerrit Uitslag //no 2nd argument for default date format 727e3d68265SGerrit Uitslag if($match[2] == null) { 728e3d68265SGerrit Uitslag $match[2] = $conf['dformat']; 729e3d68265SGerrit Uitslag } 730e3d68265SGerrit Uitslag return strftime($match[2], strtotime($match[1])); 731e3d68265SGerrit Uitslag } 732e3d68265SGerrit Uitslag 733a180c973SKlap-in /** 7341c14c879SAndreas Gohr * Load all the style sheets and apply the needed replacements 7351ef68647SAndreas Gohr */ 7361c14c879SAndreas Gohr protected function load_css() { 737737417c6SKlap-in global $conf; 7387c79bc79SGerrit Uitslag //reuse the CSS dispatcher functions without triggering the main function 7391c14c879SAndreas Gohr define('SIMPLE_TEST', 1); 7401c14c879SAndreas Gohr require_once(DOKU_INC . 'lib/exe/css.php'); 741ee19bac3SLuigi Micco 7421c14c879SAndreas Gohr // prepare CSS files 7431c14c879SAndreas Gohr $files = array_merge( 7441c14c879SAndreas Gohr array( 7451c14c879SAndreas Gohr DOKU_INC . 'lib/styles/screen.css' 7461c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7471c14c879SAndreas Gohr DOKU_INC . 'lib/styles/print.css' 7481c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7491c14c879SAndreas Gohr ), 75058e6409eSAndreas Gohr $this->css_pluginPDFstyles(), 7511c14c879SAndreas Gohr array( 7521c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.css' 7531c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7541c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' 7551c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 7561c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' 7571c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7581c14c879SAndreas Gohr ) 7591c14c879SAndreas Gohr ); 7601c14c879SAndreas Gohr $css = ''; 7611c14c879SAndreas Gohr foreach($files as $file => $location) { 76228e636eaSGerrit Uitslag $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 76328e636eaSGerrit Uitslag $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 7641c14c879SAndreas Gohr $css .= css_loadfile($file, $location); 7651ef68647SAndreas Gohr } 7661ef68647SAndreas Gohr 76728e636eaSGerrit Uitslag if(function_exists('css_parseless')) { 7681c14c879SAndreas Gohr // apply pattern replacements 7697c6ca3bcSMichael Große if (function_exists('css_styleini')) { 7707c6ca3bcSMichael Große // compatiblity layer for pre-Greebo releases of DokuWiki 77128e636eaSGerrit Uitslag $styleini = css_styleini($conf['template']); 7727c6ca3bcSMichael Große } else { 7737c6ca3bcSMichael Große // Greebo functionality 7747c6ca3bcSMichael Große $styleUtils = new \dokuwiki\StyleUtils(); 775d0f0c534SGerrit Uitslag $styleini = $styleUtils->cssStyleini($conf['template']); // older versions need still the template 7767c6ca3bcSMichael Große } 77728e636eaSGerrit Uitslag $css = css_applystyle($css, $styleini['replacements']); 77828e636eaSGerrit Uitslag 77928e636eaSGerrit Uitslag // parse less 78028e636eaSGerrit Uitslag $css = css_parseless($css); 78128e636eaSGerrit Uitslag } else { 78228e636eaSGerrit Uitslag // @deprecated 2013-12-19: fix backward compatibility 7831c14c879SAndreas Gohr $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 78428e636eaSGerrit Uitslag } 7851ef68647SAndreas Gohr 7861c14c879SAndreas Gohr return $css; 787ee19bac3SLuigi Micco } 7881c14c879SAndreas Gohr 78958e6409eSAndreas Gohr /** 79058e6409eSAndreas Gohr * Returns a list of possible Plugin PDF Styles 79158e6409eSAndreas Gohr * 79258e6409eSAndreas Gohr * Checks for a pdf.css, falls back to print.css 79358e6409eSAndreas Gohr * 79458e6409eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 79558e6409eSAndreas Gohr */ 7966be736bfSGerrit Uitslag protected function css_pluginPDFstyles() { 79758e6409eSAndreas Gohr $list = array(); 79858e6409eSAndreas Gohr $plugins = plugin_list(); 799f54b51f7SAndreas Gohr 800f54b51f7SAndreas Gohr $usestyle = explode(',', $this->getConf('usestyles')); 80158e6409eSAndreas Gohr foreach($plugins as $p) { 802f54b51f7SAndreas Gohr if(in_array($p, $usestyle)) { 803f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/"; 8048003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/"; 8058003b493SGerrit Uitslag 806f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/"; 8078003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/"; 808f54b51f7SAndreas Gohr } 809f54b51f7SAndreas Gohr 8108003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/"; 8118003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/"; 8128003b493SGerrit Uitslag 813ab96d816SMichael Große if(file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) { 81458e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/"; 8158003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/"; 81658e6409eSAndreas Gohr } else { 81758e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/"; 8188003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/"; 81958e6409eSAndreas Gohr } 82058e6409eSAndreas Gohr } 8218b8ac6ecSAndreas Gohr 8228b8ac6ecSAndreas Gohr // template support 8238b8ac6ecSAndreas Gohr foreach (['pdf.css', 'pdf.less', 'css/pdf.css', 'css/pdf.less', 'styles/pdf.css', 'styles/pdf.less'] as $file) { 8248b8ac6ecSAndreas Gohr if (file_exists(tpl_incdir() . $file)) { 8258b8ac6ecSAndreas Gohr $list[tpl_incdir() . $file] = tpl_basedir() . $file; 8268b8ac6ecSAndreas Gohr } 8278b8ac6ecSAndreas Gohr } 8288b8ac6ecSAndreas Gohr 82958e6409eSAndreas Gohr return $list; 83058e6409eSAndreas Gohr } 831ad18f4e1SGerrit Uitslag 832ad18f4e1SGerrit Uitslag /** 83360e59de7SGerrit Uitslag * Returns array of pages which will be included in the exported pdf 83460e59de7SGerrit Uitslag * 83560e59de7SGerrit Uitslag * @return array 83660e59de7SGerrit Uitslag */ 83760e59de7SGerrit Uitslag public function getExportedPages() { 83860e59de7SGerrit Uitslag return $this->list; 83960e59de7SGerrit Uitslag } 84060e59de7SGerrit Uitslag 84160e59de7SGerrit Uitslag /** 842ad18f4e1SGerrit Uitslag * usort callback to sort by file lastmodified time 84344e8e8fbSGerrit Uitslag * 84444e8e8fbSGerrit Uitslag * @param array $a 84544e8e8fbSGerrit Uitslag * @param array $b 84644e8e8fbSGerrit Uitslag * @return int 847ad18f4e1SGerrit Uitslag */ 848ad18f4e1SGerrit Uitslag public function _datesort($a, $b) { 849ad18f4e1SGerrit Uitslag if($b['rev'] < $a['rev']) return -1; 850ad18f4e1SGerrit Uitslag if($b['rev'] > $a['rev']) return 1; 851ad18f4e1SGerrit Uitslag return strcmp($b['id'], $a['id']); 852ad18f4e1SGerrit Uitslag } 853ad18f4e1SGerrit Uitslag 854ad18f4e1SGerrit Uitslag /** 855ad18f4e1SGerrit Uitslag * usort callback to sort by page id 85644e8e8fbSGerrit Uitslag * @param array $a 85744e8e8fbSGerrit Uitslag * @param array $b 85844e8e8fbSGerrit Uitslag * @return int 859ad18f4e1SGerrit Uitslag */ 860ad18f4e1SGerrit Uitslag public function _pagenamesort($a, $b) { 86120025b90SAndreas Gohr global $conf; 86220025b90SAndreas Gohr 86320025b90SAndreas Gohr $partsA = explode(':', $a['id']); 86420025b90SAndreas Gohr $countA = count($partsA); 86520025b90SAndreas Gohr $partsB = explode(':', $b['id']); 86620025b90SAndreas Gohr $countB = count($partsB); 867e00f6071SAndreas Gohr $max = max($countA, $countB); 86820025b90SAndreas Gohr 86920025b90SAndreas Gohr 87020025b90SAndreas Gohr // compare namepsace by namespace 871e00f6071SAndreas Gohr for ($i = 0; $i < $max; $i++) { 872e00f6071SAndreas Gohr $partA = $partsA[$i] ?: null; 873e00f6071SAndreas Gohr $partB = $partsB[$i] ?: null; 87420025b90SAndreas Gohr 87520025b90SAndreas Gohr // have we reached the page level? 876e00f6071SAndreas Gohr if ($i === ($countA - 1) || $i === ($countB - 1)) { 87720025b90SAndreas Gohr // start page first 87820025b90SAndreas Gohr if ($partA == $conf['start']) return -1; 87920025b90SAndreas Gohr if ($partB == $conf['start']) return 1; 88020025b90SAndreas Gohr } 88120025b90SAndreas Gohr 882e00f6071SAndreas Gohr // prefer page over namespace 883e00f6071SAndreas Gohr if($partA === $partB) { 884e00f6071SAndreas Gohr if (!isset($partsA[$i + 1])) return -1; 885e00f6071SAndreas Gohr if (!isset($partsB[$i + 1])) return 1; 886e00f6071SAndreas Gohr continue; 887e00f6071SAndreas Gohr } 888e00f6071SAndreas Gohr 889e00f6071SAndreas Gohr 89020025b90SAndreas Gohr // simply compare 89141e5d4e2SAndreas Gohr return strnatcmp($partA, $partB); 89220025b90SAndreas Gohr } 89320025b90SAndreas Gohr 89441e5d4e2SAndreas Gohr return strnatcmp($a['id'], $b['id']); 895ad18f4e1SGerrit Uitslag } 89626be4eceSGerrit Uitslag 89726be4eceSGerrit Uitslag /** 8987c79bc79SGerrit Uitslag * Collects settings from: 89902f9a447SGerrit Uitslag * 1. url parameters 90002f9a447SGerrit Uitslag * 2. plugin config 90102f9a447SGerrit Uitslag * 3. global config 90202f9a447SGerrit Uitslag */ 90302f9a447SGerrit Uitslag protected function loadExportConfig() { 90402f9a447SGerrit Uitslag global $INPUT; 90502f9a447SGerrit Uitslag global $conf; 90602f9a447SGerrit Uitslag 90702f9a447SGerrit Uitslag $this->exportConfig = array(); 90802f9a447SGerrit Uitslag 90902f9a447SGerrit Uitslag // decide on the paper setup from param or config 91002f9a447SGerrit Uitslag $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true); 91102f9a447SGerrit Uitslag $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true); 91202f9a447SGerrit Uitslag 9134870b378SLarsDW223 // decide on the font-size from param or config 9144870b378SLarsDW223 $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true); 9154870b378SLarsDW223 916213fdb75SGerrit Uitslag $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided')); 917213fdb75SGerrit Uitslag $this->exportConfig['doublesided'] = $doublesided ? '1' : '0'; 918213fdb75SGerrit Uitslag 919b80f709fSNicolas $this->exportConfig['watermark'] = $INPUT->str('watermark', ''); 920b80f709fSNicolas 921213fdb75SGerrit Uitslag $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc')); 92202f9a447SGerrit Uitslag $levels = array(); 92302f9a447SGerrit Uitslag if($hasToC) { 92402f9a447SGerrit Uitslag $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true); 92521a55743SAnna Dabrowska list($top_input, $max_input) = array_pad(explode('-', $toclevels, 2), 2, ''); 92621a55743SAnna Dabrowska list($top_conf, $max_conf) = array_pad(explode('-', $this->getConf('toclevels'), 2), 2, ''); 92702f9a447SGerrit Uitslag $bounds_input = array( 92802f9a447SGerrit Uitslag 'top' => array( 92902f9a447SGerrit Uitslag (int) $top_input, 93002f9a447SGerrit Uitslag (int) $top_conf 93102f9a447SGerrit Uitslag ), 93202f9a447SGerrit Uitslag 'max' => array( 93302f9a447SGerrit Uitslag (int) $max_input, 93402f9a447SGerrit Uitslag (int) $max_conf 93502f9a447SGerrit Uitslag ) 93602f9a447SGerrit Uitslag ); 93702f9a447SGerrit Uitslag $bounds = array( 93802f9a447SGerrit Uitslag 'top' => $conf['toptoclevel'], 93902f9a447SGerrit Uitslag 'max' => $conf['maxtoclevel'] 94002f9a447SGerrit Uitslag 94102f9a447SGerrit Uitslag ); 94202f9a447SGerrit Uitslag foreach($bounds_input as $bound => $values) { 94302f9a447SGerrit Uitslag foreach($values as $value) { 94402f9a447SGerrit Uitslag if($value > 0 && $value <= 5) { 94502f9a447SGerrit Uitslag //stop at valid value and store 94602f9a447SGerrit Uitslag $bounds[$bound] = $value; 94702f9a447SGerrit Uitslag break; 94802f9a447SGerrit Uitslag } 94902f9a447SGerrit Uitslag } 95002f9a447SGerrit Uitslag } 95102f9a447SGerrit Uitslag 95202f9a447SGerrit Uitslag if($bounds['max'] < $bounds['top']) { 95302f9a447SGerrit Uitslag $bounds['max'] = $bounds['top']; 95402f9a447SGerrit Uitslag } 95502f9a447SGerrit Uitslag 95602f9a447SGerrit Uitslag for($level = $bounds['top']; $level <= $bounds['max']; $level++) { 95702f9a447SGerrit Uitslag $levels["H$level"] = $level - 1; 95802f9a447SGerrit Uitslag } 95902f9a447SGerrit Uitslag } 96002f9a447SGerrit Uitslag $this->exportConfig['hasToC'] = $hasToC; 96102f9a447SGerrit Uitslag $this->exportConfig['levels'] = $levels; 96202f9a447SGerrit Uitslag 96302f9a447SGerrit Uitslag $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true); 96402f9a447SGerrit Uitslag 96502f9a447SGerrit Uitslag $tplconf = $this->getConf('template'); 96605d2b507SGerrit Uitslag $tpl = $INPUT->str('tpl', $tplconf, true); 96702f9a447SGerrit Uitslag if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) { 96802f9a447SGerrit Uitslag $tpl = $tplconf; 96902f9a447SGerrit Uitslag } 97002f9a447SGerrit Uitslag if(!$tpl){ 97102f9a447SGerrit Uitslag $tpl = 'default'; 97202f9a447SGerrit Uitslag } 97302f9a447SGerrit Uitslag $this->exportConfig['template'] = $tpl; 97402f9a447SGerrit Uitslag 97502f9a447SGerrit Uitslag $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml'); 97602f9a447SGerrit Uitslag } 97702f9a447SGerrit Uitslag 97802f9a447SGerrit Uitslag /** 97902f9a447SGerrit Uitslag * Returns requested config 98002f9a447SGerrit Uitslag * 98102f9a447SGerrit Uitslag * @param string $name 98202f9a447SGerrit Uitslag * @param mixed $notset 98302f9a447SGerrit Uitslag * @return mixed|bool 98402f9a447SGerrit Uitslag */ 98502f9a447SGerrit Uitslag public function getExportConfig($name, $notset = false) { 98602f9a447SGerrit Uitslag if ($this->exportConfig === null){ 98702f9a447SGerrit Uitslag $this->loadExportConfig(); 98802f9a447SGerrit Uitslag } 98902f9a447SGerrit Uitslag 99002f9a447SGerrit Uitslag if(isset($this->exportConfig[$name])){ 99102f9a447SGerrit Uitslag return $this->exportConfig[$name]; 99202f9a447SGerrit Uitslag }else{ 99302f9a447SGerrit Uitslag return $notset; 99402f9a447SGerrit Uitslag } 99502f9a447SGerrit Uitslag } 996d63e7fe7SGerrit Uitslag 997d63e7fe7SGerrit Uitslag /** 998d63e7fe7SGerrit Uitslag * Add 'export pdf'-button to pagetools 999d63e7fe7SGerrit Uitslag * 1000d63e7fe7SGerrit Uitslag * @param Doku_Event $event 1001d63e7fe7SGerrit Uitslag */ 100244e8e8fbSGerrit Uitslag public function addbutton(Doku_Event $event) { 1003e53f1ec0SSzymon Olewniczak global $ID, $REV, $DATE_AT; 1004d63e7fe7SGerrit Uitslag 1005d63e7fe7SGerrit Uitslag if($this->getConf('showexportbutton') && $event->data['view'] == 'main') { 1006d63e7fe7SGerrit Uitslag $params = array('do' => 'export_pdf'); 1007e53f1ec0SSzymon Olewniczak if($DATE_AT) { 1008e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 1009e53f1ec0SSzymon Olewniczak } elseif($REV) { 1010d63e7fe7SGerrit Uitslag $params['rev'] = $REV; 1011d63e7fe7SGerrit Uitslag } 1012d63e7fe7SGerrit Uitslag 1013d63e7fe7SGerrit Uitslag // insert button at position before last (up to top) 1014d63e7fe7SGerrit Uitslag $event->data['items'] = array_slice($event->data['items'], 0, -1, true) + 1015d63e7fe7SGerrit Uitslag array('export_pdf' => 1016d63e7fe7SGerrit Uitslag '<li>' 101772cadc31SChristian Paul . '<a href="' . wl($ID, $params) . '" class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">' 1018d63e7fe7SGerrit Uitslag . '<span>' . $this->getLang('export_pdf_button') . '</span>' 1019d63e7fe7SGerrit Uitslag . '</a>' 1020d63e7fe7SGerrit Uitslag . '</li>' 1021d63e7fe7SGerrit Uitslag ) + 1022d63e7fe7SGerrit Uitslag array_slice($event->data['items'], -1, 1, true); 1023d63e7fe7SGerrit Uitslag } 1024d63e7fe7SGerrit Uitslag } 1025026b594bSAndreas Gohr 1026026b594bSAndreas Gohr /** 1027026b594bSAndreas Gohr * Add 'export pdf' button to page tools, new SVG based mechanism 1028026b594bSAndreas Gohr * 1029026b594bSAndreas Gohr * @param Doku_Event $event 1030026b594bSAndreas Gohr */ 1031026b594bSAndreas Gohr public function addsvgbutton(Doku_Event $event) { 10324c493e44SGerrit Uitslag global $INFO; 10334c493e44SGerrit Uitslag if($event->data['view'] != 'page' || !$this->getConf('showexportbutton')) { 10344c493e44SGerrit Uitslag return; 10354c493e44SGerrit Uitslag } 10364c493e44SGerrit Uitslag 10374c493e44SGerrit Uitslag if(!$INFO['exists']) { 10384c493e44SGerrit Uitslag return; 10394c493e44SGerrit Uitslag } 10404c493e44SGerrit Uitslag 1041026b594bSAndreas Gohr array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\dw2pdf\MenuItem()]); 1042026b594bSAndreas Gohr } 1043ee19bac3SLuigi Micco} 1044