Core.php 6.0 KB

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