首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHP将水印添加到现有PDF文件?

如何使用PHP将水印添加到现有PDF文件?
EN

Stack Overflow用户
提问于 2010-05-26 22:38:30
回答 5查看 42.7K关注 0票数 12

我需要添加一个水印到现有的PDF文件使用PHP。我已经在谷歌上搜索过了,但是找不到合适的库。

我找到了创建fpdf文件预览缩略图的PDF库,但我不知道它是否会在现有的PDF文件中添加水印。有没有人可以推荐一个PHP库来显示预览和添加水印到现有的PDF文件?

EN

回答 5

Stack Overflow用户

发布于 2011-10-29 07:32:02

这只是一个使用FPDF和FPDI类的快速脏示例:

代码语言:javascript
复制
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

正如我已经说过的,这是一个非常快速和肮脏的例子,它需要一些改进。

票数 11
EN

Stack Overflow用户

发布于 2013-04-10 15:06:24

对于其他偶然发现这篇文章的人,您可以使用For循环生成更多页面

代码语言:javascript
复制
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');}   
票数 7
EN

Stack Overflow用户

发布于 2020-01-02 18:23:20

这是一个MPDF实践,将水印添加到面向横向的PDF的每一页。

代码语言:javascript
复制
    //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();

  • 我花了一段时间才使输出结果具有正确的大小,最终发现关键是您需要指定要写入的实例的宽度和高度。在我的例子中,输入的PDF文件可能有不同的大小,所以我必须启动另一个实例来获取大小,然后再编写一个。
  • 如果你的PDF版本比1.4更新,PDFI将不能使用它。您必须将其转换为1.4。我在我的拉威尔作品中使用xthiago/pdf-version-converter
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2913934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档