Test.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\task\controller;
  3. use TCPDF;
  4. use app\common\model\Certificate;
  5. class Test
  6. {
  7. public function createPdf()
  8. {
  9. // 创建新的PDF文档
  10. $pdf = new TCPDF();
  11. // 设置文档信息
  12. $pdf->SetCreator(PDF_CREATOR);
  13. $pdf->SetAuthor('Your Name');
  14. $pdf->SetTitle('PDF with Watermark');
  15. $pdf->SetSubject('TCPDF Tutorial');
  16. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  17. // 设置默认的头信息, 传入空字符串禁用
  18. $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING);
  19. // 设置头部和底部字体
  20. $pdf->setHeaderFont([PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN]);
  21. $pdf->setFooterFont([PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA]);
  22. // 移除默认的页脚
  23. $pdf->setPrintFooter(false);
  24. // 设置默认的单元格边距
  25. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  26. // 设置间距
  27. $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  28. $pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
  29. // 设置图像比例因子
  30. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  31. // 设置默认的字体,这里设置为DejaVuSans
  32. // $pdf->SetFont('dejavusans', '', 14);
  33. // 添加页面内容
  34. $pdf->AddPage();
  35. $pdf->Write(0, "I'm a simple text to test the watermark.\n");
  36. // 设置水印文字
  37. $watermarkText = "水印水印水印水印水印";
  38. $pdf->SetFont('msyh', '', 50);
  39. // 计算斜着文字的起始坐标
  40. $x = $pdf->getX();
  41. $y = $pdf->getY();
  42. // 设置斜着文字的角度
  43. $angle = 45;
  44. // 使用Rotate()函数插入斜着的文字水印
  45. $pdf->StartTransform();
  46. $pdf->Rotate($angle, $x, $y);
  47. $pdf->SetTextColor(200, 200, 200);
  48. $pdf->Text($x, $y, $watermarkText);
  49. $pdf->StopTransform();
  50. // $pdf->SetTextColor(200, 200, 200);
  51. // $pdf->SetXY(20, 50);
  52. // $pdf->Write(0, 'Watermark');
  53. // $pdf->SetXY(100, 50);
  54. // $pdf->Write(0, '水印'); //中文乱码
  55. $pdf->Output(WEB_PATH.'temp'.'/'.'AA.pdf', 'F'); //生成PDF文件到某地
  56. // 输出PDF
  57. $pdf->Output('AA.pdf', 'I');
  58. }
  59. public function repairTask()
  60. {
  61. exit();
  62. $model = new Certificate();
  63. $list = $model->with(['file', 'category'])
  64. ->where('is_delete', '=', 0)->select();
  65. return $list->each(function ($item) {
  66. $item['file_type'] = $item->file['file_type'];
  67. $item->save();
  68. });
  69. }
  70. }