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

时间:2021-03-26 19:38:51

标签: php codeigniter tcpdf

我正在使用 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, 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 中加载水印 enter image description here

2.sample2.pdf 带水印。 enter image description here

1 个答案:

答案 0 :(得分:0)

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