PdfService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Elton
  5. * Date: 2019/7/24
  6. * Time: 10:21
  7. */
  8. namespace app\common\service;
  9. class PdfService
  10. {
  11. /**
  12. * @var PdfService
  13. */
  14. private static $self;
  15. /**
  16. * @return PdfService
  17. */
  18. public static function instance()
  19. {
  20. if(self::$self == NULL){
  21. self::$self = new self();
  22. }
  23. return self::$self;
  24. }
  25. /**
  26. * @param $content
  27. * @param $outType I,在浏览器中打开;D,以文件形式下载;F,保存到服务器中;S,以字符串形式输出;E:以邮件的附件输出。
  28. */
  29. public function mpdf($filename, $content, $filetitle = '', $showType='I')
  30. {
  31. require_once ROOT_PATH .'vendor/tecnickcom/tcpdf/tcpdf.php';
  32. $title = $filename;
  33. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  34. $fontFile=ROOT_PATH . 'vendor/tecnickcom/tcpdf/fonts/stsongstdlight.php';
  35. $pdf->AddFont('stsongstdlight', '', $fontFile, FALSE);
  36. $pdf->SetCreator(PDF_CREATOR);
  37. $pdf->SetAuthor("作者");//设置作者
  38. $pdf->SetTitle($title);
  39. $pdf->SetSubject('TCPDF Tutorial');
  40. //$pdf->SetKeywords('TCPDF, PDF, example, test, guide');//设置关键字
  41. // 是否显示页眉
  42. $pdf->setPrintHeader(false);
  43. // 设置页眉显示的内容
  44. //$pdf->SetHeaderData($logo, 60, '', '');
  45. // 设置页眉字体
  46. //$pdf->setHeaderFont(Array('deja2vusans', '', '12'));
  47. // 页眉距离顶部的距离
  48. $pdf->SetHeaderMargin('5');
  49. // 是否显示页脚
  50. //$pdf->setPrintFooter(true);
  51. // 设置页脚显示的内容
  52. //$pdf->setFooterData(array(0,64,0), array(0,64,128));
  53. // 设置页脚的字体
  54. //$pdf->setFooterFont(Array('dejavusans', '', '10'));
  55. // 设置页脚距离底部的距离
  56. //$pdf->SetFooterMargin('10');
  57. // 设置默认等宽字体
  58. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  59. // 设置行高
  60. $pdf->setCellHeightRatio(1.5);
  61. // 设置左、上、右的间距
  62. $pdf->SetMargins('15', '15', '15');
  63. // set auto page breaks
  64. $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  65. // 设置字体
  66. $pdf->SetFont('stsongstdlight', '', 12, '', true);
  67. // 设置是否自动分页 距离底部多少距离时分页
  68. //$pdf->SetAutoPageBreak(TRUE, '15');
  69. // 设置图像比例因子
  70. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  71. //$pdf->setFontSubsetting(true);
  72. $pdf->AddPage("A4","Landscape",true,true);
  73. //$pdf->writeHTML($content);//HTML生成PDF
  74. $pdf->SetFont('stsongstdlight', '', 20, '', true);
  75. $pdf->writeHTMLCell(0, 10, '', '', $filetitle, 0, 1, 0, true, 'C', true);
  76. $pdf->SetFont('stsongstdlight', '', 12, '', true);
  77. $pdf->writeHTML($content, true, false, true, false, 'L');//设置logo
  78. //$pdf->writeHTMLCell(0, 0, '', '', $content, 0, 1, 0, true, '', true);
  79. $pdf->Output("{$filename}.pdf", $showType);
  80. //return $pdf->Output(ROOT_PATH."/{$filename}.pdf", $showType);
  81. //exit();
  82. }
  83. }