我需要添加一个水印到现有的PDF文件使用PHP。我已经在谷歌上搜索过了,但是找不到合适的库。
我找到了创建fpdf文件预览缩略图的PDF库,但我不知道它是否会在现有的PDF文件中添加水印。有没有人可以推荐一个PHP库来显示预览和添加水印到现有的PDF文件?
发布于 2011-10-29 07:32:02
这只是一个使用FPDF和FPDI类的快速脏示例:
function PlaceWatermark($file, $text, $xxx, $yyy, $op, $outdir) {
require_once('fpdf.php');
require_once('fpdi.php');
$name = uniqid();
$font_size = 5;
$ts=explode("\n",$text);
$width=0;
foreach ($ts as $k=>$string) {
$width=max($width,strlen($string));
}
$width = imagefontwidth($font_size)*$width;
$height = imagefontheight($font_size)*count($ts);
$el=imagefontheight($font_size);
$em=imagefontwidth($font_size);
$img = imagecreatetruecolor($width,$height);
// Background color
$bg = imagecolorallocate($img, 255, 255, 255);
imagefilledrectangle($img, 0, 0,$width ,$height , $bg);
// Font color
$color = imagecolorallocate($img, 0, 0, 0);
foreach ($ts as $k=>$string) {
$len = strlen($string);
$ypos = 0;
for($i=0;$i<$len;$i++){
$xpos = $i * $em;
$ypos = $k * $el;
imagechar($img, $font_size, $xpos, $ypos, $string, $color);
$string = substr($string, 1);
}
}
imagecolortransparent($img, $bg);
$blank = imagecreatetruecolor($width, $height);
$tbg = imagecolorallocate($blank, 255, 255, 255);
imagefilledrectangle($blank, 0, 0,$width ,$height , $tbg);
imagecolortransparent($blank, $tbg);
if ( ($op < 0) OR ($op >100) ){
$op = 100;
}
imagecopymerge($blank, $img, 0, 0, 0, 0, $width, $height, $op);
imagepng($blank,$name.".png");
// Created Watermark Image
$pdf = new FPDI();
if (file_exists("./".$file)){
$pagecount = $pdf->setSourceFile($file);
} else {
return FALSE;
}
$tpl = $pdf->importPage(1);
$pdf->addPage();
$pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE);
//Put the watermark
$pdf->Image($name.'.png', $xxx, $yyy, 0, 0, 'png');
if ($outdir === TRUE){
return $pdf->Output();
} else {
return $pdf;
}
}
PlaceWatermark("filename.pdf", "This is a lazy, but still simple test\n This should stand on a new line!", 30, 120, 100,TRUE);用法:PlaceWatermark($filename, $text, $x, $y, $opacity, $directoutput);
$filename -要在其中放置水印的PDF的路径
$text -要添加的水印文本
要放置水印的$x -x坐标
要放置水印的$y -y坐标
$opacity -文本的不透明度
$directoutput -如果为TRUE,函数将输出PDF文件,否则将返回$pdf
正如我已经说过的,这是一个非常快速和肮脏的例子,它需要一些改进。
发布于 2013-04-10 15:06:24
对于其他偶然发现这篇文章的人,您可以使用For循环生成更多页面
for($i=1; $i <= $pagecount; $i++) {
$tpl = $pdf->importPage($i);
$pdf->addPage();
$pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE);
//Put the watermark
$pdf->Image($name.'.png', $xxx, $yyy, 0, 0, 'png');} 发布于 2020-01-02 18:23:20
这是一个MPDF实践,将水印添加到面向横向的PDF的每一页。
//First, get the correct document size.
$mpdf = new \Mpdf\Mpdf([
'tempDir' => storage_path('app'),
'orientation' => 'L'
]);
$pagecount = $mpdf->SetSourceFile('[path]');
$tplId = $mpdf->ImportPage(1);
$size = $mpdf->getTemplateSize($tplId);
//Open a new instance with specified width and height, read the file again
$mpdf = new \Mpdf\Mpdf([
'tempDir' => storage_path('app'),
'format' => [$size['width'], $size['height']]
]);
$mpdf->SetSourceFile('[path]');
//Write into the instance and output it
for ($i=1; $i <= $pagecount; $i++) {
$tplId = $mpdf->ImportPage($i);
$mpdf->addPage();
$mpdf->UseTemplate($tplId);
$mpdf->SetWatermarkText('[Watermark Text]');
$mpdf->showWatermarkText = true;
}
return $mpdf->output();https://stackoverflow.com/questions/2913934
复制相似问题