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 $html = '<html><head>'; 79 $html = $html . "<style> 80 table { 81 border: 1px solid #808080; 82 border-collapse: collapse; 83 } 84 td, th { 85 border: 1px solid #808080; 86 }"; 87 88 //load userdefined CSS? 89 if ($this->getConf("loadusercss") && @file_exists(DOKU_PLUGIN.'dw2pdf/user/user.css')) { 90 $html = $html . io_readFile(DOKU_PLUGIN.'dw2pdf/user/user.css'); 91 } 92 $html = $html . "</style>"; 93 $html = $html . '</head><body>'; 94 95 96 if ( $ACT == 'export_pdf' ) { 97 $list = array(); 98 $list[0] = $ID; 99 } else { 100 if (isset($_COOKIE['list-pagelist'])) { 101 $list = explode("|", $_COOKIE['list-pagelist']); 102 } 103 if ($_GET['pdfbook_title']) { 104 $pdftitle = $_GET['pdfbook_title']; 105 } else { 106 $pdftitle = $conf['title']; 107 } 108 } 109 110 for ($n = 0; $n < count($list); $n++) { 111 $page = $list[$n]; 112 113 $idparam = $page; 114 if ($REV != 0) { $idparam = $idparam."&rev=".$REV; }; 115 116 $pos = strrpos(utf8_decode($ID), ':'); 117 $pageName = p_get_first_heading($ID); 118 if($pageName == NULL) { 119 if($pos != FALSE) { 120 $pageName = utf8_substr($page, $pos+1, utf8_strlen($page)); 121 } else { 122 $pageName = $page; 123 } 124 $pageName = str_replace('_', ' ', $pageName); 125 } 126 127 $iddata = p_get_metadata($page,'date'); 128 129 $html = $html . p_wiki_xhtml($page,$REV,false); 130 131 if ($n == 0) { 132 // standard replacements 133 $replace = array( 134 '@ID@' => $ID, 135 '@PAGE@' => '{PAGENO}', 136 '@PAGES@' => '{nb}', 137 '@TITLE@' => $pageName, 138 '@WIKI@' => $conf['title'], 139 '@WIKIURL@' => DOKU_URL, 140 '@UPDATE@' => dformat($iddata['modified']), 141 '@PAGEURL@' => wl($idparam, false, true, "&"), 142 '@DATE@' => strftime($conf['dformat']), 143 ); 144 145 // do the replace 146 $footer_odd = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_odd")); 147 $footer_even = str_replace(array_keys($replace), array_values($replace), $this->getConf("footer_even")); 148 $header_odd = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_odd")); 149 $header_even = str_replace(array_keys($replace), array_values($replace), $this->getConf("header_even")); 150 151 $mpdf->SetHeader($header_odd); 152 $mpdf->SetHeader($header_even, 'E'); 153 154 $mpdf->SetFooter($footer_odd); 155 $mpdf->SetFooter($footer_even, 'E'); 156 } 157 158 $html = $this->citation($html, $conf['title'], $idparam, $iddata, $this->getConf('addcitation')); 159 160 if ($n < (count($list) - 1)) $html = $html . "<pagebreak />"; 161 162 } 163 164 $html = $this->arrangeHtml($html, $this->getConf("maxbookmarks"), $this->getConf("norender")); 165 166 $mpdf->SetTitle($pageName); 167 $mpdf->WriteHTML($html); 168 169 if (count($list) == 1) $pdftitle = $pageName; 170 171 $output = 'I'; 172 if($this->getConf('output') == 'file') $output = 'D'; 173 $mpdf->Output(urlencode($pdftitle).'.pdf', $output); 174 175 die(); 176 } 177 } 178 179 // thanks to Jared Ong 180 // Custom function for help in stripping span tags 181 function strip_only($str, $tags) { 182 if(!is_array($tags)) { 183 $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 184 if(end($tags) == '') array_pop($tags); 185 } 186 foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 187 return $str; 188 } 189 // Custom function for help in stripping span tags 190 191 // Custom function for help in replacing ' " > < & 192 function strip_htmlencodedchars($str) { 193 $str = str_replace(''', '\'', $str); 194 $str = str_replace('"', '"', $str); 195 $str = str_replace('>', '>', $str); 196 $str = str_replace('<', '<', $str); 197 $str = str_replace('&', '&', $str); 198 return $str; 199 } 200 // Custom function for help in replacing ' " > < & 201 202 203 function arrangeHtml($html, $bookmark = 0, $norendertags = '' ) { 204 205 // add bookmark links 206 if ($bookmark > 0) { 207 $html = preg_replace("/\<a name=(.+?)\>(.+?)\<\/a\>/s",'$2',$html); 208 for ($j = 1; $j<=$bookmark; $j++) { 209 $html = preg_replace("/\<h".$j."\>(.+?)\<\/h".$j."\>/s",'<h'.$j.'>$1<bookmark content="$1" level="'.($j-1).'"/></h'.$j.'>',$html); 210 } 211 } 212 // add bookmark links 213 214 // insert a pagebreak for support of WRAP and PAGEBREAK plugins 215 $html = str_replace('<br style="page-break-after:always;">','<pagebreak />',$html); 216 $html = str_replace('<div class="wrap_pagebreak"></div>','<pagebreak />',$html); 217 218 // thanks to Jared Ong 219 // Customized to strip all span tags so that the wiki <code> SQL would display properly 220 $norender = explode(',',$norendertags); 221 $html = $this->strip_only($html, $norender ); //array('span','acronym')); 222 $html = $this->strip_htmlencodedchars($html); 223 // Customized to strip all span tags so that the wiki <code> SQL would display properly 224 225 $html = str_replace('href="/','href="http://'.$_SERVER['HTTP_HOST'].'/',$html); 226 227 return $html; 228 } 229 230 function citation($html, $title, $idparam, $date, $flag = false) { 231 232 if($flag) { 233 $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%;'>"; 234 $html = $html . "From:<br>"; 235 $html = $html . "<a href='".DOKU_URL."'>".DOKU_URL."</a> - "."<b>".$title."</b>"; 236 $html = $html . "<br><br>Permanent link:<br>"; 237 $html = $html . "<b><a href='".wl($idparam, false, true, "&")."'>".wl($idparam, false, true, "&")."</a></b>"; 238 $html = $html . "<br><br>Last update: <b>".dformat($date['modified'])."</b><br>"; 239 $html = $html . "</div>"; 240 } 241 return $html; 242 } 243 244} 245?> 246