FPDI/FPDF: Watermark and Print Multiple Pages

前端 未结 3 1018
你的背包
你的背包 2021-02-03 15:42

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

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-03 16:33

    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.

提交回复
热议问题