1ee19bac3SLuigi Micco<?php 2ee19bac3SLuigi Micco/** 3ee19bac3SLuigi Micco * dw2Pdf Plugin: Conversion from dokuwiki content to pdf. 4ee19bac3SLuigi Micco * 5ee19bac3SLuigi Micco * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6ee19bac3SLuigi Micco * @author Luigi Micco <l.micco@tiscali.it> 75db42babSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8ee19bac3SLuigi Micco */ 9ee19bac3SLuigi Micco 10ee19bac3SLuigi Micco// must be run within Dokuwiki 11ee19bac3SLuigi Miccoif(!defined('DOKU_INC')) die(); 12ee19bac3SLuigi Micco 130639157eSGerrit Uitslag/** 140639157eSGerrit Uitslag * Class action_plugin_dw2pdf 150639157eSGerrit Uitslag * 160639157eSGerrit Uitslag * Export hmtl content to pdf, for different url parameter configurations 170639157eSGerrit Uitslag * DokuPDF which extends mPDF is used for generating the pdf from html. 180639157eSGerrit Uitslag */ 191ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin { 2002f9a447SGerrit Uitslag /** 2102f9a447SGerrit Uitslag * Settings for current export, collected from url param, plugin config, global config 2202f9a447SGerrit Uitslag * 2302f9a447SGerrit Uitslag * @var array 2402f9a447SGerrit Uitslag */ 25213fdb75SGerrit Uitslag protected $exportConfig = null; 2660e59de7SGerrit Uitslag protected $tpl; 2703352761SKirsten Roschanski protected $title; 2860e59de7SGerrit Uitslag protected $list = array(); 291c14c879SAndreas Gohr 301c14c879SAndreas Gohr /** 311c14c879SAndreas Gohr * Constructor. Sets the correct template 3203352761SKirsten Roschanski * 3303352761SKirsten Roschanski * @param string $title 341c14c879SAndreas Gohr */ 3503352761SKirsten Roschanski public function __construct($title=null) { 3602f9a447SGerrit Uitslag $this->tpl = $this->getExportConfig('template'); 3703352761SKirsten Roschanski $this->title = $title ? $title : ''; 381c14c879SAndreas Gohr } 391c14c879SAndreas Gohr 40ee19bac3SLuigi Micco /** 41ee19bac3SLuigi Micco * Register the events 42177a7d30SGerrit Uitslag * 43177a7d30SGerrit Uitslag * @param Doku_Event_Handler $controller 44ee19bac3SLuigi Micco */ 456be736bfSGerrit Uitslag public function register(Doku_Event_Handler $controller) { 46ee19bac3SLuigi Micco $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert', array()); 476be736bfSGerrit Uitslag $controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'addbutton', array()); 48ee19bac3SLuigi Micco } 49ee19bac3SLuigi Micco 501c14c879SAndreas Gohr /** 511c14c879SAndreas Gohr * Do the HTML to PDF conversion work 52737417c6SKlap-in * 53737417c6SKlap-in * @param Doku_Event $event 54737417c6SKlap-in * @return bool 551c14c879SAndreas Gohr */ 5644e8e8fbSGerrit Uitslag public function convert(Doku_Event $event) { 57ee19bac3SLuigi Micco global $ACT; 58ee19bac3SLuigi Micco global $ID; 59*f00df45eSMichael Große global $REV, $DATE_AT; 60ee19bac3SLuigi Micco 611ef68647SAndreas Gohr // our event? 62ad18f4e1SGerrit Uitslag if(($ACT != 'export_pdfbook') && ($ACT != 'export_pdf') && ($ACT != 'export_pdfns')) return false; 63ee19bac3SLuigi Micco 641ef68647SAndreas Gohr // check user's rights 651ef68647SAndreas Gohr if(auth_quickaclcheck($ID) < AUTH_READ) return false; 661ef68647SAndreas Gohr 67d63e7fe7SGerrit Uitslag if($data = $this->collectExportPages($event)) { 6803352761SKirsten Roschanski list($this->title, $this->list) = $data; 69d63e7fe7SGerrit Uitslag } else { 70d63e7fe7SGerrit Uitslag return false; 71d63e7fe7SGerrit Uitslag } 72d63e7fe7SGerrit Uitslag 73d63e7fe7SGerrit Uitslag // it's ours, no one else's 74d63e7fe7SGerrit Uitslag $event->preventDefault(); 75d63e7fe7SGerrit Uitslag 76*f00df45eSMichael Große $generateNewPdf = false; 77*f00df45eSMichael Große if ($REV || $DATE_AT) { 78*f00df45eSMichael Große $tempFilename = tempnam('/tmp', 'dw2pdf_'); 79*f00df45eSMichael Große $generateNewPdf = true; 80*f00df45eSMichael Große } else { 81a58f45f0SGerrit Uitslag // prepare cache and its dependencies 82a58f45f0SGerrit Uitslag $depends = array(); 8303352761SKirsten Roschanski $cache = $this->prepareCache($depends); 84*f00df45eSMichael Große $tempFilename = $cache->cache; 85*f00df45eSMichael Große } 86d63e7fe7SGerrit Uitslag 87bd977188SGerrit Uitslag // hard work only when no cache available or needed for debugging 88*f00df45eSMichael Große $generateNewPdf = $generateNewPdf 89*f00df45eSMichael Große || !$this->getConf('usecache') 90*f00df45eSMichael Große || $this->getExportConfig('isDebug') 91*f00df45eSMichael Große || !$cache->useCache($depends); 92*f00df45eSMichael Große if($generateNewPdf) { 93e5f6c2cbSMichael Große // generating the pdf may take a long time for larger wikis / namespaces with many pages 94e5f6c2cbSMichael Große set_time_limit(0); 95b34cb34eSSzymon Olewniczak try { 96*f00df45eSMichael Große $this->generatePDF($tempFilename); 97b34cb34eSSzymon Olewniczak } catch (Mpdf\MpdfException $e) { 98b34cb34eSSzymon Olewniczak //prevent act_export() 99b34cb34eSSzymon Olewniczak $ACT = 'show'; 100b34cb34eSSzymon Olewniczak msg($e->getMessage(), -1); 101b34cb34eSSzymon Olewniczak return false; 102b34cb34eSSzymon Olewniczak } 103b34cb34eSSzymon Olewniczak 104d63e7fe7SGerrit Uitslag } 105d63e7fe7SGerrit Uitslag 106d63e7fe7SGerrit Uitslag // deliver the file 107*f00df45eSMichael Große $this->sendPDFFile($tempFilename); 108d63e7fe7SGerrit Uitslag return true; 109d63e7fe7SGerrit Uitslag } 110d63e7fe7SGerrit Uitslag 111d63e7fe7SGerrit Uitslag /** 112d63e7fe7SGerrit Uitslag * Obtain list of pages and title, based on url parameters 113d63e7fe7SGerrit Uitslag * 114d63e7fe7SGerrit Uitslag * @param Doku_Event $event 115d63e7fe7SGerrit Uitslag * @return string|bool 116d63e7fe7SGerrit Uitslag */ 117d63e7fe7SGerrit Uitslag protected function collectExportPages(Doku_Event $event) { 118d63e7fe7SGerrit Uitslag global $ACT; 119d63e7fe7SGerrit Uitslag global $ID; 120d63e7fe7SGerrit Uitslag global $INPUT; 121d63e7fe7SGerrit Uitslag global $conf; 122d63e7fe7SGerrit Uitslag 123d63e7fe7SGerrit Uitslag // list of one or multiple pages 124d63e7fe7SGerrit Uitslag $list = array(); 12528e636eaSGerrit Uitslag 12687c86ddaSAndreas Gohr if($ACT == 'export_pdf') { 127d63e7fe7SGerrit Uitslag $list[0] = $ID; 12803352761SKirsten Roschanski $this->title = $INPUT->str('pdftitle'); //DEPRECATED 12903352761SKirsten Roschanski $this->title = $INPUT->str('book_title', $this->title, true); 13003352761SKirsten Roschanski if(empty($this->title)) { 13103352761SKirsten Roschanski $this->title = p_get_first_heading($ID); 13215923cb9SGerrit Uitslag } 133ad18f4e1SGerrit Uitslag 134ad18f4e1SGerrit Uitslag } elseif($ACT == 'export_pdfns') { 135ad18f4e1SGerrit Uitslag //check input for title and ns 13603352761SKirsten Roschanski if(!$this->title = $INPUT->str('book_title')) { 13726be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needtitle'); 138ad18f4e1SGerrit Uitslag return false; 139ad18f4e1SGerrit Uitslag } 140177a7d30SGerrit Uitslag $pdfnamespace = cleanID($INPUT->str('book_ns')); 141ad18f4e1SGerrit Uitslag if(!@is_dir(dirname(wikiFN($pdfnamespace . ':dummy')))) { 14226be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needns'); 143ad18f4e1SGerrit Uitslag return false; 144ad18f4e1SGerrit Uitslag } 145ad18f4e1SGerrit Uitslag 14626be4eceSGerrit Uitslag //sort order 147177a7d30SGerrit Uitslag $order = $INPUT->str('book_order', 'natural', true); 148ad18f4e1SGerrit Uitslag $sortoptions = array('pagename', 'date', 'natural'); 149ad18f4e1SGerrit Uitslag if(!in_array($order, $sortoptions)) { 150ad18f4e1SGerrit Uitslag $order = 'natural'; 151ad18f4e1SGerrit Uitslag } 152ad18f4e1SGerrit Uitslag 15326be4eceSGerrit Uitslag //search depth 154177a7d30SGerrit Uitslag $depth = $INPUT->int('book_nsdepth', 0); 155ad18f4e1SGerrit Uitslag if($depth < 0) { 156ad18f4e1SGerrit Uitslag $depth = 0; 157ad18f4e1SGerrit Uitslag } 15826be4eceSGerrit Uitslag 159ad18f4e1SGerrit Uitslag //page search 160ad18f4e1SGerrit Uitslag $result = array(); 161ad18f4e1SGerrit Uitslag $opts = array('depth' => $depth); //recursive all levels 162ad18f4e1SGerrit Uitslag $dir = utf8_encodeFN(str_replace(':', '/', $pdfnamespace)); 163ad18f4e1SGerrit Uitslag search($result, $conf['datadir'], 'search_allpages', $opts, $dir); 164ad18f4e1SGerrit Uitslag 16526be4eceSGerrit Uitslag //sorting 166ad18f4e1SGerrit Uitslag if(count($result) > 0) { 167ad18f4e1SGerrit Uitslag if($order == 'date') { 168ad18f4e1SGerrit Uitslag usort($result, array($this, '_datesort')); 169ad18f4e1SGerrit Uitslag } elseif($order == 'pagename') { 170ad18f4e1SGerrit Uitslag usort($result, array($this, '_pagenamesort')); 171ad18f4e1SGerrit Uitslag } 172ad18f4e1SGerrit Uitslag } 173ad18f4e1SGerrit Uitslag 174ad18f4e1SGerrit Uitslag foreach($result as $item) { 175d63e7fe7SGerrit Uitslag $list[] = $item['id']; 176ad18f4e1SGerrit Uitslag } 177ad18f4e1SGerrit Uitslag 178baa31dc5SGerrit Uitslag if ($pdfnamespace !== '') { 179baa31dc5SGerrit Uitslag if (!in_array($pdfnamespace . ':' . $conf['start'], $list, true)) { 180baa31dc5SGerrit Uitslag if (file_exists(wikiFN(rtrim($pdfnamespace,':')))) { 181baa31dc5SGerrit Uitslag array_unshift($list,rtrim($pdfnamespace,':')); 182baa31dc5SGerrit Uitslag } 183baa31dc5SGerrit Uitslag } 184baa31dc5SGerrit Uitslag } 185baa31dc5SGerrit Uitslag 186737417c6SKlap-in } elseif(isset($_COOKIE['list-pagelist']) && !empty($_COOKIE['list-pagelist'])) { 187b3eed6e3SGerrit Uitslag /** @deprecated April 2016 replaced by localStorage version of Bookcreator*/ 18826be4eceSGerrit Uitslag //is in Bookmanager of bookcreator plugin a title given? 18903352761SKirsten Roschanski $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED 19003352761SKirsten Roschanski $this->title = $INPUT->str('book_title', $this->title, true); 19103352761SKirsten Roschanski if(empty($this->title)) { 19226be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'needtitle'); 193737417c6SKlap-in return false; 19426be4eceSGerrit Uitslag } else { 195d63e7fe7SGerrit Uitslag $list = explode("|", $_COOKIE['list-pagelist']); 19626be4eceSGerrit Uitslag } 197ad18f4e1SGerrit Uitslag 198b3eed6e3SGerrit Uitslag } elseif($INPUT->has('selection')) { 199b3eed6e3SGerrit Uitslag //handle Bookcreator requests based at localStorage 200b3eed6e3SGerrit Uitslag// if(!checkSecurityToken()) { 201b3eed6e3SGerrit Uitslag// http_status(403); 202b3eed6e3SGerrit Uitslag// print $this->getLang('empty'); 203b3eed6e3SGerrit Uitslag// exit(); 204b3eed6e3SGerrit Uitslag// } 205b3eed6e3SGerrit Uitslag 206b3eed6e3SGerrit Uitslag $json = new JSON(JSON_LOOSE_TYPE); 207b3eed6e3SGerrit Uitslag $list = $json->decode($INPUT->post->str('selection', '', true)); 208b3eed6e3SGerrit Uitslag if(!is_array($list) || empty($list)) { 209b3eed6e3SGerrit Uitslag http_status(400); 210b3eed6e3SGerrit Uitslag print $this->getLang('empty'); 211b3eed6e3SGerrit Uitslag exit(); 212b3eed6e3SGerrit Uitslag } 213b3eed6e3SGerrit Uitslag 21403352761SKirsten Roschanski $this->title = $INPUT->str('pdfbook_title'); //DEPRECATED 21503352761SKirsten Roschanski $this->title = $INPUT->str('book_title', $this->title, true); 21603352761SKirsten Roschanski if(empty($this->title)) { 217b3eed6e3SGerrit Uitslag http_status(400); 218b3eed6e3SGerrit Uitslag print $this->getLang('needtitle'); 219b3eed6e3SGerrit Uitslag exit(); 220b3eed6e3SGerrit Uitslag } 221b3eed6e3SGerrit Uitslag 222737417c6SKlap-in } else { 22326be4eceSGerrit Uitslag //show empty bookcreator message 22426be4eceSGerrit Uitslag $this->showPageWithErrorMsg($event, 'empty'); 225737417c6SKlap-in return false; 226737417c6SKlap-in } 227737417c6SKlap-in 228719256adSGerrit Uitslag $list = array_map('cleanID', $list); 229c7138b3fSGerrit Uitslag 230c7138b3fSGerrit Uitslag $skippedpages = array(); 231c7138b3fSGerrit Uitslag foreach($list as $index => $pageid) { 232c7138b3fSGerrit Uitslag if(auth_quickaclcheck($pageid) < AUTH_READ) { 233c7138b3fSGerrit Uitslag $skippedpages[] = $pageid; 234c7138b3fSGerrit Uitslag unset($list[$index]); 235c7138b3fSGerrit Uitslag } 236c7138b3fSGerrit Uitslag } 237c7138b3fSGerrit Uitslag $list = array_filter($list); //removes also pages mentioned '0' 238c7138b3fSGerrit Uitslag 239c7138b3fSGerrit Uitslag //if selection contains forbidden pages throw (overridable) warning 240c7138b3fSGerrit Uitslag if(!$INPUT->bool('book_skipforbiddenpages') && !empty($skippedpages)) { 241c7138b3fSGerrit Uitslag $msg = hsc(join(', ', $skippedpages)); 242c7138b3fSGerrit Uitslag if($INPUT->has('selection')) { 243c7138b3fSGerrit Uitslag http_status(400); 244c7138b3fSGerrit Uitslag print sprintf($this->getLang('forbidden'), $msg); 245c7138b3fSGerrit Uitslag exit(); 246c7138b3fSGerrit Uitslag } else { 247c7138b3fSGerrit Uitslag $this->showPageWithErrorMsg($event, 'forbidden', $msg); 248c7138b3fSGerrit Uitslag return false; 249c7138b3fSGerrit Uitslag } 250c7138b3fSGerrit Uitslag 251c7138b3fSGerrit Uitslag } 252c7138b3fSGerrit Uitslag 25303352761SKirsten Roschanski return array($this->title, $list); 254d63e7fe7SGerrit Uitslag } 255d63e7fe7SGerrit Uitslag 256a58f45f0SGerrit Uitslag /** 257e53f1ec0SSzymon Olewniczak * Get $meta['relations'] for the given page and revision 258e53f1ec0SSzymon Olewniczak * 259e53f1ec0SSzymon Olewniczak * @param $id 260e53f1ec0SSzymon Olewniczak * @param string $rev 261e53f1ec0SSzymon Olewniczak * @return mixed 262e53f1ec0SSzymon Olewniczak */ 263e53f1ec0SSzymon Olewniczak protected function getMetaRelation($id, $rev='') { 264e53f1ec0SSzymon Olewniczak //current revision 265e53f1ec0SSzymon Olewniczak if ($rev == '') return p_get_metadata($id, 'relation'); 266e53f1ec0SSzymon Olewniczak 267e53f1ec0SSzymon Olewniczak // get instructions 268e53f1ec0SSzymon Olewniczak $instructions = p_cached_instructions(wikiFN($id, $rev),false,$id); 269e53f1ec0SSzymon Olewniczak 270e53f1ec0SSzymon Olewniczak // set up the renderer 271e53f1ec0SSzymon Olewniczak $renderer = new Doku_Renderer_metadata(); 272e53f1ec0SSzymon Olewniczak 273e53f1ec0SSzymon Olewniczak // loop through the instructions 274e53f1ec0SSzymon Olewniczak foreach ($instructions as $instruction){ 275e53f1ec0SSzymon Olewniczak //execute only relation['media'] and relation['haspart'] functions 276e53f1ec0SSzymon Olewniczak if ($instruction[0] != 'locallink' && 277e53f1ec0SSzymon Olewniczak $instruction[0] != 'internallink' && 278e53f1ec0SSzymon Olewniczak $instruction[0] != 'externallink' && 279e53f1ec0SSzymon Olewniczak $instruction[0] != 'interwikilink' && 280e53f1ec0SSzymon Olewniczak $instruction[0] != 'windowssharelink' && 281e53f1ec0SSzymon Olewniczak $instruction[0] != 'emaillink' && 282e53f1ec0SSzymon Olewniczak $instruction[0] != 'internalmedia' && 283e53f1ec0SSzymon Olewniczak $instruction[0] != 'rss') continue; 284e53f1ec0SSzymon Olewniczak 285e53f1ec0SSzymon Olewniczak // execute the callback against the renderer 286e53f1ec0SSzymon Olewniczak call_user_func_array(array(&$renderer, $instruction[0]), (array) $instruction[1]); 287e53f1ec0SSzymon Olewniczak } 288e53f1ec0SSzymon Olewniczak 289e53f1ec0SSzymon Olewniczak return $renderer->meta['relation']; 290e53f1ec0SSzymon Olewniczak } 291e53f1ec0SSzymon Olewniczak 292e53f1ec0SSzymon Olewniczak /** 293a58f45f0SGerrit Uitslag * Prepare cache 294a58f45f0SGerrit Uitslag * 295a58f45f0SGerrit Uitslag * @param array $depends (reference) array with dependencies 296a58f45f0SGerrit Uitslag * @return cache 297a58f45f0SGerrit Uitslag */ 29803352761SKirsten Roschanski protected function prepareCache(&$depends) { 299*f00df45eSMichael Große global $REV; 300a58f45f0SGerrit Uitslag 301ee19bac3SLuigi Micco $cachekey = join(',', $this->list) 302*f00df45eSMichael Große . $REV 303ee19bac3SLuigi Micco . $this->getExportConfig('template') 304ee19bac3SLuigi Micco . $this->getExportConfig('pagesize') 305ee19bac3SLuigi Micco . $this->getExportConfig('orientation') 306d83760efSGerrit Uitslag . $this->getExportConfig('font-size') 307ee19bac3SLuigi Micco . $this->getExportConfig('doublesided') 308ee19bac3SLuigi Micco . ($this->getExportConfig('hasToC') ? join('-', $this->getExportConfig('levels')) : '0') 30903352761SKirsten Roschanski . $this->title; 310ee19bac3SLuigi Micco $cache = new cache($cachekey, '.dw2.pdf'); 311ee19bac3SLuigi Micco 312ee19bac3SLuigi Micco $dependencies = array(); 313ee19bac3SLuigi Micco foreach($this->list as $pageid) { 314*f00df45eSMichael Große $relations = p_get_metadata($pageid, 'relation'); 315ee19bac3SLuigi Micco 316ee19bac3SLuigi Micco if(is_array($relations)) { 317ee19bac3SLuigi Micco if(array_key_exists('media', $relations) && is_array($relations['media'])) { 318ee19bac3SLuigi Micco foreach($relations['media'] as $mediaid => $exists) { 319ee19bac3SLuigi Micco if($exists) { 320*f00df45eSMichael Große $dependencies[] = mediaFN($mediaid); 321ee19bac3SLuigi Micco } 322ee19bac3SLuigi Micco } 323ee19bac3SLuigi Micco } 324ee19bac3SLuigi Micco 325ee19bac3SLuigi Micco if(array_key_exists('haspart', $relations) && is_array($relations['haspart'])) { 326ee19bac3SLuigi Micco foreach($relations['haspart'] as $part_pageid => $exists) { 327ee19bac3SLuigi Micco if($exists) { 328ee19bac3SLuigi Micco $dependencies[] = wikiFN($part_pageid); 329ee19bac3SLuigi Micco } 330ee19bac3SLuigi Micco } 331ee19bac3SLuigi Micco } 332ee19bac3SLuigi Micco } 333ee19bac3SLuigi Micco 334ee19bac3SLuigi Micco $dependencies[] = metaFN($pageid, '.meta'); 335ee19bac3SLuigi Micco } 336ee19bac3SLuigi Micco 337ee19bac3SLuigi Micco $depends['files'] = array_map('wikiFN', $this->list); 338ee19bac3SLuigi Micco $depends['files'][] = __FILE__; 339ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/renderer.php'; 340ee19bac3SLuigi Micco $depends['files'][] = dirname(__FILE__) . '/mpdf/mpdf.php'; 341ee19bac3SLuigi Micco $depends['files'] = array_merge( 342ee19bac3SLuigi Micco $depends['files'], 343ee19bac3SLuigi Micco $dependencies, 344ee19bac3SLuigi Micco getConfigFiles('main') 345ee19bac3SLuigi Micco ); 346a58f45f0SGerrit Uitslag return $cache; 347ee19bac3SLuigi Micco } 348ee19bac3SLuigi Micco 349d63e7fe7SGerrit Uitslag /** 350d63e7fe7SGerrit Uitslag * Set error notification and reload page again 351d63e7fe7SGerrit Uitslag * 352d63e7fe7SGerrit Uitslag * @param Doku_Event $event 353d63e7fe7SGerrit Uitslag * @param string $msglangkey key of translation key 354c7138b3fSGerrit Uitslag * @param string $replacement 355d63e7fe7SGerrit Uitslag */ 356c7138b3fSGerrit Uitslag private function showPageWithErrorMsg(Doku_Event $event, $msglangkey, $replacement=null) { 357c7138b3fSGerrit Uitslag if(empty($replacement)) { 358c7138b3fSGerrit Uitslag $msg = $this->getLang($msglangkey); 359c7138b3fSGerrit Uitslag } else { 360c7138b3fSGerrit Uitslag $msg = sprintf($this->getLang($msglangkey), $replacement); 361c7138b3fSGerrit Uitslag } 362c7138b3fSGerrit Uitslag msg($msg, -1); 363d63e7fe7SGerrit Uitslag 364d63e7fe7SGerrit Uitslag $event->data = 'show'; 365d63e7fe7SGerrit Uitslag $_SERVER['REQUEST_METHOD'] = 'POST'; //clears url 366d63e7fe7SGerrit Uitslag } 367d63e7fe7SGerrit Uitslag 368d63e7fe7SGerrit Uitslag /** 369e53f1ec0SSzymon Olewniczak * Returns the parsed Wikitext in dw2pdf for the given id and revision 370e53f1ec0SSzymon Olewniczak * 371e53f1ec0SSzymon Olewniczak * @param string $id page id 372e53f1ec0SSzymon Olewniczak * @param string|int $rev revision timestamp or empty string 373e53f1ec0SSzymon Olewniczak * @param string $date_at 374e53f1ec0SSzymon Olewniczak * @return null|string 375e53f1ec0SSzymon Olewniczak */ 376e53f1ec0SSzymon Olewniczak protected function p_wiki_dw2pdf($id, $rev = '', $date_at = '') { 377e53f1ec0SSzymon Olewniczak $file = wikiFN($id, $rev); 378e53f1ec0SSzymon Olewniczak 379e53f1ec0SSzymon Olewniczak if(!file_exists($file)) return ''; 380e53f1ec0SSzymon Olewniczak 381e53f1ec0SSzymon Olewniczak //ensure $id is in global $ID (needed for parsing) 382e53f1ec0SSzymon Olewniczak global $ID; 383e53f1ec0SSzymon Olewniczak $keep = $ID; 384e53f1ec0SSzymon Olewniczak $ID = $id; 385e53f1ec0SSzymon Olewniczak 386e53f1ec0SSzymon Olewniczak $ret = ''; 387e53f1ec0SSzymon Olewniczak 388e53f1ec0SSzymon Olewniczak if($rev || $date_at) { 389e53f1ec0SSzymon Olewniczak $ret = p_render('dw2pdf', p_get_instructions(io_readWikiPage($file, $id, $rev)), $info, $date_at); //no caching on old revisions 390e53f1ec0SSzymon Olewniczak } else { 391e53f1ec0SSzymon Olewniczak $ret = p_cached_output($file, 'dw2pdf', $id); 392e53f1ec0SSzymon Olewniczak } 393e53f1ec0SSzymon Olewniczak 394e53f1ec0SSzymon Olewniczak //restore ID (just in case) 395e53f1ec0SSzymon Olewniczak $ID = $keep; 396e53f1ec0SSzymon Olewniczak 397e53f1ec0SSzymon Olewniczak return $ret; 398e53f1ec0SSzymon Olewniczak } 399e53f1ec0SSzymon Olewniczak 400e53f1ec0SSzymon Olewniczak /** 401d63e7fe7SGerrit Uitslag * Build a pdf from the html 402d63e7fe7SGerrit Uitslag * 403d63e7fe7SGerrit Uitslag * @param string $cachefile 404d63e7fe7SGerrit Uitslag */ 40503352761SKirsten Roschanski protected function generatePDF($cachefile) { 406e53f1ec0SSzymon Olewniczak global $ID, $REV, $INPUT, $DATE_AT, $ACT; 407e53f1ec0SSzymon Olewniczak 408e53f1ec0SSzymon Olewniczak if ($ACT == 'export_pdf') { //only one page is exported 409e53f1ec0SSzymon Olewniczak $rev = $REV; 410e53f1ec0SSzymon Olewniczak $date_at = $DATE_AT; 411e53f1ec0SSzymon Olewniczak } else { //we are exporting entre namespace, ommit revisions 412e53f1ec0SSzymon Olewniczak $rev = $date_at = ''; 413e53f1ec0SSzymon Olewniczak } 41487c86ddaSAndreas Gohr 41502f9a447SGerrit Uitslag //some shortcuts to export settings 41602f9a447SGerrit Uitslag $hasToC = $this->getExportConfig('hasToC'); 41702f9a447SGerrit Uitslag $levels = $this->getExportConfig('levels'); 41802f9a447SGerrit Uitslag $isDebug = $this->getExportConfig('isDebug'); 4196ea88a05SAndreas Gohr 4201ef68647SAndreas Gohr // initialize PDF library 421cde5a1b3SAndreas Gohr require_once(dirname(__FILE__) . "/DokuPDF.class.php"); 4226ea88a05SAndreas Gohr 4234870b378SLarsDW223 $mpdf = new DokuPDF($this->getExportConfig('pagesize'), 4244870b378SLarsDW223 $this->getExportConfig('orientation'), 4254870b378SLarsDW223 $this->getExportConfig('font-size')); 426ee19bac3SLuigi Micco 427d62df65bSAndreas Gohr // let mpdf fix local links 428d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 429d62df65bSAndreas Gohr $url = $self['scheme'] . '://' . $self['host']; 43002f9a447SGerrit Uitslag if($self['port']) { 43102f9a447SGerrit Uitslag $url .= ':' . $self['port']; 43202f9a447SGerrit Uitslag } 433d62df65bSAndreas Gohr $mpdf->setBasePath($url); 434d62df65bSAndreas Gohr 43556d13144SAndreas Gohr // Set the title 43603352761SKirsten Roschanski $mpdf->SetTitle($this->title); 43756d13144SAndreas Gohr 438d63e7fe7SGerrit Uitslag // some default document settings 439d63e7fe7SGerrit Uitslag //note: double-sided document, starts at an odd page (first page is a right-hand side page) 440213fdb75SGerrit Uitslag // single-side document has only odd pages 441213fdb75SGerrit Uitslag $mpdf->mirrorMargins = $this->getExportConfig('doublesided'); 442daa70883SAndreas Gohr $mpdf->setAutoTopMargin = 'stretch'; 443daa70883SAndreas Gohr $mpdf->setAutoBottomMargin = 'stretch'; 44402f9a447SGerrit Uitslag// $mpdf->pagenumSuffix = '/'; //prefix for {nbpg} 44502f9a447SGerrit Uitslag if($hasToC) { 44602f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => 'i', 'suppress' => 'off'); //use italic pageno until ToC 44702f9a447SGerrit Uitslag $mpdf->h2toc = $levels; 44802f9a447SGerrit Uitslag } else { 44902f9a447SGerrit Uitslag $mpdf->PageNumSubstitutions[] = array('from' => 1, 'reset' => 0, 'type' => '1', 'suppress' => 'off'); 45002f9a447SGerrit Uitslag } 4512eedf77dSAndreas Gohr 45256d13144SAndreas Gohr // load the template 45303352761SKirsten Roschanski $template = $this->load_template(); 454ee19bac3SLuigi Micco 4551ef68647SAndreas Gohr // prepare HTML header styles 456a2c33768SGerrit Uitslag $html = ''; 45702f9a447SGerrit Uitslag if($isDebug) { 458a2c33768SGerrit Uitslag $html .= '<html><head>'; 459737417c6SKlap-in $html .= '<style type="text/css">'; 460a2c33768SGerrit Uitslag } 461db1aa1bfSKirsten Roschanski 462db1aa1bfSKirsten Roschanski $styles = '@page { size:auto; ' . $template['page'] . '}'; 463a2c33768SGerrit Uitslag $styles .= '@page :first {' . $template['first'] . '}'; 464254467c4SGerrit Uitslag 465254467c4SGerrit Uitslag $styles .= '@page landscape-page { size:landscape }'; 466254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-landscape { page:landscape-page }'; 467254467c4SGerrit Uitslag $styles .= '@page portrait-page { size:portrait }'; 468254467c4SGerrit Uitslag $styles .= 'div.dw2pdf-portrait { page:portrait-page }'; 469db1aa1bfSKirsten Roschanski $styles .= $this->load_css(); 470254467c4SGerrit Uitslag 471a2c33768SGerrit Uitslag $mpdf->WriteHTML($styles, 1); 472a2c33768SGerrit Uitslag 47302f9a447SGerrit Uitslag if($isDebug) { 474a2c33768SGerrit Uitslag $html .= $styles; 4751ef68647SAndreas Gohr $html .= '</style>'; 4761ef68647SAndreas Gohr $html .= '</head><body>'; 477a2c33768SGerrit Uitslag } 478a2c33768SGerrit Uitslag 479a2c33768SGerrit Uitslag $body_start = $template['html']; 480a2c33768SGerrit Uitslag $body_start .= '<div class="dokuwiki">'; 4812eedf77dSAndreas Gohr 4821e45476bSmnapp // insert the cover page 483a2c33768SGerrit Uitslag $body_start .= $template['cover']; 484a2c33768SGerrit Uitslag 485a2c33768SGerrit Uitslag $mpdf->WriteHTML($body_start, 2, true, false); //start body html 48602f9a447SGerrit Uitslag if($isDebug) { 487a2c33768SGerrit Uitslag $html .= $body_start; 488a2c33768SGerrit Uitslag } 48902f9a447SGerrit Uitslag if($hasToC) { 49002f9a447SGerrit Uitslag //Note: - for double-sided document the ToC is always on an even number of pages, so that the following content is on a correct odd/even page 49102f9a447SGerrit Uitslag // - first page of ToC starts always at odd page (so eventually an additional blank page is included before) 49202f9a447SGerrit Uitslag // - there is no page numbering at the pages of the ToC 49302f9a447SGerrit Uitslag $mpdf->TOCpagebreakByArray( 49402f9a447SGerrit Uitslag array( 495230b098dSGerrit Uitslag 'toc-preHTML' => '<h2>' . $this->getLang('tocheader') . '</h2>', 496230b098dSGerrit Uitslag 'toc-bookmarkText' => $this->getLang('tocheader'), 49702f9a447SGerrit Uitslag 'links' => true, 49802f9a447SGerrit Uitslag 'outdent' => '1em', 49902f9a447SGerrit Uitslag 'resetpagenum' => true, //start pagenumbering after ToC 50002f9a447SGerrit Uitslag 'pagenumstyle' => '1' 50102f9a447SGerrit Uitslag ) 50202f9a447SGerrit Uitslag ); 50302f9a447SGerrit Uitslag $html .= '<tocpagebreak>'; 50402f9a447SGerrit Uitslag } 50502f9a447SGerrit Uitslag 506c00eb13bSGerrit Uitslag // store original pageid 507c00eb13bSGerrit Uitslag $keep = $ID; 508c00eb13bSGerrit Uitslag 5091ef68647SAndreas Gohr // loop over all pages 510c7138b3fSGerrit Uitslag $counter = 0; 511c7138b3fSGerrit Uitslag $no_pages = count($this->list); 512c7138b3fSGerrit Uitslag foreach($this->list as $page) { 513c7138b3fSGerrit Uitslag $counter++; 514ee19bac3SLuigi Micco 515c00eb13bSGerrit Uitslag // set global pageid to the rendered page 516c00eb13bSGerrit Uitslag $ID = $page; 517c00eb13bSGerrit Uitslag 518e53f1ec0SSzymon Olewniczak //$pagehtml = p_cached_output($filename, 'dw2pdf', $page); 519e53f1ec0SSzymon Olewniczak $pagehtml = $this->p_wiki_dw2pdf($ID, $rev, $date_at); 520e53f1ec0SSzymon Olewniczak //file doesn't exists 521e53f1ec0SSzymon Olewniczak if($pagehtml == '') { 522e53f1ec0SSzymon Olewniczak continue; 523e53f1ec0SSzymon Olewniczak } 524719256adSGerrit Uitslag $pagehtml .= $this->page_depend_replacements($template['cite'], $page); 525c7138b3fSGerrit Uitslag if($counter < $no_pages) { 526a2c33768SGerrit Uitslag $pagehtml .= '<pagebreak />'; 527a2c33768SGerrit Uitslag } 528a2c33768SGerrit Uitslag 529a2c33768SGerrit Uitslag $mpdf->WriteHTML($pagehtml, 2, false, false); //intermediate body html 53002f9a447SGerrit Uitslag if($isDebug) { 531a2c33768SGerrit Uitslag $html .= $pagehtml; 5321ef68647SAndreas Gohr } 533ee19bac3SLuigi Micco } 534c00eb13bSGerrit Uitslag //restore ID 535c00eb13bSGerrit Uitslag $ID = $keep; 536ee19bac3SLuigi Micco 53733c15297SGerrit Uitslag // insert the back page 538a2c33768SGerrit Uitslag $body_end = $template['back']; 53933c15297SGerrit Uitslag 540a2c33768SGerrit Uitslag $body_end .= '</div>'; 541a2c33768SGerrit Uitslag 542d63e7fe7SGerrit Uitslag $mpdf->WriteHTML($body_end, 2, false, true); // finish body html 54302f9a447SGerrit Uitslag if($isDebug) { 544a2c33768SGerrit Uitslag $html .= $body_end; 545eeb17e15SAndreas Gohr $html .= '</body>'; 546eeb17e15SAndreas Gohr $html .= '</html>'; 547a2c33768SGerrit Uitslag } 548f765508eSGerrit Uitslag 549f765508eSGerrit Uitslag //Return html for debugging 55002f9a447SGerrit Uitslag if($isDebug) { 55102f9a447SGerrit Uitslag if($INPUT->str('debughtml', 'text', true) == 'html') { 55226be4eceSGerrit Uitslag echo $html; 553a2c33768SGerrit Uitslag } else { 554a2c33768SGerrit Uitslag header('Content-Type: text/plain; charset=utf-8'); 555a2c33768SGerrit Uitslag echo $html; 556a2c33768SGerrit Uitslag } 55726be4eceSGerrit Uitslag exit(); 55826be4eceSGerrit Uitslag }; 559f765508eSGerrit Uitslag 56087c86ddaSAndreas Gohr // write to cache file 561d63e7fe7SGerrit Uitslag $mpdf->Output($cachefile, 'F'); 56287c86ddaSAndreas Gohr } 56387c86ddaSAndreas Gohr 564d63e7fe7SGerrit Uitslag /** 565d63e7fe7SGerrit Uitslag * @param string $cachefile 566d63e7fe7SGerrit Uitslag */ 56703352761SKirsten Roschanski protected function sendPDFFile($cachefile) { 56887c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 569b853b723SAndreas Gohr header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0'); 57087c86ddaSAndreas Gohr header('Pragma: public'); 571d63e7fe7SGerrit Uitslag http_conditionalRequest(filemtime($cachefile)); 57287c86ddaSAndreas Gohr 57303352761SKirsten Roschanski $filename = rawurlencode(cleanID(strtr($this->title, ':/;"', ' '))); 57487c86ddaSAndreas Gohr if($this->getConf('output') == 'file') { 5759a3c8d9fSAndreas Gohr header('Content-Disposition: attachment; filename="' . $filename . '.pdf";'); 57687c86ddaSAndreas Gohr } else { 5779a3c8d9fSAndreas Gohr header('Content-Disposition: inline; filename="' . $filename . '.pdf";'); 57887c86ddaSAndreas Gohr } 579ee19bac3SLuigi Micco 580b3eed6e3SGerrit Uitslag //Bookcreator uses jQuery.fileDownload.js, which requires a cookie. 581b3eed6e3SGerrit Uitslag header('Set-Cookie: fileDownload=true; path=/'); 582b3eed6e3SGerrit Uitslag 583e993da11SGerrit Uitslag //try to send file, and exit if done 584d63e7fe7SGerrit Uitslag http_sendfile($cachefile); 58587c86ddaSAndreas Gohr 586d63e7fe7SGerrit Uitslag $fp = @fopen($cachefile, "rb"); 58787c86ddaSAndreas Gohr if($fp) { 588d63e7fe7SGerrit Uitslag http_rangeRequest($fp, filesize($cachefile), 'application/pdf'); 58987c86ddaSAndreas Gohr } else { 59087c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 59187c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 59287c86ddaSAndreas Gohr } 5931ef68647SAndreas Gohr exit(); 5941ef68647SAndreas Gohr } 5951ef68647SAndreas Gohr 5966be736bfSGerrit Uitslag /** 5972eedf77dSAndreas Gohr * Load the various template files and prepare the HTML/CSS for insertion 59844e8e8fbSGerrit Uitslag * 59944e8e8fbSGerrit Uitslag * @return array 6001ef68647SAndreas Gohr */ 60103352761SKirsten Roschanski protected function load_template() { 6021ef68647SAndreas Gohr global $ID; 6031ef68647SAndreas Gohr global $conf; 6041ef68647SAndreas Gohr 6052eedf77dSAndreas Gohr // this is what we'll return 6062eedf77dSAndreas Gohr $output = array( 6071e45476bSmnapp 'cover' => '', 6082eedf77dSAndreas Gohr 'html' => '', 6092eedf77dSAndreas Gohr 'page' => '', 6102eedf77dSAndreas Gohr 'first' => '', 6112eedf77dSAndreas Gohr 'cite' => '', 6122eedf77dSAndreas Gohr ); 6132eedf77dSAndreas Gohr 6142eedf77dSAndreas Gohr // prepare header/footer elements 6152eedf77dSAndreas Gohr $html = ''; 61633c15297SGerrit Uitslag foreach(array('header', 'footer') as $section) { 61733c15297SGerrit Uitslag foreach(array('', '_odd', '_even', '_first') as $order) { 61802f9a447SGerrit Uitslag $file = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/' . $section . $order . '.html'; 61933c15297SGerrit Uitslag if(file_exists($file)) { 62033c15297SGerrit Uitslag $html .= '<htmlpage' . $section . ' name="' . $section . $order . '">' . DOKU_LF; 62133c15297SGerrit Uitslag $html .= file_get_contents($file) . DOKU_LF; 62233c15297SGerrit Uitslag $html .= '</htmlpage' . $section . '>' . DOKU_LF; 6232eedf77dSAndreas Gohr 6242eedf77dSAndreas Gohr // register the needed pseudo CSS 62533c15297SGerrit Uitslag if($order == '_first') { 62633c15297SGerrit Uitslag $output['first'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 62733c15297SGerrit Uitslag } elseif($order == '_even') { 62833c15297SGerrit Uitslag $output['page'] .= 'even-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 62933c15297SGerrit Uitslag } elseif($order == '_odd') { 63033c15297SGerrit Uitslag $output['page'] .= 'odd-' . $section . '-name: html_' . $section . $order . ';' . DOKU_LF; 631daa70883SAndreas Gohr } else { 63233c15297SGerrit Uitslag $output['page'] .= $section . ': html_' . $section . $order . ';' . DOKU_LF; 6332eedf77dSAndreas Gohr } 6342eedf77dSAndreas Gohr } 6352eedf77dSAndreas Gohr } 6362eedf77dSAndreas Gohr } 6372eedf77dSAndreas Gohr 6381ef68647SAndreas Gohr // prepare replacements 6391ef68647SAndreas Gohr $replace = array( 6401ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 64102f9a447SGerrit Uitslag '@PAGES@' => '{nbpg}', //see also $mpdf->pagenumSuffix = ' / ' 64203352761SKirsten Roschanski '@TITLE@' => hsc($this->title), 6431ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 6441ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 6451ef68647SAndreas Gohr '@DATE@' => dformat(time()), 6465d6fbaeaSAndreas Gohr '@BASE@' => DOKU_BASE, 64702f9a447SGerrit Uitslag '@TPLBASE@' => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/' 6481ef68647SAndreas Gohr ); 6491ef68647SAndreas Gohr 6502eedf77dSAndreas Gohr // set HTML element 651a180c973SKlap-in $html = str_replace(array_keys($replace), array_values($replace), $html); 652a180c973SKlap-in //TODO For bookcreator $ID (= bookmanager page) makes no sense 653a180c973SKlap-in $output['html'] = $this->page_depend_replacements($html, $ID); 6541ef68647SAndreas Gohr 6551e45476bSmnapp // cover page 65602f9a447SGerrit Uitslag $coverfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/cover.html'; 65733c15297SGerrit Uitslag if(file_exists($coverfile)) { 65833c15297SGerrit Uitslag $output['cover'] = file_get_contents($coverfile); 6591e45476bSmnapp $output['cover'] = str_replace(array_keys($replace), array_values($replace), $output['cover']); 6609b071da5SMichael $output['cover'] = $this->page_depend_replacements($output['cover'], $ID); 6616e2ec302SGerrit Uitslag $output['cover'] .= '<pagebreak />'; 6621e45476bSmnapp } 6631e45476bSmnapp 66433c15297SGerrit Uitslag // cover page 66502f9a447SGerrit Uitslag $backfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/back.html'; 66633c15297SGerrit Uitslag if(file_exists($backfile)) { 66733c15297SGerrit Uitslag $output['back'] = '<pagebreak />'; 66833c15297SGerrit Uitslag $output['back'] .= file_get_contents($backfile); 66933c15297SGerrit Uitslag $output['back'] = str_replace(array_keys($replace), array_values($replace), $output['back']); 6709b071da5SMichael $output['back'] = $this->page_depend_replacements($output['back'], $ID); 67133c15297SGerrit Uitslag } 67233c15297SGerrit Uitslag 6732eedf77dSAndreas Gohr // citation box 67402f9a447SGerrit Uitslag $citationfile = DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/citation.html'; 67533c15297SGerrit Uitslag if(file_exists($citationfile)) { 67633c15297SGerrit Uitslag $output['cite'] = file_get_contents($citationfile); 6772eedf77dSAndreas Gohr $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 6782eedf77dSAndreas Gohr } 6791ef68647SAndreas Gohr 6802eedf77dSAndreas Gohr return $output; 6811ef68647SAndreas Gohr } 6821ef68647SAndreas Gohr 6831ef68647SAndreas Gohr /** 684a180c973SKlap-in * @param string $raw code with placeholders 685a180c973SKlap-in * @param string $id pageid 686a180c973SKlap-in * @return string 687a180c973SKlap-in */ 688a180c973SKlap-in protected function page_depend_replacements($raw, $id) { 689e53f1ec0SSzymon Olewniczak global $REV, $DATE_AT; 690a180c973SKlap-in 691a180c973SKlap-in // generate qr code for this page using google infographics api 692a180c973SKlap-in $qr_code = ''; 693a180c973SKlap-in if($this->getConf('qrcodesize')) { 694a180c973SKlap-in $url = urlencode(wl($id, '', '&', true)); 695a180c973SKlap-in $qr_code = '<img src="https://chart.googleapis.com/chart?chs=' . 696a180c973SKlap-in $this->getConf('qrcodesize') . '&cht=qr&chl=' . $url . '" />'; 697a180c973SKlap-in } 698a180c973SKlap-in // prepare replacements 699a180c973SKlap-in $replace['@ID@'] = $id; 700a180c973SKlap-in $replace['@UPDATE@'] = dformat(filemtime(wikiFN($id, $REV))); 701e53f1ec0SSzymon Olewniczak 702e53f1ec0SSzymon Olewniczak $params = array(); 703e53f1ec0SSzymon Olewniczak if($DATE_AT) { 704e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 705e53f1ec0SSzymon Olewniczak } elseif($REV) { 706e53f1ec0SSzymon Olewniczak $params['rev'] = $REV; 707e53f1ec0SSzymon Olewniczak } 708e53f1ec0SSzymon Olewniczak $replace['@PAGEURL@'] = wl($id, $params, true, "&"); 709a180c973SKlap-in $replace['@QRCODE@'] = $qr_code; 710a180c973SKlap-in 711e3d68265SGerrit Uitslag $content = str_replace(array_keys($replace), array_values($replace), $raw); 712e3d68265SGerrit Uitslag 713e3d68265SGerrit Uitslag // @DATE(<date>[, <format>])@ 714e3d68265SGerrit Uitslag $content = preg_replace_callback( 715e3d68265SGerrit Uitslag '/@DATE\((.*?)(?:,\s*(.*?))?\)@/', 716e3d68265SGerrit Uitslag array($this, 'replacedate'), 717e3d68265SGerrit Uitslag $content 718e3d68265SGerrit Uitslag ); 719e3d68265SGerrit Uitslag 720e3d68265SGerrit Uitslag return $content; 721a180c973SKlap-in } 722a180c973SKlap-in 723e3d68265SGerrit Uitslag 724e3d68265SGerrit Uitslag /** 725e3d68265SGerrit Uitslag * (callback) Replace date by request datestring 726e3d68265SGerrit Uitslag * e.g. '%m(30-11-1975)' is replaced by '11' 727e3d68265SGerrit Uitslag * 728e3d68265SGerrit Uitslag * @param array $match with [0]=>whole match, [1]=> first subpattern, [2] => second subpattern 729e3d68265SGerrit Uitslag * @return string 730e3d68265SGerrit Uitslag */ 731e3d68265SGerrit Uitslag function replacedate($match) { 732e3d68265SGerrit Uitslag global $conf; 733e3d68265SGerrit Uitslag //no 2nd argument for default date format 734e3d68265SGerrit Uitslag if($match[2] == null) { 735e3d68265SGerrit Uitslag $match[2] = $conf['dformat']; 736e3d68265SGerrit Uitslag } 737e3d68265SGerrit Uitslag return strftime($match[2], strtotime($match[1])); 738e3d68265SGerrit Uitslag } 739e3d68265SGerrit Uitslag 740a180c973SKlap-in /** 7411c14c879SAndreas Gohr * Load all the style sheets and apply the needed replacements 7421ef68647SAndreas Gohr */ 7431c14c879SAndreas Gohr protected function load_css() { 744737417c6SKlap-in global $conf; 7451c14c879SAndreas Gohr //reusue the CSS dispatcher functions without triggering the main function 7461c14c879SAndreas Gohr define('SIMPLE_TEST', 1); 7471c14c879SAndreas Gohr require_once(DOKU_INC . 'lib/exe/css.php'); 748ee19bac3SLuigi Micco 7491c14c879SAndreas Gohr // prepare CSS files 7501c14c879SAndreas Gohr $files = array_merge( 7511c14c879SAndreas Gohr array( 7521c14c879SAndreas Gohr DOKU_INC . 'lib/styles/screen.css' 7531c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7541c14c879SAndreas Gohr DOKU_INC . 'lib/styles/print.css' 7551c14c879SAndreas Gohr => DOKU_BASE . 'lib/styles/', 7561c14c879SAndreas Gohr ), 7571c14c879SAndreas Gohr css_pluginstyles('all'), 75858e6409eSAndreas Gohr $this->css_pluginPDFstyles(), 7591c14c879SAndreas Gohr array( 7601c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.css' 7611c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7621c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/tpl/' . $this->tpl . '/style.css' 7631c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/tpl/' . $this->tpl . '/', 7641c14c879SAndreas Gohr DOKU_PLUGIN . 'dw2pdf/conf/style.local.css' 7651c14c879SAndreas Gohr => DOKU_BASE . 'lib/plugins/dw2pdf/conf/', 7661c14c879SAndreas Gohr ) 7671c14c879SAndreas Gohr ); 7681c14c879SAndreas Gohr $css = ''; 7691c14c879SAndreas Gohr foreach($files as $file => $location) { 77028e636eaSGerrit Uitslag $display = str_replace(fullpath(DOKU_INC), '', fullpath($file)); 77128e636eaSGerrit Uitslag $css .= "\n/* XXXXXXXXX $display XXXXXXXXX */\n"; 7721c14c879SAndreas Gohr $css .= css_loadfile($file, $location); 7731ef68647SAndreas Gohr } 7741ef68647SAndreas Gohr 77528e636eaSGerrit Uitslag if(function_exists('css_parseless')) { 7761c14c879SAndreas Gohr // apply pattern replacements 77728e636eaSGerrit Uitslag $styleini = css_styleini($conf['template']); 77828e636eaSGerrit Uitslag $css = css_applystyle($css, $styleini['replacements']); 77928e636eaSGerrit Uitslag 78028e636eaSGerrit Uitslag // parse less 78128e636eaSGerrit Uitslag $css = css_parseless($css); 78228e636eaSGerrit Uitslag } else { 78328e636eaSGerrit Uitslag // @deprecated 2013-12-19: fix backward compatibility 7841c14c879SAndreas Gohr $css = css_applystyle($css, DOKU_INC . 'lib/tpl/' . $conf['template'] . '/'); 78528e636eaSGerrit Uitslag } 7861ef68647SAndreas Gohr 7871c14c879SAndreas Gohr return $css; 788ee19bac3SLuigi Micco } 7891c14c879SAndreas Gohr 79058e6409eSAndreas Gohr /** 79158e6409eSAndreas Gohr * Returns a list of possible Plugin PDF Styles 79258e6409eSAndreas Gohr * 79358e6409eSAndreas Gohr * Checks for a pdf.css, falls back to print.css 79458e6409eSAndreas Gohr * 79558e6409eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 79658e6409eSAndreas Gohr */ 7976be736bfSGerrit Uitslag protected function css_pluginPDFstyles() { 79858e6409eSAndreas Gohr $list = array(); 79958e6409eSAndreas Gohr $plugins = plugin_list(); 800f54b51f7SAndreas Gohr 801f54b51f7SAndreas Gohr $usestyle = explode(',', $this->getConf('usestyles')); 80258e6409eSAndreas Gohr foreach($plugins as $p) { 803f54b51f7SAndreas Gohr if(in_array($p, $usestyle)) { 804f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/screen.css"] = DOKU_BASE . "lib/plugins/$p/"; 805f54b51f7SAndreas Gohr $list[DOKU_PLUGIN . "$p/style.css"] = DOKU_BASE . "lib/plugins/$p/"; 806f54b51f7SAndreas Gohr } 807f54b51f7SAndreas Gohr 80858e6409eSAndreas Gohr if(file_exists(DOKU_PLUGIN . "$p/pdf.css")) { 80958e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/pdf.css"] = DOKU_BASE . "lib/plugins/$p/"; 81058e6409eSAndreas Gohr } else { 81158e6409eSAndreas Gohr $list[DOKU_PLUGIN . "$p/print.css"] = DOKU_BASE . "lib/plugins/$p/"; 81258e6409eSAndreas Gohr } 81358e6409eSAndreas Gohr } 81458e6409eSAndreas Gohr return $list; 81558e6409eSAndreas Gohr } 816ad18f4e1SGerrit Uitslag 817ad18f4e1SGerrit Uitslag /** 81860e59de7SGerrit Uitslag * Returns array of pages which will be included in the exported pdf 81960e59de7SGerrit Uitslag * 82060e59de7SGerrit Uitslag * @return array 82160e59de7SGerrit Uitslag */ 82260e59de7SGerrit Uitslag public function getExportedPages() { 82360e59de7SGerrit Uitslag return $this->list; 82460e59de7SGerrit Uitslag } 82560e59de7SGerrit Uitslag 82660e59de7SGerrit Uitslag /** 827ad18f4e1SGerrit Uitslag * usort callback to sort by file lastmodified time 82844e8e8fbSGerrit Uitslag * 82944e8e8fbSGerrit Uitslag * @param array $a 83044e8e8fbSGerrit Uitslag * @param array $b 83144e8e8fbSGerrit Uitslag * @return int 832ad18f4e1SGerrit Uitslag */ 833ad18f4e1SGerrit Uitslag public function _datesort($a, $b) { 834ad18f4e1SGerrit Uitslag if($b['rev'] < $a['rev']) return -1; 835ad18f4e1SGerrit Uitslag if($b['rev'] > $a['rev']) return 1; 836ad18f4e1SGerrit Uitslag return strcmp($b['id'], $a['id']); 837ad18f4e1SGerrit Uitslag } 838ad18f4e1SGerrit Uitslag 839ad18f4e1SGerrit Uitslag /** 840ad18f4e1SGerrit Uitslag * usort callback to sort by page id 84144e8e8fbSGerrit Uitslag * @param array $a 84244e8e8fbSGerrit Uitslag * @param array $b 84344e8e8fbSGerrit Uitslag * @return int 844ad18f4e1SGerrit Uitslag */ 845ad18f4e1SGerrit Uitslag public function _pagenamesort($a, $b) { 846ad18f4e1SGerrit Uitslag if($a['id'] <= $b['id']) return -1; 847ad18f4e1SGerrit Uitslag if($a['id'] > $b['id']) return 1; 848ad18f4e1SGerrit Uitslag return 0; 849ad18f4e1SGerrit Uitslag } 85026be4eceSGerrit Uitslag 85126be4eceSGerrit Uitslag /** 85202f9a447SGerrit Uitslag * Return settings read from: 85302f9a447SGerrit Uitslag * 1. url parameters 85402f9a447SGerrit Uitslag * 2. plugin config 85502f9a447SGerrit Uitslag * 3. global config 85602f9a447SGerrit Uitslag * 85702f9a447SGerrit Uitslag * @return array 85802f9a447SGerrit Uitslag */ 85902f9a447SGerrit Uitslag protected function loadExportConfig() { 86002f9a447SGerrit Uitslag global $INPUT; 86102f9a447SGerrit Uitslag global $conf; 86202f9a447SGerrit Uitslag 86302f9a447SGerrit Uitslag $this->exportConfig = array(); 86402f9a447SGerrit Uitslag 86502f9a447SGerrit Uitslag // decide on the paper setup from param or config 86602f9a447SGerrit Uitslag $this->exportConfig['pagesize'] = $INPUT->str('pagesize', $this->getConf('pagesize'), true); 86702f9a447SGerrit Uitslag $this->exportConfig['orientation'] = $INPUT->str('orientation', $this->getConf('orientation'), true); 86802f9a447SGerrit Uitslag 8694870b378SLarsDW223 // decide on the font-size from param or config 8704870b378SLarsDW223 $this->exportConfig['font-size'] = $INPUT->str('font-size', $this->getConf('font-size'), true); 8714870b378SLarsDW223 872213fdb75SGerrit Uitslag $doublesided = $INPUT->bool('doublesided', (bool) $this->getConf('doublesided')); 873213fdb75SGerrit Uitslag $this->exportConfig['doublesided'] = $doublesided ? '1' : '0'; 874213fdb75SGerrit Uitslag 875213fdb75SGerrit Uitslag $hasToC = $INPUT->bool('toc', (bool) $this->getConf('toc')); 87602f9a447SGerrit Uitslag $levels = array(); 87702f9a447SGerrit Uitslag if($hasToC) { 87802f9a447SGerrit Uitslag $toclevels = $INPUT->str('toclevels', $this->getConf('toclevels'), true); 87902f9a447SGerrit Uitslag list($top_input, $max_input) = explode('-', $toclevels, 2); 88002f9a447SGerrit Uitslag list($top_conf, $max_conf) = explode('-', $this->getConf('toclevels'), 2); 88102f9a447SGerrit Uitslag $bounds_input = array( 88202f9a447SGerrit Uitslag 'top' => array( 88302f9a447SGerrit Uitslag (int) $top_input, 88402f9a447SGerrit Uitslag (int) $top_conf 88502f9a447SGerrit Uitslag ), 88602f9a447SGerrit Uitslag 'max' => array( 88702f9a447SGerrit Uitslag (int) $max_input, 88802f9a447SGerrit Uitslag (int) $max_conf 88902f9a447SGerrit Uitslag ) 89002f9a447SGerrit Uitslag ); 89102f9a447SGerrit Uitslag $bounds = array( 89202f9a447SGerrit Uitslag 'top' => $conf['toptoclevel'], 89302f9a447SGerrit Uitslag 'max' => $conf['maxtoclevel'] 89402f9a447SGerrit Uitslag 89502f9a447SGerrit Uitslag ); 89602f9a447SGerrit Uitslag foreach($bounds_input as $bound => $values) { 89702f9a447SGerrit Uitslag foreach($values as $value) { 89802f9a447SGerrit Uitslag if($value > 0 && $value <= 5) { 89902f9a447SGerrit Uitslag //stop at valid value and store 90002f9a447SGerrit Uitslag $bounds[$bound] = $value; 90102f9a447SGerrit Uitslag break; 90202f9a447SGerrit Uitslag } 90302f9a447SGerrit Uitslag } 90402f9a447SGerrit Uitslag } 90502f9a447SGerrit Uitslag 90602f9a447SGerrit Uitslag if($bounds['max'] < $bounds['top']) { 90702f9a447SGerrit Uitslag $bounds['max'] = $bounds['top']; 90802f9a447SGerrit Uitslag } 90902f9a447SGerrit Uitslag 91002f9a447SGerrit Uitslag for($level = $bounds['top']; $level <= $bounds['max']; $level++) { 91102f9a447SGerrit Uitslag $levels["H$level"] = $level - 1; 91202f9a447SGerrit Uitslag } 91302f9a447SGerrit Uitslag } 91402f9a447SGerrit Uitslag $this->exportConfig['hasToC'] = $hasToC; 91502f9a447SGerrit Uitslag $this->exportConfig['levels'] = $levels; 91602f9a447SGerrit Uitslag 91702f9a447SGerrit Uitslag $this->exportConfig['maxbookmarks'] = $INPUT->int('maxbookmarks', $this->getConf('maxbookmarks'), true); 91802f9a447SGerrit Uitslag 91902f9a447SGerrit Uitslag $tplconf = $this->getConf('template'); 92005d2b507SGerrit Uitslag $tpl = $INPUT->str('tpl', $tplconf, true); 92102f9a447SGerrit Uitslag if(!is_dir(DOKU_PLUGIN . 'dw2pdf/tpl/' . $tpl)) { 92202f9a447SGerrit Uitslag $tpl = $tplconf; 92302f9a447SGerrit Uitslag } 92402f9a447SGerrit Uitslag if(!$tpl){ 92502f9a447SGerrit Uitslag $tpl = 'default'; 92602f9a447SGerrit Uitslag } 92702f9a447SGerrit Uitslag $this->exportConfig['template'] = $tpl; 92802f9a447SGerrit Uitslag 92902f9a447SGerrit Uitslag $this->exportConfig['isDebug'] = $conf['allowdebug'] && $INPUT->has('debughtml'); 93002f9a447SGerrit Uitslag } 93102f9a447SGerrit Uitslag 93202f9a447SGerrit Uitslag /** 93302f9a447SGerrit Uitslag * Returns requested config 93402f9a447SGerrit Uitslag * 93502f9a447SGerrit Uitslag * @param string $name 93602f9a447SGerrit Uitslag * @param mixed $notset 93702f9a447SGerrit Uitslag * @return mixed|bool 93802f9a447SGerrit Uitslag */ 93902f9a447SGerrit Uitslag public function getExportConfig($name, $notset = false) { 94002f9a447SGerrit Uitslag if ($this->exportConfig === null){ 94102f9a447SGerrit Uitslag $this->loadExportConfig(); 94202f9a447SGerrit Uitslag } 94302f9a447SGerrit Uitslag 94402f9a447SGerrit Uitslag if(isset($this->exportConfig[$name])){ 94502f9a447SGerrit Uitslag return $this->exportConfig[$name]; 94602f9a447SGerrit Uitslag }else{ 94702f9a447SGerrit Uitslag return $notset; 94802f9a447SGerrit Uitslag } 94902f9a447SGerrit Uitslag } 950d63e7fe7SGerrit Uitslag 951d63e7fe7SGerrit Uitslag /** 952d63e7fe7SGerrit Uitslag * Add 'export pdf'-button to pagetools 953d63e7fe7SGerrit Uitslag * 954d63e7fe7SGerrit Uitslag * @param Doku_Event $event 955d63e7fe7SGerrit Uitslag */ 95644e8e8fbSGerrit Uitslag public function addbutton(Doku_Event $event) { 957e53f1ec0SSzymon Olewniczak global $ID, $REV, $DATE_AT; 958d63e7fe7SGerrit Uitslag 959d63e7fe7SGerrit Uitslag if($this->getConf('showexportbutton') && $event->data['view'] == 'main') { 960d63e7fe7SGerrit Uitslag $params = array('do' => 'export_pdf'); 961e53f1ec0SSzymon Olewniczak if($DATE_AT) { 962e53f1ec0SSzymon Olewniczak $params['at'] = $DATE_AT; 963e53f1ec0SSzymon Olewniczak } elseif($REV) { 964d63e7fe7SGerrit Uitslag $params['rev'] = $REV; 965d63e7fe7SGerrit Uitslag } 966d63e7fe7SGerrit Uitslag 967d63e7fe7SGerrit Uitslag // insert button at position before last (up to top) 968d63e7fe7SGerrit Uitslag $event->data['items'] = array_slice($event->data['items'], 0, -1, true) + 969d63e7fe7SGerrit Uitslag array('export_pdf' => 970d63e7fe7SGerrit Uitslag '<li>' 97172cadc31SChristian Paul . '<a href="' . wl($ID, $params) . '" class="action export_pdf" rel="nofollow" title="' . $this->getLang('export_pdf_button') . '">' 972d63e7fe7SGerrit Uitslag . '<span>' . $this->getLang('export_pdf_button') . '</span>' 973d63e7fe7SGerrit Uitslag . '</a>' 974d63e7fe7SGerrit Uitslag . '</li>' 975d63e7fe7SGerrit Uitslag ) + 976d63e7fe7SGerrit Uitslag array_slice($event->data['items'], -1, 1, true); 977d63e7fe7SGerrit Uitslag } 978d63e7fe7SGerrit Uitslag } 979ee19bac3SLuigi Micco} 980