1<?php 2 /** 3 * dw2Pdf Plugin: Conversion from dokuwiki content to pdf. 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Luigi Micco <l.micco@tiscali.it> 7 * @author Andreas Gohr <andi@splitbrain.org> 8 */ 9 10// must be run within Dokuwiki 11if (!defined('DOKU_INC')) die(); 12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 13 14class action_plugin_dw2pdf extends DokuWiki_Action_Plugin { 15 16 private $tpl; 17 18 /** 19 * Constructor. Sets the correct template 20 */ 21 function __construct(){ 22 $tpl; 23 if(isset($_REQUEST['tpl'])){ 24 $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/','',$_REQUEST['tpl'])); 25 } 26 if(!$tpl) $tpl = $this->getConf('template'); 27 if(!$tpl) $tpl = 'default'; 28 if(!is_dir(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl)) $tpl = 'default'; 29 30 $this->tpl = $tpl; 31 } 32 33 /** 34 * Register the events 35 */ 36 function register(&$controller) { 37 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array()); 38 } 39 40 /** 41 * Do the HTML to PDF conversion work 42 */ 43 function convert(&$event, $param) { 44 global $ACT; 45 global $REV; 46 global $ID; 47 global $conf; 48 49 // our event? 50 if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false; 51 52 // check user's rights 53 if ( auth_quickaclcheck($ID) < AUTH_READ ) return false; 54 55 // it's ours, no one else's 56 $event->preventDefault(); 57 58 // one or multiple pages? 59 $list = array(); 60 if ( $ACT == 'export_pdf' ) { 61 $list[0] = $ID; 62 } elseif (isset($_COOKIE['list-pagelist'])) { 63 $list = explode("|", $_COOKIE['list-pagelist']); 64 } 65 66 // prepare cache 67 $cache = new cache(join(',',$list).$REV.$this->tpl,'.dw2.pdf'); 68 $depends['files'] = array_map('wikiFN',$list); 69 $depends['files'][] = __FILE__; 70 $depends['files'][] = dirname(__FILE__).'/renderer.php'; 71 $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php'; 72 73 // hard work only when no cache available 74 if(!$this->getConf('usecache') || !$cache->useCache($depends)){ 75 // initialize PDF library 76 require_once(dirname(__FILE__)."/DokuPDF.class.php"); 77 $mpdf = new DokuPDF(); 78 79 // let mpdf fix local links 80 $self = parse_url(DOKU_URL); 81 $url = $self['scheme'].'://'.$self['host']; 82 if($self['port']) $url .= ':'.$port; 83 $mpdf->setBasePath($url); 84 85 // Set the title 86 $title = $_GET['pdfbook_title']; 87 if(!$title) $title = p_get_first_heading($ID); 88 $mpdf->SetTitle($title); 89 90 // some default settings 91 $mpdf->mirrorMargins = 1; 92 $mpdf->useOddEven = 1; 93 $mpdf->setAutoTopMargin = 'stretch'; 94 $mpdf->setAutoBottomMargin = 'stretch'; 95 96 // load the template 97 $template = $this->load_template($title); 98 99 // prepare HTML header styles 100 $html = '<html><head>'; 101 $html .= '<style>'; 102 $html .= $this->load_css(); 103 $html .= '@page { size:auto; '.$template['page'].'}'; 104 $html .= '@page :first {'.$template['first'].'}'; 105 $html .= '@page :last {'.$template['last'].'}'; 106 $html .= $template['css']; 107 $html .= '</style>'; 108 $html .= '</head><body>'; 109 $html .= $template['html']; 110 $html .= '<div class="dokuwiki">'; 111 112 // loop over all pages 113 $cnt = count($list); 114 for($n=0; $n<$cnt; $n++){ 115 $page = $list[$n]; 116 117 $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page); 118 $html .= $template['cite']; 119 if ($n < ($cnt - 1)){ 120 $html .= '<pagebreak />'; 121 } 122 } 123 124 $html .= '</div>'; 125 $mpdf->WriteHTML($html); 126 127 // write to cache file 128 $mpdf->Output($cache->cache, 'F'); 129 } 130 131 // deliver the file 132 header('Content-Type: application/pdf'); 133 header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT'); 134 header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600)); 135 header('Pragma: public'); 136 http_conditionalRequest(filemtime($cache->cache)); 137 138 if($this->getConf('output') == 'file'){ 139 header('Content-Disposition: attachment; filename="'.rawurlencode($title).'.pdf";'); 140 }else{ 141 header('Content-Disposition: inline; filename="'.rawurlencode($title).'.pdf";'); 142 } 143 144 if (http_sendfile($cache->cache)) exit; 145 146 $fp = @fopen($cache->cache,"rb"); 147 if($fp){ 148 http_rangeRequest($fp,filesize($cache->cache),'application/pdf'); 149 }else{ 150 header("HTTP/1.0 500 Internal Server Error"); 151 print "Could not read file - bad permissions?"; 152 } 153 exit(); 154 } 155 156 157 /** 158 * Load the various template files and prepare the HTML/CSS for insertion 159 */ 160 protected function load_template($title){ 161 global $ID; 162 global $REV; 163 global $conf; 164 $tpl = $this->tpl; 165 166 // this is what we'll return 167 $output = array( 168 'html' => '', 169 'css' => '', 170 'page' => '', 171 'first' => '', 172 'last' => '', 173 'cite' => '', 174 'oe' => 0 175 ); 176 177 // prepare header/footer elements 178 $html = ''; 179 foreach(array('header','footer') as $t){ 180 foreach(array('','_odd','_even','_first','_last') as $h){ 181 if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){ 182 $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF; 183 $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF; 184 $html .= '</htmlpage'.$t.'>'.DOKU_LF; 185 186 // register the needed pseudo CSS 187 if($h == '_last'){ 188 $output['last'] .= $t.': html_'.$t.$h.';'.DOKU_LF; 189 }elseif($h == '_first'){ 190 $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF; 191 }elseif($h == '_even'){ 192 $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF; 193 }elseif($h == '_odd'){ 194 $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF; 195 }else{ 196 $output['page'] .= $t.': html_'.$t.$h.';'.DOKU_LF; 197 } 198 } 199 } 200 } 201 202 // prepare replacements 203 $replace = array( 204 '@ID@' => $ID, 205 '@PAGE@' => '{PAGENO}', 206 '@PAGES@' => '{nb}', 207 '@TITLE@' => hsc($title), 208 '@WIKI@' => $conf['title'], 209 '@WIKIURL@' => DOKU_URL, 210 '@UPDATE@' => dformat(filemtime(wikiFN($ID,$REV))), 211 '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"), 212 '@DATE@' => dformat(time()), 213 '@BASE@' => DOKU_BASE, 214 '@TPLBASE@' => DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/', 215 ); 216 217 // set HTML element 218 $output['html'] = str_replace(array_keys($replace), array_values($replace), $html); 219 220 // citation box 221 if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){ 222 $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html'); 223 $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 224 } 225 226 // set custom styles 227 if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css')){ 228 $output['css'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css'); 229 } 230 231 return $output; 232 } 233 234 /** 235 * Load all the style sheets and apply the needed replacements 236 */ 237 protected function load_css(){ 238 //reusue the CSS dispatcher functions without triggering the main function 239 define('SIMPLE_TEST',1); 240 require_once(DOKU_INC.'lib/exe/css.php'); 241 242 // prepare CSS files 243 $files = array_merge( 244 array( 245 DOKU_INC.'lib/styles/screen.css' 246 => DOKU_BASE.'lib/styles/', 247 DOKU_INC.'lib/styles/print.css' 248 => DOKU_BASE.'lib/styles/', 249 ), 250 css_pluginstyles('all'), 251 css_pluginstyles('print'), 252 css_pluginstyles('pdf'), 253 array( 254 DOKU_PLUGIN.'dw2pdf/conf/style.css' 255 => DOKU_BASE.'lib/plugins/dw2pdf/conf/', 256 DOKU_PLUGIN.'dw2pdf/tpl/'.$this->tpl.'/style.css' 257 => DOKU_BASE.'lib/plugins/dw2pdf/tpl/'.$this->tpl.'/', 258 DOKU_PLUGIN.'dw2pdf/conf/style.local.css' 259 => DOKU_BASE.'lib/plugins/dw2pdf/conf/', 260 ) 261 ); 262 263 $css = ''; 264 foreach($files as $file => $location){ 265 $css .= css_loadfile($file, $location); 266 } 267 268 // apply pattern replacements 269 $css = css_applystyle($css,DOKU_INC.'lib/tpl/'.$conf['template'].'/'); 270 271 return $css; 272 } 273 274} 275