2016-09-11 33 views
1

私はすべてのページの右上にウォーターマークイメージを含むpdfを生成するスクリプトを持っています。それはPDFに変換されたPNG画像がある場合を除いて正常に動作します。ウォーターマークはPDF内の画像の後ろに表示されます。透かしの優先順位を付けてPDFページのすべてのものの後ろに表示する方法はありますか?FPDF/FPDIウォーターマークがページの後ろにある

これは私のコードです:

<?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

あなたのスクリプトは、やや奇妙で混乱しています。しかし、あなたの主な質問は簡単な答えです:Image()メソッドまたはテキストメソッドを呼び出す前に、インポートされたページを使用し、後で使用しないでください。

関連する問題