cpdf_adapter.cls.php

来自「国外很不错的一个开源OA系统Group-Office」· PHP 代码 · 共 774 行 · 第 1/2 页

PHP
774
字号
   * Sets the fill colour   *   * See {@link Style::set_colour()} for the format of the colour array.   * @param array $color   */  protected function _set_fill_color($color) {    list($r, $g, $b) = $color;          $this->_pdf->setColor($r, $g, $b);  }  /**   * Sets line transparency   * @see Cpdf::setLineTransparency()   *   * Valid blend modes are (case-sensitive):   *   * Normal, Multiply, Screen, Overlay, Darken, Lighten,   * ColorDodge, ColorBurn, HardLight, SoftLight, Difference,   * Exclusion   *   * @param string $mode the blending mode to use   * @param float $opacity 0.0 fully transparent, 1.0 fully opaque   */  protected function _set_line_transparency($mode, $opacity) {    $this->_pdf->setLineTransparency($mode, $opacity);  }    /**   * Sets fill transparency   * @see Cpdf::setFillTransparency()   *   * Valid blend modes are (case-sensitive):   *   * Normal, Multiply, Screen, Overlay, Darken, Lighten,   * ColorDogde, ColorBurn, HardLight, SoftLight, Difference,   * Exclusion   *   * @param string $mode the blending mode to use   * @param float $opacity 0.0 fully transparent, 1.0 fully opaque   */  protected function _set_fill_transparency($mode, $opacity) {    $this->_pdf->setFillTransparency($mode, $opacity);  }  /**   * Sets the line style   *   * @see Cpdf::setLineStyle()   *   * @param float width   * @param string cap   * @param string join   * @param array dash   */  protected function _set_line_style($width, $cap, $join, $dash) {    $this->_pdf->setLineStyle($width, $cap, $join, $dash);  }    //........................................................................    /**   * Remaps y coords from 4th to 1st quadrant   *   * @param float $y   * @return float   */  protected function y($y) { return $this->_height - $y; }  // Canvas implementation  function line($x1, $y1, $x2, $y2, $color, $width, $style = array(),                $blend = "Normal", $opacity = 1.0) {    //pre_r(compact("x1", "y1", "x2", "y2", "color", "width", "style"));    $this->_set_stroke_color($color);    $this->_set_line_style($width, "butt", "", $style);    $this->_set_line_transparency($blend, $opacity);        $this->_pdf->line($x1, $this->y($y1),                      $x2, $this->y($y2));  }                                //........................................................................  /**   * Convert a GIF image to a PNG image   *   * @return string The url of the newly converted image    */  protected function _convert_gif_to_png($image_url) {    global $_dompdf_warnings;        if ( !function_exists("imagecreatefromgif") ) {      $_dompdf_warnings[] = "Function imagecreatefromgif() not found.  Cannot convert gif image: $image_url.";            return DOMPDF_LIB_DIR . "/res/broken_image.png";    }    $old_err = set_error_handler("record_warnings");    $im = imagecreatefromgif($image_url);    if ( $im ) {      imageinterlace($im, 0);          $filename = tempnam(DOMPDF_TEMP_DIR, "dompdf_img_");      imagepng($im, $filename);    } else {      $filename = DOMPDF_LIB_DIR . "/res/broken_image.png";    }    restore_error_handler();    $this->_image_cache[] = $filename;        return $filename;      }  function rectangle($x1, $y1, $w, $h, $color, $width, $style = array(),                     $blend = "Normal", $opacity = 1.0) {    $this->_set_stroke_color($color);    $this->_set_line_style($width, "square", "miter", $style);    $this->_set_line_transparency($blend, $opacity);        $this->_pdf->rectangle($x1, $this->y($y1) - $h, $w, $h);  }  //........................................................................    function filled_rectangle($x1, $y1, $w, $h, $color, $blend = "Normal", $opacity = 1.0) {    $this->_set_fill_color($color);    $this->_set_line_style(1, "square", "miter", array());    $this->_set_line_transparency($blend, $opacity);    $this->_set_fill_transparency($blend, $opacity);        $this->_pdf->filledRectangle($x1, $this->y($y1) - $h, $w, $h);  }  //........................................................................  function polygon($points, $color, $width = null, $style = array(),                   $fill = false, $blend = "Normal", $opacity = 1.0) {    $this->_set_fill_color($color);    $this->_set_stroke_color($color);    $this->_set_line_transparency($blend, $opacity);    $this->_set_fill_transparency($blend, $opacity);        if ( !$fill && isset($width) )      $this->_set_line_style($width, "square", "miter", $style);        // Adjust y values    for ( $i = 1; $i < count($points); $i += 2)      $points[$i] = $this->y($points[$i]);        $this->_pdf->polygon($points, count($points) / 2, $fill);  }  //........................................................................  function circle($x, $y, $r1, $color, $width = null, $style = null,                  $fill = false, $blend = "Normal", $opacity = 1.0) {    $this->_set_fill_color($color);    $this->_set_stroke_color($color);        $this->_set_line_transparency($blend, $opacity);    $this->_set_fill_transparency($blend, $opacity);    if ( !$fill && isset($width) )      $this->_set_line_style($width, "round", "round", $style);    $this->_pdf->filledEllipse($x, $this->y($y), $r1, 0, 0, 8, 0, 360, 1, $fill);  }    //........................................................................  function image($img_url, $img_type, $x, $y, $w, $h) {    $img_type = mb_strtolower($img_type);        switch ($img_type) {    case "jpeg":    case "jpg":      $this->_pdf->addJpegFromFile($img_url, $x, $this->y($y) - $h, $w, $h);      break;    case "png":      $this->_pdf->addPngFromFile($img_url, $x, $this->y($y) - $h, $w, $h);      break;    case "gif":      // Convert gifs to pngs      $img_url = $this->_convert_gif_to_png($img_url);      $this->_pdf->addPngFromFile($img_url, $x, $this->y($y) - $h, $w, $h);      break;          default:            break;    }        return;  }  //........................................................................  function text($x, $y, $text, $font, $size, $color = array(0,0,0),                $adjust = 0, $angle = 0, $blend = "Normal", $opacity = 1.0) {    list($r, $g, $b) = $color;    $this->_pdf->setColor($r, $g, $b);    $this->_set_line_transparency($blend, $opacity);    $this->_set_fill_transparency($blend, $opacity);    $font .= ".afm";        $this->_pdf->selectFont($font);    $this->_pdf->addText($x, $this->y($y) - Font_Metrics::get_font_height($font, $size), $size, utf8_decode($text), $angle, $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->addDestination($anchorname,"Fit");  }  //........................................................................  /**   * 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->addInternalLink($name, $x, $y, $x + $width, $y + $height);    } else {      $this->_pdf->addLink(rawurldecode($url), $x, $y, $x + $width, $y + $height);    }      }  //........................................................................  function get_text_width($text, $font, $size, $spacing = 0) {    $this->_pdf->selectFont($font);    return $this->_pdf->getTextWidth($size, utf8_decode($text), $spacing);  }  //........................................................................  function get_font_height($font, $size) {    $this->_pdf->selectFont($font);    return $this->_pdf->getFontHeight($size);  }  /**   * Writes text at the specified x and y coordinates on every page   *   * The strings '{PAGE_NUM}' and '{PAGE_COUNT}' are automatically replaced   * with their current values.   *   * See {@link Style::munge_colour()} for the format of the colour array.   *   * @param float $x   * @param float $y   * @param string $text the text to write   * @param string $font the font file to use   * @param float $size the font size, in points   * @param array $color   * @param float $adjust word spacing adjustment   * @param float $angle angle to write the text at, measured CW starting from the x-axis   */  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() {    $this->_page_count++;    $ret = $this->_pdf->newPage();    $this->_pages[] = $ret;    return $ret;  }    //........................................................................  /**   * Add text to each page after rendering is complete   */  protected function _add_page_text() {        if ( !count($this->_page_text) )      return;    $page_number = 1;    foreach ($this->_pages as $pid) {      foreach ($this->_page_text as $pt) {        extract($pt);        $text = str_replace(array("{PAGE_NUM}","{PAGE_COUNT}"),                            array($page_number, $this->_page_count), $text);        $this->reopen_object($pid);                $this->text($x, $y, $text, $font, $size, $color, $adjust, $angle);        $this->close_object();              }      $page_number++;          }  }    /**   * Streams the PDF directly to the browser   *   * @param string $filename the name of the PDF file   * @param array  $options associative array, 'Attachment' => 0 or 1, 'compress' => 1 or 0   */  function stream($filename, $options = null) {    // Add page text    $this->_add_page_text();        $options["Content-Disposition"] = $filename;    $this->_pdf->stream($options);  }  //........................................................................  /**   * Returns the PDF as a string   *   * @return string   */  function output($options = null) {    // Add page text    $this->_add_page_text();    if ( isset($options["compress"]) && $options["compress"] != 1 )      $debug = 1;    else      $debug = 0;        return $this->_pdf->output($debug);      }    //........................................................................  /**   * Returns logging messages generated by the Cpdf class   *   * @return string   */  function get_messages() { return $this->_pdf->messages; }  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?