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) { 185d31b75d5SAnna Dabrowska return array_search($item['id'], $excludes) === false; 186d31b75d5SAnna Dabrowska }); 18764541781SAnna Dabrowska } 18864541781SAnna Dabrowska 18926be4eceSGerrit Uitslag //sorting 190ad18f4e1SGerrit Uitslag if(count($result) > 0) { 191ad18f4e1SGerrit Uitslag if($order == 'date') { 192ad18f4e1SGerrit Uitslag usort($result, array($this, '_datesort')); 19341e5d4e2SAndreas Gohr } elseif ($order == 'pagename' || $order == 'natural') { 194ad18f4e1SGerrit Uitslag usort($result, array($this, '_pagenamesort')); 195ad18f4e1SGerrit Uitslag } 196ad18f4e1SGerrit Uitslag } 197ad18f4e1SGerrit Uitslag 198ad18f4e1SGerrit Uitslag foreach($result as $item) { 199d63e7fe7SGerrit Uitslag $list[] = $item['id']; 200ad18f4e1SGerrit Uitslag } 201ad18f4e1SGerrit Uitslag 202baa31dc5SGerrit Uitslag if ($pdfnamespace !== '') { 203baa31dc5SGerrit Uitslag if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { 204baa31dc5SGerrit Uitslag if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) { 205baa31dc5SGerrit Uitslag array_unshift($list,rtrim($pdfnamespace,':')); 206baa31dc5SGerrit Uitslag } 207baa31dc5SGerrit Uitslag } 208baa31dc5SGerrit Uitslag } 209baa31dc5SGerrit Uitslag 210737417c6SKlap-in } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 211b3eed6e3SGerrit Uitslag /** @deprecated April 2016 replaced by localStorage version of Bookcreator*/ 21226be4eceSGerrit Uitslag //is in Bookmanager of bookcreator plugin a title given? 2132a127a1dSGerrit Uitslag $title = $INPUT->str('pdfbook_title'); //DEPRECATED 2142a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 2152a127a1dSGerrit Uitslag if(empty($title)) { 2162a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 21726be4eceSGerrit Uitslag } 218ad18f4e1SGerrit Uitslag 2192a127a1dSGerrit Uitslag $list = explode("|", $_COOKIE['list-pagelist']); 2202a127a1dSGerrit Uitslag 221b3eed6e3SGerrit Uitslag } elseif($INPUT->has('selection')) { 222b3eed6e3SGerrit Uitslag //handle Bookcreator requests based at localStorage 223b3eed6e3SGerrit Uitslag// if(!checkSecurityToken()) { 224b3eed6e3SGerrit Uitslag// http_status(403); 225b3eed6e3SGerrit Uitslag// print $this->getLang('empty'); 226b3eed6e3SGerrit Uitslag// exit(); 227b3eed6e3SGerrit Uitslag// } 228b3eed6e3SGerrit Uitslag 2297c79bc79SGerrit Uitslag $list = json_decode($INPUT->str('selection', '', true), true); 230b3eed6e3SGerrit Uitslag if (!is_array($list) || empty($list)) { 2312a127a1dSGerrit Uitslag throw new Exception($this->getLang('empty')); 232b3eed6e3SGerrit Uitslag } 233b3eed6e3SGerrit Uitslag 2342a127a1dSGerrit Uitslag $title = $INPUT->str('pdfbook_title'); //DEPRECATED 2352a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 2362a127a1dSGerrit Uitslag if (empty($title)) { 2372a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 2382a127a1dSGerrit Uitslag } 2392a127a1dSGerrit Uitslag 2402a127a1dSGerrit Uitslag } elseif($INPUT->has('savedselection')) { 2412a127a1dSGerrit Uitslag //export a saved selection of the Bookcreator Plugin 2422a127a1dSGerrit Uitslag if(plugin_isdisabled('bookcreator')) { 2432a127a1dSGerrit Uitslag throw new Exception($this->getLang('missingbookcreator')); 2442a127a1dSGerrit Uitslag } 2452a127a1dSGerrit Uitslag /** @var action_plugin_bookcreator_handleselection $SelectionHandling */ 2462a127a1dSGerrit Uitslag $SelectionHandling = plugin_load('action', 'bookcreator_handleselection'); 2472a127a1dSGerrit Uitslag $savedselection = $SelectionHandling->loadSavedSelection($INPUT->str('savedselection')); 2482a127a1dSGerrit Uitslag $title = $savedselection['title']; 2492a127a1dSGerrit Uitslag $title = $INPUT->str('book_title', $title, true); 2502a127a1dSGerrit Uitslag $list = $savedselection['selection']; 2512a127a1dSGerrit Uitslag 2522a127a1dSGerrit Uitslag if(empty($title)) { 2532a127a1dSGerrit Uitslag throw new Exception($this->getLang('needtitle')); 254b3eed6e3SGerrit Uitslag } 255b3eed6e3SGerrit Uitslag 256737417c6SKlap-in } else { 25726be4eceSGerrit Uitslag //show empty bookcreator message 2582a127a1dSGerrit Uitslag throw new Exception($this->getLang('empty')); 259737417c6SKlap-in } 260737417c6SKlap-in 261719256adSGerrit Uitslag $list = array_map('cleanID', $list); 262c7138b3fSGerrit Uitslag 263c7138b3fSGerrit Uitslag $skippedpages = array(); 264c7138b3fSGerrit Uitslag foreach($list as $index => $pageid) { 265c7138b3fSGerrit Uitslag if(auth_quickaclcheck($pageid) < AUTH_READ) { 266c7138b3fSGerrit Uitslag $skippedpages[] = $pageid; 267c7138b3fSGerrit Uitslag unset($list[$index]); 268c7138b3fSGerrit Uitslag } 269c7138b3fSGerrit Uitslag } 2702a127a1dSGerrit Uitslag $list = array_filter($list, 'strlen'); //use of strlen() callback prevents removal of pagename '0' 271c7138b3fSGerrit Uitslag 272c7138b3fSGerrit Uitslag //if selection contains forbidden pages throw (overridable) warning 273c7138b3fSGerrit Uitslag if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) { 274c7138b3fSGerrit Uitslag $msg = hsc(join(', ', $skippedpages)); 2752a127a1dSGerrit Uitslag throw new Exception(sprintf($this->getLang('forbidden'), $msg)); 276c7138b3fSGerrit Uitslag } 277c7138b3fSGerrit Uitslag 2782a127a1dSGerrit Uitslag return array($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 /** 339e53f1ec0SSzymon Olewniczak * Returns the parsed Wikitext in dw2pdf for the given id and revision 340e53f1ec0SSzymon Olewniczak * 341e53f1ec0SSzymon Olewniczak * @param string $id page id 342e53f1ec0SSzymon Olewniczak * @param string|int $rev revision timestamp or empty string 343e53f1ec0SSzymon Olewniczak * @param string $date_at 344e53f1ec0SSzymon Olewniczak * @return null|string 345e53f1ec0SSzymon Olewniczak */ 346e53f1ec0SSzymon Olewniczak protected function p_wiki_dw2pdf($id, $rev = '', $date_at = '') { 347e53f1ec0SSzymon Olewniczak $file = wikiFN($id, $rev); 348e53f1ec0SSzymon Olewniczak 349e53f1ec0SSzymon Olewniczak if(!file_exists($file)) return ''; 350e53f1ec0SSzymon Olewniczak 351e53f1ec0SSzymon Olewniczak //ensure $id is in global $ID (needed for parsing) 352e53f1ec0SSzymon Olewniczak global $ID; 353e53f1ec0SSzymon Olewniczak $keep = $ID; 354e53f1ec0SSzymon Olewniczak $ID = $id; 355e53f1ec0SSzymon Olewniczak 356e53f1ec0SSzymon Olewniczak if($rev || $date_at) { 357e53f1ec0SSzymon Olewniczak $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); //no caching on old revisions 358e53f1ec0SSzymon Olewniczak } else { 359e53f1ec0SSzymon Olewniczak $ret = p_cached_output($file, 'dw2pdf', $id); 360e53f1ec0SSzymon Olewniczak } 361e53f1ec0SSzymon Olewniczak 362e53f1ec0SSzymon Olewniczak //restore ID (just in case) 363e53f1ec0SSzymon Olewniczak $ID = $keep; 364e53f1ec0SSzymon Olewniczak 365e53f1ec0SSzymon Olewniczak return $ret; 366e53f1ec0SSzymon Olewniczak } 367e53f1ec0SSzymon Olewniczak 368e53f1ec0SSzymon Olewniczak /** 369d63e7fe7SGerrit Uitslag * Build a pdf from the html 370d63e7fe7SGerrit Uitslag * 371d63e7fe7SGerrit Uitslag * @param string $cachefile 372d9c13ec7SGerrit Uitslag * @param Doku_Event $event 3737c79bc79SGerrit Uitslag * @throws \Mpdf\MpdfException 374d63e7fe7SGerrit Uitslag */ 3752d9cd424SGerrit Uitslag protected function generatePDF($cachefile, $event) { 3762d9cd424SGerrit Uitslag global $REV, $INPUT, $DATE_AT; 377e53f1ec0SSzymon Olewniczak 3782d9cd424SGerrit Uitslag if ($event->data == 'export_pdf') { //only one page is exported 379e53f1ec0SSzymon Olewniczak $rev = $REV; 380e53f1ec0SSzymon Olewniczak $date_at = $DATE_AT; 3812a127a1dSGerrit Uitslag } else { //we are exporting entire namespace, ommit revisions 382e53f1ec0SSzymon Olewniczak $rev = $date_at = ''; 383e53f1ec0SSzymon Olewniczak } 38487c86ddaSAndreas Gohr 38502f9a447SGerrit Uitslag //some shortcuts to export settings 38602f9a447SGerrit Uitslag $hasToC = $this->getExportConfig('hasToC'); 38702f9a447SGerrit Uitslag $levels = $this->getExportConfig('levels'); 38802f9a447SGerrit Uitslag $isDebug = $this->getExportConfig('isDebug'); 389b80f709fSNicolas $watermark = $this->getExportConfig('watermark'); 3906ea88a05SAndreas Gohr 3911ef68647SAndreas Gohr // initialize PDF library 392cde5a1b3SAndreas Gohr require_once(dirname(__FILE__) . "/DokuPDF.class.php"); 3936ea88a05SAndreas Gohr 3944870b378SLarsDW223 $mpdf = new DokuPDF($this->getExportConfig('pagesize'), 3954870b378SLarsDW223 $this->getExportConfig('orientation'), 3964870b378SLarsDW223 $this->getExportConfig('font-size')); 397ee19bac3SLuigi Micco 398d62df65bSAndreas Gohr // let mpdf fix local links 399d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 400d62df65bSAndreas Gohr $url = $self['scheme'] . '://' . $self['host']; 40102f9a447SGerrit Uitslag if($self['port']) { 40202f9a447SGerrit Uitslag $url .= ':' . $self['port']; 40302f9a447SGerrit Uitslag } 404d9c13ec7SGerrit Uitslag $mpdf->SetBasePath($url); 405d62df65bSAndreas Gohr 40656d13144SAndreas Gohr // Set the title 40703352761SKirsten Roschanski $mpdf->SetTitle($this->title); 40856d13144SAndreas Gohr 409d63e7fe7SGerrit Uitslag // some default document settings 410d63e7fe7SGerrit Uitslag //note: double-sided document, starts at an odd page (first page is a right-hand side page) 411213fdb75SGerrit Uitslag // single-side document has only odd pages 412213fdb75SGerrit Uitslag $mpdf->mirrorMargins = $this->getExportConfig('doublesided'); 413daa70883SAndreas Gohr $mpdf->setAutoTopMargin = 'stretch'; 414daa70883SAndreas Gohr $mpdf->setAutoBottomMargin = 'stretch'; 41502f9a447SGerrit Uitslag// $mpdf->pagenumSuffix = '/'; //prefix for {nbpg} 41602f9a447SGerrit Uitslag if($hasToC) { 41702f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC 41802f9a447SGerrit Uitslag $mpdf->h2toc = $levels; 41902f9a447SGerrit Uitslag } else { 42002f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'); 42102f9a447SGerrit Uitslag } 4222eedf77dSAndreas Gohr 423b80f709fSNicolas // Watermarker 424b80f709fSNicolas if($watermark) { 425b80f709fSNicolas $mpdf->SetWatermarkText($watermark); 426b80f709fSNicolas $mpdf->showWatermarkText = true; 427b80f709fSNicolas } 428b80f709fSNicolas 42956d13144SAndreas Gohr // load the template 43003352761SKirsten Roschanski $template = $this->load_template(); 431ee19bac3SLuigi Micco 4321ef68647SAndreas Gohr // prepare HTML header styles 433a2c33768SGerrit Uitslag $html = ''; 43402f9a447SGerrit Uitslag if($isDebug) { 435a2c33768SGerrit Uitslag $html .= '<html><head>'; 436737417c6SKlap-in $html .= '<style type="text/css">'; 437a2c33768SGerrit Uitslag } 438db1aa1bfSKirsten Roschanski 439db1aa1bfSKirsten Roschanski $styles = '@page { size:auto; ' . $template['page'] . '}'; 440a2c33768SGerrit Uitslag $styles .= '@page :first {' . $template['first'] . '}'; 441254467c4SGerrit Uitslag 442254467c4SGerrit Uitslag $styles .= '@page landscape-page { size:landscape }'; 443254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-landscape { page:landscape-page }'; 444254467c4SGerrit Uitslag $styles .= '@page portrait-page { size:portrait }'; 445254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-portrait { page:portrait-page }'; 446db1aa1bfSKirsten Roschanski $styles .= $this->load_css(); 447254467c4SGerrit Uitslag 448a2c33768SGerrit Uitslag $mpdf->WriteHTML($styles, 1); 449a2c33768SGerrit Uitslag 45002f9a447SGerrit Uitslag if($isDebug) { 451a2c33768SGerrit Uitslag $html .= $styles; 4521ef68647SAndreas Gohr $html .= '</style>'; 4531ef68647SAndreas Gohr $html .= '</head><body>'; 454a2c33768SGerrit Uitslag } 455a2c33768SGerrit Uitslag 456a2c33768SGerrit Uitslag $body_start = $template['html']; 457a2c33768SGerrit Uitslag $body_start .= '<div class="dokuwiki">'; 4582eedf77dSAndreas Gohr 4591e45476bSmnapp // insert the cover page 460a2c33768SGerrit Uitslag $body_start .= $template['cover']; 461a2c33768SGerrit Uitslag 462a2c33768SGerrit Uitslag $mpdf->WriteHTML($body_start, 2, true, false); //start body html 46302f9a447SGerrit Uitslag if($isDebug) { 464a2c33768SGerrit Uitslag $html .= $body_start; 465a2c33768SGerrit Uitslag } 46602f9a447SGerrit Uitslag if($hasToC) { 46702f9a447SGerrit 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 46802f9a447SGerrit Uitslag // - first page of ToC starts always at odd page (so eventually an additional blank page is included before) 46902f9a447SGerrit Uitslag // - there is no page numbering at the pages of the ToC 47002f9a447SGerrit Uitslag $mpdf->TOCpagebreakByArray( 47102f9a447SGerrit Uitslag array( 472230b098dSGerrit Uitslag 'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>', 473230b098dSGerrit Uitslag 'toc-bookmarkText' => $this->getLang('tocheader'), 47402f9a447SGerrit Uitslag 'links' => true, 47502f9a447SGerrit Uitslag 'outdent' => '1em', 47602f9a447SGerrit Uitslag 'resetpagenum' => true, //start pagenumbering after ToC 47702f9a447SGerrit Uitslag 'pagenumstyle' => '1' 47802f9a447SGerrit Uitslag ) 47902f9a447SGerrit Uitslag ); 48002f9a447SGerrit Uitslag $html .= '<tocpagebreak>'; 48102f9a447SGerrit Uitslag } 48202f9a447SGerrit Uitslag 4831ef68647SAndreas Gohr // loop over all pages 484c7138b3fSGerrit Uitslag $counter = 0; 485c7138b3fSGerrit Uitslag $no_pages = count($this->list); 486c7138b3fSGerrit Uitslag foreach($this->list as $page) { 487c7138b3fSGerrit Uitslag $counter++; 488b3eed6e3SGerrit Uitslag 48979be256fSMichael Große $pagehtml = $this->p_wiki_dw2pdf($page, $rev, $date_at); 490e53f1ec0SSzymon Olewniczak //file doesn't exists 491e53f1ec0SSzymon Olewniczak if($pagehtml == '') { 492b3eed6e3SGerrit Uitslag continue; 493b3eed6e3SGerrit Uitslag } 494719256adSGerrit Uitslag $pagehtml .= $this->page_depend_replacements($template['cite'], $page); 495c7138b3fSGerrit Uitslag if($counter < $no_pages) { 496a2c33768SGerrit Uitslag $pagehtml .= '<pagebreak />'; 497a2c33768SGerrit Uitslag } 498a2c33768SGerrit Uitslag 499a2c33768SGerrit Uitslag $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html 50002f9a447SGerrit Uitslag if($isDebug) { 501a2c33768SGerrit Uitslag $html .= $pagehtml; 5021ef68647SAndreas Gohr } 503ee19bac3SLuigi Micco } 504ee19bac3SLuigi Micco 50533c15297SGerrit Uitslag // insert the back page 506a2c33768SGerrit Uitslag $body_end = $template['back']; 50733c15297SGerrit Uitslag 508a2c33768SGerrit Uitslag $body_end .= '</div>'; 509a2c33768SGerrit Uitslag 510d63e7fe7SGerrit Uitslag $mpdf->WriteHTML($body_end, 2, false, true); // finish body html 51102f9a447SGerrit Uitslag if($isDebug) { 512a2c33768SGerrit Uitslag $html .= $body_end; 513eeb17e15SAndreas Gohr $html .= '</body>'; 514eeb17e15SAndreas Gohr $html .= '</html>'; 515a2c33768SGerrit Uitslag } 516f765508eSGerrit Uitslag 517f765508eSGerrit Uitslag //Return html for debugging 51802f9a447SGerrit Uitslag if($isDebug) { 51902f9a447SGerrit Uitslag if($INPUT->str('debughtml', 'text', true) == 'html') { 52026be4eceSGerrit Uitslag echo $html; 521a2c33768SGerrit Uitslag } else { 522a2c33768SGerrit Uitslag header('Content-Type: text/plain; charset=utf-8'); 523a2c33768SGerrit Uitslag echo $html; 524a2c33768SGerrit Uitslag } 52526be4eceSGerrit Uitslag exit(); 5267c79bc79SGerrit Uitslag } 527f765508eSGerrit Uitslag 52887c86ddaSAndreas Gohr // write to cache file 529d63e7fe7SGerrit Uitslag $mpdf->Output($cachefile, 'F'); 53087c86ddaSAndreas Gohr } 53187c86ddaSAndreas Gohr 532d63e7fe7SGerrit Uitslag /** 533d63e7fe7SGerrit Uitslag * @param string $cachefile 534d63e7fe7SGerrit Uitslag */ 53503352761SKirsten Roschanski protected function sendPDFFile($cachefile) { 53687c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 537b853b723SAndreas Gohr header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 53887c86ddaSAndreas Gohr header('Pragma: public'); 539d63e7fe7SGerrit Uitslag http_conditionalRequest(filemtime($cachefile)); 540c0956f69SMichael Große global $INPUT; 541f7b24f48SMichael Große $outputTarget = $INPUT->str('outputTarget', $this->getConf('output')); 54287c86ddaSAndreas Gohr 54303352761SKirsten Roschanski $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', ' '))); 544c0956f69SMichael Große if($outputTarget === 'file') { 5459a3c8d9fSAndreas Gohr header('Content-Disposition: attachment; filename="' . $filename . '.pdf";'); 54687c86ddaSAndreas Gohr } else { 5479a3c8d9fSAndreas Gohr header('Content-Disposition: inline; filename="' . $filename . '.pdf";'); 54887c86ddaSAndreas Gohr } 549ee19bac3SLuigi Micco 550b3eed6e3SGerrit Uitslag //Bookcreator uses jQuery.fileDownload.js, which requires a cookie. 551b3eed6e3SGerrit Uitslag header('Set-Cookie: fileDownload=true; path=/'); 552b3eed6e3SGerrit Uitslag 553e993da11SGerrit Uitslag //try to send file, and exit if done 554d63e7fe7SGerrit Uitslag http_sendfile($cachefile); 55587c86ddaSAndreas Gohr 556d63e7fe7SGerrit Uitslag $fp = @fopen($cachefile, "rb"); 55787c86ddaSAndreas Gohr if($fp) { 558d63e7fe7SGerrit Uitslag http_rangeRequest($fp, filesize($cachefile), 'application/pdf'); 55987c86ddaSAndreas Gohr } else { 56087c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 56187c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 56287c86ddaSAndreas Gohr } 5631ef68647SAndreas Gohr exit(); 5641ef68647SAndreas Gohr } 5651ef68647SAndreas Gohr 5666be736bfSGerrit Uitslag /** 5672eedf77dSAndreas Gohr * Load the various template files and prepare the HTML/CSS for insertion 56844e8e8fbSGerrit Uitslag * 56944e8e8fbSGerrit Uitslag * @return array 5701ef68647SAndreas Gohr */ 57103352761SKirsten Roschanski protected function load_template() { 5721ef68647SAndreas Gohr global $ID; 5731ef68647SAndreas Gohr global $conf; 5741ef68647SAndreas Gohr 5752eedf77dSAndreas Gohr // this is what we'll return 5762eedf77dSAndreas Gohr $output = array( 5771e45476bSmnapp 'cover' => '', 5782eedf77dSAndreas Gohr 'html' => '', 5792eedf77dSAndreas Gohr 'page' => '', 5802eedf77dSAndreas Gohr 'first' => '', 5812eedf77dSAndreas Gohr 'cite' => '', 5822eedf77dSAndreas Gohr ); 5832eedf77dSAndreas Gohr 5842eedf77dSAndreas Gohr // prepare header/footer elements 5852eedf77dSAndreas Gohr $html = ''; 58633c15297SGerrit Uitslag foreach(array('header', 'footer') as $section) { 58733c15297SGerrit Uitslag foreach(array('', '_odd', '_even', '_first') as $order) { 58802f9a447SGerrit Uitslag $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html'; 58933c15297SGerrit Uitslag if(file_exists($file)) { 59033c15297SGerrit Uitslag $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF; 59133c15297SGerrit Uitslag $html .= file_get_contents($file) . DOKU_LF; 59233c15297SGerrit Uitslag $html .= '</htmlpage' . $section . '>' . DOKU_LF; 5932eedf77dSAndreas Gohr 5942eedf77dSAndreas Gohr // register the needed pseudo CSS 59533c15297SGerrit Uitslag if($order == '_first') { 59633c15297SGerrit Uitslag $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 59733c15297SGerrit Uitslag } elseif($order == '_even') { 59833c15297SGerrit Uitslag $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 59933c15297SGerrit Uitslag } elseif($order == '_odd') { 60033c15297SGerrit Uitslag $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 601daa70883SAndreas Gohr } else { 60233c15297SGerrit Uitslag $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 6032eedf77dSAndreas Gohr } 6042eedf77dSAndreas Gohr } 6052eedf77dSAndreas Gohr } 6062eedf77dSAndreas Gohr } 6072eedf77dSAndreas Gohr 6081ef68647SAndreas Gohr // prepare replacements 6091ef68647SAndreas Gohr $replace = array( 6101ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 61102f9a447SGerrit Uitslag '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / ' 61203352761SKirsten Roschanski '@TITLE@' => hsc($this->title), 6131ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 6141ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 6151ef68647SAndreas Gohr '@DATE@' => dformat(time()), 6165d6fbaeaSAndreas Gohr '@BASE@' => DOKU_BASE, 617893987a2SAndreas Gohr '@INC@' => DOKU_INC, 618893987a2SAndreas Gohr '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 619893987a2SAndreas Gohr '@TPLINC@' => DOKU_INC . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/' 6201ef68647SAndreas Gohr ); 6211ef68647SAndreas Gohr 6222eedf77dSAndreas Gohr // set HTML element 623a180c973SKlap-in $html = str_replace(array_keys($replace), array_values($replace), $html); 624a180c973SKlap-in //TODO For bookcreator $ID (= bookmanager page) makes no sense 625a180c973SKlap-in $output['html'] = $this->page_depend_replacements($html, $ID); 6261ef68647SAndreas Gohr 6271e45476bSmnapp // cover page 62802f9a447SGerrit Uitslag $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html'; 62933c15297SGerrit Uitslag if(file_exists($coverfile)) { 63033c15297SGerrit Uitslag $output['cover'] = file_get_contents($coverfile); 6311e45476bSmnapp $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']); 6329b071da5SMichael $output['cover'] = $this->page_depend_replacements($output['cover'], $ID); 6336e2ec302SGerrit Uitslag $output['cover'] .= '<pagebreak />'; 6341e45476bSmnapp } 6351e45476bSmnapp 63633c15297SGerrit Uitslag // cover page 63702f9a447SGerrit Uitslag $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html'; 63833c15297SGerrit Uitslag if(file_exists($backfile)) { 63933c15297SGerrit Uitslag $output['back'] = '<pagebreak />'; 64033c15297SGerrit Uitslag $output['back'] .= file_get_contents($backfile); 64133c15297SGerrit Uitslag $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']); 6429b071da5SMichael $output['back'] = $this->page_depend_replacements($output['back'], $ID); 64333c15297SGerrit Uitslag } 64433c15297SGerrit Uitslag 6452eedf77dSAndreas Gohr // citation box 64602f9a447SGerrit Uitslag $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html'; 64733c15297SGerrit Uitslag if(file_exists($citationfile)) { 64833c15297SGerrit Uitslag $output['cite'] = file_get_contents($citationfile); 6492eedf77dSAndreas Gohr $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 6502eedf77dSAndreas Gohr } 6511ef68647SAndreas Gohr 6522eedf77dSAndreas Gohr return $output; 6531ef68647SAndreas Gohr } 6541ef68647SAndreas Gohr 6551ef68647SAndreas Gohr /** 656a180c973SKlap-in * @param string $raw code with placeholders 657a180c973SKlap-in * @param string $id pageid 658a180c973SKlap-in * @return string 659a180c973SKlap-in */ 660a180c973SKlap-in protected function page_depend_replacements($raw, $id) { 661e53f1ec0SSzymon Olewniczak global $REV, $DATE_AT; 662a180c973SKlap-in 6639b2636c8SDiego Belmar // generate qr code for this page using quickchart.io (Google infographics api was deprecated in March 14, 2019) 664a180c973SKlap-in $qr_code = ''; 665a180c973SKlap-in if($this->getConf('qrcodesize')) { 666a180c973SKlap-in $url = urlencode(wl($id, '', '&', true)); 6679b2636c8SDiego Belmar $qr_code = '<img src="https://quickchart.io/qr?size=' . 6689b2636c8SDiego Belmar $this->getConf('qrcodesize') . '&text=' . $url . '&margin=1&ecLevel=Q" />'; 669a180c973SKlap-in } 670a180c973SKlap-in // prepare replacements 671a180c973SKlap-in $replace['@ID@'] = $id; 672a180c973SKlap-in $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV))); 673e53f1ec0SSzymon Olewniczak 674e53f1ec0SSzymon Olewniczak $params = array(); 675e53f1ec0SSzymon Olewniczak if($DATE_AT) { 676e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 677e53f1ec0SSzymon Olewniczak } elseif($REV) { 678e53f1ec0SSzymon Olewniczak $params['rev'] = $REV; 679e53f1ec0SSzymon Olewniczak } 680e53f1ec0SSzymon Olewniczak $replace['@PAGEURL@'] = wl($id, $params, true, "&"); 681a180c973SKlap-in $replace['@QRCODE@'] = $qr_code; 682a180c973SKlap-in 683d824d23dSAnna Dabrowska $content = $raw; 684a12c41c3SAnna Dabrowska 685a12c41c3SAnna Dabrowska // let other plugins define their own replacements 686d824d23dSAnna Dabrowska $evdata = ['id' => $id, 'replace' => &$replace, 'content' => &$content]; 687a12c41c3SAnna Dabrowska $event = new Doku_Event('PLUGIN_DW2PDF_REPLACE', $evdata); 688d824d23dSAnna Dabrowska if ($event->advise_before()) { 689e3d68265SGerrit Uitslag $content = str_replace(array_keys($replace), array_values($replace), $raw); 690d824d23dSAnna Dabrowska } 691e3d68265SGerrit Uitslag 692a12c41c3SAnna Dabrowska // plugins may post-process HTML, e.g to clean up unused replacements 693a12c41c3SAnna Dabrowska $event->advise_after(); 694a12c41c3SAnna Dabrowska 695e3d68265SGerrit Uitslag // @DATE(<date>[, <format>])@ 696e3d68265SGerrit Uitslag $content = preg_replace_callback( 697e3d68265SGerrit Uitslag '/@DATE\((.*?)(?:,\s*(.*?))?\)@/', 698e3d68265SGerrit Uitslag array($this, 'replacedate'), 699e3d68265SGerrit Uitslag $content 700e3d68265SGerrit Uitslag ); 701e3d68265SGerrit Uitslag 702e3d68265SGerrit Uitslag return $content; 703a180c973SKlap-in } 704a180c973SKlap-in 705e3d68265SGerrit Uitslag 706e3d68265SGerrit Uitslag /** 707e3d68265SGerrit Uitslag * (callback) Replace date by request datestring 708e3d68265SGerrit Uitslag * e.g. '%m(30-11-1975)' is replaced by '11' 709e3d68265SGerrit Uitslag * 710e3d68265SGerrit Uitslag * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern 711e3d68265SGerrit Uitslag * @return string 712e3d68265SGerrit Uitslag */ 713e3d68265SGerrit Uitslag function replacedate($match) { 714e3d68265SGerrit Uitslag global $conf; 715e3d68265SGerrit Uitslag //no 2nd argument for default date format 716e3d68265SGerrit Uitslag if($match[2] == null) { 717e3d68265SGerrit Uitslag $match[2] = $conf['dformat']; 718e3d68265SGerrit Uitslag } 719e3d68265SGerrit Uitslag return strftime($match[2], strtotime($match[1])); 720e3d68265SGerrit Uitslag } 721e3d68265SGerrit Uitslag 722a180c973SKlap-in /** 7231c14c879SAndreas Gohr * Load all the style sheets and apply the needed replacements 7241ef68647SAndreas Gohr */ 7251c14c879SAndreas Gohr protected function load_css() { 726737417c6SKlap-in global $conf; 7277c79bc79SGerrit Uitslag //reuse the CSS dispatcher functions without triggering the main function 7281c14c879SAndreas Gohr define('SIMPLE_TEST', 1); 7291c14c879SAndreas Gohr require_once(DOKU_INC . 'lib/exe/css.php'); 730ee19bac3SLuigi Micco 7311c14c879SAndreas Gohr // prepare CSS files 7321c14c879SAndreas Gohr $files = array_merge( 7331c14c879SAndreas Gohr array( 7341c14c879SAndreas Gohr DOKU_INC . 'lib/styles/screen.css' 7351c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7361c14c879SAndreas Gohr DOKU_INC . 'lib/styles/print.css' 7371c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7381c14c879SAndreas Gohr ), 73958e6409eSAndreas Gohr $this->css_pluginPDFstyles(), 7401c14c879SAndreas Gohr array( 7411c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.css' 7421c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7431c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' 7441c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 7451c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' 7461c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7471c14c879SAndreas Gohr ) 7481c14c879SAndreas Gohr ); 7491c14c879SAndreas Gohr $css = ''; 7501c14c879SAndreas Gohr foreach($files as $file => $location) { 75128e636eaSGerrit Uitslag $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 75228e636eaSGerrit Uitslag $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 7531c14c879SAndreas Gohr $css .= css_loadfile($file, $location); 7541ef68647SAndreas Gohr } 7551ef68647SAndreas Gohr 75628e636eaSGerrit Uitslag if(function_exists('css_parseless')) { 7571c14c879SAndreas Gohr // apply pattern replacements 7587c6ca3bcSMichael Große if (function_exists('css_styleini')) { 7597c6ca3bcSMichael Große // compatiblity layer for pre-Greebo releases of DokuWiki 76028e636eaSGerrit Uitslag $styleini = css_styleini($conf['template']); 7617c6ca3bcSMichael Große } else { 7627c6ca3bcSMichael Große // Greebo functionality 7637c6ca3bcSMichael Große $styleUtils = new \dokuwiki\StyleUtils(); 764d0f0c534SGerrit Uitslag $styleini = $styleUtils->cssStyleini($conf['template']); // older versions need still the template 7657c6ca3bcSMichael Große } 76628e636eaSGerrit Uitslag $css = css_applystyle($css, $styleini['replacements']); 76728e636eaSGerrit Uitslag 76828e636eaSGerrit Uitslag // parse less 76928e636eaSGerrit Uitslag $css = css_parseless($css); 77028e636eaSGerrit Uitslag } else { 77128e636eaSGerrit Uitslag // @deprecated 2013-12-19: fix backward compatibility 7721c14c879SAndreas Gohr $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 77328e636eaSGerrit Uitslag } 7741ef68647SAndreas Gohr 7751c14c879SAndreas Gohr return $css; 776ee19bac3SLuigi Micco } 7771c14c879SAndreas Gohr 77858e6409eSAndreas Gohr /** 77958e6409eSAndreas Gohr * Returns a list of possible Plugin PDF Styles 78058e6409eSAndreas Gohr * 78158e6409eSAndreas Gohr * Checks for a pdf.css, falls back to print.css 78258e6409eSAndreas Gohr * 78358e6409eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 78458e6409eSAndreas Gohr */ 7856be736bfSGerrit Uitslag protected function css_pluginPDFstyles() { 78658e6409eSAndreas Gohr $list = array(); 78758e6409eSAndreas Gohr $plugins = plugin_list(); 788f54b51f7SAndreas Gohr 789f54b51f7SAndreas Gohr $usestyle = explode(',', $this->getConf('usestyles')); 79058e6409eSAndreas Gohr foreach($plugins as $p) { 791f54b51f7SAndreas Gohr if(in_array($p, $usestyle)) { 792f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/"; 7938003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/screen.less"] = DOKU_BASE . "lib/plugins/$p/"; 7948003b493SGerrit Uitslag 795f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/"; 7968003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/style.less"] = DOKU_BASE . "lib/plugins/$p/"; 797f54b51f7SAndreas Gohr } 798f54b51f7SAndreas Gohr 7998003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/all.css"] = DOKU_BASE . "lib/plugins/$p/"; 8008003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/all.less"] = DOKU_BASE . "lib/plugins/$p/"; 8018003b493SGerrit Uitslag 802ab96d816SMichael Große if(file_exists(DOKU_PLUGIN . "$p/pdf.css") || file_exists(DOKU_PLUGIN . "$p/pdf.less")) { 80358e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/"; 8048003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/pdf.less"] = DOKU_BASE . "lib/plugins/$p/"; 80558e6409eSAndreas Gohr } else { 80658e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/"; 8078003b493SGerrit Uitslag $list[DOKU_PLUGIN . "$p/print.less"] = DOKU_BASE . "lib/plugins/$p/"; 80858e6409eSAndreas Gohr } 80958e6409eSAndreas Gohr } 810*8b8ac6ecSAndreas Gohr 811*8b8ac6ecSAndreas Gohr // template support 812*8b8ac6ecSAndreas Gohr foreach (['pdf.css', 'pdf.less', 'css/pdf.css', 'css/pdf.less', 'styles/pdf.css', 'styles/pdf.less'] as $file) { 813*8b8ac6ecSAndreas Gohr if (file_exists(tpl_incdir() . $file)) { 814*8b8ac6ecSAndreas Gohr $list[tpl_incdir() . $file] = tpl_basedir() . $file; 815*8b8ac6ecSAndreas Gohr } 816*8b8ac6ecSAndreas Gohr } 817*8b8ac6ecSAndreas Gohr 81858e6409eSAndreas Gohr return $list; 81958e6409eSAndreas Gohr } 820ad18f4e1SGerrit Uitslag 821ad18f4e1SGerrit Uitslag /** 82260e59de7SGerrit Uitslag * Returns array of pages which will be included in the exported pdf 82360e59de7SGerrit Uitslag * 82460e59de7SGerrit Uitslag * @return array 82560e59de7SGerrit Uitslag */ 82660e59de7SGerrit Uitslag public function getExportedPages() { 82760e59de7SGerrit Uitslag return $this->list; 82860e59de7SGerrit Uitslag } 82960e59de7SGerrit Uitslag 83060e59de7SGerrit Uitslag /** 831ad18f4e1SGerrit Uitslag * usort callback to sort by file lastmodified time 83244e8e8fbSGerrit Uitslag * 83344e8e8fbSGerrit Uitslag * @param array $a 83444e8e8fbSGerrit Uitslag * @param array $b 83544e8e8fbSGerrit Uitslag * @return int 836ad18f4e1SGerrit Uitslag */ 837ad18f4e1SGerrit Uitslag public function _datesort($a, $b) { 838ad18f4e1SGerrit Uitslag if($b['rev'] < $a['rev']) return -1; 839ad18f4e1SGerrit Uitslag if($b['rev'] > $a['rev']) return 1; 840ad18f4e1SGerrit Uitslag return strcmp($b['id'], $a['id']); 841ad18f4e1SGerrit Uitslag } 842ad18f4e1SGerrit Uitslag 843ad18f4e1SGerrit Uitslag /** 844ad18f4e1SGerrit Uitslag * usort callback to sort by page id 84544e8e8fbSGerrit Uitslag * @param array $a 84644e8e8fbSGerrit Uitslag * @param array $b 84744e8e8fbSGerrit Uitslag * @return int 848ad18f4e1SGerrit Uitslag */ 849ad18f4e1SGerrit Uitslag public function _pagenamesort($a, $b) { 85020025b90SAndreas Gohr global $conf; 85120025b90SAndreas Gohr 85220025b90SAndreas Gohr $partsA = explode(':', $a['id']); 85320025b90SAndreas Gohr $countA = count($partsA); 85420025b90SAndreas Gohr $partsB = explode(':', $b['id']); 85520025b90SAndreas Gohr $countB = count($partsB); 856e00f6071SAndreas Gohr $max = max($countA, $countB); 85720025b90SAndreas Gohr 85820025b90SAndreas Gohr 85920025b90SAndreas Gohr // compare namepsace by namespace 860e00f6071SAndreas Gohr for ($i = 0; $i < $max; $i++) { 861e00f6071SAndreas Gohr $partA = $partsA[$i] ?: null; 862e00f6071SAndreas Gohr $partB = $partsB[$i] ?: null; 86320025b90SAndreas Gohr 86420025b90SAndreas Gohr // have we reached the page level? 865e00f6071SAndreas Gohr if ($i === ($countA - 1) || $i === ($countB - 1)) { 86620025b90SAndreas Gohr // start page first 86720025b90SAndreas Gohr if ($partA == $conf['start']) return -1; 86820025b90SAndreas Gohr if ($partB == $conf['start']) return 1; 86920025b90SAndreas Gohr } 87020025b90SAndreas Gohr 871e00f6071SAndreas Gohr // prefer page over namespace 872e00f6071SAndreas Gohr if($partA === $partB) { 873e00f6071SAndreas Gohr if (!isset($partsA[$i + 1])) return -1; 874e00f6071SAndreas Gohr if (!isset($partsB[$i + 1])) return 1; 875e00f6071SAndreas Gohr continue; 876e00f6071SAndreas Gohr } 877e00f6071SAndreas Gohr 878e00f6071SAndreas Gohr 87920025b90SAndreas Gohr // simply compare 88041e5d4e2SAndreas Gohr return strnatcmp($partA, $partB); 88120025b90SAndreas Gohr } 88220025b90SAndreas Gohr 88341e5d4e2SAndreas Gohr return strnatcmp($a['id'], $b['id']); 884ad18f4e1SGerrit Uitslag } 88526be4eceSGerrit Uitslag 88626be4eceSGerrit Uitslag /** 8877c79bc79SGerrit Uitslag * Collects settings from: 88802f9a447SGerrit Uitslag * 1. url parameters 88902f9a447SGerrit Uitslag * 2. plugin config 89002f9a447SGerrit Uitslag * 3. global config 89102f9a447SGerrit Uitslag */ 89202f9a447SGerrit Uitslag protected function loadExportConfig() { 89302f9a447SGerrit Uitslag global $INPUT; 89402f9a447SGerrit Uitslag global $conf; 89502f9a447SGerrit Uitslag 89602f9a447SGerrit Uitslag $this->exportConfig = array(); 89702f9a447SGerrit Uitslag 89802f9a447SGerrit Uitslag // decide on the paper setup from param or config 89902f9a447SGerrit Uitslag $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true); 90002f9a447SGerrit Uitslag $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true); 90102f9a447SGerrit Uitslag 9024870b378SLarsDW223 // decide on the font-size from param or config 9034870b378SLarsDW223 $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true); 9044870b378SLarsDW223 905213fdb75SGerrit Uitslag $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided')); 906213fdb75SGerrit Uitslag $this->exportConfig['doublesided'] = $doublesided ? '1' : '0'; 907213fdb75SGerrit Uitslag 908b80f709fSNicolas $this->exportConfig['watermark'] = $INPUT->str('watermark', ''); 909b80f709fSNicolas 910213fdb75SGerrit Uitslag $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc')); 91102f9a447SGerrit Uitslag $levels = array(); 91202f9a447SGerrit Uitslag if($hasToC) { 91302f9a447SGerrit Uitslag $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true); 91402f9a447SGerrit Uitslag list($top_input, $max_input) = explode('-', $toclevels, 2); 91502f9a447SGerrit Uitslag list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2); 91602f9a447SGerrit Uitslag $bounds_input = array( 91702f9a447SGerrit Uitslag 'top' => array( 91802f9a447SGerrit Uitslag (int) $top_input, 91902f9a447SGerrit Uitslag (int) $top_conf 92002f9a447SGerrit Uitslag ), 92102f9a447SGerrit Uitslag 'max' => array( 92202f9a447SGerrit Uitslag (int) $max_input, 92302f9a447SGerrit Uitslag (int) $max_conf 92402f9a447SGerrit Uitslag ) 92502f9a447SGerrit Uitslag ); 92602f9a447SGerrit Uitslag $bounds = array( 92702f9a447SGerrit Uitslag 'top' => $conf['toptoclevel'], 92802f9a447SGerrit Uitslag 'max' => $conf['maxtoclevel'] 92902f9a447SGerrit Uitslag 93002f9a447SGerrit Uitslag ); 93102f9a447SGerrit Uitslag foreach($bounds_input as $bound => $values) { 93202f9a447SGerrit Uitslag foreach($values as $value) { 93302f9a447SGerrit Uitslag if($value > 0 && $value <= 5) { 93402f9a447SGerrit Uitslag //stop at valid value and store 93502f9a447SGerrit Uitslag $bounds[$bound] = $value; 93602f9a447SGerrit Uitslag break; 93702f9a447SGerrit Uitslag } 93802f9a447SGerrit Uitslag } 93902f9a447SGerrit Uitslag } 94002f9a447SGerrit Uitslag 94102f9a447SGerrit Uitslag if($bounds['max'] < $bounds['top']) { 94202f9a447SGerrit Uitslag $bounds['max'] = $bounds['top']; 94302f9a447SGerrit Uitslag } 94402f9a447SGerrit Uitslag 94502f9a447SGerrit Uitslag for($level = $bounds['top']; $level <= $bounds['max']; $level++) { 94602f9a447SGerrit Uitslag $levels["H$level"] = $level - 1; 94702f9a447SGerrit Uitslag } 94802f9a447SGerrit Uitslag } 94902f9a447SGerrit Uitslag $this->exportConfig['hasToC'] = $hasToC; 95002f9a447SGerrit Uitslag $this->exportConfig['levels'] = $levels; 95102f9a447SGerrit Uitslag 95202f9a447SGerrit Uitslag $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true); 95302f9a447SGerrit Uitslag 95402f9a447SGerrit Uitslag $tplconf = $this->getConf('template'); 95505d2b507SGerrit Uitslag $tpl = $INPUT->str('tpl', $tplconf, true); 95602f9a447SGerrit Uitslag if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) { 95702f9a447SGerrit Uitslag $tpl = $tplconf; 95802f9a447SGerrit Uitslag } 95902f9a447SGerrit Uitslag if(!$tpl){ 96002f9a447SGerrit Uitslag $tpl = 'default'; 96102f9a447SGerrit Uitslag } 96202f9a447SGerrit Uitslag $this->exportConfig['template'] = $tpl; 96302f9a447SGerrit Uitslag 96402f9a447SGerrit Uitslag $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml'); 96502f9a447SGerrit Uitslag } 96602f9a447SGerrit Uitslag 96702f9a447SGerrit Uitslag /** 96802f9a447SGerrit Uitslag * Returns requested config 96902f9a447SGerrit Uitslag * 97002f9a447SGerrit Uitslag * @param string $name 97102f9a447SGerrit Uitslag * @param mixed $notset 97202f9a447SGerrit Uitslag * @return mixed|bool 97302f9a447SGerrit Uitslag */ 97402f9a447SGerrit Uitslag public function getExportConfig($name, $notset = false) { 97502f9a447SGerrit Uitslag if ($this->exportConfig === null){ 97602f9a447SGerrit Uitslag $this->loadExportConfig(); 97702f9a447SGerrit Uitslag } 97802f9a447SGerrit Uitslag 97902f9a447SGerrit Uitslag if(isset($this->exportConfig[$name])){ 98002f9a447SGerrit Uitslag return $this->exportConfig[$name]; 98102f9a447SGerrit Uitslag }else{ 98202f9a447SGerrit Uitslag return $notset; 98302f9a447SGerrit Uitslag } 98402f9a447SGerrit Uitslag } 985d63e7fe7SGerrit Uitslag 986d63e7fe7SGerrit Uitslag /** 987d63e7fe7SGerrit Uitslag * Add 'export pdf'-button to pagetools 988d63e7fe7SGerrit Uitslag * 989d63e7fe7SGerrit Uitslag * @param Doku_Event $event 990d63e7fe7SGerrit Uitslag */ 99144e8e8fbSGerrit Uitslag public function addbutton(Doku_Event $event) { 992e53f1ec0SSzymon Olewniczak global $ID, $REV, $DATE_AT; 993d63e7fe7SGerrit Uitslag 994d63e7fe7SGerrit Uitslag if($this->getConf('showexportbutton') && $event->data['view'] == 'main') { 995d63e7fe7SGerrit Uitslag $params = array('do' => 'export_pdf'); 996e53f1ec0SSzymon Olewniczak if($DATE_AT) { 997e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 998e53f1ec0SSzymon Olewniczak } elseif($REV) { 999d63e7fe7SGerrit Uitslag $params['rev'] = $REV; 1000d63e7fe7SGerrit Uitslag } 1001d63e7fe7SGerrit Uitslag 1002d63e7fe7SGerrit Uitslag // insert button at position before last (up to top) 1003d63e7fe7SGerrit Uitslag $event->data['items'] = array_slice($event->data['items'], 0, -1, true) + 1004d63e7fe7SGerrit Uitslag array('export_pdf' => 1005d63e7fe7SGerrit Uitslag '<li>' 100672cadc31SChristian Paul . '<a href="' . wl($ID, $params) . '" class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">' 1007d63e7fe7SGerrit Uitslag . '<span>' . $this->getLang('export_pdf_button') . '</span>' 1008d63e7fe7SGerrit Uitslag . '</a>' 1009d63e7fe7SGerrit Uitslag . '</li>' 1010d63e7fe7SGerrit Uitslag ) + 1011d63e7fe7SGerrit Uitslag array_slice($event->data['items'], -1, 1, true); 1012d63e7fe7SGerrit Uitslag } 1013d63e7fe7SGerrit Uitslag } 1014026b594bSAndreas Gohr 1015026b594bSAndreas Gohr /** 1016026b594bSAndreas Gohr * Add 'export pdf' button to page tools, new SVG based mechanism 1017026b594bSAndreas Gohr * 1018026b594bSAndreas Gohr * @param Doku_Event $event 1019026b594bSAndreas Gohr */ 1020026b594bSAndreas Gohr public function addsvgbutton(Doku_Event $event) { 10214c493e44SGerrit Uitslag global $INFO; 10224c493e44SGerrit Uitslag if($event->data['view'] != 'page' || !$this->getConf('showexportbutton')) { 10234c493e44SGerrit Uitslag return; 10244c493e44SGerrit Uitslag } 10254c493e44SGerrit Uitslag 10264c493e44SGerrit Uitslag if(!$INFO['exists']) { 10274c493e44SGerrit Uitslag return; 10284c493e44SGerrit Uitslag } 10294c493e44SGerrit Uitslag 1030026b594bSAndreas Gohr array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\dw2pdf\MenuItem()]); 1031026b594bSAndreas Gohr } 1032ee19bac3SLuigi Micco} 1033