1<?php 2 3if(!defined('DOKU_PLUGIN')) die('meh'); 4 5if ( !empty($_REQUEST['pdfExport']) && 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 $mpdf->useOddEven = 1; 58 59 $mpdf->WriteHTML($html); 60 $mpdf->Output($filename, "F"); 61 62 $this->functions->debug->message("Used images:", $mpdf->images, 1); 63 $this->functions->debug->message("Failed images:", $mpdf->failedimages, 1); 64 65 return $html; 66 } 67 68 function arrangeHtml(&$html, $norendertags = '' ) 69 { 70 global $conf; 71 72 // add bookmark links 73 $html = preg_replace_callback("/<h(\d)(.*?)>(.*?)<\/h\\1>/s", array($this, '__pdfHeaderCallback'), $html); 74 $html = preg_replace_callback("/<\/div>\s*?<h({$conf['plugin']['siteexport']['PDFHeaderPagebreak']})(.*?)>/s", array($this, '__pdfHeaderCallbackPagebreak'), $html); 75 $html = preg_replace("/(<img.*?mediacenter.*?\/>)/", "<table style=\"width:100%; border: 0px solid #000;\"><tr><td style=\"text-align: center\">$1</td></tr></table>", $html); 76 $html = preg_replace("/<p>(\s*?<table.*?<\/table>\s*?)<\/p>/s", "$1", $html); 77 $html = preg_replace_callback("/<pre(.*?)>(.*?)<\/pre>/s", array($this, '__pdfPreCodeCallback'), $html); 78 $html = preg_replace_callback("/<a href=\"mailto:(.*?)\".*?>(.*?)<\/a>/s", array($this, '__pdfMailtoCallback'), $html); 79 /**/ 80 81 $standardReplacer = array ( 82 // insert a pagebreak for support of WRAP and PAGEBREAK plugins 83 '<br style="page-break-after:always;">' => '<pagebreak />', 84 '<div class="wrap_pagebreak"></div>' => '<pagebreak />', 85 '<sup>' => '<sup class="sup">', 86 '<sub>' => '<sub class="sub">', 87 '<code>' => '<code class="code">' 88 ); 89 $html = str_replace(array_keys($standardReplacer), array_values($standardReplacer), $html); 90 91 // thanks to Jared Ong 92 // Customized to strip all span tags so that the wiki <code> SQL would display properly 93 $norender = explode(',',$norendertags); 94 $html = $this->strip_only($html, $norender ); //array('span','acronym')); 95 $html = $this->strip_htmlencodedchars($html); 96 // Customized to strip all span tags so that the wiki <code> SQL would display properly 97 } 98 99 private function __pdfMailtoCallback($DATA) { 100 if ( $DATA[1] == $DATA[2] ) { 101 $DATA[2] = $this->deobfuscate($DATA[2]); 102 } 103 $DATA[1] = $this->deobfuscate($DATA[1]); 104 return "<a href=\"mailto:{$DATA[1]}\">{$DATA[2]}</a>"; 105 } 106 107 private function __pdfPreCodeCallback($DATA) { 108 109 $code = nl2br($DATA[2]); 110 $code = preg_replace_callback("/(^|<br \/>)(\s+)(\S)/s", array($this, '__pdfPreWhitespacesCallback'), $code); 111 112 return "\n<pre" . $DATA[1] . ">\n" . $code . "\n</pre>\n"; 113 } 114 115 private function __pdfPreWhitespacesCallback( $DATA ) { 116 return $DATA[1] . "\n" . str_repeat(" ", strlen($DATA[2])-($DATA[2]{0}=="\n"?1:0) ) . $DATA[3]; 117 } 118 119 private function __pdfHeaderCallback($DATA) { 120 //* 121 $contentText = htmlspecialchars_decode(preg_replace("/<\/?.*?>/s", '', $DATA[3]), ENT_NOQUOTES); // 2014-07-23 Do not encode again. or ä -> &auml; 122 /*/ 123 $contentText = $this->xmlEntities(preg_replace("/<\/?.*?>/s", '', $DATA[3])); // Double encoding - has to be decoded in mpdf once more. 124 //*/ 125 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] . '></bookmark></tocentry>'; 126 } 127 128 private function __pdfHeaderCallbackPagebreak($DATA) { 129 return '</div>' . "\r\n" . '<pagebreak />' . "\r\n\r\n" . '<h' . $DATA[1] . $DATA[2] . '>'; 130 } 131 // thanks to Jared Ong 132 // Custom function for help in stripping span tags 133 private function strip_only($str, $tags) { 134 if(!is_array($tags)) { 135 $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 136 if(end($tags) == '') array_pop($tags); 137 } 138 139 foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 140 return $str; 141 } 142 // Custom function for help in stripping span tags 143 144 // Custom function for help in replacing ' " > < & 145 private function strip_htmlencodedchars($str) { 146 $str = str_replace(''', '\'', $str); 147 // $str = str_replace('"', '"', $str); 148 // $str = str_replace('>', '>', $str); 149 // $str = str_replace('<', '<', $str); 150 // $str = str_replace('&', '&', $str); 151 return $str; 152 } 153 // Custom function for help in replacing ' " > < & 154 155 /** 156 * return an de-obfuscated email address in line with $conf['mailguard'] setting 157 */ 158 private function deobfuscate($email) { 159 global $conf; 160 161 switch ($conf['mailguard']) { 162 case 'visible' : 163 $obfuscate = array(' [at] ' => '@', ' [dot] ' => '.', ' [dash] ' => '-'); 164 return strtr($email, $obfuscate); 165 166 case 'hex' : 167 $encode = ''; 168 $len = strlen($email); 169 for ($x=0; $x < $len; $x+=6){ 170 $encode .= chr(hexdec($email{$x+3}.$email{($x+4)})); 171 } 172 return $encode; 173 174 case 'none' : 175 default : 176 return $email; 177 } 178 } 179 180 /** 181 * Encoding ()taken from DW - but without needing the renderer 182 **/ 183 private function xmlEntities($string) { 184 return htmlspecialchars($string,ENT_QUOTES,'UTF-8'); 185 } 186 } 187} 188?>