1<?php 2 3if(!defined('DOKU_PLUGIN')) die('meh'); 4 5if ( intval($_REQUEST['pdfExport']) == 1 && file_exists(DOKU_PLUGIN . 'dw2pdf/mpdf/mpdf.php') ) { 6 7 require_once(DOKU_PLUGIN . 'siteexport/inc/mpdf.php'); 8 class siteexport_pdfgenerator 9 { 10 private $functions; 11 12 public function siteexport_pdfgenerator( $functions=null ) 13 { 14 $this->functions = $functions; 15 } 16 17 function createPDFFromFile($filename, &$NAME) { 18 19 if ( !preg_match("/" . $this->settings->fileType . "$/", $NAME) ) { 20 $this->functions->debug->message("Filetype {$this->settings->fileType} did not match filename '$NAME'", null, 4); 21 return false; 22 } 23 24 $mpdf = new siteexportPDF($this->functions->debug); 25 26 if ( !$mpdf ) { 27 $this->functions->debug->message("Could not instantiate MPDF", null, 4); 28 return false; 29 } 30 31 $html = file_get_contents($filename); 32 33 if ( !strstr($html, "<html") ) { 34 $this->functions->debug->message("Filecontent had no HTML starting tag", null, 4); 35 return false; 36 } 37 38 // Save HTML too 39 $this->functions->debug->message("Arranging HTML", null, 2); 40 $this->arrangeHtml($html, 'bl,acronym'); 41 $this->functions->debug->message("Done arranging HTML:", $html, 1); 42 43 $mpdf->debug = false; 44 $mpdf->list_indent_first_level = 1; // Indents the first level of lists. 45 //$mpdf->SetBasePath("/"); 46 $mpdf->usepre = false; 47 $mpdf->margin_bottom_collapse = true; 48 $mpdf->SetDisplayMode('fullpage'); 49 $mpdf->restoreBlockPageBreaks = true; 50 $this->img_dpi = 300; 51 52 $mpdf->setBasePath(empty($this->functions->settings->depth) ? './' : $this->functions->settings->depth); 53 $mpdf->SetAutoFont(AUTOFONT_ALL); 54 55 $mpdf->ignore_invalid_utf8 = true; 56 $mpdf->mirrorMargins = 0; // don't mirror margins 57 58 $mpdf->WriteHTML($html); 59 $mpdf->Output($filename, "F"); 60 61 $this->functions->debug->message("Used images:", $mpdf->images, 1); 62 $this->functions->debug->message("Failed images:", $mpdf->failedimages, 1); 63 64 return true; 65 } 66 67 function arrangeHtml(&$html, $norendertags = '' ) 68 { 69 global $conf; 70 71 // add bookmark links 72 $html = preg_replace_callback("/<h(\d)(.*?)>(.+?)<\/h\\1>/s", array($this, '__pdfHeaderCallback'), $html); 73 $html = preg_replace_callback("/<\/div>\s*?<h({$conf['plugin']['siteexport']['PDFHeaderPagebreak']})(.*?)>/s", array($this, '__pdfHeaderCallbackPagebreak'), $html); 74 $html = preg_replace("/(<img.*?mediacenter.*?\/>)/", "<table style=\"width:100%; border: 0px solid #000;\"><tr><td style=\"text-align: center\">$1</td></tr></table>", $html); 75 $html = preg_replace("/<p>(\s*?<table.*?<\/table>\s*?)<\/p>/s", "$1", $html); 76 $html = preg_replace_callback("/<pre(.*?)>(.*?)<\/pre>/s", array($this, '__pdfPreCodeCallback'), $html); 77 $html = preg_replace_callback("/<a href=\"mailto:(.*?)\".*?>(.*?)<\/a>/s", array($this, '__pdfMailtoCallback'), $html); 78 /**/ 79 80 $standardReplacer = array ( 81 // insert a pagebreak for support of WRAP and PAGEBREAK plugins 82 '<br style="page-break-after:always;">' => '<pagebreak />', 83 '<div class="wrap_pagebreak"></div>' => '<pagebreak />', 84 '<sup>' => '<sup class="sup">', 85 '<sub>' => '<sub class="sub">', 86 '<code>' => '<code class="code">' 87 ); 88 $html = str_replace(array_keys($standardReplacer), array_values($standardReplacer), $html); 89 90 // thanks to Jared Ong 91 // Customized to strip all span tags so that the wiki <code> SQL would display properly 92 $norender = explode(',',$norendertags); 93 $html = $this->strip_only($html, $norender ); //array('span','acronym')); 94 $html = $this->strip_htmlencodedchars($html); 95 // Customized to strip all span tags so that the wiki <code> SQL would display properly 96 } 97 98 private function __pdfMailtoCallback($DATA) { 99 if ( $DATA[1] == $DATA[2] ) { 100 $DATA[2] = $this->deobfuscate($DATA[2]); 101 } 102 $DATA[1] = $this->deobfuscate($DATA[1]); 103 return "<a href=\"mailto:{$DATA[1]}\">{$DATA[2]}</a>"; 104 } 105 106 private function __pdfPreCodeCallback($DATA) { 107 108 $code = nl2br($DATA[2]); 109 $code = preg_replace_callback("/(^|<br \/>)(\s+)(\S)/s", array($this, '__pdfPreWhitespacesCallback'), $code); 110 111 return "\n<pre" . $DATA[1] . ">\n" . $code . "\n</pre>\n"; 112 } 113 114 private function __pdfPreWhitespacesCallback( $DATA ) { 115 return $DATA[1] . "\n" . str_repeat(" ", strlen($DATA[2])-($DATA[2]{0}=="\n"?1:0) ) . $DATA[3]; 116 } 117 118 private function __pdfHeaderCallback($DATA) { 119 $contentText = $this->xmlEntities(preg_replace("/<\/?.*?>/s", '', $DATA[3])); // Double encoding - has to be decoded in mpdf once more. 120 return '<tocentry content="' . $contentText . '" level="' . ($DATA[1]-1) . '" /><bookmark content="' . $contentText . '" level="' . ($DATA[1]-1) . '" /><h' . $DATA[1] . $DATA[2] . '>' . $DATA[3] . '</h' . $DATA[1] . '>'; 121 } 122 123 private function __pdfHeaderCallbackPagebreak($DATA) { 124 return '</div>' . "\r\n" . '<pagebreak />' . "\r\n\r\n" . '<h' . $DATA[1] . $DATA[2] . '>'; 125 } 126 // thanks to Jared Ong 127 // Custom function for help in stripping span tags 128 private function strip_only($str, $tags) { 129 if(!is_array($tags)) { 130 $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 131 if(end($tags) == '') array_pop($tags); 132 } 133 134 foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 135 return $str; 136 } 137 // Custom function for help in stripping span tags 138 139 // Custom function for help in replacing ' " > < & 140 private function strip_htmlencodedchars($str) { 141 $str = str_replace(''', '\'', $str); 142 // $str = str_replace('"', '"', $str); 143 // $str = str_replace('>', '>', $str); 144 // $str = str_replace('<', '<', $str); 145 // $str = str_replace('&', '&', $str); 146 return $str; 147 } 148 // Custom function for help in replacing ' " > < & 149 150 /** 151 * return an de-obfuscated email address in line with $conf['mailguard'] setting 152 */ 153 private function deobfuscate($email) { 154 global $conf; 155 156 switch ($conf['mailguard']) { 157 case 'visible' : 158 $obfuscate = array(' [at] ' => '@', ' [dot] ' => '.', ' [dash] ' => '-'); 159 return strtr($email, $obfuscate); 160 161 case 'hex' : 162 $encode = ''; 163 $len = strlen($email); 164 for ($x=0; $x < $len; $x+=6){ 165 $encode .= chr(hexdec($email{$x+3}.$email{($x+4)})); 166 } 167 return $encode; 168 169 case 'none' : 170 default : 171 return $email; 172 } 173 } 174 175 /** 176 * Encoding ()taken from DW - but without needing the renderer 177 **/ 178 private function xmlEntities($string) { 179 return htmlspecialchars($string,ENT_QUOTES,'UTF-8'); 180 } 181 } 182} 183?>