首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在现有PDF中添加水印图像

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

Stack Overflow用户
提问于 2021-03-27 03:38:52
回答 1查看 214关注 0票数 0

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

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

代码语言:javascript
复制
use \setasign\Fpdi\Fpdi;
use \setasign\Fpdi\PdfParser\StreamReader;
function Header()  {
         ////Logo
        $logo= FCPATH.'logo.png';
    
        $this->Image($logo, 10, 10, 20, 15, 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);

       // Set font
        $this->SetFont('freeserif', 'B', 15);
     
        $this->Cell(0, 20, $this->CustomHeaderText, 0, false, 'C', 0, '', 0, false, 'T', 'C');
        
        // Set font
        $this->SetFont('freeserif', 'B', 9);
        
        $this->Cell(0, 10, ''.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', '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, 0, 110, 50, 50, 'PNG', '', 'M', false, 300, 'C', false, false, 0);

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

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

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

2.sample2.pdf带水印。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-04 17:39:03

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

//初始化PDF库

代码语言:javascript
复制
$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, 10, 15, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);

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

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

  $pdf->SetFont('freeserif', 'b', 9);
  
  $pdf->Cell(0, 2, '' . $pdf->getAliasNumPage() . '/' . $pdf->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', '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, '', '', 'C', true, 300);
  $pdf->SetAlpha(1);
  $pdf->SetFooterMargin(0);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66823319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档