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> 71ef68647SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 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 381ef68647SAndreas Gohr // initialize PDF library 39cde5a1b3SAndreas Gohr require_once(dirname(__FILE__)."/DokuPDF.class.php"); 401ef68647SAndreas Gohr $mpdf = new DokuPDF(); 41ee19bac3SLuigi Micco 42*d62df65bSAndreas Gohr // let mpdf fix local links 43*d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 44*d62df65bSAndreas Gohr $url = $self['scheme'].'://'.$self['host']; 45*d62df65bSAndreas Gohr if($self['port']) $url .= ':'.$port; 46*d62df65bSAndreas Gohr $mpdf->setBasePath($url); 47*d62df65bSAndreas Gohr 481ef68647SAndreas Gohr // some default settings 49ee19bac3SLuigi Micco $mpdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins 501ef68647SAndreas Gohr $mpdf->defaultheaderfontsize = 8; // in pts 511ef68647SAndreas Gohr $mpdf->defaultheaderfontstyle = ''; // blank, B, I, or BI 521ef68647SAndreas Gohr $mpdf->defaultheaderline = 1; // 1 to include line below header/above footer 531ef68647SAndreas Gohr $mpdf->defaultfooterfontsize = 8; // in pts 541ef68647SAndreas Gohr $mpdf->defaultfooterfontstyle = ''; // blank, B, I, or BI 551ef68647SAndreas Gohr $mpdf->defaultfooterline = 1; // 1 to include line below header/above footer 56ee19bac3SLuigi Micco 571ef68647SAndreas Gohr // prepare HTML header styles 58ee19bac3SLuigi Micco $html = '<html><head>'; 591ef68647SAndreas Gohr $html .= '<style>'; 601ef68647SAndreas Gohr $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css'); 611ef68647SAndreas Gohr $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css'); 621ef68647SAndreas Gohr $html .= '</style>'; 631ef68647SAndreas Gohr $html .= '</head><body>'; 64ee19bac3SLuigi Micco 651ef68647SAndreas Gohr // set headers/footers 661ef68647SAndreas Gohr $this->prepare_headers($mpdf); 67ee19bac3SLuigi Micco 681ef68647SAndreas Gohr // one or multiple pages? 69ee19bac3SLuigi Micco $list = array(); 701ef68647SAndreas Gohr if ( $ACT == 'export_pdf' ) { 71ee19bac3SLuigi Micco $list[0] = $ID; 721ef68647SAndreas Gohr } elseif (isset($_COOKIE['list-pagelist'])) { 73ee19bac3SLuigi Micco $list = explode("|", $_COOKIE['list-pagelist']); 74ee19bac3SLuigi Micco } 75ee19bac3SLuigi Micco 761ef68647SAndreas Gohr // loop over all pages 771ef68647SAndreas Gohr $cnt = count($list); 781ef68647SAndreas Gohr for($n=0; $n<$cnt; $n++){ 79ee19bac3SLuigi Micco $page = $list[$n]; 80ee19bac3SLuigi Micco 811ef68647SAndreas Gohr $html .= p_wiki_xhtml($page,$REV,false); 821ef68647SAndreas Gohr if($this->getConf('addcitation')){ 831ef68647SAndreas Gohr $html .= $this->citation($page); 84ee19bac3SLuigi Micco } 851ef68647SAndreas Gohr if ($n < ($cnt - 1)){ 861ef68647SAndreas Gohr $html .= '<pagebreak />'; 871ef68647SAndreas Gohr } 88ee19bac3SLuigi Micco } 89ee19bac3SLuigi Micco 901ef68647SAndreas Gohr $this->arrangeHtml($html, $this->getConf("norender")); 91ee19bac3SLuigi Micco 92ee19bac3SLuigi Micco $mpdf->WriteHTML($html); 93cde5a1b3SAndreas Gohr $title = $_GET['pdfbook_title']; 94cde5a1b3SAndreas Gohr if(!$title) $title = noNS($ID); 95ee19bac3SLuigi Micco $output = 'I'; 96ee19bac3SLuigi Micco if($this->getConf('output') == 'file') $output = 'D'; 97cde5a1b3SAndreas Gohr $mpdf->Output(urlencode($title).'.pdf', $output); 98ee19bac3SLuigi Micco 991ef68647SAndreas Gohr exit(); 1001ef68647SAndreas Gohr } 1011ef68647SAndreas Gohr 1021ef68647SAndreas Gohr /** 1031ef68647SAndreas Gohr * Setup the page headers and footers 1041ef68647SAndreas Gohr */ 1051ef68647SAndreas Gohr protected function prepare_headers(&$mpdf){ 1061ef68647SAndreas Gohr global $ID; 1071ef68647SAndreas Gohr global $REV; 1081ef68647SAndreas Gohr global $conf; 1091ef68647SAndreas Gohr 1101ef68647SAndreas Gohr if($_GET['pdfbook_title']){ 1111ef68647SAndreas Gohr $title = $_GET['pdfbook_title']; 1121ef68647SAndreas Gohr }else{ 1131ef68647SAndreas Gohr $title = p_get_first_heading($ID); 1141ef68647SAndreas Gohr } 1151ef68647SAndreas Gohr if(!$title) $title = noNS($ID); 1161ef68647SAndreas Gohr 1171ef68647SAndreas Gohr // prepare replacements 1181ef68647SAndreas Gohr $replace = array( 1191ef68647SAndreas Gohr '@ID@' => $ID, 1201ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 1211ef68647SAndreas Gohr '@PAGES@' => '{nb}', 1221ef68647SAndreas Gohr '@TITLE@' => $title, 1231ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 1241ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 1253ae7a8dfSAndreas Gohr '@UPDATE@' => dformat(filemtime(wikiFN($ID,$REV))), 1263ae7a8dfSAndreas Gohr '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"), 1271ef68647SAndreas Gohr '@DATE@' => dformat(time()), 1281ef68647SAndreas Gohr ); 1291ef68647SAndreas Gohr 1301ef68647SAndreas Gohr // do the replacements 1311ef68647SAndreas Gohr $fo = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd")); 1321ef68647SAndreas Gohr $fe = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even")); 1331ef68647SAndreas Gohr $ho = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd")); 1341ef68647SAndreas Gohr $he = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even")); 1351ef68647SAndreas Gohr 1361ef68647SAndreas Gohr // set the headers/footers 1371ef68647SAndreas Gohr $mpdf->SetHeader($ho); 1381ef68647SAndreas Gohr $mpdf->SetHeader($he, 'E'); 1391ef68647SAndreas Gohr $mpdf->SetFooter($fo); 1401ef68647SAndreas Gohr $mpdf->SetFooter($fe, 'E'); 1411ef68647SAndreas Gohr 1421ef68647SAndreas Gohr // title 1431ef68647SAndreas Gohr $mpdf->SetTitle($title); 1441ef68647SAndreas Gohr } 1451ef68647SAndreas Gohr 1461ef68647SAndreas Gohr /** 1471ef68647SAndreas Gohr * Fix up the HTML a bit 1481ef68647SAndreas Gohr * 1491ef68647SAndreas Gohr * FIXME This is far from perfect and will modify things within code and 1501ef68647SAndreas Gohr * nowiki blocks. It would probably be a good idea to use a real HTML 1511ef68647SAndreas Gohr * parser or our own renderer instead of modifying the HTML at all. 1521ef68647SAndreas Gohr */ 1531ef68647SAndreas Gohr protected function arrangeHtml(&$html, $norendertags = '' ) { 1541ef68647SAndreas Gohr // add bookmark links 1551ef68647SAndreas Gohr $bmlevel = $this->getConf('maxbookmarks'); 1561ef68647SAndreas Gohr if($bmlevel > 0) { 1571ef68647SAndreas Gohr $html = preg_replace("/\<a name=(.+?)\>(.+?)\<\/a\>/s",'$2',$html); 1581ef68647SAndreas Gohr for ($j = 1; $j<=$bmlevel; $j++) { 1591ef68647SAndreas Gohr $html = preg_replace("/\<h".$j."\>(.+?)\<\/h".$j."\>/s",'<h'.$j.'>$1<bookmark content="$1" level="'.($j-1).'"/></h'.$j.'>',$html); 160ee19bac3SLuigi Micco } 161ee19bac3SLuigi Micco } 162ee19bac3SLuigi Micco 1631ef68647SAndreas Gohr // insert a pagebreak for support of WRAP and PAGEBREAK plugins 1641ef68647SAndreas Gohr $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html); 1651ef68647SAndreas Gohr $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html); 1661ef68647SAndreas Gohr $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html); 1671ef68647SAndreas Gohr 1681ef68647SAndreas Gohr // Customized to strip all span tags so that the wiki <code> SQL would display properly 1691ef68647SAndreas Gohr $norender = explode(',',$this->getConf('norender')); 1701ef68647SAndreas Gohr $this->strip_only($html, $norender); 171cde5a1b3SAndreas Gohr $this->strip_htmlencodedchars($html); 1721ef68647SAndreas Gohr } 1731ef68647SAndreas Gohr 1741ef68647SAndreas Gohr /** 1751ef68647SAndreas Gohr * Create the citation box 1761ef68647SAndreas Gohr * 1771ef68647SAndreas Gohr * @todo can we drop the inline style here? 1781ef68647SAndreas Gohr */ 1791ef68647SAndreas Gohr protected function citation($page) { 1801ef68647SAndreas Gohr global $conf; 1811ef68647SAndreas Gohr 1821ef68647SAndreas Gohr $date = filemtime(wikiFN($page)); 1831ef68647SAndreas Gohr $html = ''; 1841ef68647SAndreas 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%;'>"; 1851ef68647SAndreas Gohr $html .= "From:<br>"; 1861ef68647SAndreas Gohr $html .= "<a href='".DOKU_URL."'>".DOKU_URL."</a> - "."<b>".$conf['title']."</b>"; 1871ef68647SAndreas Gohr $html .= "<br><br>Permanent link:<br>"; 1881ef68647SAndreas Gohr $html .= "<b><a href='".wl($page, false, true, "&")."'>".wl($page, false, true, "&")."</a></b>"; 1891ef68647SAndreas Gohr $html .= "<br><br>Last update: <b>".dformat($date)."</b><br>"; 1901ef68647SAndreas Gohr $html .= "</div>"; 1911ef68647SAndreas Gohr return $html; 1921ef68647SAndreas Gohr } 1931ef68647SAndreas Gohr 1941ef68647SAndreas Gohr /** 1951ef68647SAndreas Gohr * Strip unwanted tags 1961ef68647SAndreas Gohr * 1971ef68647SAndreas Gohr * @fixme could this be done by strip_tags? 1981ef68647SAndreas Gohr * @author Jared Ong 1991ef68647SAndreas Gohr */ 2001ef68647SAndreas Gohr protected function strip_only(&$str, $tags) { 201ee19bac3SLuigi Micco if(!is_array($tags)) { 202ee19bac3SLuigi Micco $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 203ee19bac3SLuigi Micco if(end($tags) == '') array_pop($tags); 204ee19bac3SLuigi Micco } 205ee19bac3SLuigi Micco foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 206ee19bac3SLuigi Micco } 207ee19bac3SLuigi Micco 2081ef68647SAndreas Gohr /** 2091ef68647SAndreas Gohr * Replace ' " > < & 2101ef68647SAndreas Gohr * 2111ef68647SAndreas Gohr * @fixme do we really need this? wouldn't this break things? 21208b99b84SAndreas Gohr * @fixme and if we really need it, do correct numeric decoding 2131ef68647SAndreas Gohr */ 2141ef68647SAndreas Gohr protected function strip_htmlencodedchars(&$str) { 21508b99b84SAndreas Gohr $str = htmlspecialchars_decode($str); 216ee19bac3SLuigi Micco $str = str_replace(''', '\'', $str); 217ee19bac3SLuigi Micco } 218ee19bac3SLuigi Micco} 219