protected function _image($val) { $DEBUGCSS = DEBUGCSS; if (mb_strpos($val, "url") === false) { $path = "none"; //Don't resolve no image -> otherwise would prefix path and no longer recognize as none } else { $val = preg_replace("/url\\(['\"]?([^'\")]+)['\"]?\\)/", "\\1", trim($val)); // Resolve the url now in the context of the current stylesheet $parsed_url = explode_url($val); if ($parsed_url["protocol"] == "" && $this->_stylesheet->get_protocol() == "") { if ($parsed_url["path"][0] == '/' || $parsed_url["path"][0] == '\\') { $path = $_SERVER["DOCUMENT_ROOT"] . '/'; } else { $path = $this->_stylesheet->get_base_path(); } $path .= $parsed_url["path"] . $parsed_url["file"]; $path = dompdf_realpath($path); } else { $path = build_url($this->_stylesheet->get_protocol(), $this->_stylesheet->get_host(), $this->_stylesheet->get_base_path(), $val); } } if ($DEBUGCSS) { print "<pre>[_image\n"; print_r($parsed_url); print $this->_stylesheet->get_protocol() . "\n" . $this->_stylesheet->get_base_path() . "\n" . $path . "\n"; print "_image]</pre>"; } return $path; }
$dompdf->render(); if ($_dompdf_show_warnings) { global $_dompdf_warnings; foreach ($_dompdf_warnings as $msg) { echo $msg . "\n"; } echo $dompdf->get_canvas()->get_cpdf()->messages; flush(); } if ($save_file) { // if ( !is_writable($outfile) ) // throw new DOMPDF_Exception("'$outfile' is not writable."); if (strtolower(DOMPDF_PDF_BACKEND) == "gd") { $outfile = str_replace(".pdf", ".png", $outfile); } list($proto, $host, $path, $file) = explode_url($outfile); if ($proto != "") { // i.e. not file:// $outfile = $file; } // just save it locally, FIXME? could save it like wget: ./host/basepath/file $outfile = dompdf_realpath($outfile); if (strpos($outfile, DOMPDF_CHROOT) !== 0) { throw new DOMPDF_Exception("Permission denied."); } file_put_contents($outfile, $dompdf->output(array("compress" => 0))); exit(0); } if (!headers_sent()) { $dompdf->stream($outfile); }
/** * builds a full url given a protocol, hostname, base path and url * * @param string $protocol * @param string $host * @param string $base_path * @param string $url * @return string * * Initially the trailing slash of $base_path was optional, and conditionally appended. * However on dynamically created sites, where the page is given as url parameter, * the base path might not end with an url. * Therefore do not append a slash, and **require** the $base_url to ending in a slash * when needed. * Vice versa, on using the local file system path of a file, make sure that the slash * is appended (o.k. also for Windows) */ function build_url($protocol, $host, $base_path, $url) { if (mb_strlen($url) == 0) { //return $protocol . $host . rtrim($base_path, "/\\") . "/"; return $protocol . $host . $base_path; } // Is the url already fully qualified? if (mb_strpos($url, "://") !== false) { return $url; } $ret = $protocol; if (!in_array(mb_strtolower($protocol), array("http://", "https://", "ftp://", "ftps://"))) { //On Windows local file, an abs path can begin also with a '\' or a drive letter and colon //drive: followed by a relative path would be a drive specific default folder. //not known in php app code, treat as abs path //($url{1} !== ':' || ($url{2}!=='\\' && $url{2}!=='/')) if ($url[0] !== '/' && (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' || $url[0] != '\\' && $url[1] !== ':')) { // For rel path and local acess we ignore the host, and run the path through realpath() $ret .= dompdf_realpath($base_path) . '/'; } $ret .= $url; return $ret; } //remote urls with backslash in html/css are not really correct, but lets be genereous if ($url[0] === '/' || $url[0] === '\\') { // Absolute path $ret .= $host . $url; } else { // Relative path //$base_path = $base_path !== "" ? rtrim($base_path, "/\\") . "/" : ""; $ret .= $host . $base_path . $url; } return $ret; }
/** * Loads an HTML file * * Parse errors are stored in the global array _dompdf_warnings. * * @param string $file a filename or url to load */ function load_html_file($file) { // Store parsing warnings as messages (this is to prevent output to the // browser if the html is ugly and the dom extension complains, // preventing the pdf from being streamed.) if (!$this->_protocol && !$this->_base_host && !$this->_base_path) { list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($file); } if (!DOMPDF_ENABLE_REMOTE && ($this->_protocol != "" && $this->_protocol != "file://")) { throw new DOMPDF_Exception("Remote file requested, but DOMPDF_ENABLE_REMOTE is false."); } if ($this->_protocol == "" || $this->_protocol == "file://") { $realfile = dompdf_realpath($file); if (!$file) { throw new DOMPDF_Exception("File '{$file}' not found."); } if (strpos($realfile, DOMPDF_CHROOT) !== 0) { throw new DOMPDF_Exception("Permission denied on {$file}."); } // Exclude dot files (e.g. .htaccess) if (substr(basename($realfile), 0, 1) == ".") { throw new DOMPDF_Exception("Permission denied on {$file}."); } $file = $realfile; } if (!DOMPDF_ENABLE_PHP) { set_error_handler("record_warnings"); $this->_xml->loadHTMLFile($file); restore_error_handler(); } else { $this->load_html(file_get_contents($file)); } }
/** * Set the background image url * * @link http://www.w3.org/TR/CSS21/colors.html#background-properties * @param string $url */ function set_background_image($val) { if (mb_strpos($val, "url") !== false) { $val = preg_replace("/url\\(['\"]?([^'\")]+)['\"]?\\)/", "\\1", trim($val)); } else { $val = "none"; } // Resolve the url now in the context of the current stylesheet $parsed_url = explode_url($val); if ($parsed_url["protocol"] == "" && $this->_stylesheet->get_protocol() == "") { $url = dompdf_realpath(rtrim($this->_stylesheet->get_base_path(), "/") . "/" . $parsed_url["path"] . $parsed_url["file"]); } else { $url = build_url($this->_stylesheet->get_protocol(), $this->_stylesheet->get_host(), $this->_stylesheet->get_base_path(), $val); } $this->_props["background_image"] = $url; }