readConfig(''); } $this->config = $config; if (!isset($config[SYSTEM_BASE_URL_PRINT]) || $config[SYSTEM_BASE_URL_PRINT] == '') { throw new \exception(CONFIG_INI . ' - Missing ' . SYSTEM_BASE_URL_PRINT); } if (!isset($config[SYSTEM_WKHTMLTOPDF]) || $config[SYSTEM_WKHTMLTOPDF] == '') { throw new \exception(CONFIG_INI . ' - Missing ' . SYSTEM_WKHTMLTOPDF); } if (!is_executable($config[SYSTEM_WKHTMLTOPDF])) { throw new \exception(CONFIG_INI . ' - ' . SYSTEM_WKHTMLTOPDF . '=' . $config[SYSTEM_WKHTMLTOPDF] . ' - not found or not executable.'); } } /** * Return an array with GET params who are clean - they do not violate $pattern. * * @return array */ private function readCleanGetParam(array $get) { $param = array(); $pattern = '^[\-_\.,;:\/a-zA-Z0-9]*$'; // ':alnum:' does not work here in FF foreach ($get as $key => $value) { if (preg_match("/$pattern/", $value) === 1) { $param[$key] = $value; } } return $param; } /** * Set HTML Header to initiate PDF download. * * @param $filename */ private function setHeader($filename) { header("Content-Disposition: inline; filename=\"$filename\""); header("Content-Type: application/pdf"); header("Content-Transfer-Encoding: binary"); } /** * @return string * @throws \exception */ public function page2pdf(array $get) { if (!isset($get[HTML2PDF_PAGEID]) || $get[HTML2PDF_PAGEID] == '') { throw new \exception("Missing GET Parameter '" . HTML2PDF_PAGEID . "'."); } $urlPrint = escapeshellarg($this->config[SYSTEM_BASE_URL_PRINT] . '/?' . KeyValueStringParser::unparse($get, '=', '&')); $wkhtml = $this->config[SYSTEM_WKHTMLTOPDF]; $rc = 0; $filename = tempnam(sys_get_temp_dir(), DOWNLOAD_FILE_PREFIX); $filenameEscape = escapeshellarg($filename); // $cmd = $wkhtml . " '" . $urlPrint . "' " . $filename . " > $filename.log 2>&1"; // $cmd = "$wkhtml '$urlPrint' $filename > $filename.log 2>&1"; $cmd = "$wkhtml $urlPrint $filenameEscape"; $line = system($cmd, $rc); if ($rc != 0) { throw new \exception("Error [RC=$rc] $line: $cmd"); } return $filename; } /** * @throws \exception */ public function outputHtml2Pdf() { $get = $this->readCleanGetParam($_GET); $pageId = Support::setIfNotSet($get, HTML2PDF_PAGEID, 0); $filename = $this->page2pdf($get); $this->setHeader('print.' . $pageId . '.pdf'); @readfile($filename); @unlink($filename); // @unlink($filename . '.log'); exit; // Do an extremely hard exit here to make sure there are no more additional bytes sent (makes the delivered PDF unusable). } }