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 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 //* 120 $contentText = preg_replace("/<\/?.*?>/s", '', $DATA[3]); // 2014-07-23 Do not encode again. 121 /*/ 122 $contentText = $this->xmlEntities(preg_replace("/<\/?.*?>/s", '', $DATA[3])); // Double encoding - has to be decoded in mpdf once more. 123 //*/ 124 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] . '>'; 125 } 126 127 private function __pdfHeaderCallbackPagebreak($DATA) { 128 return '</div>' . "\r\n" . '<pagebreak />' . "\r\n\r\n" . '<h' . $DATA[1] . $DATA[2] . '>'; 129 } 130 // thanks to Jared Ong 131 // Custom function for help in stripping span tags 132 private function strip_only($str, $tags) { 133 if(!is_array($tags)) { 134 $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 135 if(end($tags) == '') array_pop($tags); 136 } 137 138 foreach($tags as $tag) $str = preg_replace('#</?'.$tag.'[^>]*>#is', '', $str); 139 return $str; 140 } 141 // Custom function for help in stripping span tags 142 143 // Custom function for help in replacing ' " > < & 144 private function strip_htmlencodedchars($str) { 145 $str = str_replace(''', '\'', $str); 146 // $str = str_replace('"', '"', $str); 147 // $str = str_replace('>', '>', $str); 148 // $str = str_replace('<', '<', $str); 149 // $str = str_replace('&', '&', $str); 150 return $str; 151 } 152 // Custom function for help in replacing ' " > < & 153 154 /** 155 * return an de-obfuscated email address in line with $conf['mailguard'] setting 156 */ 157 private function deobfuscate($email) { 158 global $conf; 159 160 switch ($conf['mailguard']) { 161 case 'visible' : 162 $obfuscate = array(' [at] ' => '@', ' [dot] ' => '.', ' [dash] ' => '-'); 163 return strtr($email, $obfuscate); 164 165 case 'hex' : 166 $encode = ''; 167 $len = strlen($email); 168 for ($x=0; $x < $len; $x+=6){ 169 $encode .= chr(hexdec($email{$x+3}.$email{($x+4)})); 170 } 171 return $encode; 172 173 case 'none' : 174 default : 175 return $email; 176 } 177 } 178 179 /** 180 * Encoding ()taken from DW - but without needing the renderer 181 **/ 182 private function xmlEntities($string) { 183 return htmlspecialchars($string,ENT_QUOTES,'UTF-8'); 184 } 185 } 186} 187?>