首页 文章

将动态水印添加到PDF

提问于
浏览
0

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

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

基本流程将是

  • PDF被下载到准备好要处理的帐户的目录中

  • 帐户处理PDF

  • 自动创建发票编号

  • 将发票编号加水印到PDF上,并将PDF移动到已处理的目录

所有这些都有效,直到我们进入水印 .

有谁知道这个解决方案?

2 回答

  • 0

    您可以将(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

    您使用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();
    

相关问题