【问题标题】:FPDF/FPDI watermark goes behind the pageFPDF/FPDI 水印在页面后面
【发布时间】:2016-09-11 01:04:02
【问题描述】:

我有一个脚本可以生成一个 pdf,每个页面的右上角都有一个水印图像。它工作正常,除非我有转换为 PDF 的 PNG 图像。水印出现在 PDF 中的图像后面。有没有办法让水印优先出现在 PDF 页面中其他所有内容的前面和后面?

这是我的代码:

<?php
//This page contains edit the existing file by using fpdi.
require('include/fpdf/fpdf.php');
require_once 'include/FPDI/fpdi.php';

class PDF_Rotate extends FPDI {

    var $angle = 0;

    function Rotate($angle, $x = -1, $y = -1) {
        if ($x == -1)
            $x = $this->x;
        if ($y == -1)
            $y = $this->y;
        if ($this->angle != 0)
            $this->_out('Q');
        $this->angle = $angle;
        if ($angle != 0) {
            $angle*=M_PI / 180;
            $c = cos($angle);
            $s = sin($angle);
            $cx = $x * $this->k;
            $cy = ($this->h - $y) * $this->k;
            $this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm', $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy));
        }
    }

    function _endpage() {
        if ($this->angle != 0) {
            $this->angle = 0;
            $this->_out('Q');
        }
        parent::_endpage();
    }

}

$fullPathToFile = "file.pdf";

class PDF extends PDF_Rotate {

    var $_tplIdx;

    function Header() {
        global $fullPathToFile;

        //Put the watermark
        $this->Image("https://example.com/watermark.png", 160, 0, 50, 0, 'PNG'); //[0] = how much right, [1] = the less, the higher.
        $this->SetFont('Arial', 'B', 50);
        $this->SetTextColor(255, 192, 203);
        $this->RotatedText(20, 230, '', 45);

        if (is_null($this->_tplIdx)) {

            // THIS IS WHERE YOU GET THE NUMBER OF PAGES
            $this->numPages = $this->setSourceFile($fullPathToFile);
            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx, 0, 0, 200);


    }

    function RotatedText($x, $y, $txt, $angle) {
        //Text rotated around its origin
        $this->Rotate($angle, $x, $y);
        $this->Text($x, $y, $txt);
        $this->Rotate(0);
    }

}

# ==========================

$pdf = new PDF();
//$pdf = new FPDI();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);


/*$txt = "FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say " .
        "without using the PDFlib library. F from FPDF stands for Free: you may use it for any " .
        "kind of usage and modify it to suit your needs.\n\n";
for ($i = 0; $i < 25; $i++) {
    $pdf->MultiCell(0, 5, $txt, 0, 'J');
}*/


if($pdf->numPages>1) {
    for($i=2;$i<=$pdf->numPages;$i++) {
        //$pdf->endPage();
        $pdf->_tplIdx = $pdf->importPage($i);
        $pdf->AddPage();
    }
}

$pdf->Output(); 
?>

【问题讨论】:

    标签: php fpdf fpdi


    【解决方案1】:

    您的脚本有些奇怪和混乱。但是您的主要问题很简单:在调用 Image() 或 text 方法之前使用导入的页面,而不是之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 2012-06-16
      • 2011-01-04
      • 1970-01-01
      相关资源
      最近更新 更多