|
@@ -2,10 +2,12 @@
|
|
|
|
|
|
|
|
namespace app\common\service\watermark\pdf;
|
|
namespace app\common\service\watermark\pdf;
|
|
|
|
|
|
|
|
|
|
+use Grafika\Grafika;
|
|
|
use Mpdf\Mpdf;
|
|
use Mpdf\Mpdf;
|
|
|
use Mpdf\Config\ConfigVariables;
|
|
use Mpdf\Config\ConfigVariables;
|
|
|
use Mpdf\Config\FontVariables;
|
|
use Mpdf\Config\FontVariables;
|
|
|
use TCPDF;
|
|
use TCPDF;
|
|
|
|
|
+use setasign\Fpdi\Tcpdf\Fpdi;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Class Pdf
|
|
* Class Pdf
|
|
@@ -58,36 +60,123 @@ class Pdf
|
|
|
* @throws \setasign\Fpdi\PdfParser\PdfParserException
|
|
* @throws \setasign\Fpdi\PdfParser\PdfParserException
|
|
|
* @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
|
|
* @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
|
|
|
*/
|
|
*/
|
|
|
- public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $pageWidth = 210, $pageHeight = 297)
|
|
|
|
|
|
|
+// public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $pageWidth = 210, $pageHeight = 297)
|
|
|
|
|
+// {
|
|
|
|
|
+// $defaultConfig = (new ConfigVariables())->getDefaults();
|
|
|
|
|
+// $fontDirs = $defaultConfig['fontDir'];
|
|
|
|
|
+// $defaultFontConfig = (new FontVariables())->getDefaults();
|
|
|
|
|
+// $fontData = $defaultFontConfig['fontdata'];
|
|
|
|
|
+// $mpdf = new Mpdf(['mode' => '+aCJK',
|
|
|
|
|
+// 'fontDir' => array_merge($fontDirs, [__DIR__.'/fonts']),
|
|
|
|
|
+// 'fontdata' => $fontData + ['msyh' => ['R' => 'msyh.ttf',],],
|
|
|
|
|
+// 'default_font' => 'msyh',
|
|
|
|
|
+// 'format' => [$pageWidth ?: 210, $pageHeight ?: 297],
|
|
|
|
|
+// ]);
|
|
|
|
|
+// $mpdf->autoScriptToLang = true;
|
|
|
|
|
+// $mpdf->autoLangToFont = true;
|
|
|
|
|
+//
|
|
|
|
|
+// $text = $waterText.$waterText2;
|
|
|
|
|
+// //文字水印
|
|
|
|
|
+// $alpha = number_format((100 - $alpha) / 100, 2);
|
|
|
|
|
+// $watermarkText = new \Mpdf\WatermarkText($text, 30, 30, '#cccccc', $alpha);
|
|
|
|
|
+// $mpdf->SetWatermarkText($watermarkText, $alpha);
|
|
|
|
|
+// $mpdf->showWatermarkText = true;
|
|
|
|
|
+// $mpdf->watermarkAngle = '45';
|
|
|
|
|
+// $pageCount = $mpdf->SetSourceFile($filePath); //读取原始文件页数
|
|
|
|
|
+// for ($i = 1; $i <= $pageCount; $i++) {
|
|
|
|
|
+// $import_page = $mpdf->ImportPage($i);
|
|
|
|
|
+// $mpdf->UseTemplate($import_page);
|
|
|
|
|
+// if ($i < $pageCount)
|
|
|
|
|
+// $mpdf->AddPage();
|
|
|
|
|
+// }
|
|
|
|
|
+// $mpdf->Output($filePath, 'F');// 下载修改后的PDF
|
|
|
|
|
+// return true;
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * [waterMark]
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Author: Yeshihao
|
|
|
|
|
+ * @DateTime: 2025/4/27 16:48
|
|
|
|
|
+ * @param $filePath
|
|
|
|
|
+ * @param $waterText
|
|
|
|
|
+ * @param int $alpha
|
|
|
|
|
+ * @param int $type
|
|
|
|
|
+ * @param string $waterText2
|
|
|
|
|
+ * @param int $pageWidth
|
|
|
|
|
+ * @param int $pageHeight
|
|
|
|
|
+ * @param string $waterText3
|
|
|
|
|
+ * @return bool
|
|
|
|
|
+ * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
|
|
|
|
|
+ * @throws \setasign\Fpdi\PdfParser\Filter\FilterException
|
|
|
|
|
+ * @throws \setasign\Fpdi\PdfParser\PdfParserException
|
|
|
|
|
+ * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
|
|
|
|
|
+ * @throws \setasign\Fpdi\PdfReader\PdfReaderException
|
|
|
|
|
+ */
|
|
|
|
|
+ public function waterMark($filePath, $waterText, $alpha = 30, $type = 0, $waterText2 = '', $pageWidth = 210, $pageHeight = 297, $waterText3 = '')
|
|
|
{
|
|
{
|
|
|
- $defaultConfig = (new ConfigVariables())->getDefaults();
|
|
|
|
|
- $fontDirs = $defaultConfig['fontDir'];
|
|
|
|
|
- $defaultFontConfig = (new FontVariables())->getDefaults();
|
|
|
|
|
- $fontData = $defaultFontConfig['fontdata'];
|
|
|
|
|
- $mpdf = new Mpdf(['mode' => '+aCJK',
|
|
|
|
|
- 'fontDir' => array_merge($fontDirs, [__DIR__.'/fonts']),
|
|
|
|
|
- 'fontdata' => $fontData + ['msyh' => ['R' => 'msyh.ttf',],],
|
|
|
|
|
- 'default_font' => 'msyh',
|
|
|
|
|
- 'format' => [$pageWidth ?: 210, $pageHeight ?: 297],
|
|
|
|
|
- ]);
|
|
|
|
|
- $mpdf->autoScriptToLang = true;
|
|
|
|
|
- $mpdf->autoLangToFont = true;
|
|
|
|
|
-
|
|
|
|
|
- $text = $waterText.$waterText2;
|
|
|
|
|
- //文字水印
|
|
|
|
|
- $alpha = number_format((100 - $alpha) / 100, 2);
|
|
|
|
|
- $watermarkText = new \Mpdf\WatermarkText($text, 30, 30, '#cccccc', $alpha);
|
|
|
|
|
- $mpdf->SetWatermarkText($watermarkText, $alpha);
|
|
|
|
|
- $mpdf->showWatermarkText = true;
|
|
|
|
|
- $mpdf->watermarkAngle = '45';
|
|
|
|
|
- $pageCount = $mpdf->SetSourceFile($filePath); //读取原始文件页数
|
|
|
|
|
|
|
+ // 初始化 FPDI
|
|
|
|
|
+ $pdf = new Fpdi();
|
|
|
|
|
+
|
|
|
|
|
+// $fontRegular = WEB_PATH . "static/common/fonts/msyh.ttf";
|
|
|
|
|
+// $pdf->AddFont('msyh', '', $fontRegular, true); // 常规
|
|
|
|
|
+//
|
|
|
|
|
+//// 设置默认字体
|
|
|
|
|
+// $pdf->SetFont('msyh', '', 40); // 使用常规字体
|
|
|
|
|
+
|
|
|
|
|
+// 确保 PHP 文件编码为 UTF-8(无 BOM)
|
|
|
|
|
+// $pdf->setHeaderData('', 0, '', '', [0, 0, 0], [255, 255, 255]);
|
|
|
|
|
+// $pdf->setDocInfo('Creator', 'Your App', 'Title', 'Subject', 'Keywords', 'UTF-8');
|
|
|
|
|
+
|
|
|
|
|
+ $pdf->SetFont('cid0cs', '', 30); // 使用内置中文字体
|
|
|
|
|
+ $pdf->setLanguageArray(['a_meta_charset' => 'UTF-8']);
|
|
|
|
|
+
|
|
|
|
|
+ // 加载已有 PDF
|
|
|
|
|
+ $pageCount = $pdf->setSourceFile($filePath);
|
|
|
|
|
+
|
|
|
|
|
+ //透明度
|
|
|
|
|
+ $alpha = number_format((100 - $alpha) / 100, 2);
|
|
|
|
|
+
|
|
|
|
|
+ // 遍历每一页
|
|
|
for ($i = 1; $i <= $pageCount; $i++) {
|
|
for ($i = 1; $i <= $pageCount; $i++) {
|
|
|
- $import_page = $mpdf->ImportPage($i);
|
|
|
|
|
- $mpdf->UseTemplate($import_page);
|
|
|
|
|
- if ($i < $pageCount)
|
|
|
|
|
- $mpdf->AddPage();
|
|
|
|
|
|
|
+ // 导入当前页
|
|
|
|
|
+ $templateId = $pdf->importPage($i);
|
|
|
|
|
+ $size = $pdf->getTemplateSize($templateId);
|
|
|
|
|
+ $pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
|
|
|
|
|
+ $pdf->useTemplate($templateId);
|
|
|
|
|
+
|
|
|
|
|
+ $pdf->SetTextColor(204, 204, 204); // 透明度
|
|
|
|
|
+ $pdf->SetAlpha($alpha); // 整体透明度
|
|
|
|
|
+
|
|
|
|
|
+ // 计算居中位置
|
|
|
|
|
+ $x = $size['width'] / 2;
|
|
|
|
|
+ $y = $size['height'] / 2;
|
|
|
|
|
+
|
|
|
|
|
+ // 第一行水印
|
|
|
|
|
+ $pdf->StartTransform();
|
|
|
|
|
+ $pdf->Rotate(45, $x, $y);
|
|
|
|
|
+ $pdf->Text($x - 80, $y, $waterText);
|
|
|
|
|
+ $pdf->StopTransform();
|
|
|
|
|
+
|
|
|
|
|
+ if ($waterText2) {
|
|
|
|
|
+ // 第二行水印
|
|
|
|
|
+ $pdf->StartTransform();
|
|
|
|
|
+ $pdf->Rotate(45, $x, $y);
|
|
|
|
|
+ $pdf->Text($x - 80, $y + 15, $waterText2); // 调整 Y 坐标控制行间距
|
|
|
|
|
+ $pdf->StopTransform();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($waterText3) {
|
|
|
|
|
+ // 第三行水印
|
|
|
|
|
+ $pdf->StartTransform();
|
|
|
|
|
+ $pdf->Rotate(45, $x, $y);
|
|
|
|
|
+ $pdf->Text($x - 80, $y - 15, $waterText3); // 调整 Y 坐标控制行间距
|
|
|
|
|
+ $pdf->StopTransform();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- $mpdf->Output($filePath, 'F');// 下载修改后的PDF
|
|
|
|
|
|
|
+
|
|
|
|
|
+// 输出 PDF
|
|
|
|
|
+ $pdf->Output($filePath, 'F');
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|