上回书说到FPDF的构造函数调用了7个方法,下面来逐一分析它们:
- function _dochecks()
- {
- // 检查是否支持 %F
- if(sprintf('%.1F', 1.0) != '1.0')
- $this->Error('This version of PHP is not supported');
- // 检查 mbstring 是否装载
- if(ini_get('mbstring.func_overload') & 2)
- $this->Error('mbstring overloading must be disabled');
- // 关闭运行时的魔术引号
- if(get_magic_quotes_runtime())
- @set_magic_quotes_runtime(0);
- }
这里对系统做了一些检查,用到了几个PHP函数:
string sprintf ( string $format [, mixed $args [, mixed $... ]] ),用于将字符串格式化,其中参数里的 F 是从 PHP 4.3.10 和 PHP 5.0.3 开始使用的。
string ini_get ( string $varname ),检查php.ini,这里检查是否装载了 mbstring 扩展,这个什么扩展的偶倒从来没用过。
int get_magic_quotes_runtime ( void ),检查运行时魔术引号是否打开,通常这个是不开的,如果开了,可以用 bool set_magic_quotes_runtime ( int $new_setting )来关闭它。
- function _getpageformat($format)
- {
- $format = strtolower($format);
- if(!isset($this->PageFormats[$format]))
- $this->Error('Unknown page format: '.$format);
- $a = $this->PageFormats[$format];
- return array($a[0] / $this->k, $a[1] / $this->k);
- }
这里用于根据纸张样式获取纸张大小,参数是纸张样式,比如A4,从类的属性 $PageFormats 中得到对应的长度和宽度,形成一个数组返回。
- function Error($msg)
- {
- // 致命错误
- die('<b>FPDF error:</b> '.$msg);
- }
这个很简单了,出现致命错误,中断PHP程序,把传递来的错误信息输出。
- function SetMargins($left, $top, $right=null)
- {
- // 设置页面的左、上、右边距
- $this->lMargin=$left;
- $this->tMargin=$top;
- if($right===null)
- $right=$left;
- $this->rMargin=$right;
- }
这是遇到的第一个真正被外部使用的方法,虽然被构造函数调用,但只是初步按照默认值(都为 1 cm)设定,该方法可以修改这种设定。如果使用该方法,其前两个参数必须设置,第三个参数如果不设置,默认使用第一个参数,单位是 cm 。
- function SetAutoPageBreak($auto, $margin=0)
- {
- // 设置自动分页模式和触发分页的边距
- $this->AutoPageBreak = $auto;
- $this->bMargin = $margin;
- $this->PageBreakTrigger = $this->h - $margin;
- }
这也是一个可以被外部使用的方法,该方法第一个参数 $auto 用于设置是否自动分页,如果为自动分页(TRUE),第二个参数则设置页面的下边距,系统初始化时默认下边距为 2 cm,但该方法的默认值为 0。在该方法中,下边距是通过自动分页的触发值来实现的。
- function SetDisplayMode($zoom, $layout='continuous')
- {
- // 设置显示模式
- if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom))
- $this->ZoomMode = $zoom;
- else
- $this->Error('Incorrect zoom display mode: '.$zoom);
- if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
- $this->LayoutMode = $layout;
- else
- $this->Error('Incorrect layout display mode: '.$layout);
- }
这个方法用于设置页面的显示模式,前一个参数是缩放模式,后一个参数是分页模式,具体的参数解释在手册里有,这里就不说了。
- function SetCompression($compress)
- {
- // 设置页面压缩
- if(function_exists('gzcompress'))
- $this->compress = $compress;
- else
- $this->compress = false;
- }
这个方法是设置页面压缩的,依赖于PHP的Zlib,所以先进行检测是否支持 gzcompress() 如果不支持,即使设置为 true,也会设置为 false。

