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

FPDF代码研究(Part 2,构造函数)

  类需要一个初始化函数,函数名与类名相同,初始化类的时候这个构造函数就开始运行,构造函数可以使用参数,也就是调用类的时候需要的参数。

  下面是FPDF类的构造函数,带了三个参数,分别是页面方向(默认为纵向)、使用的单位(默认为 mm)、页面样式(默认为 A4)。

PHP代码
  1. function FPDF($orientation='P'$unit='mm'$format='A4')
  2. {
  3.     // 调用私有方法 _dochecks() 进行检查
  4.     $this->_dochecks();
  5.     // 属性初始化
  6.     $this->page = 0;
  7.     $this->n = 2;
  8.     $this->buffer = '';
  9.     $this->pages = array();
  10.     $this->PageSizes = array();
  11.     $this->state = 0;
  12.     $this->fonts = array();
  13.     $this->FontFiles = array();
  14.     $this->diffs = array();
  15.     $this->images = array();
  16.     $this->links = array();
  17.     $this->InHeader = false;
  18.     $this->InFooter = false;
  19.     $this->lasth = 0;
  20.     $this->FontFamily = '';
  21.     $this->FontStyle = '';
  22.     $this->FontSizePt = 12;
  23.     $this->underline = false;
  24.     $this->DrawColor = '0 G';
  25.     $this->FillColor = '0 g';
  26.     $this->TextColor = '0 g';
  27.     $this->ColorFlag = false;
  28.     $this->ws=0;
  29.     // 标准字体
  30.     $this->CoreFonts = array('courier'=>'Courier''courierB'=>'Courier-Bold',
  31.         'courierI'=>'Courier-Oblique''courierBI'=>'Courier-BoldOblique',
  32.         'helvetica'=>'Helvetica''helveticaB'=>'Helvetica-Bold',
  33.         'helveticaI'=>'Helvetica-Oblique','helveticaBI'=>'Helvetica-BoldOblique',
  34.         'times'=>'Times-Roman''timesB'=>'Times-Bold''timesI'=>'Times-Italic',
  35.         'timesBI'=>'Times-BoldItalic', 'symbol'=>'Symbol',
  36.         'zapfdingbats'=>'ZapfDingbats');   
  37.     // 比例因子
  38.     if($unit == 'pt')
  39.         $this->k = 1;
  40.     elseif($unit == 'mm')
  41.         $this->k = 72/25.4;
  42.     elseif($unit == 'cm')
  43.         $this->k = 72/2.54;
  44.     elseif($unit == 'in')
  45.         $this->k = 72;
  46.     else
  47.         $this->Error('Incorrect unit: '.$unit);
  48.     // 页面式样,调用私有方法 _getpageformat()
  49.     $this->PageFormats = array('a3'=>array(841.89,1190.55),
  50.         'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28),
  51.         'letter'=>array(612,792), 'legal'=>array(612,1008));
  52.     if(is_string($format))
  53.         $format = $this->_getpageformat($format);
  54.     $this->DefPageFormat = $format;
  55.     $this->CurPageFormat = $format;
  56.     // 页面方向
  57.     $orientation = strtolower($orientation);
  58.     if($orientation == 'p' || $orientation == 'portrait')
  59.     {
  60.         $this->DefOrientation = 'P';
  61.         $this->w = $this->DefPageFormat[0];
  62.         $this->h = $this->DefPageFormat[1];
  63.     }
  64.     elseif($orientation == 'l' || $orientation == 'landscape')
  65.     {
  66.         $this->DefOrientation = 'L';
  67.         $this->w = $this->DefPageFormat[1];
  68.         $this->h = $this->DefPageFormat[0];
  69.     }
  70.     else
  71.         $this->Error('Incorrect orientation: '.$orientation);
  72.     $this->CurOrientation = $this->DefOrientation;
  73.     $this->wPt = $this->w * $this->k;
  74.     $this->hPt = $this->h * $this->k;
  75.     // 页边距(默认为 1 cm),调用SetMargins()
  76.     $margin = 28.35 / $this->k;
  77.     $this->SetMargins($margin, $margin);
  78.     // 内部单元边距(默认为 1 mm)
  79.     $this->cMargin = $margin / 10;
  80.     // 线的粗细(默认为 0.2 mm)
  81.     $this->LineWidth = .567 / $this->k;
  82.     // 换页边距,即页面下边距(默认为 2 cm),调用SetAutoPageBreak()
  83.     $this->SetAutoPageBreak(true, 2*$margin);
  84.     // 显示模式,默认为窗口最大宽度,连续分页,调用SetDisplayMode()
  85.     $this->SetDisplayMode('fullwidth');
  86.     // 使用压缩,调用SetCompression()
  87.     $this->SetCompression(true);
  88.     // 设置PDF文件版本号,默认为1.3,即4.x以上Adobe Reader可读
  89.     $this->PDFVersion = '1.3';
  90. }

  构造函数中调用了2个私有方法 _dochecks() 和 _getpageformat(),还有5个公共方法 Error()、SetMargins()、SetAutoPageBreak()、SetDisplayMode() 和 SetCompression(),下一篇逐个分析它们。

Tags: php, fpdf

« 上一篇 | 下一篇 »

只显示5条记录相关文章

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

发表评论

评论内容 (必填):