Pdf.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\common\service\watermark\pdf;
  3. use Grafika\Grafika;
  4. use Mpdf\Mpdf;
  5. use Mpdf\Config\ConfigVariables;
  6. use Mpdf\Config\FontVariables;
  7. use TCPDF;
  8. use setasign\Fpdi\Tcpdf\Fpdi;
  9. /**
  10. * Class Pdf
  11. *
  12. * @Author: Yeshihao
  13. * @DateTime: 2024/11/28 11:17
  14. * @package app\common\service\watermark\pdf
  15. */
  16. class Pdf
  17. {
  18. // public function waterMark($filePath, $waterText, $type = 0)
  19. // {
  20. // $pdf = new Fpdi();
  21. // $pdf->AddFont('msyh', '', 'msyh.ttf', true); //简体中文可正常显示
  22. // $pdf->SetFont('msyh', '', 40);
  23. //
  24. // $pageCount = $pdf->setSourceFile($filePath);
  25. //
  26. // for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
  27. //$templateId = $pdf->importPage($pageNo);
  28. // $pdf->AddPage();
  29. // $pdf->useTemplate($templateId, ['adjustPageSize' => true]);
  30. //$size = $pdf->getTemplateSize($templateId);
  31. // $center_x = $size['width'] / 2;
  32. // $center_y = $size['height'] / 2;
  33. // $pdf->SetXY($center_x, $center_y);
  34. // //文字水印
  35. // $pdf->SetTextColor(200, 200, 200);
  36. // $pdf->Write(0, $waterText);
  37. // }
  38. //
  39. // $pdf->Output('F', $filePath); //下载修改后的PDF
  40. // }
  41. /**
  42. * [waterMark]
  43. * 打印水印
  44. *
  45. * @Author: Yeshihao
  46. * @DateTime: 2024/11/28 13:39
  47. * @param $filePath
  48. * @param $waterText
  49. * @param int $type
  50. * @return bool
  51. * @throws \Mpdf\MpdfException
  52. * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
  53. * @throws \setasign\Fpdi\PdfParser\PdfParserException
  54. * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
  55. */
  56. // public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $pageWidth = 210, $pageHeight = 297)
  57. // {
  58. // $defaultConfig = (new ConfigVariables())->getDefaults();
  59. // $fontDirs = $defaultConfig['fontDir'];
  60. // $defaultFontConfig = (new FontVariables())->getDefaults();
  61. // $fontData = $defaultFontConfig['fontdata'];
  62. // $mpdf = new Mpdf(['mode' => '+aCJK',
  63. // 'fontDir' => array_merge($fontDirs, [__DIR__.'/fonts']),
  64. // 'fontdata' => $fontData + ['msyh' => ['R' => 'msyh.ttf',],],
  65. // 'default_font' => 'msyh',
  66. // 'format' => [$pageWidth ?: 210, $pageHeight ?: 297],
  67. // ]);
  68. // $mpdf->autoScriptToLang = true;
  69. // $mpdf->autoLangToFont = true;
  70. //
  71. // $text = $waterText.$waterText2;
  72. // //文字水印
  73. // $alpha = number_format((100 - $alpha) / 100, 2);
  74. // $watermarkText = new \Mpdf\WatermarkText($text, 30, 30, '#cccccc', $alpha);
  75. // $mpdf->SetWatermarkText($watermarkText, $alpha);
  76. // $mpdf->showWatermarkText = true;
  77. // $mpdf->watermarkAngle = '45';
  78. // $pageCount = $mpdf->SetSourceFile($filePath); //读取原始文件页数
  79. // for ($i = 1; $i <= $pageCount; $i++) {
  80. // $import_page = $mpdf->ImportPage($i);
  81. // $mpdf->UseTemplate($import_page);
  82. // if ($i < $pageCount)
  83. // $mpdf->AddPage();
  84. // }
  85. // $mpdf->Output($filePath, 'F');// 下载修改后的PDF
  86. // return true;
  87. // }
  88. /**
  89. * [waterMark]
  90. *
  91. * @Author: Yeshihao
  92. * @DateTime: 2025/4/27 16:48
  93. * @param $filePath
  94. * @param $waterText
  95. * @param int $alpha
  96. * @param int $type
  97. * @param string $waterText2
  98. * @param int $pageWidth
  99. * @param int $pageHeight
  100. * @param string $waterText3
  101. * @return bool
  102. * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
  103. * @throws \setasign\Fpdi\PdfParser\Filter\FilterException
  104. * @throws \setasign\Fpdi\PdfParser\PdfParserException
  105. * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
  106. * @throws \setasign\Fpdi\PdfReader\PdfReaderException
  107. */
  108. public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $pageWidth = 210, $pageHeight = 297, $waterText3 = '')
  109. {
  110. // 初始化 FPDI
  111. $pdf = new Fpdi();
  112. $fontRegular = __DIR__."/fonts/msyh.php";
  113. $pdf->AddFont('msyh', '', $fontRegular); // 常规
  114. //
  115. //// 设置默认字体
  116. $pdf->SetFont('msyh', '', 30); // 使用常规字体
  117. // 确保 PHP 文件编码为 UTF-8(无 BOM)
  118. // $pdf->setHeaderData('', 0, '', '', [0, 0, 0], [255, 255, 255]);
  119. // $pdf->setDocInfo('Creator', 'Your App', 'Title', 'Subject', 'Keywords', 'UTF-8');
  120. // $pdf->SetFont('cid0cs', '', 30); // 使用内置中文字体
  121. // $pdf->setLanguageArray(['a_meta_charset' => 'UTF-8']);
  122. // 加载已有 PDF
  123. $pageCount = $pdf->setSourceFile($filePath);
  124. //透明度
  125. $alpha = number_format((100 - $alpha) / 100, 2);
  126. // 遍历每一页
  127. for ($i = 1; $i <= $pageCount; $i++) {
  128. // 导入当前页
  129. $templateId = $pdf->importPage($i);
  130. $size = $pdf->getTemplateSize($templateId);
  131. $pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
  132. $pdf->useTemplate($templateId);
  133. $pdf->SetTextColor(204, 204, 204); // 透明度
  134. $pdf->SetAlpha($alpha); // 整体透明度
  135. // 计算居中位置
  136. $x = $size['width'] / 2;
  137. $y = $size['height'] / 2;
  138. // 第一行水印
  139. $pdf->StartTransform();
  140. $pdf->Rotate(45, $x, $y);
  141. $pdf->Text($x - 80, $y, $waterText);
  142. $pdf->StopTransform();
  143. if ($waterText2) {
  144. // 第二行水印
  145. $pdf->StartTransform();
  146. $pdf->Rotate(45, $x, $y);
  147. $pdf->Text($x - 80, $y + 15, $waterText2); // 调整 Y 坐标控制行间距
  148. $pdf->StopTransform();
  149. }
  150. if ($waterText3) {
  151. // 第三行水印
  152. $pdf->StartTransform();
  153. $pdf->Rotate(45, $x, $y);
  154. $pdf->Text($x - 80, $y - 15, $waterText3); // 调整 Y 坐标控制行间距
  155. $pdf->StopTransform();
  156. }
  157. }
  158. // 输出 PDF
  159. $pdf->Output($filePath, 'F');
  160. return true;
  161. }
  162. }