微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在现有 PDF 中添加水印图像

如何解决无法在现有 PDF 中添加水印图像

我正在使用 TCPDF 向我现有的 pdf 添加图像水印。只有少数 pdf 文件正确加载水印(第二张图像),其中一些不支持水印图像(第一张图像)。另外,我想从生成的pdf的最后一页中删除水印。我正在分享一个没有水印的 pdf 样本。

按照代码,用于创建水印图像以退出pdf。我创建了一个库来添加 header() 和 footer()。为什么它在某些 PDF 中可以正常工作,而在其他 PDF 中却不能?

use \setasign\Fpdi\Fpdi;
use \setasign\Fpdi\PdfParser\StreamReader;
function Header()  {
         ////logo
        $logo= FCPATH.'logo.png';
    
        $this->Image($logo,10,20,15,'PNG','','T',false,300,false);

       // Set font
        $this->SetFont('freeserif','B',15);
     
        $this->Cell(0,$this->CustomHeaderText,'C','C');
        
        // Set font
        $this->SetFont('freeserif',9);
        
        $this->Cell(0,''.$this->getAliasNumPage().'/'.$this->getAliasNbPages(),'M');
         
         
  // Get the current page break margin
        $bMargin = $this->getBreakMargin();

        // Get current auto-page-break mode
        $auto_page_break = $this->AutopageBreak;

         //watermar opacity
         $this->SetAlpha(0.3);

        // Define the path to the image that you want to use as a watermark.
        $watermark_img= FCPATH.'watermark.png';

        // Render the image
        $this->Image($watermark_img,110,50,'M',0);

        // Restore the auto-page-break status
        $this->SetAutopageBreak(true,15);

        // Set the starting point for the page content
        $this->setPageMark();    }
    

1.Sample.pdf 未在 pdf 中加载水印

enter image description here

2.sample2.pdf 带水印。

enter image description here

解决方法

我创建了一个库,它定义了所有 header() 、 footer() 函数。我在库文件中进行了更改。我已从库中删除所有水印生成代码,并在实际 PDF 生成函数的位置调用它,我的错误已解决

// 启动 PDF 库

$pdf = new Digilib();

$pdf->setDate($date);
$pdf->name = ucfirst($name); 

$url_curl = $distination_folder . $Filename;
$fileContent = file_get_contents($url_curl,false,stream_context_create(array('ssl' => array('verify_peer' => false,'verify_peer_name' => false))));
$pageCount = $pdf->setSourceFile(StreamReader::createByString($fileContent));

for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
  $templateId = $pdf->importPage($pageNo);

  $size = $pdf->getTemplateSize($templateid);

  $pdf->AddPage('P',array($size['width'],$size['height']));

  $pdf->useTemplate($templateId);

  $pdf->Image($logopath,10,15,'','PNG','T',300,false);

  $pdf->SetFont('freeserif','b',15);

  $pdf->Cell(0,2,$CustomHeaderText,'C','C');

  $pdf->SetFont('freeserif',9);
  
  $pdf->Cell(0,'' . $pdf->getAliasNumPage() . '/' . $pdf->getAliasNbPages(),'M');

  $ImageW = 105; //WaterMark Size
  $ImageH = 80;
  $pdf->setPage($pageNo); //WaterMark Page
  $myPageWidth = $pdf->getPageWidth();
  $myPageHeight = $pdf->getPageHeight();
  $myX = ($myPageWidth / 2) - 50;  //WaterMark Positioning
  $myY = ($myPageHeight / 2) - 40;

  $pdf->SetAlpha(0.35);
  $pdf->Image($watermarkpath,$myX,$myY,$ImageW,$ImageH,true,300);
  $pdf->SetAlpha(1);
  $pdf->SetFooterMargin(0);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。