首页 > 解决方案 > 无法在现有 PDF 中添加水印图像

问题描述

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

按照代码,用于创建水印图像以退出 pdf。我创建了一个库来添加页眉()和页脚()。为什么它在某些 PDF 中运行良好,而在其他 PDF 中却不行?

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();    }
    

1.Sample.pdf 未在 pdf 中加载水印 在此处输入图像描述

2.sample2.pdf 带水印。 在此处输入图像描述

标签: phpcodeignitertcpdf

解决方案


我创建了一个定义所有 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, 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);
}

推荐阅读