继续来分析FPDF类,上次看到了 SetMargins() 方法,用于设置页面的上、左、右边距,FPDF还提供了三个单独的方法分别设置它们:
SetLeftMargin()、SetTopMargin()、SetRightMargin()
- function SetLeftMargin($margin)
- {
- // 设置左边距
- $this->lMargin = $margin;
- if($this->page > 0 && $this->x < $margin)
- $this->x = $margin;
- }
- function SetTopMargin($margin)
- {
- // 设置上边距
- $this->tMargin = $margin;
- }
- function SetRightMargin($margin)
- {
- // 设置右边距
- $this->rMargin = $margin;
- }
这三个方法在一起实现的就是 SetMargins() 的作用,因为它们可能是在运行过程中设置的,因此在设置左边距的时候特别识别了一下当前横坐标,如果当前横坐标在左边距外,那就调整下从左边距开始。
SetTitle()、SetSubject()、SetAuthor()、SetKeywords()、SetCreator()
- function SetTitle($title, $isUTF8=false)
- {
- // 文档标题
- if($isUTF8)
- $title = $this->_UTF8toUTF16($title);
- $this->title = $title;
- }
- function SetSubject($subject, $isUTF8=false)
- {
- // 文档主题
- if($isUTF8)
- $subject = $this->_UTF8toUTF16($subject);
- $this->subject = $subject;
- }
- function SetAuthor($author, $isUTF8=false)
- {
- // 文档作者
- if($isUTF8)
- $author = $this->_UTF8toUTF16($author);
- $this->author = $author;
- }
- function SetKeywords($keywords, $isUTF8=false)
- {
- // 文档关键词
- if($isUTF8)
- $keywords=$this->_UTF8toUTF16($keywords);
- $this->keywords=$keywords;
- }
- function SetCreator($creator, $isUTF8=false)
- {
- // 文档应用程序
- if($isUTF8)
- $creator = $this->_UTF8toUTF16($creator);
- $this->creator = $creator;
- }
这5个方法是设置PDF文档属性的,也是设置类属性的,只是其中调用了一个 _UTF8toUTF16 () 内部方法,用于把UTF8编码的文字变成UTF16编码的,注意一下,由于fpdf.php文件本身不是UTF8编码的,所以如果使用GBK编码做PDF的话,需要先将GBK文字变成UTF8编码的,然后在第二个参数设置为 TRUE,否则汉字显示的就是乱码,戒烟如你亲自测试的。
_UTF8toUTF16()
- function _UTF8toUTF16($s)
- {
- // 把UTF-8编码转换成UTF-16BE编码
- $res = "\xFE\xFF";
- $nb = strlen($s);
- $i = 0;
- while($i<$nb)
- {
- $c1 = ord($s[$i++]);
- if($c1 >= 224)
- {
- // 3字节字符
- $c2 = ord($s[$i++]);
- $c3 = ord($s[$i++]);
- $res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2));
- $res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F));
- }
- elseif($c1 >= 192)
- {
- // 2字节字符
- $c2 = ord($s[$i++]);
- $res .= chr(($c1 & 0x1C)>>2);
- $res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F));
- }
- else
- {
- // 单字节字符
- $res .= "\0".chr($c1);
- }
- }
- return $res;
- }
该方法似乎以后会有用,先留在这里,暂时不研究它了,知道它的作用就OK!
#1
