我有一个脚本,可以在每个页面的右上角生成带有水印图像的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();
?>
答案 0 :(得分:0)
你的脚本有点奇怪和令人困惑。但是您的主要问题答案很简单:在调用Image()或文本方法之前使用导入的页面,而不是之后。