我修改了這個堆棧問題:Applying watermarks on pdf files when users try to download the files但我遇到了一個錯誤,雖然有評論說如何解決它,但它不夠詳盡。FPDI/FPDF:水印和打印多個頁面
下面是代碼:
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
class WaterMark
{
public $pdf, $file, $newFile,
$wmText = "STACKOVERFLOW";
/** $file and $newFile have to include the full path. */
public function __construct($file, $newFile)
{
$this->pdf = new FPDI();
$this->file = $file;
$this->newFile = $newFile;
}
/** $file and $newFile have to include the full path. */
public static function applyAndSpit($file, $newFile)
{
$wm = new WaterMark($file, $newFile);
if($wm->isWaterMarked())
return $wm->spitWaterMarked();
else{
$wm->doWaterMark();
return $wm->spitWaterMarked();
}
}
/** @todo Make the text nicer and add to all pages */
public function doWaterMark()
{
$currentFile = $this->file;
$newFile = $this->newFile;
$this->pdf->addPage();
$pagecount = $this->pdf->setSourceFile($currentFile);
for($i = 1; $i <= $pagecount; $i++){
$tplidx = $this->pdf->importPage($i);
$this->pdf->useTemplate($tplidx, 10, 10, 100);
// now write some text above the imported page
$this->pdf->SetFont('Arial', 'I', 40);
$this->pdf->SetTextColor(255,0,0);
$this->pdf->SetXY(25, 135);
$this->_rotate(55);
$this->pdf->Write(0, $this->wmText);
}
$this->pdf->Output($newFile, 'F');
}
public function isWaterMarked()
{
return (file_exists($this->newFile));
}
public function spitWaterMarked()
{
return readfile($this->newFile);
}
protected function _rotate($angle,$x=-1,$y=-1) {
if($x==-1)
$x=$this->pdf->x;
if($y==-1)
$y=$this->pdf->y;
if($this->pdf->angle!=0)
$this->pdf->_out('Q');
$this->pdf->angle=$angle;
if($angle!=0){
$angle*=M_PI/180;
$c=cos($angle);
$s=sin($angle);
$cx=$x*$this->pdf->k;
$cy=($this->pdf->h-$y)*$this->pdf->k;
$this->pdf->_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));
}
}
}
header('Content-type: application/pdf');
//header('Content-Disposition: attachment; filename="downloaded.pdf"');
WaterMark::applyAndSpit('C:\xampp\htdocs\tst\test0.pdf','C:\xampp\htdocs\tst\output0.pdf');
當我裝載有2個以上所有頁面的一個頁面合併的PDF文件。我在這篇文章中附加了圖片。
謝謝。
@vascowhite任何想法如何解決這個問題? –
不是,但我會先檢查插入的內容。它看起來像整個頁面旋轉。對不起,但我對fpdf沒有太多經驗。我已經使用過它,但只用於非常基本的東西。如果今天晚些時候我有機會玩,我會看到我能找到的。祝你好運。 – vascowhite
你見過這個嗎? http://www.setasign.de/products/pdf-php-solutions/setapdf-stamper/ – vascowhite