如何使用PHP为现有PDF文件添加水印?

Yas*_*sir 11 php

我需要使用PHP为现有的PDF文件添加水印.我在Google上搜索过它,但找不到合适的库.

我找到了创建PDF文件预览缩略图的fpdf库,但我不知道它是否为现有PDF文件添加了水印.任何人都可以建议一个PHP库,而不是显示预览并为现有PDF文件添加水印?

ePi*_*rat 10

只是一个使用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);
Run Code Online (Sandbox Code Playgroud)

用法: PlaceWatermark($filename, $text, $x, $y, $opacity, $directoutput);

$filename- 要在其中放置水印的PDF路径
$text- 要添加的水印文本 - 要在其中放置水印的
$xx坐标 - 要在其中放置水印的
$yy坐标
$opacity- 文本的不透明度
$directoutput- 如果为TRUE函数将输出PDF文件,否则它将返回$ pdf
正如我已经说过的,这是一个非常快速和肮脏的例子,它需要一些改进.


Nat*_*han 6

对于偶然发现这篇文章的其他人,您可以使用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');}   
Run Code Online (Sandbox Code Playgroud)


Yas*_*sir 1

在马克的帮助下得到了它,我们去http://www.fpdf.de/downloads/addons/9/如果你们认为是的,我会将我的答案标记为获胜者。

感谢 Jyelton 对我的问题的回答,看起来 stackover flow 不活动......