将动态水印添加到PDF

时间:2018-06-06 14:21:58

标签: php linux pdf watermark

所以我开发了一个PHP工具,允许我们的帐户部门操纵PDF。 完成修改PDF后,他们希望能够添加一个水印,该水印将是其发票编号。 我正在使用一个名为FPDF的PHP库,但如果PDF由于某种原因是版本3,则会失败。

我无法通过PHP或使用Linux命令(使用PHPs shell_exec函数)找到一种方法。 另一个问题是,有时PDF是加密的,需要修改密码,我们不知道。

基本流程将是

  1. PDF已下载到准备好处理帐户的目录中
  2. 帐户处理PDF
  3. 自动创建发票编号
  4. 发票编号已加水印到PDF上,PDF将移至已处理目录
  5. 所有这些都有效,直到我们进入水印。

    有人知道解决方案吗?

2 个答案:

答案 0 :(得分:1)

您可以将(pdf)图像与Imagick合并,您可以对水印进行分层并设置不透明度:

    $combined = new \Imagick("background.jpg");

    $image = new \Imagick("watermark.jpg");
    $image->setImageOpacity(0.7);
    $combined->compositeImage($image, \Imagick::COMPOSITE_DEFAULT, 354, 237);

    $combined->setImageFormat("pdf");
    $combined->setResolution(300,300);
    $combined->setImageProperty('title', 'your file');
    $combined->setFilename("your file.pdf");

    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="your file.pdf"');
    echo $combined;
    exit;

或设置注释:

function annotateImage($imagePath, $strokeColor, $fillColor)
{
    $imagick = new \Imagick(realpath($imagePath));

    $draw = new \ImagickDraw();
    $draw->setStrokeColor($strokeColor);
    $draw->setFillColor($fillColor);

    $draw->setStrokeWidth(1);
    $draw->setFontSize(36);

    $text = "Imagick is a native php \nextension to create and \nmodify images using the\nImageMagick API.";

    $draw->setFont("../fonts/Arial.ttf");
    $imagick->annotateimage($draw, 40, 40, 0, $text);

    header("Content-Type: image/jpg");
    echo $imagick->getImageBlob();
}

答案 1 :(得分:0)

以前使用过的内容的一些信息:FPDF无法处理现有的PDF,因此我猜您已将FPDI与FPDF结合使用以重新创建初始文档。

FPDI的免费版本不支持PDF 1.5中引入的压缩功能。无论如何,我们提供了一个商业插件,增加了对此的支持:FPDI PDF-Parser

无论如何,FPDI和FPDI PDF-Parser都不支持读取加密/受保护的PDF文档。

但是:特别是对于水印,我们提供另一种产品(不是免费的!),它是为了这个目的而制作的:SetaPDF-Stamper组件。

它还允许您向也加密/保护的PDF文档添加新内容。这可以通过authenticating使用所有者密码或绕过限制来完成(如果我们通过支持渠道知道您正在做什么,我们可以为此提供支持)。

可以这样做一个简单的水印:

require_once('library/SetaPDF/Autoload.php');
// or if you use composer require_once('vendor/autoload.php');

// create a file writer
$writer = new SetaPDF_Core_Writer_File('processed/directory/result.pdf');
// load document by filename
$document = SetaPDF_Core_Document::loadByFilename('your.pdf', $writer);

// create a stamper instance for the document
$stamper = new SetaPDF_Stamper($document);

// create a font for this document
$font = new SetaPDF_Core_Font_TrueType_Subset($document, 'fonts/DejaVuSans.ttf');

// create a stamp with the created font and font size 60
$stamp = new SetaPDF_Stamper_Stamp_Text($font, 60);
// center the text to the text block
$stamp->setAlign(SetaPDF_Core_Text::ALIGN_CENTER);
// set text for the stamp
$stamp->setText($theInvoiceNo);

// add the stamp to the center of the page and rotate it by 60 degrees
$stamper->addStamp(
    $stamp,
    [
        'position' => SetaPDF_Stamper::POSITION_CENTER_MIDDLE,
        'rotation' => 60
    ]       
);

// stamp the document
$stamper->stamp();

// save the file and finish the writer (e.g. file handler will closed)
$document->save()->finish();
相关问题