Applying watermarks on pdf files when users try to download the files

后端 未结 4 726
悲哀的现实
悲哀的现实 2020-12-28 21:46

The solutions for my school\'s assignments all have waterstamps on the PDFs with our username on it.

I was wondering if you guys know how to do something like that u

4条回答
  •  再見小時候
    2020-12-28 22:31

    Needed to do this yesterday and here's how without any external non-PHP libs that need to be installed. The only 2 libs needed are both PHP libs and are easy to get.

    • FPDF -> http://www.fpdf.org/ latest version
    • FPDI -> http://www.setasign.de/products/pdf-php-solutions/fpdi/downloads/ (make sure to throw the fpdf_tpl.php file in the same folder as fpdi.php)

    Now you can use the class below to achieve watermarking

    /** MAKE SURE TO HAVE THE INCLUDES RUNNING PROPERLY */
    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;
    
            $pagecount = $this->pdf->setSourceFile($currentFile);
    
            for($i = 1; $i <= $pagecount; $i++){
                                $this->pdf->addPage();
                $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);
            }
    
            $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));
            }
        } 
    
    }
    

    You now can run this as:

    WaterMark::applyAndSpit($fileWithFullPath);
    

提交回复
热议问题