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 Miccoif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 13ee19bac3SLuigi Micco 141ef68647SAndreas Gohrclass action_plugin_dw2pdf extends DokuWiki_Action_Plugin { 15ee19bac3SLuigi Micco 16ee19bac3SLuigi Micco /** 17ee19bac3SLuigi Micco * Register the events 18ee19bac3SLuigi Micco */ 191ef68647SAndreas Gohr function register(&$controller) { 20ee19bac3SLuigi Micco $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array()); 21ee19bac3SLuigi Micco } 22ee19bac3SLuigi Micco 231ef68647SAndreas Gohr function convert(&$event, $param) { 24ee19bac3SLuigi Micco global $ACT; 25ee19bac3SLuigi Micco global $REV; 26ee19bac3SLuigi Micco global $ID; 27ee19bac3SLuigi Micco global $conf; 28ee19bac3SLuigi Micco 291ef68647SAndreas Gohr // our event? 301ef68647SAndreas Gohr if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false; 31ee19bac3SLuigi Micco 321ef68647SAndreas Gohr // check user's rights 331ef68647SAndreas Gohr if ( auth_quickaclcheck($ID) < AUTH_READ ) return false; 341ef68647SAndreas Gohr 351ef68647SAndreas Gohr // it's ours, no one else's 36ee19bac3SLuigi Micco $event->preventDefault(); 37ee19bac3SLuigi Micco 3887c86ddaSAndreas Gohr // one or multiple pages? 3987c86ddaSAndreas Gohr $list = array(); 4087c86ddaSAndreas Gohr if ( $ACT == 'export_pdf' ) { 4187c86ddaSAndreas Gohr $list[0] = $ID; 4287c86ddaSAndreas Gohr } elseif (isset($_COOKIE['list-pagelist'])) { 4387c86ddaSAndreas Gohr $list = explode("|", $_COOKIE['list-pagelist']); 4487c86ddaSAndreas Gohr } 4587c86ddaSAndreas Gohr 4687c86ddaSAndreas Gohr // prepare cache 4787c86ddaSAndreas Gohr $cache = new cache(join(',',$list).$REV,'.dw2.pdf'); 4887c86ddaSAndreas Gohr $depends['files'] = array_map('wikiFN',$list); 4987c86ddaSAndreas Gohr $depends['files'][] = __FILE__; 5087c86ddaSAndreas Gohr $depends['files'][] = dirname(__FILE__).'/renderer.php'; 5187c86ddaSAndreas Gohr $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php'; 5287c86ddaSAndreas Gohr 5387c86ddaSAndreas Gohr 5487c86ddaSAndreas Gohr if(!$this->getConf('usecache') || !$cache->useCache($depends)){ 551ef68647SAndreas Gohr // initialize PDF library 56cde5a1b3SAndreas Gohr require_once(dirname(__FILE__)."/DokuPDF.class.php"); 571ef68647SAndreas Gohr $mpdf = new DokuPDF(); 58ee19bac3SLuigi Micco 59d62df65bSAndreas Gohr // let mpdf fix local links 60d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 61d62df65bSAndreas Gohr $url = $self['scheme'].'://'.$self['host']; 62d62df65bSAndreas Gohr if($self['port']) $url .= ':'.$port; 63d62df65bSAndreas Gohr $mpdf->setBasePath($url); 64d62df65bSAndreas Gohr 651ef68647SAndreas Gohr // some default settings 66ee19bac3SLuigi Micco $mpdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins 67*2eedf77dSAndreas Gohr $mdpf->useOddEven = 1; 68*2eedf77dSAndreas Gohr/* $mpdf->defaultheaderfontsize = 8; // in pts 691ef68647SAndreas Gohr $mpdf->defaultheaderfontstyle = ''; // blank, B, I, or BI 701ef68647SAndreas Gohr $mpdf->defaultheaderline = 1; // 1 to include line below header/above footer 711ef68647SAndreas Gohr $mpdf->defaultfooterfontsize = 8; // in pts 721ef68647SAndreas Gohr $mpdf->defaultfooterfontstyle = ''; // blank, B, I, or BI 731ef68647SAndreas Gohr $mpdf->defaultfooterline = 1; // 1 to include line below header/above footer 74*2eedf77dSAndreas Gohr*/ 75*2eedf77dSAndreas Gohr 76*2eedf77dSAndreas Gohr // title 77*2eedf77dSAndreas Gohr// $mpdf->SetTitle($title); //FIXME 78*2eedf77dSAndreas Gohr 79*2eedf77dSAndreas Gohr $template = $this->load_template('default'); //FIXME 80ee19bac3SLuigi Micco 811ef68647SAndreas Gohr // prepare HTML header styles 82ee19bac3SLuigi Micco $html = '<html><head>'; 831ef68647SAndreas Gohr $html .= '<style>'; 8483dae0dbSAndreas Gohr $html .= file_get_contents(DOKU_INC.'lib/styles/screen.css'); 8583dae0dbSAndreas Gohr $html .= file_get_contents(DOKU_INC.'lib/styles/print.css'); 861ef68647SAndreas Gohr $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css'); 871ef68647SAndreas Gohr $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css'); 88*2eedf77dSAndreas Gohr $html .= '@page {'.$template['page'].'}'; 89*2eedf77dSAndreas Gohr $html .= '@page :first {'.$template['first'].'}'; 90*2eedf77dSAndreas Gohr $html .= '@page :last {'.$template['last'].'}'; 91*2eedf77dSAndreas Gohr $html .= $template['css']; 921ef68647SAndreas Gohr $html .= '</style>'; 931ef68647SAndreas Gohr $html .= '</head><body>'; 94*2eedf77dSAndreas Gohr $html .= $template['html']; 95*2eedf77dSAndreas Gohr 96ee19bac3SLuigi Micco 971ef68647SAndreas Gohr // set headers/footers 98*2eedf77dSAndreas Gohr // $this->prepare_headers($mpdf); 99ee19bac3SLuigi Micco 1001ef68647SAndreas Gohr // loop over all pages 1011ef68647SAndreas Gohr $cnt = count($list); 1021ef68647SAndreas Gohr for($n=0; $n<$cnt; $n++){ 103ee19bac3SLuigi Micco $page = $list[$n]; 104ee19bac3SLuigi Micco 105a876b55bSAndreas Gohr $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page); 106*2eedf77dSAndreas Gohr $html .= $template['cite']; 1071ef68647SAndreas Gohr if ($n < ($cnt - 1)){ 1081ef68647SAndreas Gohr $html .= '<pagebreak />'; 1091ef68647SAndreas Gohr } 110ee19bac3SLuigi Micco } 111ee19bac3SLuigi Micco 1121ef68647SAndreas Gohr $this->arrangeHtml($html, $this->getConf("norender")); 113ee19bac3SLuigi Micco $mpdf->WriteHTML($html); 114a06728a6SAndreas Gohr 11587c86ddaSAndreas Gohr // write to cache file 11687c86ddaSAndreas Gohr $mpdf->Output($cache->cache, 'F'); 11787c86ddaSAndreas Gohr } 11887c86ddaSAndreas Gohr 11987c86ddaSAndreas Gohr // deliver the file 12087c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 12187c86ddaSAndreas Gohr header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT'); 12287c86ddaSAndreas Gohr header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600)); 12387c86ddaSAndreas Gohr header('Pragma: public'); 12487c86ddaSAndreas Gohr http_conditionalRequest(filemtime($cache->cache)); 12587c86ddaSAndreas Gohr 126cde5a1b3SAndreas Gohr $title = $_GET['pdfbook_title']; 127cde5a1b3SAndreas Gohr if(!$title) $title = noNS($ID); 12887c86ddaSAndreas Gohr if($this->getConf('output') == 'file'){ 12987c86ddaSAndreas Gohr header('Content-Disposition: attachment; filename="'.urlencode($title).'.pdf";'); 13087c86ddaSAndreas Gohr }else{ 13187c86ddaSAndreas Gohr header('Content-Disposition: inline; filename="'.urlencode($title).'.pdf";'); 13287c86ddaSAndreas Gohr } 133ee19bac3SLuigi Micco 13487c86ddaSAndreas Gohr if (http_sendfile($cache->cache)) exit; 13587c86ddaSAndreas Gohr 13687c86ddaSAndreas Gohr $fp = @fopen($cache->cache,"rb"); 13787c86ddaSAndreas Gohr if($fp){ 13887c86ddaSAndreas Gohr http_rangeRequest($fp,filesize($cache->cache),'application/pdf'); 13987c86ddaSAndreas Gohr }else{ 14087c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 14187c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 14287c86ddaSAndreas Gohr } 1431ef68647SAndreas Gohr exit(); 1441ef68647SAndreas Gohr } 1451ef68647SAndreas Gohr 1461ef68647SAndreas Gohr /** 147*2eedf77dSAndreas Gohr * Load the various template files and prepare the HTML/CSS for insertion 1481ef68647SAndreas Gohr */ 149*2eedf77dSAndreas Gohr protected function load_template($tpl){ 1501ef68647SAndreas Gohr global $ID; 1511ef68647SAndreas Gohr global $REV; 1521ef68647SAndreas Gohr global $conf; 1531ef68647SAndreas Gohr 154*2eedf77dSAndreas Gohr // this is what we'll return 155*2eedf77dSAndreas Gohr $output = array( 156*2eedf77dSAndreas Gohr 'html' => '', 157*2eedf77dSAndreas Gohr 'css' => '', 158*2eedf77dSAndreas Gohr 'page' => '', 159*2eedf77dSAndreas Gohr 'first' => '', 160*2eedf77dSAndreas Gohr 'last' => '', 161*2eedf77dSAndreas Gohr 'cite' => '', 162*2eedf77dSAndreas Gohr ); 163*2eedf77dSAndreas Gohr 164*2eedf77dSAndreas Gohr // prepare header/footer elements 165*2eedf77dSAndreas Gohr $html = ''; 166*2eedf77dSAndreas Gohr foreach(array('header','footer') as $t){ 167*2eedf77dSAndreas Gohr foreach(array('','_odd','_even','_first','_last') as $h){ 168*2eedf77dSAndreas Gohr if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){ 169*2eedf77dSAndreas Gohr $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF; 170*2eedf77dSAndreas Gohr $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF; 171*2eedf77dSAndreas Gohr $html .= '</htmlpage'.$t.'>'.DOKU_LF; 172*2eedf77dSAndreas Gohr 173*2eedf77dSAndreas Gohr // register the needed pseudo CSS 174*2eedf77dSAndreas Gohr if($h == '_last'){ 175*2eedf77dSAndreas Gohr $output['last'] .= $t.': html_'.$t.$h.';'.DOKU_LF; 176*2eedf77dSAndreas Gohr }elseif($h == '_first'){ 177*2eedf77dSAndreas Gohr $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF; 178*2eedf77dSAndreas Gohr }elseif($h == '_even'){ 179*2eedf77dSAndreas Gohr $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF; 180*2eedf77dSAndreas Gohr }else{ 181*2eedf77dSAndreas Gohr $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF; 182*2eedf77dSAndreas Gohr } 183*2eedf77dSAndreas Gohr } 184*2eedf77dSAndreas Gohr } 185*2eedf77dSAndreas Gohr } 186*2eedf77dSAndreas Gohr 187*2eedf77dSAndreas Gohr // fixme move title to function params 1881ef68647SAndreas Gohr if($_GET['pdfbook_title']){ 1891ef68647SAndreas Gohr $title = $_GET['pdfbook_title']; 1901ef68647SAndreas Gohr }else{ 1911ef68647SAndreas Gohr $title = p_get_first_heading($ID); 1921ef68647SAndreas Gohr } 1931ef68647SAndreas Gohr if(!$title) $title = noNS($ID); 1941ef68647SAndreas Gohr 1951ef68647SAndreas Gohr // prepare replacements 1961ef68647SAndreas Gohr $replace = array( 1971ef68647SAndreas Gohr '@ID@' => $ID, 1981ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 1991ef68647SAndreas Gohr '@PAGES@' => '{nb}', 200*2eedf77dSAndreas Gohr '@TITLE@' => hsc($title), 2011ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 2021ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 2033ae7a8dfSAndreas Gohr '@UPDATE@' => dformat(filemtime(wikiFN($ID,$REV))), 2043ae7a8dfSAndreas Gohr '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"), 2051ef68647SAndreas Gohr '@DATE@' => dformat(time()), 2061ef68647SAndreas Gohr ); 2071ef68647SAndreas Gohr 208*2eedf77dSAndreas Gohr // set HTML element 209*2eedf77dSAndreas Gohr $output['html'] = str_replace(array_keys($replace), array_values($replace), $html); 2101ef68647SAndreas Gohr 211*2eedf77dSAndreas Gohr // citation box 212*2eedf77dSAndreas Gohr if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){ 213*2eedf77dSAndreas Gohr $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html'); 214*2eedf77dSAndreas Gohr $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 215*2eedf77dSAndreas Gohr } 2161ef68647SAndreas Gohr 217*2eedf77dSAndreas Gohr // set custom styles 218*2eedf77dSAndreas Gohr if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css')){ 219*2eedf77dSAndreas Gohr $output['css'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css'); 220*2eedf77dSAndreas Gohr } 221*2eedf77dSAndreas Gohr 222*2eedf77dSAndreas Gohr return $output; 2231ef68647SAndreas Gohr } 2241ef68647SAndreas Gohr 2251ef68647SAndreas Gohr /** 2261ef68647SAndreas Gohr * Fix up the HTML a bit 2271ef68647SAndreas Gohr * 228a876b55bSAndreas Gohr * FIXME This is far from perfect and most of it should be moved to 229a876b55bSAndreas Gohr * our own renderer instead of modifying the HTML at all. 2301ef68647SAndreas Gohr */ 2311ef68647SAndreas Gohr protected function arrangeHtml(&$html, $norendertags = '' ) { 232ee19bac3SLuigi Micco 2331ef68647SAndreas Gohr // insert a pagebreak for support of WRAP and PAGEBREAK plugins 2341ef68647SAndreas Gohr $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html); 2351ef68647SAndreas Gohr $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html); 2361ef68647SAndreas Gohr $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html); 2371ef68647SAndreas Gohr 2381ef68647SAndreas Gohr } 2391ef68647SAndreas Gohr 2401ef68647SAndreas Gohr 2411ef68647SAndreas Gohr /** 2421ef68647SAndreas Gohr * Strip unwanted tags 2431ef68647SAndreas Gohr * 2441ef68647SAndreas Gohr * @fixme could this be done by strip_tags? 2451ef68647SAndreas Gohr * @author Jared Ong 2461ef68647SAndreas Gohr */ 2471ef68647SAndreas Gohr protected function strip_only(&$str, $tags) { 248ee19bac3SLuigi Micco if(!is_array($tags)) { 249ee19bac3SLuigi Micco $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 250ee19bac3SLuigi Micco if(end($tags) == '') array_pop($tags); 251ee19bac3SLuigi Micco } 252ee19bac3SLuigi Micco foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 253ee19bac3SLuigi Micco } 254ee19bac3SLuigi Micco} 255