【问题标题】:Add dynamic watermark to PDF为 PDF 添加动态水印
【发布时间】:2018-06-06 23:53:39
【问题描述】:

所以我开发了一个 PHP 工具,它允许我们的会计部门处理 PDF。 一旦他们完成了对 PDF 的修改,他们希望能够添加一个水印,这将是其发票编号。 我使用的是一个名为 FPDF 的 PHP 库,但如果 PDF 是版本 3,由于某种原因,这会失败。

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

基本流程是

  1. PDF 已下载到可供帐户处理的目录中
  2. 帐户处理 PDF
  3. 会自动创建发票编号
  4. 发票编号在 PDF 上加了水印,并且 PDF 被移动到已处理的目录中

在我们得到水印之前,所有这些都有效。

有人知道解决办法吗?

【问题讨论】:

    标签: php linux pdf watermark


    【解决方案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();
    }
    

    【讨论】:

      【解决方案2】:

      您之前使用的一些信息: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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-24
        • 1970-01-01
        • 2018-03-31
        • 1970-01-01
        • 2020-12-15
        • 1970-01-01
        相关资源
        最近更新 更多