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 */ 8 9// must be run within Dokuwiki 10if (!defined('DOKU_INC')) die(); 11if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 12 13require_once (DOKU_PLUGIN . 'action.php'); 14 15class action_plugin_dw2pdf extends DokuWiki_Action_Plugin 16{ 17 /** 18 * Constructor. 19 */ 20 function action_plugin_dw2pdf(){ 21 } 22 23 /** 24 * return some info 25 */ 26 function getInfo(){ 27 return array ( 28 'author' => 'Luigi Micco', 29 'email' => 'l.micco@tiscali.it', 30 'date' => '2010-02-04', 31 'name' => 'Dw2Pdf plugin (action component)', 32 'desc' => 'DokuWiki to Pdf converter', 33 'url' => 'http://www.bitlibero.com/dokuwiki/dw2pdf-02.04.2010.zip', 34 ); 35 } 36 37 /** 38 * Register the events 39 */ 40 function register(&$controller) 41 { 42 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'convert',array()); 43 } 44 45 function convert(&$event, $param) 46 { 47 global $ACT; 48 global $REV; 49 global $ID; 50 global $conf; 51 52 if (( $ACT == 'export_pdfbook' ) || ( $ACT == 'export_pdf' )) { 53 // check user's rights 54 if ( auth_quickaclcheck($ID) < AUTH_READ ) { 55 return false; 56 } 57 58 $event->preventDefault(); 59 60 require_once(dirname(__FILE__)."/mpdf/mpdf.php"); 61 $mpdf=new mPDF('UTF-8-s'); 62 $mpdf->SetAutoFont(AUTOFONT_ALL); 63 64 // Temp dir 65 define("_MPDF_TEMP_PATH", $conf['savedir'].'/tmp/'); 66 67 $mpdf->ignore_invalid_utf8 = true; 68 $mpdf->mirrorMargins = 1; // Use different Odd/Even headers and footers and mirror margins 69 70 $mpdf->defaultheaderfontsize = 8; /* in pts */ 71 $mpdf->defaultheaderfontstyle = ''; /* blank, B, I, or BI */ 72 $mpdf->defaultheaderline = 1; /* 1 to include line below header/above footer */ 73 74 $mpdf->defaultfooterfontsize = 8; /* in pts */ 75 $mpdf->defaultfooterfontstyle = ''; /* blank, B, I, or BI */ 76 $mpdf->defaultfooterline = 1; /* 1 to include line below header/above footer */ 77 78 $mpdf->basepathIsLocal = 1; 79 80 $html = '<html><head>'; 81 $html = $html . "<style> 82 table { 83 border: 1px solid #808080; 84 border-collapse: collapse; 85 } 86 td, th { 87 border: 1px solid #808080; 88 }"; 89 90 //load userdefined CSS? 91 if ($this->getConf("loadusercss") && @file_exists(DOKU_PLUGIN.'dw2pdf/user/user.css')) { 92 $html = $html . io_readFile(DOKU_PLUGIN.'dw2pdf/user/user.css'); 93 } 94 $html = $html . "</style>"; 95 $html = $html . '</head><body>'; 96 97 98 if ( $ACT == 'export_pdf' ) { 99 $list = array(); 100 $list[0] = $ID; 101 } else { 102 if (isset($_COOKIE['list-pagelist'])) { 103 $list = explode("|", $_COOKIE['list-pagelist']); 104 } 105 if ($_GET['pdfbook_title']) { 106 $pdftitle = $_GET['pdfbook_title']; 107 } else { 108 $pdftitle = $conf['title']; 109 } 110 } 111 112 for ($n = 0; $n < count($list); $n++) { 113 $page = $list[$n]; 114 115 $idparam = $page; 116 if ($REV != 0) { $idparam = $idparam."&rev=".$REV; }; 117 118 $pos = strrpos(utf8_decode($ID), ':'); 119 $pageName = p_get_first_heading($ID); 120 if($pageName == NULL) { 121 if($pos != FALSE) { 122 $pageName = utf8_substr($page, $pos+1, utf8_strlen($page)); 123 } else { 124 $pageName = $page; 125 } 126 $pageName = str_replace('_', ' ', $pageName); 127 } 128 129 $iddata = p_get_metadata($page,'date'); 130 131 $html = $html . p_wiki_xhtml($page,$REV,false); 132 133 if ($n == 0) { 134 // standard replacements 135 $replace = array( 136 '@ID@' => $ID, 137 '@PAGE@' => '{PAGENO}', 138 '@PAGES@' => '{nb}', 139 '@TITLE@' => $pageName, 140 '@WIKI@' => $conf['title'], 141 '@WIKIURL@' => DOKU_URL, 142 '@UPDATE@' => dformat($iddata['modified']), 143 '@PAGEURL@' => wl($idparam, false, true, "&"), 144 '@DATE@' => strftime($conf['dformat']), 145 ); 146 147 // do the replace 148 $footer_odd = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd")); 149 $footer_even = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even")); 150 $header_odd = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd")); 151 $header_even = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even")); 152 153 $mpdf->SetHeader($header_odd); 154 $mpdf->SetHeader($header_even, 'E'); 155 156 $mpdf->SetFooter($footer_odd); 157 $mpdf->SetFooter($footer_even, 'E'); 158 } 159 160 $html = $this->citation($html, $conf['title'], $idparam, $iddata, $this->getConf('addcitation')); 161 162 if ($n < (count($list) - 1)) $html = $html . "<pagebreak />"; 163 164 } 165 166 $html = $this->arrangeHtml($html, $this->getConf("maxbookmarks"), $this->getConf("norender")); 167 168 $mpdf->SetTitle($pageName); 169 $mpdf->WriteHTML($html); 170 171 if (count($list) == 1) $pdftitle = $pageName; 172 173 $output = 'I'; 174 if($this->getConf('output') == 'file') $output = 'D'; 175 $mpdf->Output(urlencode($pdftitle).'.pdf', $output); 176 177 die(); 178 } 179 } 180 181 // thanks to Jared Ong 182 // Custom function for help in stripping span tags 183 function strip_only($str, $tags) { 184 if(!is_array($tags)) { 185 $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 186 if(end($tags) == '') array_pop($tags); 187 } 188 foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 189 return $str; 190 } 191 // Custom function for help in stripping span tags 192 193 // Custom function for help in replacing ' " > < & 194 function strip_htmlencodedchars($str) { 195 $str = str_replace(''', '\'', $str); 196 $str = str_replace('"', '"', $str); 197 $str = str_replace('>', '>', $str); 198 $str = str_replace('<', '<', $str); 199 $str = str_replace('&', '&', $str); 200 return $str; 201 } 202 // Custom function for help in replacing ' " > < & 203 204 205 function arrangeHtml($html, $bookmark = 0, $norendertags = '' ) { 206 207 // add bookmark links 208 if ($bookmark > 0) { 209 $html = preg_replace("/\<a name=(.+?)\>(.+?)\<\/a\>/s",'$2',$html); 210 for ($j = 1; $j<=$bookmark; $j++) { 211 $html = preg_replace("/\<h".$j."\>(.+?)\<\/h".$j."\>/s",'<h'.$j.'>$1<bookmark content="$1" level="'.($j-1).'"/></h'.$j.'>',$html); 212 } 213 } 214 // add bookmark links 215 216 // insert a pagebreak for support of WRAP and PAGEBREAK plugins 217 $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html); 218 $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html); 219 220 // thanks to Jared Ong 221 // Customized to strip all span tags so that the wiki <code> SQL would display properly 222 $norender = explode(',',$norendertags); 223 $html = $this->strip_only($html, $norender ); //array('span','acronym')); 224 $html = $this->strip_htmlencodedchars($html); 225 // Customized to strip all span tags so that the wiki <code> SQL would display properly 226 227 $html = str_replace('href="/','href="http://'.$_SERVER['HTTP_HOST'].'/',$html); 228 229 global $conf; 230 $html = str_replace('src="/_media/','src="'.$conf['mediadir'].'/',$html); 231 232 return $html; 233 } 234 235 function citation($html, $title, $idparam, $date, $flag = false) { 236 237 if($flag) { 238 $html = $html . "<br><br><div style='font-size: 80%; border: solid 0.5mm #DDDDDD;background-color: #EEEEEE; padding: 2mm; border-radius: 2mm 2mm; width: 100%;'>"; 239 $html = $html . "From:<br>"; 240 $html = $html . "<a href='".DOKU_URL."'>".DOKU_URL."</a> - "."<b>".$title."</b>"; 241 $html = $html . "<br><br>Permanent link:<br>"; 242 $html = $html . "<b><a href='".wl($idparam, false, true, "&")."'>".wl($idparam, false, true, "&")."</a></b>"; 243 $html = $html . "<br><br>Last update: <b>".dformat($date['modified'])."</b><br>"; 244 $html = $html . "</div>"; 245 } 246 return $html; 247 } 248 249} 250?> 251