Pdf.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\index\controller;
  3. use think\facade\View;
  4. class Pdf extends Base
  5. {
  6. /*
  7. * 文档:https://tcpdf.org/examples/
  8. * composer 安装命令:composer require tecnickcom/tcpdf
  9. *
  10. * */
  11. /**
  12. * @var bool
  13. */
  14. protected $checkSignOpen = false;//是否校验签名,true校验,false不校验
  15. public function htmlTest1(){
  16. View::assign("title","这是标是,从后台来的");
  17. return view();
  18. }
  19. public function createPdfTest1(){
  20. // echo "test1 begin";
  21. $pdf=new \TCPDF("P","mm","A4");
  22. // 设置文档信息
  23. $pdf->SetCreator('Helloweba');
  24. $pdf->SetAuthor('yueguangguang');
  25. $pdf->SetTitle('Welcome to helloweba.com!');
  26. $pdf->SetSubject('TCPDF Tutorial');
  27. $pdf->SetKeywords('TCPDF, PDF, PHP');
  28. // 设置页眉和页脚信息
  29. $pdf->SetHeaderData('logo.png', 30, 'Helloweba.com', '致力于WEB前端技术在中国的应用',
  30. array(0,64,255), array(0,64,128));
  31. $pdf->setFooterData(array(0,64,0), array(0,64,128));
  32. // 设置页眉和页脚字体
  33. $pdf->setHeaderFont(Array('stsongstdlight', '', '10'));
  34. $pdf->setFooterFont(Array('helvetica', '', '8'));
  35. // 设置默认等宽字体
  36. $pdf->SetDefaultMonospacedFont('courier');
  37. // 设置间距
  38. $pdf->SetMargins(15, 27, 15);
  39. $pdf->SetHeaderMargin(5);
  40. $pdf->SetFooterMargin(10);
  41. // 设置分页
  42. $pdf->SetAutoPageBreak(TRUE, 25);
  43. // set image scale factor
  44. $pdf->setImageScale(1.25);
  45. // set default font subsetting mode
  46. $pdf->setFontSubsetting(true);
  47. //设置字体
  48. $pdf->SetFont('stsongstdlight', '', 14);
  49. $pdf->AddPage();
  50. $str1 = '<h1>这是h1</h1><div style="color: #f00;font-size: 14px;">这是红色</div><div style="color: #ddd;font-size: 20px;">这是灰色</div>';
  51. $pdf->writeHTML($str1, true, false, true, false, '');
  52. $pdf->AddPage();
  53. $str1 = '<h1>这是第2页</h1><div style="color: #f00;font-size: 14px;">这是红色</div><div style="color: #ddd;font-size: 20px;">这是灰色</div>';
  54. $pdf->writeHTML($str1, true, false, true, false, '');
  55. $pdf->AddPage();
  56. View::assign("title","这是标是,从后台来的");
  57. $str1=View::fetch("html_test1");
  58. $pdf->writeHTML($str1, true, false, true, false, '');
  59. //输出PDF
  60. $path=public_path()."/storage/pdf/".date("YmdHis").".pdf";
  61. $pdf->Output($path, 'F');
  62. $this->success("","成功");
  63. }
  64. }