Pdf.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\common\service\watermark\pdf;
  3. use Mpdf\Mpdf;
  4. use Mpdf\Config\ConfigVariables;
  5. use Mpdf\Config\FontVariables;
  6. /**
  7. * Class Pdf
  8. *
  9. * @Author: Yeshihao
  10. * @DateTime: 2024/11/28 11:17
  11. * @package app\common\service\watermark\pdf
  12. */
  13. class Pdf
  14. {
  15. // public function waterMark($filePath, $waterText, $type = 0)
  16. // {
  17. // $pdf = new Fpdi();
  18. // $pdf->AddFont('msyh', '', 'msyh.ttf', true); //简体中文可正常显示
  19. // $pdf->SetFont('msyh', '', 40);
  20. //
  21. // $pageCount = $pdf->setSourceFile($filePath);
  22. //
  23. // for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
  24. // $templateId = $pdf->importPage($pageNo);
  25. // $pdf->AddPage();
  26. // $pdf->useTemplate($templateId, ['adjustPageSize' => true]);
  27. // $size = $pdf->getTemplateSize($templateId);
  28. // $center_x = $size['width'] / 2;
  29. // $center_y = $size['height'] / 2;
  30. // $pdf->SetXY($center_x, $center_y);
  31. // //文字水印
  32. // $pdf->SetTextColor(200, 200, 200);
  33. // $pdf->Write(0, $waterText);
  34. // }
  35. //
  36. // $pdf->Output('F', $filePath); //下载修改后的PDF
  37. // }
  38. /**
  39. * [waterMark]
  40. * 打印水印
  41. *
  42. * @Author: Yeshihao
  43. * @DateTime: 2024/11/28 13:39
  44. * @param $filePath
  45. * @param $waterText
  46. * @param int $type
  47. * @return bool
  48. * @throws \Mpdf\MpdfException
  49. * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
  50. * @throws \setasign\Fpdi\PdfParser\PdfParserException
  51. * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
  52. */
  53. public function waterMark($filePath, $waterText, $type = 0, $waterText2 = '')
  54. {
  55. $defaultConfig = (new ConfigVariables())->getDefaults();
  56. $fontDirs = $defaultConfig['fontDir'];
  57. $defaultFontConfig = (new FontVariables())->getDefaults();
  58. $fontData = $defaultFontConfig['fontdata'];
  59. $mpdf = new Mpdf(['mode' => '+aCJK',
  60. 'fontDir' => array_merge($fontDirs, [__DIR__.'/fonts']),
  61. 'fontdata' => $fontData + ['msyh' => ['R' => 'msyh.ttf',],],
  62. 'default_font' => 'msyh'
  63. ]);
  64. $mpdf->autoScriptToLang = true;
  65. $mpdf->autoLangToFont = true;
  66. $text = $waterText.$waterText2;
  67. //文字水印
  68. $watermarkText = new \Mpdf\WatermarkText($text, 30, 30, '#cccccc', 0.2);
  69. $mpdf->SetWatermarkText($watermarkText, 0.3);
  70. $mpdf->showWatermarkText = true;
  71. $mpdf->watermarkAngle = '45';
  72. $pageCount = $mpdf->SetSourceFile($filePath); //读取原始文件页数
  73. for ($i = 1; $i <= $pageCount; $i++) {
  74. $import_page = $mpdf->ImportPage($i);
  75. $mpdf->UseTemplate($import_page);
  76. if ($i < $pageCount)
  77. $mpdf->AddPage();
  78. }
  79. $mpdf->Output($filePath, 'F');// 下载修改后的PDF
  80. return true;
  81. }
  82. }