composer
"setasign/fpdf": "^1.8",
"setasign/fpdi": "^2.6",
"setasign/tfpdf": "^1.33",
"tecnickcom/tcpdf": "6.6.*",
"mpdf/mpdf": "^8.2"
Fpdi水印无法旋转,mpdf可设置旋转角度
原文地址:php给pdf文件加上水印 - carol2014 - 博客园
mpdf:
$defaultConfig = (new ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$mpdf = new Mpdf([
'mode' => '+aCJK',
'fontDir' => array_merge($fontDirs, [ROOT_PATH . '/public/static']),
'fontdata' => $fontData +
[
'msyh' => [
'R' => 'msyh.ttf',
],
],
'default_font' => 'msyh'
]);
$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;
$text = 'H.E. Adeeb Alaama' . ' ' . date('Y-m-d H:i') . ' ' . getip();
//文字水印
$watermarkText = new \Mpdf\WatermarkText($text, 30, 30, '#996633', 0.4);
$mpdf->SetWatermarkText($watermarkText, 0.3);
$mpdf->showWatermarkText = true;
// $mpdf->watermarkAngle = '30';
$pageCount = $mpdf->SetSourceFile(ROOT_PATH . '/public/storage/'.$link_url); //读取原始文件页数
for ($i = 1; $i <= $pageCount; $i++) {
$import_page = $mpdf->ImportPage($i);
$mpdf->UseTemplate($import_page);
if ($i < $pageCount)
$mpdf->AddPage();
}
$mpdf->Output(ROOT_PATH . '/public/upload/watermarked_pdf.pdf', 'F');// 下载修改后的PDF
Fpdi:
$pdf = new \setasign\Fpdi\Tfpdf\Fpdi();
$pdf->AddFont('msyh', '', 'msyh.ttf', true); //简体中文可正常显示
$pdf->SetFont('msyh', '', 14);
$pageCount = $pdf->setSourceFile(ROOT_PATH . '/public/storage/'.$link_url);
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$templateId = $pdf->importPage($pageNo);
$pdf->AddPage();
$pdf->useTemplate($templateId, ['adjustPageSize' => true]);
//文字水印
$pdf->SetTextColor(200, 200, 200);
$pdf->SetXY(20, 30);
$pdf->Write(0, 'H.E. Adeeb Alaama');
$pdf->SetXY(80, 30);
$pdf->Write(0, date('Y-m-d H:i'));
$pdf->SetXY(140, 30);
$pdf->Write(0, getip());
//设置位置 - 加在中间位置
$size = $pdf->getTemplateSize($templateId);
$center_x = $size['width']/2;
$center_y = $size['height']/2;
$pdf->SetXY($center_x, $center_y);
}
$pdf->Output('F', ROOT_PATH . '/public/upload/watermarked_pdf.pdf');// 下载修改后的PDF