Core.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace Pdf\Lib;
  3. use Dever;
  4. Dever::apply('fpdf/chinese', 'pdf');
  5. Dever::apply('tcpdf/tcpdf', 'pdf');
  6. class Core
  7. {
  8. public function init($page = 'P', $size = 'mm', $type = 'A4')
  9. {
  10. $this->pdf = new Pdf($page, $size, $type);
  11. $this->pdf->SetCreator('dever');
  12. $this->pdf->SetAuthor('dever');
  13. $this->pdf->SetFont('stsongstdlight', '', 10);
  14. $this->pdf->setHeaderFont(Array('stsongstdlight', '', '10'));
  15. $this->pdf->setFooterFont(Array('stsongstdlight', '', '8'));
  16. # 删除预定义的打印 页眉/页尾
  17. $this->pdf->setPrintHeader(false);
  18. $this->pdf->setPrintFooter(false);
  19. $this->pdf->AddPage();
  20. return $this->pdf;
  21. }
  22. }
  23. class Pdf extends \TCPDF
  24. {
  25. public function __construct($name, $size, $page)
  26. {
  27. parent::__construct($name, $size, $page, true, 'UTF-8', false);
  28. }
  29. /*
  30. font:表示字体;
  31. style:可选参数,表示样式,默认为普通样式;
  32. 取值:B:粗体 I:斜体 U:下划线
  33. size:可选参数,表示字体大小,默认为12pt;
  34. */
  35. public function font($size, $style = '', $font = '')
  36. {
  37. $this->SetFont($font, $style, $size);
  38. return $this;
  39. }
  40. # 段落
  41. public function content($name, $content, $size = 15)
  42. {
  43. if ($name) {
  44. $this->br(1, 1);
  45. $this->font($size, 'B');
  46. $this->left($name, 100);
  47. $this->br();
  48. }
  49. $this->font($size, '');
  50. $this->mleft($content);
  51. return $this;
  52. }
  53. /*设置单行单元格:
  54. W:设置单元格的宽
  55. H:设置单元格的高
  56. Text:单元格文本
  57. Border:设置单元格的边框。0,无边框,1,一个框,L,左边框,R,右边框,B, 底边框,T,顶边框,LTRB指四个边都显示
  58. Ln:0,单元格后的内容插到表格右边或左边,1,单元格的下一行,2,在单元格下面
  59. Align:文本位置。L,左对齐,R,右对齐,C,居中,J,自动对齐
  60. Fill:填充。false,单元格的背景为透明,true,单元格必需被填充
  61. Link:设置单元格文本的链接。
  62. $pdf->Cell(0, 10, 'test', 1, 1, 'C');
  63. */
  64. public function add($text, $align = 'L', $width = 0, $height = 8, $border = 0, $ln = 0, $link = '', $fill = false)
  65. {
  66. if (!$height) {
  67. $height = 8;
  68. }
  69. if (!$width) {
  70. $this->br();
  71. }
  72. $this->Cell($width, $height, $text, $border, $ln, $align, $fill, $link);
  73. return $this;
  74. }
  75. public function left($text, $width = 0, $border = 0, $height = 8)
  76. {
  77. $this->add($text, 'L', $width, $height, $border);
  78. return $this;
  79. }
  80. public function right($text, $width = 0, $border = 0, $height = 8)
  81. {
  82. $this->add($text, 'R', $width, $height, $border);
  83. return $this;
  84. }
  85. public function center($text, $width = 0, $border = 0, $height = 8)
  86. {
  87. $this->add($text, 'C', $width, $height, $border);
  88. return $this;
  89. }
  90. /*设置多行单元格。注意跟Cell的参数位置有些差别,Cell是用来输出单行文本的,MultiCell就能用来输出多行文本
  91. W:设置多行单元格的宽
  92. H: 设置多行单元格的单行的高
  93. Text:文本
  94. Border:边框
  95. Align:文本位置
  96. Fill:填充
  97. Ln:0,单元格后的内容插到表格右边或左边,1,单元格的下一行,2,在单元格下面
  98. X:设置多行单元格的行坐标
  99. Y:设置多行单元格的纵坐标
  100. Reseth:true,重新设置最后一行的高度
  101. Stretch:调整文本宽度适应单元格的宽度
  102. Ishtml:true,可以输出html文本,有时很有用的
  103. Autopadding:true,自动调整文本与单元格之间的距离
  104. Maxh:设置单元格最大的高度
  105. Valign:设置文本在纵坐标中的位置,T,偏上,M,居中,B,偏下
  106. Fillcell:自动调整文本字体大小来适应单元格大小。
  107. $pdf->MultiCell($w, $h, $txt, $border=0, $align='J',$fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0,$ishtml=false, $autopadding=true, $maxh=0, $valign='T', $fitcell=false);
  108. */
  109. public function madd($text, $align = 'L', $width = 0, $height = 8, $border = 0, $ln = 1, $fill = false, $x = '', $y = '', $reseth = true, $stretch = 0,$ishtml = false, $autopadding = false, $maxh = 0, $valign = 'T', $fitcell = false)
  110. {
  111. if (!$height) {
  112. $height = 8;
  113. }
  114. if (!$width) {
  115. //$this->br();
  116. }
  117. $this->MultiCell($width, $height, $text, $border, $align, $fill, $ln, $x, $y, $reseth, $stretch, $ishtml, $autopadding, $maxh, $valign, $fitcell);
  118. return $this;
  119. }
  120. public function mleft($text, $width = 0, $border = 0, $height = 8)
  121. {
  122. $this->madd($text, 'L', $width, $height, $border);
  123. return $this;
  124. }
  125. public function mright($text, $width = 0, $border = 0, $height = 8)
  126. {
  127. $this->madd($text, 'R', $width, $height, $border);
  128. return $this;
  129. }
  130. public function mcenter($text, $width = 0, $border = 0, $height = 8)
  131. {
  132. $this->madd($text, 'C', $width, $height, $border);
  133. return $this;
  134. }
  135. public function img($file, $x, $y = false, $w = 30, $h = 30)
  136. {
  137. if (!$y) {
  138. $y = $this->y();
  139. }
  140. $this->Image(Dever::pic($file), $x, $y, $w, $h);
  141. return $this;
  142. }
  143. public function hr($str = '-', $name = '')
  144. {
  145. $text = str_pad($name, 142, $str, STR_PAD_BOTH);
  146. $this->add($text);
  147. return $this;
  148. }
  149. public function br($num = 1, $size = null)
  150. {
  151. for ($i = 0; $i < $num; $i++) {
  152. $this->Ln($size);
  153. }
  154. return $this;
  155. }
  156. public function y()
  157. {
  158. return $this->GetY();
  159. }
  160. public function x()
  161. {
  162. return $this->GetX();
  163. }
  164. public function convert($string)
  165. {
  166. return Dever::convert($string);
  167. }
  168. /*
  169. name:可选参数,表示要储存的文件名。
  170. dest:可选参数,操作内容。
  171. 取值:
  172. I,默认值,在浏览器中打开;D,点击下载按钮, PDF文件会被下载下来;F,文件会被保存在服务器中;S,PDF会以字符串形式输出;E:PDF以邮件的附件输出
  173. */
  174. public function out($name, $dest = 'I')
  175. {
  176. $this->Output($name, $dest);
  177. }
  178. }