也许过去我不是一个好孩子,但现在我要做个好爸爸...... 注册 | 登陆

FPDF代码研究(Part 4)

  继续来分析FPDF类,上次看到了 SetMargins() 方法,用于设置页面的上、左、右边距,FPDF还提供了三个单独的方法分别设置它们:

SetLeftMargin()、SetTopMargin()、SetRightMargin()
  1. function SetLeftMargin($margin)
  2. {
  3.     // 设置左边距
  4.     $this->lMargin = $margin;
  5.     if($this->page > 0 && $this->x < $margin)
  6.         $this->x = $margin;
  7. }
  8.  
  9. function SetTopMargin($margin)
  10. {
  11.     // 设置上边距
  12.     $this->tMargin = $margin;
  13. }
  14.  
  15. function SetRightMargin($margin)
  16. {
  17.     // 设置右边距
  18.     $this->rMargin = $margin;
  19. }

  这三个方法在一起实现的就是 SetMargins() 的作用,因为它们可能是在运行过程中设置的,因此在设置左边距的时候特别识别了一下当前横坐标,如果当前横坐标在左边距外,那就调整下从左边距开始。

SetTitle()、SetSubject()、SetAuthor()、SetKeywords()、SetCreator()
  1. function SetTitle($title$isUTF8=false)
  2. {
  3.     // 文档标题
  4.     if($isUTF8)
  5.         $title = $this->_UTF8toUTF16($title);
  6.     $this->title = $title;
  7. }
  8.  
  9. function SetSubject($subject$isUTF8=false)
  10. {
  11.     // 文档主题
  12.     if($isUTF8)
  13.         $subject = $this->_UTF8toUTF16($subject);
  14.     $this->subject = $subject;
  15. }
  16.  
  17. function SetAuthor($author$isUTF8=false)
  18. {
  19.     // 文档作者
  20.     if($isUTF8)
  21.         $author = $this->_UTF8toUTF16($author);
  22.     $this->author = $author;
  23. }
  24.  
  25. function SetKeywords($keywords$isUTF8=false)
  26. {
  27.     // 文档关键词
  28.     if($isUTF8)
  29.         $keywords=$this->_UTF8toUTF16($keywords);
  30.     $this->keywords=$keywords;
  31. }
  32.  
  33. function SetCreator($creator$isUTF8=false)
  34. {
  35.     // 文档应用程序
  36.     if($isUTF8)
  37.         $creator = $this->_UTF8toUTF16($creator);
  38.     $this->creator = $creator;
  39. }

  这5个方法是设置PDF文档属性的,也是设置类属性的,只是其中调用了一个 _UTF8toUTF16 () 内部方法,用于把UTF8编码的文字变成UTF16编码的,注意一下,由于fpdf.php文件本身不是UTF8编码的,所以如果使用GBK编码做PDF的话,需要先将GBK文字变成UTF8编码的,然后在第二个参数设置为 TRUE,否则汉字显示的就是乱码,戒烟如你亲自测试的。

_UTF8toUTF16()
  1. function _UTF8toUTF16($s)
  2. {
  3.     // 把UTF-8编码转换成UTF-16BE编码
  4.     $res = "\xFE\xFF";
  5.     $nb = strlen($s);
  6.     $i = 0;
  7.     while($i<$nb)
  8.     {
  9.         $c1 = ord($s[$i++]);
  10.         if($c1 >= 224)
  11.         {
  12.             // 3字节字符
  13.             $c2 = ord($s[$i++]);
  14.             $c3 = ord($s[$i++]);
  15.             $res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2));
  16.             $res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F));
  17.         }
  18.         elseif($c1 >= 192)
  19.         {
  20.             // 2字节字符
  21.             $c2 = ord($s[$i++]);
  22.             $res .= chr(($c1 & 0x1C)>>2);
  23.             $res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F));
  24.         }
  25.         else
  26.         {
  27.             // 单字节字符
  28.             $res .= "\0".chr($c1);
  29.         }
  30.     }
  31.     return $res;
  32. }

  该方法似乎以后会有用,先留在这里,暂时不研究它了,知道它的作用就OK!

Tags: php, fpdf

« 上一篇 | 下一篇 »

只显示5条记录相关文章

截取固定长度UTF-8字符串的PHP函数 (浏览: 34265, 评论: 2)
FPDF代码研究(Part 5) (浏览: 30548, 评论: 1)
FPDF代码研究(Part 3) (浏览: 11838, 评论: 0)
FPDF代码研究(Part 2,构造函数) (浏览: 22514, 评论: 0)
FPDF代码研究(Part 1,类的属性) (浏览: 11080, 评论: 0)

1条记录访客评论

呵呵 有点意思  下班回家再来看

Post by power4leveling on 2009, June 14, 5:05 AM 引用此文发表评论 #1


发表评论

评论内容 (必填):