fpdf.php

来自「php绿色服务器,让大家试用greenamp」· PHP 代码 · 共 2,044 行 · 第 1/5 页

PHP
2,044
字号
<?php/* $Id: fpdf.php,v 2.3 2004/05/20 16:14:13 nijel Exp $ */// vim: expandtab sw=4 ts=4 sts=4:/***************************************************************************** Software : FPDF                                                           ** Version :  1.51                                                           ** Date :     2002/08/03                                                     ** Author :   Olivier PLATHEY                                                ** Website :  http://www.fpdf.org                                            ** Licence :  Freeware                                                       **                                                                           ** You are entitled to modify this soft as you want to.                      *****************************************************************************/$FPDF_version = (string) '1.51';/** * The FPDF class */class FPDF{    /**     * Defines private properties     */    var $page;               // current page number    var $n;                  // current object number    var $offsets;            // array of object offsets    var $buffer;             // buffer holding in-memory PDF    var $pages;              // array containing pages    var $state;              // current document state    var $compress;           // compression flag    var $DefOrientation;     // default orientation    var $CurOrientation;     // current orientation    var $OrientationChanges; // array indicating orientation changes    var $fwPt, $fhPt;        // dimensions of page format in points    var $fw, $fh;            // dimensions of page format in user unit    var $wPt, $hPt;          // current dimensions of page in points    var $k;                  // scale factor (number of points in user unit)    var $w, $h;              // current dimensions of page in user unit    var $lMargin;            // left margin    var $tMargin;            // top margin    var $rMargin;            // right margin    var $bMargin;            // page break margin    var $cMargin;            // cell margin    var $x, $y;              // current position in user unit for cell positionning    var $lasth;              // height of last cell printed    var $LineWidth;          // line width in user unit    var $CoreFonts;          // array of standard font names    var $fonts;              // array of used fonts    var $FontFiles;          // array of font files    var $diffs;              // array of encoding differences    var $images;             // array of used images    var $PageLinks;          // array of links in pages    var $links;              // array of internal links    var $FontFamily;         // current font family    var $FontStyle;          // current font style    var $CurrentFont;        // current font info    var $FontSizePt;         // current font size in points    var $FontSize;           // current font size in user unit    var $DrawColor;          // commands for drawing color    var $FillColor;          // commands for filling color    var $TextColor;          // commands for text color    var $ColorFlag;          // indicates whether fill and text colors are different    var $ws;                 // word spacing    var $underline;          // whether underline is current state or not    var $AutoPageBreak;      // automatic page breaking    var $PageBreakTrigger;   // threshold used to trigger page breaks    var $InFooter;           // flag set when processing footer    var $ZoomMode;           // zoom display mode    var $LayoutMode;         // layout display mode    var $title;              // title    var $subject;            // subject    var $author;             // author    var $keywords;           // keywords    var $creator;            // creator    var $AliasNbPages;       // alias for total number of pages    /**************************************************************************    *                                                                         *    *      Public methods below are used by some private ones. Then they      *    *      are placed at the top of the class.                                *    *                                                                         *    **************************************************************************/    /**     * Gets the width of a string in the current font     *     * @param   string   The string to check     *     * @return  double  The string width     *     * @access  public     */    function GetStringWidth($s)    {        $s     = (string) $s;        // loic1: PHP3 compatibility        // $cw    = &$this->CurrentFont['cw'];        $w     = 0;        $l     = strlen($s);        for ($i = 0; $i < $l; $i++) {            // $w += $cw[$s[$i]];            $w += $this->CurrentFont['cw'][$s[$i]];        } // end for        return $w * $this->FontSize / 1000;    } // end of the "GetStringWidth()" method    /**     * Displays an error message then exists     *     * @param  string  The error message     *     * @access public     */    function Error($msg)    {        die('<b>FPDF error: </b>' . $msg);    } // end of the "Error()" method    /**************************************************************************    *                                                                         *    *                             Private methods                             *    *                                                                         *    **************************************************************************/    /**     * Adds a line to the document     *     * @param   string   The string to add     *     * @access  private     */    function _out($s)    {        if ($this->state == 2) {            $this->pages[$this->page] .= $s . "\n";        } else {            $this->buffer             .= $s . "\n";        }    } // end of the "_out()" method    /**     * Starts a new object     *     * @access private     */    function _newobj()    {        $this->n++;        $this->offsets[$this->n] = strlen($this->buffer);        $this->_out($this->n . ' 0 obj');    } // end of the "_newobj()" method    /**     * Adds a "\" before "\", "(" and ")" characters     *     * @param   string   The string to slash     *     * @return  integer  The slashed string     *     * @access  private     */    function _escape($s)    {        return str_replace(')', '\\)', str_replace('(', '\\(', str_replace('\\', '\\\\', $s)));    } // end of the "_escape()" method    /**     * Formats a text stringrs     *     * @param   string   The string to format     *     * @return  integer  The formatted string     *     * @access  private     *     * @see     _escape()     */    function _textstring($s)    {        return '(' . $this->_escape($s) . ')';    } // end of the "_textstring()" method    /**     * Outputs a stream     *     * @param   string   The stream to ouput     *     * @access  private     *     * @see     _out()     */    function _putstream($s)    {        $this->_out('stream');        $this->_out($s);        $this->_out('endstream');    } // end of the "_putstream()" method    /**     * Starts document     *     * @access private     */    function _begindoc()    {        $this->state = 1;        $this->_out('%PDF-1.3');    } // end of the "_begindoc()" method    /**     * Puts pages     *     * @access private     */    function _putpages()    {        $nb = $this->page;        if (!empty($this->AliasNbPages)) {            // Replaces number of pages            for ($n = 1; $n <= $nb; $n++) {                $this->pages[$n] = str_replace($this->AliasNbPages, $nb, $this->pages[$n]);            }        }        if ($this->DefOrientation == 'P') {            $wPt = $this->fwPt;            $hPt = $this->fhPt;        } else {            $wPt = $this->fhPt;            $hPt = $this->fwPt;        }        $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';        for ($n=1; $n <= $nb; $n++) {            // Page            $this->_newobj();            $this->_out('<</Type /Page');            $this->_out('/Parent 1 0 R');            if (isset($this->OrientationChanges[$n])) {                $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]', $hPt, $wPt));            }            $this->_out('/Resources 2 0 R');            if (isset($this->PageLinks[$n])) {                // Links                $annots         = '/Annots [';                reset($this->PageLinks[$n]);                while (list(, $pl) = each($this->PageLinks[$n])) {                    $rect       = sprintf('%.2f %.2f %.2f %.2f', $pl[0], $pl[1], $pl[0] + $pl[2], $pl[1] - $pl[3]);                    $annots     .= '<</Type /Annot /Subtype /Link /Rect [' . $rect . '] /Border [0 0 0] ';                    if (is_string($pl[4])) {                        $annots .= '/A <</S /URI /URI ' . $this->_textstring($pl[4]) . '>>>>';                    }                    else {                        $l      = $this->links[$pl[4]];                        $h      = isset($this->OrientationChanges[$l[0]]) ? $wPt : $hPt;                        $annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]>>', 1 + 2 * $l[0], $h - $l[1] * $this->k);                    }                } // end while                $this->_out($annots . ']');            } // end if            $this->_out('/Contents ' . ($this->n+1).' 0 R>>');            $this->_out('endobj');            // Page content            $p = ($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];            $this->_newobj();            $this->_out('<<' . $filter . '/Length ' . strlen($p) . '>>');            $this->_putstream($p);            $this->_out('endobj');        } // end for        // Pages root        $this->offsets[1]=strlen($this->buffer);        $this->_out('1 0 obj');        $this->_out('<</Type /Pages');        $kids     = '/Kids [';        for ($i = 0; $i < $nb; $i++) {            $kids .= (3 + 2 * $i) . ' 0 R ';        }        $this->_out($kids . ']');        $this->_out('/Count ' . $nb);        $this->_out(sprintf('/MediaBox [0 0 %.2f %.2f]', $wPt, $hPt));        $this->_out('>>');        $this->_out('endobj');    } // end of the "_putpages()" method    /**     * Puts font faces     *     * @access private     */    function _putfonts()    {        $nf = $this->n;        foreach ($this->diffs AS $diff) {            // Encodings            $this->_newobj();            $this->_out('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences [' . $diff . ']>>');            $this->_out('endobj');        } // end while        $mqr = get_magic_quotes_runtime();        set_magic_quotes_runtime(0);        foreach ($this->FontFiles AS $file => $info) {            // Font file embedding            $this->_newobj();            $this->FontFiles[$file]['n'] = $this->n;            if (isset($GLOBALS['FPDF_font_path'])) {                $file = $GLOBALS['FPDF_font_path'] . $file;            }            $size     = filesize($file);            if (!$size) {                $this->Error('Font file not found');            }            $this->_out('<</Length ' . $size);            if (substr($file, -2) == '.z') {                $this->_out('/Filter /FlateDecode');            }            $this->_out('/Length1 ' . $info['length1']);            if (isset($info['length2'])) {                $this->_out('/Length2 ' . $info['length2'] . ' /Length3 0');            }            $this->_out('>>');            $f = fopen($file, 'rb');            $this->_putstream(fread($f, $size));            fclose($f);            $this->_out('endobj');        } // end while        set_magic_quotes_runtime($mqr);        foreach ($this->fonts AS $k => $font) {            // Font objects            $this->_newobj();            $this->fonts[$k]['n'] = $this->n;            $name                 = $font['name'];            $this->_out('<</Type /Font');            $this->_out('/BaseFont /' . $name);            if ($font['type'] == 'core')  {                // Standard font                $this->_out('/Subtype /Type1');                if ($name != 'Symbol' && $name != 'ZapfDingbats') {                    $this->_out('/Encoding /WinAnsiEncoding');                }            }            else {                // Additional font                $this->_out('/Subtype /' . $font['type']);                $this->_out('/FirstChar 32');                $this->_out('/LastChar 255');                $this->_out('/Widths ' . ($this->n + 1) . ' 0 R');                $this->_out('/FontDescriptor ' . ($this->n + 2) . ' 0 R');                if ($font['enc']) {                    if (isset($font['diff'])) {                        $this->_out('/Encoding ' . ($nf + $font['diff']) . ' 0 R');                    } else {                        $this->_out('/Encoding /WinAnsiEncoding');                    }                }            } // end if... else...            $this->_out('>>');            $this->_out('endobj');            if ($font['type'] != 'core')  {                // Widths                $this->_newobj();                $s     = '[';                for ($i = 32; $i <= 255; $i++) {                    $s .= $font['cw'][chr($i)] . ' ';                }                $this->_out($s . ']');                $this->_out('endobj');                // Descriptor                $this->_newobj();                $s     = '<</Type /FontDescriptor /FontName /' . $name;                foreach ($font['desc'] AS $k => $v) {                    $s .= ' /' . $k . ' ' . $v;                }                $file = $font['file'];                if ($file) {                    $s .= ' /FontFile' . ($font['type'] == 'Type1' ? '' : '2') . ' ' . $this->FontFiles[$file]['n'] . ' 0 R';                }                $this->_out($s . '>>');                $this->_out('endobj');            } // end if        } // end while    } // end of the "_putfonts()" method    /**     * Puts images     *

⌨️ 快捷键说明

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