pdflib_adapter.cls.php
来自「国外很不错的一个开源OA系统Group-Office」· PHP 代码 · 共 891 行 · 第 1/2 页
PHP
891 行
break; } switch ( $cap ) { case "butt": $this->_pdf->setlinecap(0); break; case "round": $this->_pdf->setlinecap(1); break; case "square": $this->_pdf->setlinecap(2); break; default: break; } $this->_pdf->setlinewidth($width); } /** * Sets the line color * * @param array $color array(r,g,b) */ protected function _set_stroke_color($color) { if($this->_last_stroke_color == $color) return; $this->_last_stroke_color = $color; list($r,$g,$b) = $color; $this->_pdf->setcolor("stroke", "rgb", $r, $g, $b, 0); } /** * Sets the fill color * * @param array $color array(r,g,b) */ protected function _set_fill_color($color) { if($this->_last_fill_color == $color) return; $this->_last_fill_color = $color; list($r,$g,$b) = $color; $this->_pdf->setcolor("fill", "rgb", $r, $g, $b, 0); } /** * Loads a specific font and stores the corresponding descriptor. * * @param string $font * @return int the font descriptor for the font */ protected function _load_font($font, $encoding = null, $options = "") { // Check if the font is a native PDF font // Embed non-native fonts $native_fonts = array("courier", "courier-bold", "courier-oblique", "courier-boldoblique", "helvetica", "helvetica-bold", "helvetica-oblique", "helvetica-boldoblique", "times-roman", "times-bold", "times-italic", "times-bolditalic", "symbol", "zapfdinbats"); $test = strtolower(basename($font)); if ( in_array($test, $native_fonts) ) { $font = basename($font); } else { // Embed non-native fonts $options .= " embedding=true"; } if ( is_null($encoding) ) { // Unicode encoding is only available for the commerical // version of PDFlib and not PDFlib-Lite if ( defined("DOMPDF_PDFLIB_LICENSE") ) $encoding = "unicode"; else $encoding = "auto"; } $key = $font .":". $encoding .":". $options; if ( isset($this->_fonts[$key]) ) return $this->_fonts[$key]; else { $this->_fonts[$key] = $this->_pdf->load_font($font, $encoding, $options); return $this->_fonts[$key]; } } /** * Remaps y coords from 4th to 1st quadrant * * @param float $y * @return float */ protected function y($y) { return $this->_height - $y; } //........................................................................ function line($x1, $y1, $x2, $y2, $color, $width, $style = null) { $this->_set_line_style($width, "butt", "", $style); $this->_set_stroke_color($color); $y1 = $this->y($y1); $y2 = $this->y($y2); $this->_pdf->moveto($x1,$y1); $this->_pdf->lineto($x2, $y2); $this->_pdf->stroke(); } //........................................................................ function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) { $this->_set_stroke_color($color); $this->_set_line_style($width, "square", "miter", $style); $y1 = $this->y($y1) - $h; $this->_pdf->rect($x1, $y1, $w, $h); $this->_pdf->stroke(); } //........................................................................ function filled_rectangle($x1, $y1, $w, $h, $color) { $this->_set_fill_color($color); $y1 = $this->y($y1) - $h; $this->_pdf->rect($x1, $y1, $w, $h); $this->_pdf->fill(); } //........................................................................ function polygon($points, $color, $width = null, $style = null, $fill = false) { $this->_set_fill_color($color); $this->_set_stroke_color($color); if ( !$fill && isset($width) ) $this->_set_line_style($width, "square", "miter", $style); $y = $this->y(array_pop($points)); $x = array_pop($points); $this->_pdf->moveto($x,$y); while (count($points) > 1) { $y = $this->y(array_pop($points)); $x = array_pop($points); $this->_pdf->lineto($x,$y); } if ( $fill ) $this->_pdf->fill(); else $this->_pdf->closepath_stroke(); } //........................................................................ function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false) { $this->_set_fill_color($color); $this->_set_stroke_color($color); if ( !$fill && isset($width) ) $this->_set_line_style($width, "round", "round", $style); $y = $this->y($y); $this->_pdf->circle($x, $y, $r); if ( $fill ) $this->_pdf->fill(); else $this->_pdf->stroke(); } //........................................................................ function image($img_url, $img_type, $x, $y, $w, $h) { $w = (int)$w; $h = (int)$h; $img_type = strtolower($img_type); if ( $img_type == "jpg" ) $img_type = "jpeg"; if ( isset($this->_imgs[$img_url]) ) $img = $this->_imgs[$img_url]; else { $img = $this->_imgs[$img_url] = $this->_pdf->load_image($img_type, $img_url, ""); } $y = $this->y($y) - $h; $this->_pdf->fit_image($img, $x, $y, 'boxsize={'. "$w $h" .'} fitmethod=entire'); } //........................................................................ function text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0, $angle = 0) { $fh = $this->_load_font($font); $this->_pdf->setfont($fh, $size); $this->_set_fill_color($color); $y = $this->y($y) - Font_Metrics::get_font_height($font, $size); $adjust = (float)$adjust; $angle = -(float)$angle; //$this->_pdf->fit_textline(utf8_decode($text), $x, $y, "rotate=$angle wordspacing=$adjust"); $this->_pdf->fit_textline($text, $x, $y, "rotate=$angle wordspacing=$adjust"); } //........................................................................ /** * Add a named destination (similar to <a name="foo">...</a> in html) * * @param string $anchorname The name of the named destination */ function add_named_dest($anchorname) { $this->_pdf->add_nameddest($anchorname,""); } //........................................................................ /** * Add a link to the pdf * * @param string $url The url to link to * @param float $x The x position of the link * @param float $y The y position of the link * @param float $width The width of the link * @param float $height The height of the link */ function add_link($url, $x, $y, $width, $height) { $y = $this->y($y) - $height; if ( strpos($url, '#') === 0 ) { // Local link $name = substr($url,1); if ( $name ) $this->_pdf->create_annotation($x, $y, $x + $width, $y + $height, 'Link', "contents={$url} destname=". substr($url,1) . " linewidth=0"); } else { list($proto, $host, $path, $file) = explode_url($url); if ( $proto == "" || $proto == "file://" ) return; // Local links are not allowed $url = build_url($proto, $host, $path, $file); $url = str_replace("=", "%3D", rawurldecode($url)); $action = $this->_pdf->create_action("URI", "url=" . $url); $this->_pdf->create_annotation($x, $y, $x + $width, $y + $height, 'Link', "contents={$url} action={activate=$action} linewidth=0"); } } //........................................................................ function get_text_width($text, $font, $size, $spacing = 0) { $fh = $this->_load_font($font); // Determine the additional width due to extra spacing $num_spaces = mb_substr_count($text," "); $delta = $spacing * $num_spaces; return $this->_pdf->stringwidth($text, $fh, $size) + $delta; } //........................................................................ function get_font_height($font, $size) { $fh = $this->_load_font($font); $this->_pdf->setfont($fh, $size); $asc = $this->_pdf->get_value("ascender", $fh); $desc = $this->_pdf->get_value("descender", $fh); // $desc is usually < 0, return self::FONT_HEIGHT_SCALE * $size * ($asc - $desc); } //........................................................................ function page_text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0, $angle = 0, $blend = "Normal", $opacity = 1.0) { $this->_page_text[] = compact("x", "y", "text", "font", "size", "color", "adjust", "angle"); } //........................................................................ function new_page() { // Add objects to the current page $this->_place_objects(); $this->_pdf->suspend_page(""); $this->_pdf->begin_page_ext($this->_width, $this->_height, ""); $this->_page_number = ++$this->_page_count; } //........................................................................ /** * Add text to each page after rendering is complete */ protected function _add_page_text() { if ( !count($this->_page_text) ) return; $this->_pdf->suspend_page(""); for ($p = 1; $p <= $this->_page_count; $p++) { $this->_pdf->resume_page("pagenumber=$p"); foreach ($this->_page_text as $pt) { extract($pt); $text = str_replace(array("{PAGE_NUM}","{PAGE_COUNT}"), array($p, $this->_page_count), $text); $this->text($x, $y, $text, $font, $size, $color, $adjust, $angle); } $this->_pdf->suspend_page(""); } $this->_pdf->resume_page("pagenumber=".$this->_page_number); } //........................................................................ function stream($filename, $options = null) { // Add page text $this->_add_page_text(); if ( isset($options["compress"]) && $options["compress"] != 1 ) $this->_pdf->set_value("compress", 0); else $this->_pdf->set_value("compress", 6); $this->_close(); if ( self::$IN_MEMORY ) { $data = $this->_pdf->get_buffer(); $size = strlen($data); } else $size = filesize($this->_file); $filename = str_replace(array("\n","'"),"", $filename); $attach = (isset($options["Attachment"]) && $options["Attachment"]) ? "attachment" : "inline"; header("Cache-Control: private"); header("Content-type: application/pdf"); header("Content-Disposition: $attach; filename=\"$filename\""); //header("Content-length: " . $size); if ( self::$IN_MEMORY ) echo $data; else { // Chunked readfile() $chunk = (1 << 21); // 2 MB $fh = fopen($this->_file, "rb"); if ( !$fh ) throw new DOMPDF_Exception("Unable to load temporary PDF file: " . $this->_file); while ( !feof($fh) ) echo fread($fh,$chunk); fclose($fh); unlink($this->_file); $this->_file = null; } flush(); } //........................................................................ function output($options = null) { // Add page text $this->_add_page_text(); if ( isset($options["compress"]) && $options["compress"] != 1 ) $this->_pdf->set_value("compress", 0); else $this->_pdf->set_value("compress", 6); $this->_close(); if ( self::$IN_MEMORY ) $data = $this->_pdf->get_buffer(); else { $data = file_get_contents($this->_file); unlink($this->_file); $this->_file = null; } return $data; }}// Workaround for idiotic limitation on statics...PDFLib_Adapter::$PAPER_SIZES = CPDF_Adapter::$PAPER_SIZES;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?