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 38*87c86ddaSAndreas Gohr // one or multiple pages? 39*87c86ddaSAndreas Gohr $list = array(); 40*87c86ddaSAndreas Gohr if ( $ACT == 'export_pdf' ) { 41*87c86ddaSAndreas Gohr $list[0] = $ID; 42*87c86ddaSAndreas Gohr } elseif (isset($_COOKIE['list-pagelist'])) { 43*87c86ddaSAndreas Gohr $list = explode("|", $_COOKIE['list-pagelist']); 44*87c86ddaSAndreas Gohr } 45*87c86ddaSAndreas Gohr 46*87c86ddaSAndreas Gohr // prepare cache 47*87c86ddaSAndreas Gohr $cache = new cache(join(',',$list).$REV,'.dw2.pdf'); 48*87c86ddaSAndreas Gohr $depends['files'] = array_map('wikiFN',$list); 49*87c86ddaSAndreas Gohr $depends['files'][] = __FILE__; 50*87c86ddaSAndreas Gohr $depends['files'][] = dirname(__FILE__).'/renderer.php'; 51*87c86ddaSAndreas Gohr $depends['files'][] = dirname(__FILE__).'/mpdf/mpdf.php'; 52*87c86ddaSAndreas Gohr 53*87c86ddaSAndreas Gohr 54*87c86ddaSAndreas 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 671ef68647SAndreas Gohr $mpdf->defaultheaderfontsize = 8; // in pts 681ef68647SAndreas Gohr $mpdf->defaultheaderfontstyle = ''; // blank, B, I, or BI 691ef68647SAndreas Gohr $mpdf->defaultheaderline = 1; // 1 to include line below header/above footer 701ef68647SAndreas Gohr $mpdf->defaultfooterfontsize = 8; // in pts 711ef68647SAndreas Gohr $mpdf->defaultfooterfontstyle = ''; // blank, B, I, or BI 721ef68647SAndreas Gohr $mpdf->defaultfooterline = 1; // 1 to include line below header/above footer 73ee19bac3SLuigi Micco 741ef68647SAndreas Gohr // prepare HTML header styles 75ee19bac3SLuigi Micco $html = '<html><head>'; 761ef68647SAndreas Gohr $html .= '<style>'; 7783dae0dbSAndreas Gohr $html .= file_get_contents(DOKU_INC.'lib/styles/screen.css'); 7883dae0dbSAndreas Gohr $html .= file_get_contents(DOKU_INC.'lib/styles/print.css'); 791ef68647SAndreas Gohr $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css'); 801ef68647SAndreas Gohr $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css'); 811ef68647SAndreas Gohr $html .= '</style>'; 821ef68647SAndreas Gohr $html .= '</head><body>'; 83ee19bac3SLuigi Micco 841ef68647SAndreas Gohr // set headers/footers 851ef68647SAndreas Gohr $this->prepare_headers($mpdf); 86ee19bac3SLuigi Micco 871ef68647SAndreas Gohr // loop over all pages 881ef68647SAndreas Gohr $cnt = count($list); 891ef68647SAndreas Gohr for($n=0; $n<$cnt; $n++){ 90ee19bac3SLuigi Micco $page = $list[$n]; 91ee19bac3SLuigi Micco 92a876b55bSAndreas Gohr $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page); 931ef68647SAndreas Gohr if($this->getConf('addcitation')){ 941ef68647SAndreas Gohr $html .= $this->citation($page); 95ee19bac3SLuigi Micco } 961ef68647SAndreas Gohr if ($n < ($cnt - 1)){ 971ef68647SAndreas Gohr $html .= '<pagebreak />'; 981ef68647SAndreas Gohr } 99ee19bac3SLuigi Micco } 100ee19bac3SLuigi Micco 1011ef68647SAndreas Gohr $this->arrangeHtml($html, $this->getConf("norender")); 102ee19bac3SLuigi Micco $mpdf->WriteHTML($html); 103a06728a6SAndreas Gohr 104*87c86ddaSAndreas Gohr // write to cache file 105*87c86ddaSAndreas Gohr $mpdf->Output($cache->cache, 'F'); 106*87c86ddaSAndreas Gohr } 107*87c86ddaSAndreas Gohr 108*87c86ddaSAndreas Gohr // deliver the file 109*87c86ddaSAndreas Gohr header('Content-Type: application/pdf'); 110*87c86ddaSAndreas Gohr header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT'); 111*87c86ddaSAndreas Gohr header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600)); 112*87c86ddaSAndreas Gohr header('Pragma: public'); 113*87c86ddaSAndreas Gohr http_conditionalRequest(filemtime($cache->cache)); 114*87c86ddaSAndreas Gohr 115cde5a1b3SAndreas Gohr $title = $_GET['pdfbook_title']; 116cde5a1b3SAndreas Gohr if(!$title) $title = noNS($ID); 117*87c86ddaSAndreas Gohr if($this->getConf('output') == 'file'){ 118*87c86ddaSAndreas Gohr header('Content-Disposition: attachment; filename="'.urlencode($title).'.pdf";'); 119*87c86ddaSAndreas Gohr }else{ 120*87c86ddaSAndreas Gohr header('Content-Disposition: inline; filename="'.urlencode($title).'.pdf";'); 121*87c86ddaSAndreas Gohr } 122ee19bac3SLuigi Micco 123*87c86ddaSAndreas Gohr if (http_sendfile($cache->cache)) exit; 124*87c86ddaSAndreas Gohr 125*87c86ddaSAndreas Gohr $fp = @fopen($cache->cache,"rb"); 126*87c86ddaSAndreas Gohr if($fp){ 127*87c86ddaSAndreas Gohr http_rangeRequest($fp,filesize($cache->cache),'application/pdf'); 128*87c86ddaSAndreas Gohr }else{ 129*87c86ddaSAndreas Gohr header("HTTP/1.0 500 Internal Server Error"); 130*87c86ddaSAndreas Gohr print "Could not read file - bad permissions?"; 131*87c86ddaSAndreas Gohr } 1321ef68647SAndreas Gohr exit(); 1331ef68647SAndreas Gohr } 1341ef68647SAndreas Gohr 1351ef68647SAndreas Gohr /** 1361ef68647SAndreas Gohr * Setup the page headers and footers 1371ef68647SAndreas Gohr */ 1381ef68647SAndreas Gohr protected function prepare_headers(&$mpdf){ 1391ef68647SAndreas Gohr global $ID; 1401ef68647SAndreas Gohr global $REV; 1411ef68647SAndreas Gohr global $conf; 1421ef68647SAndreas Gohr 1431ef68647SAndreas Gohr if($_GET['pdfbook_title']){ 1441ef68647SAndreas Gohr $title = $_GET['pdfbook_title']; 1451ef68647SAndreas Gohr }else{ 1461ef68647SAndreas Gohr $title = p_get_first_heading($ID); 1471ef68647SAndreas Gohr } 1481ef68647SAndreas Gohr if(!$title) $title = noNS($ID); 1491ef68647SAndreas Gohr 1501ef68647SAndreas Gohr // prepare replacements 1511ef68647SAndreas Gohr $replace = array( 1521ef68647SAndreas Gohr '@ID@' => $ID, 1531ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 1541ef68647SAndreas Gohr '@PAGES@' => '{nb}', 1551ef68647SAndreas Gohr '@TITLE@' => $title, 1561ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 1571ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 1583ae7a8dfSAndreas Gohr '@UPDATE@' => dformat(filemtime(wikiFN($ID,$REV))), 1593ae7a8dfSAndreas Gohr '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"), 1601ef68647SAndreas Gohr '@DATE@' => dformat(time()), 1611ef68647SAndreas Gohr ); 1621ef68647SAndreas Gohr 1631ef68647SAndreas Gohr // do the replacements 1641ef68647SAndreas Gohr $fo = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd")); 1651ef68647SAndreas Gohr $fe = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even")); 1661ef68647SAndreas Gohr $ho = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd")); 1671ef68647SAndreas Gohr $he = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even")); 1681ef68647SAndreas Gohr 1691ef68647SAndreas Gohr // set the headers/footers 1701ef68647SAndreas Gohr $mpdf->SetHeader($ho); 1711ef68647SAndreas Gohr $mpdf->SetHeader($he, 'E'); 1721ef68647SAndreas Gohr $mpdf->SetFooter($fo); 1731ef68647SAndreas Gohr $mpdf->SetFooter($fe, 'E'); 1741ef68647SAndreas Gohr 1751ef68647SAndreas Gohr // title 1761ef68647SAndreas Gohr $mpdf->SetTitle($title); 1771ef68647SAndreas Gohr } 1781ef68647SAndreas Gohr 1791ef68647SAndreas Gohr /** 1801ef68647SAndreas Gohr * Fix up the HTML a bit 1811ef68647SAndreas Gohr * 182a876b55bSAndreas Gohr * FIXME This is far from perfect and most of it should be moved to 183a876b55bSAndreas Gohr * our own renderer instead of modifying the HTML at all. 1841ef68647SAndreas Gohr */ 1851ef68647SAndreas Gohr protected function arrangeHtml(&$html, $norendertags = '' ) { 186ee19bac3SLuigi Micco 1871ef68647SAndreas Gohr // insert a pagebreak for support of WRAP and PAGEBREAK plugins 1881ef68647SAndreas Gohr $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html); 1891ef68647SAndreas Gohr $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html); 1901ef68647SAndreas Gohr $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html); 1911ef68647SAndreas Gohr 1921ef68647SAndreas Gohr } 1931ef68647SAndreas Gohr 1941ef68647SAndreas Gohr /** 1951ef68647SAndreas Gohr * Create the citation box 1961ef68647SAndreas Gohr * 1971ef68647SAndreas Gohr * @todo can we drop the inline style here? 1981ef68647SAndreas Gohr */ 1991ef68647SAndreas Gohr protected function citation($page) { 2001ef68647SAndreas Gohr global $conf; 2011ef68647SAndreas Gohr 2021ef68647SAndreas Gohr $date = filemtime(wikiFN($page)); 2031ef68647SAndreas Gohr $html = ''; 2041ef68647SAndreas Gohr $html .= "<br><br><div style='font-size: 80%; border: solid 0.5mm #DDDDDD;background-color: #EEEEEE; padding: 2mm; border-radius: 2mm 2mm; width: 100%;'>"; 2051ef68647SAndreas Gohr $html .= "From:<br>"; 2061ef68647SAndreas Gohr $html .= "<a href='".DOKU_URL."'>".DOKU_URL."</a> - "."<b>".$conf['title']."</b>"; 2071ef68647SAndreas Gohr $html .= "<br><br>Permanent link:<br>"; 2081ef68647SAndreas Gohr $html .= "<b><a href='".wl($page, false, true, "&")."'>".wl($page, false, true, "&")."</a></b>"; 2091ef68647SAndreas Gohr $html .= "<br><br>Last update: <b>".dformat($date)."</b><br>"; 2101ef68647SAndreas Gohr $html .= "</div>"; 2111ef68647SAndreas Gohr return $html; 2121ef68647SAndreas Gohr } 2131ef68647SAndreas Gohr 2141ef68647SAndreas Gohr /** 2151ef68647SAndreas Gohr * Strip unwanted tags 2161ef68647SAndreas Gohr * 2171ef68647SAndreas Gohr * @fixme could this be done by strip_tags? 2181ef68647SAndreas Gohr * @author Jared Ong 2191ef68647SAndreas Gohr */ 2201ef68647SAndreas Gohr protected function strip_only(&$str, $tags) { 221ee19bac3SLuigi Micco if(!is_array($tags)) { 222ee19bac3SLuigi Micco $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 223ee19bac3SLuigi Micco if(end($tags) == '') array_pop($tags); 224ee19bac3SLuigi Micco } 225ee19bac3SLuigi Micco foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 226ee19bac3SLuigi Micco } 227ee19bac3SLuigi Micco} 228