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