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 381ef68647SAndreas Gohr // initialize PDF library 39cde5a1b3SAndreas Gohr require_once(dirname(__FILE__)."/DokuPDF.class.php"); 401ef68647SAndreas Gohr $mpdf = new DokuPDF(); 41ee19bac3SLuigi Micco 42d62df65bSAndreas Gohr // let mpdf fix local links 43d62df65bSAndreas Gohr $self = parse_url(DOKU_URL); 44d62df65bSAndreas Gohr $url = $self['scheme'].'://'.$self['host']; 45d62df65bSAndreas Gohr if($self['port']) $url .= ':'.$port; 46d62df65bSAndreas Gohr $mpdf->setBasePath($url); 47d62df65bSAndreas 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>'; 60*83dae0dbSAndreas Gohr $html .= file_get_contents(DOKU_INC.'lib/styles/screen.css'); 61*83dae0dbSAndreas Gohr $html .= file_get_contents(DOKU_INC.'lib/styles/print.css'); 621ef68647SAndreas Gohr $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css'); 631ef68647SAndreas Gohr $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css'); 641ef68647SAndreas Gohr $html .= '</style>'; 651ef68647SAndreas Gohr $html .= '</head><body>'; 66ee19bac3SLuigi Micco 671ef68647SAndreas Gohr // set headers/footers 681ef68647SAndreas Gohr $this->prepare_headers($mpdf); 69ee19bac3SLuigi Micco 701ef68647SAndreas Gohr // one or multiple pages? 71ee19bac3SLuigi Micco $list = array(); 721ef68647SAndreas Gohr if ( $ACT == 'export_pdf' ) { 73ee19bac3SLuigi Micco $list[0] = $ID; 741ef68647SAndreas Gohr } elseif (isset($_COOKIE['list-pagelist'])) { 75ee19bac3SLuigi Micco $list = explode("|", $_COOKIE['list-pagelist']); 76ee19bac3SLuigi Micco } 77ee19bac3SLuigi Micco 781ef68647SAndreas Gohr // loop over all pages 791ef68647SAndreas Gohr $cnt = count($list); 801ef68647SAndreas Gohr for($n=0; $n<$cnt; $n++){ 81ee19bac3SLuigi Micco $page = $list[$n]; 82ee19bac3SLuigi Micco 83a876b55bSAndreas Gohr $html .= p_cached_output(wikiFN($page,$REV),'dw2pdf',$page); 841ef68647SAndreas Gohr if($this->getConf('addcitation')){ 851ef68647SAndreas Gohr $html .= $this->citation($page); 86ee19bac3SLuigi Micco } 871ef68647SAndreas Gohr if ($n < ($cnt - 1)){ 881ef68647SAndreas Gohr $html .= '<pagebreak />'; 891ef68647SAndreas Gohr } 90ee19bac3SLuigi Micco } 91ee19bac3SLuigi Micco 921ef68647SAndreas Gohr $this->arrangeHtml($html, $this->getConf("norender")); 93ee19bac3SLuigi Micco $mpdf->WriteHTML($html); 94a06728a6SAndreas Gohr 95cde5a1b3SAndreas Gohr $title = $_GET['pdfbook_title']; 96cde5a1b3SAndreas Gohr if(!$title) $title = noNS($ID); 97ee19bac3SLuigi Micco $output = 'I'; 98ee19bac3SLuigi Micco if($this->getConf('output') == 'file') $output = 'D'; 99cde5a1b3SAndreas Gohr $mpdf->Output(urlencode($title).'.pdf', $output); 100ee19bac3SLuigi Micco 1011ef68647SAndreas Gohr exit(); 1021ef68647SAndreas Gohr } 1031ef68647SAndreas Gohr 1041ef68647SAndreas Gohr /** 1051ef68647SAndreas Gohr * Setup the page headers and footers 1061ef68647SAndreas Gohr */ 1071ef68647SAndreas Gohr protected function prepare_headers(&$mpdf){ 1081ef68647SAndreas Gohr global $ID; 1091ef68647SAndreas Gohr global $REV; 1101ef68647SAndreas Gohr global $conf; 1111ef68647SAndreas Gohr 1121ef68647SAndreas Gohr if($_GET['pdfbook_title']){ 1131ef68647SAndreas Gohr $title = $_GET['pdfbook_title']; 1141ef68647SAndreas Gohr }else{ 1151ef68647SAndreas Gohr $title = p_get_first_heading($ID); 1161ef68647SAndreas Gohr } 1171ef68647SAndreas Gohr if(!$title) $title = noNS($ID); 1181ef68647SAndreas Gohr 1191ef68647SAndreas Gohr // prepare replacements 1201ef68647SAndreas Gohr $replace = array( 1211ef68647SAndreas Gohr '@ID@' => $ID, 1221ef68647SAndreas Gohr '@PAGE@' => '{PAGENO}', 1231ef68647SAndreas Gohr '@PAGES@' => '{nb}', 1241ef68647SAndreas Gohr '@TITLE@' => $title, 1251ef68647SAndreas Gohr '@WIKI@' => $conf['title'], 1261ef68647SAndreas Gohr '@WIKIURL@' => DOKU_URL, 1273ae7a8dfSAndreas Gohr '@UPDATE@' => dformat(filemtime(wikiFN($ID,$REV))), 1283ae7a8dfSAndreas Gohr '@PAGEURL@' => wl($ID,($REV)?array('rev'=>$REV):false, true, "&"), 1291ef68647SAndreas Gohr '@DATE@' => dformat(time()), 1301ef68647SAndreas Gohr ); 1311ef68647SAndreas Gohr 1321ef68647SAndreas Gohr // do the replacements 1331ef68647SAndreas Gohr $fo = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd")); 1341ef68647SAndreas Gohr $fe = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even")); 1351ef68647SAndreas Gohr $ho = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd")); 1361ef68647SAndreas Gohr $he = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even")); 1371ef68647SAndreas Gohr 1381ef68647SAndreas Gohr // set the headers/footers 1391ef68647SAndreas Gohr $mpdf->SetHeader($ho); 1401ef68647SAndreas Gohr $mpdf->SetHeader($he, 'E'); 1411ef68647SAndreas Gohr $mpdf->SetFooter($fo); 1421ef68647SAndreas Gohr $mpdf->SetFooter($fe, 'E'); 1431ef68647SAndreas Gohr 1441ef68647SAndreas Gohr // title 1451ef68647SAndreas Gohr $mpdf->SetTitle($title); 1461ef68647SAndreas Gohr } 1471ef68647SAndreas Gohr 1481ef68647SAndreas Gohr /** 1491ef68647SAndreas Gohr * Fix up the HTML a bit 1501ef68647SAndreas Gohr * 151a876b55bSAndreas Gohr * FIXME This is far from perfect and most of it should be moved to 152a876b55bSAndreas Gohr * our own renderer instead of modifying the HTML at all. 1531ef68647SAndreas Gohr */ 1541ef68647SAndreas Gohr protected function arrangeHtml(&$html, $norendertags = '' ) { 155ee19bac3SLuigi Micco 1561ef68647SAndreas Gohr // insert a pagebreak for support of WRAP and PAGEBREAK plugins 1571ef68647SAndreas Gohr $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html); 1581ef68647SAndreas Gohr $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html); 1591ef68647SAndreas Gohr $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html); 1601ef68647SAndreas Gohr 1611ef68647SAndreas Gohr } 1621ef68647SAndreas Gohr 1631ef68647SAndreas Gohr /** 1641ef68647SAndreas Gohr * Create the citation box 1651ef68647SAndreas Gohr * 1661ef68647SAndreas Gohr * @todo can we drop the inline style here? 1671ef68647SAndreas Gohr */ 1681ef68647SAndreas Gohr protected function citation($page) { 1691ef68647SAndreas Gohr global $conf; 1701ef68647SAndreas Gohr 1711ef68647SAndreas Gohr $date = filemtime(wikiFN($page)); 1721ef68647SAndreas Gohr $html = ''; 1731ef68647SAndreas 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%;'>"; 1741ef68647SAndreas Gohr $html .= "From:<br>"; 1751ef68647SAndreas Gohr $html .= "<a href='".DOKU_URL."'>".DOKU_URL."</a> - "."<b>".$conf['title']."</b>"; 1761ef68647SAndreas Gohr $html .= "<br><br>Permanent link:<br>"; 1771ef68647SAndreas Gohr $html .= "<b><a href='".wl($page, false, true, "&")."'>".wl($page, false, true, "&")."</a></b>"; 1781ef68647SAndreas Gohr $html .= "<br><br>Last update: <b>".dformat($date)."</b><br>"; 1791ef68647SAndreas Gohr $html .= "</div>"; 1801ef68647SAndreas Gohr return $html; 1811ef68647SAndreas Gohr } 1821ef68647SAndreas Gohr 1831ef68647SAndreas Gohr /** 1841ef68647SAndreas Gohr * Strip unwanted tags 1851ef68647SAndreas Gohr * 1861ef68647SAndreas Gohr * @fixme could this be done by strip_tags? 1871ef68647SAndreas Gohr * @author Jared Ong 1881ef68647SAndreas Gohr */ 1891ef68647SAndreas Gohr protected function strip_only(&$str, $tags) { 190ee19bac3SLuigi Micco if(!is_array($tags)) { 191ee19bac3SLuigi Micco $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 192ee19bac3SLuigi Micco if(end($tags) == '') array_pop($tags); 193ee19bac3SLuigi Micco } 194ee19bac3SLuigi Micco foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 195ee19bac3SLuigi Micco } 196ee19bac3SLuigi Micco} 197