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 53d01d2a42SAndreas Gohr // hard work only when no cache available 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 6556d13144SAndreas Gohr // Set the title 6656d13144SAndreas Gohr $title = $_GET['pdfbook_title']; 6756d13144SAndreas Gohr if(!$title) $title = p_get_first_heading($ID); 6856d13144SAndreas Gohr $mpdf->SetTitle($title); 6956d13144SAndreas Gohr 701ef68647SAndreas Gohr // some default settings 71ee19bac3SLuigi Micco $mpdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins 722eedf77dSAndreas Gohr $mdpf->useOddEven = 1; 732eedf77dSAndreas Gohr 7456d13144SAndreas Gohr // load the template 75d01d2a42SAndreas Gohr $template = $this->load_template($title); //FIXME 76ee19bac3SLuigi Micco 771ef68647SAndreas Gohr // prepare HTML header styles 78ee19bac3SLuigi Micco $html = '<html><head>'; 791ef68647SAndreas Gohr $html .= '<style>'; 8083dae0dbSAndreas Gohr $html .= file_get_contents(DOKU_INC.'lib/styles/screen.css'); 8183dae0dbSAndreas Gohr $html .= file_get_contents(DOKU_INC.'lib/styles/print.css'); 821ef68647SAndreas Gohr $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css'); 831ef68647SAndreas Gohr $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css'); 842eedf77dSAndreas Gohr $html .= '@page {'.$template['page'].'}'; 852eedf77dSAndreas Gohr $html .= '@page :first {'.$template['first'].'}'; 862eedf77dSAndreas Gohr $html .= '@page :last {'.$template['last'].'}'; 872eedf77dSAndreas Gohr $html .= $template['css']; 881ef68647SAndreas Gohr $html .= '</style>'; 891ef68647SAndreas Gohr $html .= '</head><body>'; 902eedf77dSAndreas Gohr $html .= $template['html']; 912eedf77dSAndreas Gohr 921ef68647SAndreas Gohr // loop over all pages 931ef68647SAndreas Gohr $cnt = count($list); 941ef68647SAndreas Gohr for($n=0; $n<$cnt; $n++){ 95ee19bac3SLuigi Micco $page = $list[$n]; 96ee19bac3SLuigi Micco 97a876b55bSAndreas Gohr $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page); 982eedf77dSAndreas Gohr $html .= $template['cite']; 991ef68647SAndreas Gohr if ($n < ($cnt - 1)){ 1001ef68647SAndreas Gohr $html .= '<pagebreak />'; 1011ef68647SAndreas Gohr } 102ee19bac3SLuigi Micco } 103ee19bac3SLuigi Micco 1041ef68647SAndreas Gohr $this->arrangeHtml($html, $this->getConf("norender")); 105ee19bac3SLuigi Micco $mpdf->WriteHTML($html); 106a06728a6SAndreas Gohr 10787c86ddaSAndreas Gohr // write to cache file 10887c86ddaSAndreas Gohr $mpdf->Output($cache->cache, 'F'); 10987c86ddaSAndreas Gohr } 11087c86ddaSAndreas Gohr 11187c86ddaSAndreas Gohr // deliver the file 11287c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 11387c86ddaSAndreas Gohr header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT'); 11487c86ddaSAndreas Gohr header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600)); 11587c86ddaSAndreas Gohr header('Pragma: public'); 11687c86ddaSAndreas Gohr http_conditionalRequest(filemtime($cache->cache)); 11787c86ddaSAndreas Gohr 11887c86ddaSAndreas Gohr if($this->getConf('output') == 'file'){ 11956d13144SAndreas Gohr header('Content-Disposition: attachment; filename="'.rawurlencode($title).'.pdf";'); 12087c86ddaSAndreas Gohr }else{ 12156d13144SAndreas Gohr header('Content-Disposition: inline; filename="'.rawurlencode($title).'.pdf";'); 12287c86ddaSAndreas Gohr } 123ee19bac3SLuigi Micco 12487c86ddaSAndreas Gohr if (http_sendfile($cache->cache)) exit; 12587c86ddaSAndreas Gohr 12687c86ddaSAndreas Gohr $fp = @fopen($cache->cache,"rb"); 12787c86ddaSAndreas Gohr if($fp){ 12887c86ddaSAndreas Gohr http_rangeRequest($fp,filesize($cache->cache),'application/pdf'); 12987c86ddaSAndreas Gohr }else{ 13087c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 13187c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 13287c86ddaSAndreas Gohr } 1331ef68647SAndreas Gohr exit(); 1341ef68647SAndreas Gohr } 1351ef68647SAndreas Gohr 1361ef68647SAndreas Gohr /** 137d01d2a42SAndreas Gohr * Choose the correct template 138d01d2a42SAndreas Gohr */ 139d01d2a42SAndreas Gohr protected function select_template(){ 140d01d2a42SAndreas Gohr $tpl; 141d01d2a42SAndreas Gohr if(isset($_REQUEST['tpl'])){ 142d01d2a42SAndreas Gohr $tpl = trim(preg_replace('/[^A-Za-z0-9_\-]+/','',$_REQUEST['tpl'])); 143d01d2a42SAndreas Gohr } 144d01d2a42SAndreas Gohr if(!$tpl) $tpl = $this->getConf('template'); 145d01d2a42SAndreas Gohr if(!$tpl) $tpl = 'default'; 146d01d2a42SAndreas Gohr if(!is_dir(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl)) $tpl = 'default'; 147d01d2a42SAndreas Gohr 148d01d2a42SAndreas Gohr return $tpl; 149d01d2a42SAndreas Gohr } 150d01d2a42SAndreas Gohr 151d01d2a42SAndreas Gohr /** 1522eedf77dSAndreas Gohr * Load the various template files and prepare the HTML/CSS for insertion 1531ef68647SAndreas Gohr */ 154d01d2a42SAndreas Gohr protected function load_template($title){ 1551ef68647SAndreas Gohr global $ID; 1561ef68647SAndreas Gohr global $REV; 1571ef68647SAndreas Gohr global $conf; 1581ef68647SAndreas Gohr 159d01d2a42SAndreas Gohr $tpl = $this->select_template(); 160d01d2a42SAndreas Gohr 1612eedf77dSAndreas Gohr // this is what we'll return 1622eedf77dSAndreas Gohr $output = array( 1632eedf77dSAndreas Gohr 'html' => '', 1642eedf77dSAndreas Gohr 'css' => '', 1652eedf77dSAndreas Gohr 'page' => '', 1662eedf77dSAndreas Gohr 'first' => '', 1672eedf77dSAndreas Gohr 'last' => '', 1682eedf77dSAndreas Gohr 'cite' => '', 1692eedf77dSAndreas Gohr ); 1702eedf77dSAndreas Gohr 1712eedf77dSAndreas Gohr // prepare header/footer elements 1722eedf77dSAndreas Gohr $html = ''; 1732eedf77dSAndreas Gohr foreach(array('header','footer') as $t){ 1742eedf77dSAndreas Gohr foreach(array('','_odd','_even','_first','_last') as $h){ 1752eedf77dSAndreas Gohr if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html')){ 1762eedf77dSAndreas Gohr $html .= '<htmlpage'.$t.' name="'.$t.$h.'">'.DOKU_LF; 1772eedf77dSAndreas Gohr $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/'.$t.$h.'.html').DOKU_LF; 1782eedf77dSAndreas Gohr $html .= '</htmlpage'.$t.'>'.DOKU_LF; 1792eedf77dSAndreas Gohr 1802eedf77dSAndreas Gohr // register the needed pseudo CSS 1812eedf77dSAndreas Gohr if($h == '_last'){ 1822eedf77dSAndreas Gohr $output['last'] .= $t.': html_'.$t.$h.';'.DOKU_LF; 1832eedf77dSAndreas Gohr }elseif($h == '_first'){ 1842eedf77dSAndreas Gohr $output['first'] .= $t.': html_'.$t.$h.';'.DOKU_LF; 1852eedf77dSAndreas Gohr }elseif($h == '_even'){ 1862eedf77dSAndreas Gohr $output['page'] .= 'even-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF; 1872eedf77dSAndreas Gohr }else{ 1882eedf77dSAndreas Gohr $output['page'] .= 'odd-'.$t.'-name: html_'.$t.$h.';'.DOKU_LF; 1892eedf77dSAndreas Gohr } 1902eedf77dSAndreas Gohr } 1912eedf77dSAndreas Gohr } 1922eedf77dSAndreas Gohr } 1932eedf77dSAndreas Gohr 1941ef68647SAndreas Gohr // prepare replacements 1951ef68647SAndreas Gohr $replace = array( 1961ef68647SAndreas Gohr '@ID@' => $ID, 1971ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 1981ef68647SAndreas Gohr '@PAGES@' => '{nb}', 1992eedf77dSAndreas Gohr '@TITLE@' => hsc($title), 2001ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 2011ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 2023ae7a8dfSAndreas Gohr '@UPDATE@' => dformat(filemtime(wikiFN($ID,$REV))), 2033ae7a8dfSAndreas Gohr '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"), 2041ef68647SAndreas Gohr '@DATE@' => dformat(time()), 205*5d6fbaeaSAndreas Gohr '@BASE@' => DOKU_BASE, 206*5d6fbaeaSAndreas Gohr '@TPLBASE@' => DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/', 2071ef68647SAndreas Gohr ); 2081ef68647SAndreas Gohr 2092eedf77dSAndreas Gohr // set HTML element 2102eedf77dSAndreas Gohr $output['html'] = str_replace(array_keys($replace), array_values($replace), $html); 2111ef68647SAndreas Gohr 2122eedf77dSAndreas Gohr // citation box 2132eedf77dSAndreas Gohr if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html')){ 2142eedf77dSAndreas Gohr $output['cite'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/citation.html'); 2152eedf77dSAndreas Gohr $output['cite'] = str_replace(array_keys($replace), array_values($replace), $output['cite']); 2162eedf77dSAndreas Gohr } 2171ef68647SAndreas Gohr 2182eedf77dSAndreas Gohr // set custom styles 2192eedf77dSAndreas Gohr if(file_exists(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css')){ 2202eedf77dSAndreas Gohr $output['css'] = file_get_contents(DOKU_PLUGIN.'dw2pdf/tpl/'.$tpl.'/style.css'); 2212eedf77dSAndreas Gohr } 2222eedf77dSAndreas Gohr 2232eedf77dSAndreas Gohr return $output; 2241ef68647SAndreas Gohr } 2251ef68647SAndreas Gohr 2261ef68647SAndreas Gohr /** 2271ef68647SAndreas Gohr * Fix up the HTML a bit 2281ef68647SAndreas Gohr * 229a876b55bSAndreas Gohr * FIXME This is far from perfect and most of it should be moved to 230a876b55bSAndreas Gohr * our own renderer instead of modifying the HTML at all. 2311ef68647SAndreas Gohr */ 2321ef68647SAndreas Gohr protected function arrangeHtml(&$html, $norendertags = '' ) { 233ee19bac3SLuigi Micco 2341ef68647SAndreas Gohr // insert a pagebreak for support of WRAP and PAGEBREAK plugins 2351ef68647SAndreas Gohr $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html); 2361ef68647SAndreas Gohr $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html); 2371ef68647SAndreas Gohr $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html); 2381ef68647SAndreas Gohr 2391ef68647SAndreas Gohr } 2401ef68647SAndreas Gohr 2411ef68647SAndreas Gohr 2421ef68647SAndreas Gohr /** 2431ef68647SAndreas Gohr * Strip unwanted tags 2441ef68647SAndreas Gohr * 2451ef68647SAndreas Gohr * @fixme could this be done by strip_tags? 2461ef68647SAndreas Gohr * @author Jared Ong 2471ef68647SAndreas Gohr */ 2481ef68647SAndreas Gohr protected function strip_only(&$str, $tags) { 249ee19bac3SLuigi Micco if(!is_array($tags)) { 250ee19bac3SLuigi Micco $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 251ee19bac3SLuigi Micco if(end($tags) == '') array_pop($tags); 252ee19bac3SLuigi Micco } 253ee19bac3SLuigi Micco foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 254ee19bac3SLuigi Micco } 255ee19bac3SLuigi Micco} 256