如何使用PHP在现有的PDF文件中添加水印? [英] How can I add a watermark to an existing PDF file using PHP?

查看:1357
本文介绍了如何使用PHP在现有的PDF文件中添加水印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用PHP在现有的PDF文件中添加水印.我已经在Google上搜索了它,但是找不到任何合适的库.

I am in need of adding a watermark to an existing PDF file using PHP. I have searched on Google for it, but couldn't find any suitable library.

我找到了 fpdf 库,该库创建PDF文件的预览缩略图,但是我不知道是否是否在现有的PDF文件中添加水印.谁能建议一个PHP库,而不是可以显示预览并向现有PDF文件添加水印?

I found the fpdf library that creates preview thumbnails of PDF files, but I don't know if it adds watermarks to existing PDF files or not. Can anyone suggest a PHP library than can show preview and add watermarks to existing PDF files?

推荐答案

使用FPDF和FPDI类只是一个简单的例子:

Just a quick'n'dirty example using FPDF and the FPDI Classes:

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
正如我已经说过的,这是一个非常快速和肮脏的示例,它需要一些改进.

$filename – The path of the PDF in which you want to put the Watermark
$text – The Watermark text you want to add
$x – x coordinate where you want to put the Watermark
$y – y coordinate where you want to put the Watermark
$opacity – Opacity of the text
$directoutput – If TRUE function will output a PDF File, else it will return the $pdf
As I already said, this is a very quick and dirty example, it needs some improvements.

这篇关于如何使用PHP在现有的PDF文件中添加水印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆