AddFont('msyh', '', 'msyh.ttf', true); //简体中文可正常显示 // $pdf->SetFont('msyh', '', 40); // // $pageCount = $pdf->setSourceFile($filePath); // // for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) { //$templateId = $pdf->importPage($pageNo); // $pdf->AddPage(); // $pdf->useTemplate($templateId, ['adjustPageSize' => true]); //$size = $pdf->getTemplateSize($templateId); // $center_x = $size['width'] / 2; // $center_y = $size['height'] / 2; // $pdf->SetXY($center_x, $center_y); // //文字水印 // $pdf->SetTextColor(200, 200, 200); // $pdf->Write(0, $waterText); // } // // $pdf->Output('F', $filePath); //下载修改后的PDF // } /** * [waterMark] * 打印水印 * * @Author: Yeshihao * @DateTime: 2024/11/28 13:39 * @param $filePath * @param $waterText * @param int $type * @return bool * @throws \Mpdf\MpdfException * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException * @throws \setasign\Fpdi\PdfParser\PdfParserException * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException */ // 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 = '') { // 初始化 FPDI $pdf = new Fpdi(); $fontRegular = __DIR__."/fonts/msyh.php"; $pdf->AddFont('msyh', '', $fontRegular); // 常规 // //// 设置默认字体 $pdf->SetFont('msyh', '', 30); // 使用常规字体 // 确保 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++) { // 导入当前页 $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(); } } // 输出 PDF $pdf->Output($filePath, 'F'); return true; } }