I modified this stack question: Applying watermarks on pdf files when users try to download the files but I encountered an error, though there was a comment that says on how to
I have found a couple of things wrong with that script. To get it working change the doWatermark()
method to this:-
public function doWaterMark()
{
$currentFile = $this->file;
$newFile = $this->newFile;
$pagecount = $this->pdf->setSourceFile($currentFile);
for($i = 1; $i <= $pagecount; $i++){
$this->pdf->addPage();//<- moved from outside loop
$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->_rotate(0);//<-added
}
$this->pdf->Output($newFile, 'F');
}
I moved the line $this->pdf->addPage();
into the loop, as otherwise everything is output onto one page. I also added $this->_rotate(0);
to bring the document back upright before saving it out. Pretty simple really. I have commented the changed lines for you.
I tested it on a 32 page pdf and it seemed to work fine.