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 if (!preg_match("/" . $this->functions->settings->fileType . "$/", $NAME)) { 20 $this->functions->debug->message("Filetype " . $this->functions->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 46 $mpdf->usepre = false; 47 $mpdf->margin_bottom_collapse = true; 48 $mpdf->SetDisplayMode('fullpage'); 49 $mpdf->restoreBlockPageBreaks = true; 50 $mpdf->img_dpi = 300; 51 52 $mpdf->setBasePath(empty($this->functions->settings->depth) ? './' : $this->functions->settings->depth); 53 54 $mpdf->ignore_invalid_utf8 = true; 55 $mpdf->mirrorMargins = $this->functions->getConf('useOddEven'); // don't mirror margins 56 $mpdf->setAutoTopMargin = 'pad'; 57 $mpdf->setAutoBottomMargin = 'pad'; 58 59 $mpdf->WriteHTML($html); 60 $mpdf->Output($filename, "F"); 61 62 return $html; 63 } 64 65 private function arrangeHtml(&$html, $norendertags = '') 66 { 67 global $conf; 68 69 // add bookmark links 70 $html = preg_replace_callback("/<h(\d)(.*?)>(.*?)<\/h\\1>/s", array($this, '__pdfHeaderCallback'), $html); 71 $html = preg_replace_callback("/<\/div>\s*?<h({$conf['plugin']['siteexport']['PDFHeaderPagebreak']})(.*?)>/s", array($this, '__pdfHeaderCallbackPagebreak'), $html); 72 $html = preg_replace("/(<img.*?mediacenter.*?\/>)/", "<table style=\"width:100%; border: 0px solid #000;\"><tr><td style=\"text-align: center\">$1</td></tr></table>", $html); 73 74 // Remove p arround img and table 75 $html = preg_replace("/<p[^>]*?>(\s*?<img[^>]*?\/?>\s*?)<\/p>/s", "$1", $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 $contentText = htmlspecialchars_decode(preg_replace("/<\/?.*?>/s", '', $DATA[3]), ENT_NOQUOTES); // 2014-07-23 Do not encode again. or ä -> &auml; 121 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] . '>'; 122 } 123 124 private function __pdfHeaderCallbackPagebreak($DATA) { 125 return '</div>' . "\r\n" . '<pagebreak />' . "\r\n\r\n" . '<h' . $DATA[1] . $DATA[2] . '>'; 126 } 127 // thanks to Jared Ong 128 // Custom function for help in stripping span tags 129 private function strip_only($str, $tags) { 130 if (!is_array($tags)) { 131 $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags)); 132 if (end($tags) == '') array_pop($tags); 133 } 134 135 foreach ($tags as $tag) $str = preg_replace('#</?' . $tag . '[^>]*>#is', '', $str); 136 return $str; 137 } 138 // Custom function for help in stripping span tags 139 140 // Custom function for help in replacing ' " > < & 141 private function strip_htmlencodedchars($str) { 142 $str = str_replace(''', '\'', $str); 143 return $str; 144 } 145 // Custom function for help in replacing ' " > < & 146 147 /** 148 * return an de-obfuscated email address in line with $conf['mailguard'] setting 149 */ 150 private function deobfuscate($email) { 151 global $conf; 152 153 switch ($conf['mailguard']) { 154 case 'visible' : 155 return /** @scrutinizer ignore-call */ strtr($email, array(' [at] ' => '@', ' [dot] ' => '.', ' [dash] ' => '-')); 156 157 case 'hex' : 158 $encode = ''; 159 $len = strlen($email); 160 for ($x = 0; $x < $len; $x += 6) { 161 $encode .= chr((int)hexdec($email{$x+3} . $email{($x+4)})); 162 } 163 return $encode; 164 165 case 'none' : 166 default : 167 return $email; 168 } 169 } 170 } 171} 172