1<?php 2 /** 3 * dw2Pdf Plugin: Conversion from dokuwiki content to pdf. 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Luigi Micco <l.micco@tiscali.it> 7 * @author Andreas Gohr <gohr@cosmocode.de> 8 */ 9 10// must be run within Dokuwiki 11if (!defined('DOKU_INC')) die(); 12if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 13 14require_once (DOKU_PLUGIN . 'action.php'); 15 16class action_plugin_dw2pdf extends DokuWiki_Action_Plugin { 17 18 /** 19 * Register the events 20 */ 21 function register(&$controller) { 22 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array()); 23 } 24 25 function convert(&$event, $param) { 26 global $ACT; 27 global $REV; 28 global $ID; 29 global $conf; 30 31 // our event? 32 if (( $ACT != 'export_pdfbook' ) && ( $ACT != 'export_pdf' )) return false; 33 34 // check user's rights 35 if ( auth_quickaclcheck($ID) < AUTH_READ ) return false; 36 37 // it's ours, no one else's 38 $event->preventDefault(); 39 40 // initialize PDF library 41 require_once(dirname(__FILE__)."/DokuPDF.php"); 42 $mpdf = new DokuPDF(); 43 44 // some default settings 45 $mpdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins 46 $mpdf->defaultheaderfontsize = 8; // in pts 47 $mpdf->defaultheaderfontstyle = ''; // blank, B, I, or BI 48 $mpdf->defaultheaderline = 1; // 1 to include line below header/above footer 49 $mpdf->defaultfooterfontsize = 8; // in pts 50 $mpdf->defaultfooterfontstyle = ''; // blank, B, I, or BI 51 $mpdf->defaultfooterline = 1; // 1 to include line below header/above footer 52 53 // prepare HTML header styles 54 $html = '<html><head>'; 55 $html .= '<style>'; 56 $html .= file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.css'); 57 $html .= @file_get_contents(DOKU_PLUGIN.'dw2pdf/conf/style.local.css'); 58 $html .= '</style>'; 59 $html .= '</head><body>'; 60 61 // set headers/footers 62 $this->prepare_headers($mpdf); 63 64 // one or multiple pages? 65 $list = array(); 66 if ( $ACT == 'export_pdf' ) { 67 $list[0] = $ID; 68 } elseif (isset($_COOKIE['list-pagelist'])) { 69 $list = explode("|", $_COOKIE['list-pagelist']); 70 } 71 72 // loop over all pages 73 $cnt = count($list); 74 for($n=0; $n<$cnt; $n++){ 75 $page = $list[$n]; 76 77 $html .= p_wiki_xhtml($page,$REV,false); 78 if($this->getConf('addcitation')){ 79 $html .= $this->citation($page); 80 } 81 if ($n < ($cnt - 1)){ 82 $html .= '<pagebreak />'; 83 } 84 } 85 86 $this->arrangeHtml($html, $this->getConf("norender")); 87 88 $mpdf->WriteHTML($html); 89 90 91 $output = 'I'; 92 if($this->getConf('output') == 'file') $output = 'D'; 93 $mpdf->Output(urlencode($pdftitle).'.pdf', $output); 94 95 exit(); 96 } 97 98 /** 99 * Setup the page headers and footers 100 */ 101 protected function prepare_headers(&$mpdf){ 102 global $ID; 103 global $REV; 104 global $conf; 105 106 if($_GET['pdfbook_title']){ 107 $title = $_GET['pdfbook_title']; 108 }else{ 109 $title = p_get_first_heading($ID); 110 } 111 if(!$title) $title = noNS($ID); 112 113 $iddata = p_get_metadata($ID,'date'); 114 115 // prepare replacements 116 $replace = array( 117 '@ID@' => $ID, 118 '@PAGE@' => '{PAGENO}', 119 '@PAGES@' => '{nb}', 120 '@TITLE@' => $title, 121 '@WIKI@' => $conf['title'], 122 '@WIKIURL@' => DOKU_URL, 123 '@UPDATE@' => dformat($iddata['modified']), 124 '@PAGEURL@' => wl($ID,array('rev'=>$REV), true, "&"), 125 '@DATE@' => dformat(time()), 126 ); 127 128 // do the replacements 129 $fo = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd")); 130 $fe = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even")); 131 $ho = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd")); 132 $he = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even")); 133 134 // set the headers/footers 135 $mpdf->SetHeader($ho); 136 $mpdf->SetHeader($he, 'E'); 137 $mpdf->SetFooter($fo); 138 $mpdf->SetFooter($fe, 'E'); 139 140 // title 141 $mpdf->SetTitle($title); 142 } 143 144 /** 145 * Fix up the HTML a bit 146 * 147 * FIXME This is far from perfect and will modify things within code and 148 * nowiki blocks. It would probably be a good idea to use a real HTML 149 * parser or our own renderer instead of modifying the HTML at all. 150 */ 151 protected function arrangeHtml(&$html, $norendertags = '' ) { 152 // add bookmark links 153 $bmlevel = $this->getConf('maxbookmarks'); 154 if($bmlevel > 0) { 155 $html = preg_replace("/\<a name=(.+?)\>(.+?)\<\/a\>/s",'$2',$html); 156 for ($j = 1; $j<=$bmlevel; $j++) { 157 $html = preg_replace("/\<h".$j."\>(.+?)\<\/h".$j."\>/s",'<h'.$j.'>$1<bookmark content="$1" level="'.($j-1).'"/></h'.$j.'>',$html); 158 } 159 } 160 161 // insert a pagebreak for support of WRAP and PAGEBREAK plugins 162 $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html); 163 $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html); 164 $html = str_replace('<span class="wrap_pagebreak"></span>','<pagebreak />',$html); 165 166 // Customized to strip all span tags so that the wiki <code> SQL would display properly 167 $norender = explode(',',$this->getConf('norender')); 168 $this->strip_only($html, $norender); 169#FIXME why?? $html = $this->strip_htmlencodedchars($html); 170 171 $html = str_replace('href="/','href="http://'.$_SERVER['HTTP_HOST'].'/',$html); 172 173 } 174 175 /** 176 * Create the citation box 177 * 178 * @todo can we drop the inline style here? 179 */ 180 protected function citation($page) { 181 global $conf; 182 183 $date = filemtime(wikiFN($page)); 184 $html = ''; 185 $html .= "<br><br><div style='font-size: 80%; border: solid 0.5mm #DDDDDD;background-color: #EEEEEE; padding: 2mm; border-radius: 2mm 2mm; width: 100%;'>"; 186 $html .= "From:<br>"; 187 $html .= "<a href='".DOKU_URL."'>".DOKU_URL."</a> - "."<b>".$conf['title']."</b>"; 188 $html .= "<br><br>Permanent link:<br>"; 189 $html .= "<b><a href='".wl($page, false, true, "&")."'>".wl($page, false, true, "&")."</a></b>"; 190 $html .= "<br><br>Last update: <b>".dformat($date)."</b><br>"; 191 $html .= "</div>"; 192 return $html; 193 } 194 195 /** 196 * Strip unwanted tags 197 * 198 * @fixme could this be done by strip_tags? 199 * @author Jared Ong 200 */ 201 protected function strip_only(&$str, $tags) { 202 if(!is_array($tags)) { 203 $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 204 if(end($tags) == '') array_pop($tags); 205 } 206 foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 207 } 208 209 /** 210 * Replace ' " > < & 211 * 212 * @fixme do we really need this? wouldn't this break things? 213 */ 214 protected function strip_htmlencodedchars(&$str) { 215 $str = str_replace(''', '\'', $str); 216 $str = str_replace('"', '"', $str); 217 $str = str_replace('>', '>', $str); 218 $str = str_replace('<', '<', $str); 219 $str = str_replace('&', '&', $str); 220 } 221} 222