Pdf.php 2.9 KB

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