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 __construct($functions = null) 13 { 14 $this->functions = $functions; 15 } 16 17 public function createPDFFromFile($filename, &$NAME) { 18 19 global $INPUT; 20 21 if (!preg_match("/" . $this->functions->settings->fileType . "$/", $NAME)) { 22 $this->functions->debug->message("Filetype " . $this->functions->settings->fileType . " did not match filename '$NAME'", null, 4); 23 return false; 24 } 25 26 $mpdf = new siteexportPDF($this->functions->debug); 27 28 if (!$mpdf) { 29 $this->functions->debug->message("Could not instantiate MPDF", null, 4); 30 return false; 31 } 32 33 $html = @file_get_contents($filename); 34 35 if (!strstr($html, "<html")) { 36 $this->functions->debug->message("Filecontent had no HTML starting tag", null, 4); 37 return false; 38 } 39 40 // Save HTML too 41 $this->functions->debug->message("Arranging HTML", null, 2); 42 $this->arrangeHtml($html, 'bl,acronym'); 43 $this->functions->debug->message("Done arranging HTML:", $html, 1); 44 45 $mpdf->debug = false; 46 $mpdf->list_indent_first_level = 1; // Indents the first level of lists. 47 48 $mpdf->usepre = false; 49 $mpdf->margin_bottom_collapse = true; 50 $mpdf->SetDisplayMode('fullpage'); 51 $mpdf->restoreBlockPageBreaks = true; 52 53 $mpdf->dpi = $INPUT->int('dpi', 96, true); 54 $mpdf->img_dpi = $INPUT->int('dpi', 96, true); 55 56 $mpdf->setBasePath(empty($this->functions->settings->depth) ? './' : $this->functions->settings->depth); 57 58 $mpdf->ignore_invalid_utf8 = true; 59 $mpdf->mirrorMargins = $this->functions->getConf('useOddEven'); // don't mirror margins 60 $mpdf->setAutoTopMargin = 'pad'; 61 $mpdf->setAutoBottomMargin = 'pad'; 62 63 $mpdf->WriteHTML($html); 64 $mpdf->Output($filename, "F"); 65 66 return $html; 67 } 68 69 private function arrangeHtml(&$html, $norendertags = '') 70 { 71 global $conf; 72 73 // add bookmark links 74 $html = preg_replace_callback("/<h(\d)(.*?)>(.*?)<\/h\\1>/s", array($this, '__pdfHeaderCallback'), $html); 75 $html = preg_replace_callback("/<\/div>\s*?<h({$conf['plugin']['siteexport']['PDFHeaderPagebreak']})(.*?)>/s", array($this, '__pdfHeaderCallbackPagebreak'), $html); 76 $html = preg_replace("/(<img.*?mediacenter.*?\/>)/", "<table style=\"width:100%; border: 0px solid #000;\"><tr><td style=\"text-align: center\">$1</td></tr></table>", $html); 77 78 // Remove p arround img and table 79 $html = preg_replace("/<p[^>]*?>(\s*?<img[^>]*?\/?>\s*?)<\/p>/s", "$1", $html); 80 $html = preg_replace("/<p[^>]*?>(\s*?<table.*?<\/table>\s*?)<\/p>/s", "$1", $html); 81 $html = preg_replace_callback("/<pre(.*?)>(.*?)<\/pre>/s", array($this, '__pdfPreCodeCallback'), $html); 82 $html = preg_replace_callback("/<a href=\"mailto:(.*?)\".*?>(.*?)<\/a>/s", array($this, '__pdfMailtoCallback'), $html); 83 /**/ 84 85 $standardReplacer = array( 86 // insert a pagebreak for support of WRAP and PAGEBREAK plugins 87 '<br style="page-break-after:always;">' => '<pagebreak />', 88 '<div class="wrap_pagebreak"></div>' => '<pagebreak />', 89 '<sup>' => '<sup class="sup">', 90 '<sub>' => '<sub class="sub">', 91 '<code>' => '<code class="code">' 92 ); 93 $html = str_replace(array_keys($standardReplacer), array_values($standardReplacer), $html); 94 95 // thanks to Jared Ong 96 // Customized to strip all span tags so that the wiki <code> SQL would display properly 97 $norender = explode(',', $norendertags); 98 $html = $this->strip_only($html, $norender); //array('span','acronym')); 99 $html = $this->strip_htmlencodedchars($html); 100 // Customized to strip all span tags so that the wiki <code> SQL would display properly 101 } 102 103 private function __pdfMailtoCallback($DATA) { 104 if ($DATA[1] == $DATA[2]) { 105 $DATA[2] = $this->deobfuscate($DATA[2]); 106 } 107 $DATA[1] = $this->deobfuscate($DATA[1]); 108 return "<a href=\"mailto:{$DATA[1]}\">{$DATA[2]}</a>"; 109 } 110 111 private function __pdfPreCodeCallback($DATA) { 112 113 $code = nl2br($DATA[2]); 114 $code = preg_replace_callback("/(^|<br \/>)(\s+)(\S)/s", array($this, '__pdfPreWhitespacesCallback'), $code); 115 116 return "\n<pre" . $DATA[1] . ">\n" . $code . "\n</pre>\n"; 117 } 118 119 private function __pdfPreWhitespacesCallback($DATA) { 120 return $DATA[1] . "\n" . str_repeat(" ", strlen($DATA[2])-($DATA[2][0] == "\n" ? 1 : 0)) . $DATA[3]; 121 } 122 123 private function __pdfHeaderCallback($DATA) { 124 $contentText = htmlspecialchars_decode(preg_replace("/<\/?.*?>/s", '', $DATA[3]), ENT_NOQUOTES); // 2014-07-23 Do not encode again. or ä -> &auml; 125 return '<h' . $DATA[1] . $DATA[2] . '><tocentry content="' . $contentText . '" level="' . ($DATA[1]-1) . '" /><bookmark content="' . $contentText . '" level="' . ($DATA[1]-1) . '" />' . $DATA[3] . '</h' . $DATA[1] . '>'; 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 return $str; 148 } 149 // Custom function for help in replacing ' " > < & 150 151 /** 152 * return an de-obfuscated email address in line with $conf['mailguard'] setting 153 */ 154 private function deobfuscate($email) { 155 global $conf; 156 157 switch ($conf['mailguard']) { 158 case 'visible' : 159 return /** @scrutinizer ignore-call */ strtr($email, array(' [at] ' => '@', ' [dot] ' => '.', ' [dash] ' => '-')); 160 161 case 'hex' : 162 $encode = ''; 163 $len = strlen($email); 164 for ($x = 0; $x < $len; $x += 6) { 165 $encode .= chr((int)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