chinmay235 / php-pdf-watermark

http://chinmay235.github.io/php-pdf-watermark/
17 stars 14 forks source link

cannot see watermark on image scanned pdf #1

Open puspabs opened 6 years ago

puspabs commented 6 years ago

Hi there, thank you for creating this. but I face a problem when i want to make a watermark for a image scanned pdf. It seems that the watermark is below the image of the pdf content so it cant be seen.

is there any tips to resolve this?

thank you

siupud commented 6 years ago

Hello, You are right it's a problem of z-index but hope somebidy can help us! I read that the order of opening the pdf and write the watermark is important,but can"t change the order. Best regards

puspabs commented 6 years ago

Hi There, finally, somehow I manage this problem. here's my working code

<?php
//This page contains edit the existing file by using fpdi.
require('fpdf/fpdf.php');
require_once 'FPDI/fpdi.php';
//require('fpdf/alphapdf.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 = "chinmay235.pdf";
class PDF extends PDF_Rotate {
    var $_tplIdx;
    function Header() {
        global $fullPathToFile;

        //Put the watermark
       // $this->Image('http://chart.googleapis.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World', 40, 100, 100, 0, 'PNG');
        //$this->SetFont('Arial', 'B', 50);
      //  $this->SetTextColor(255, 192, 203);
      //  $this->RotatedText(20, 230, 'Muvi Entertainment Pvt. Ltd.', 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);
    }

    var $extgstates = array();

    // alpha: real value from 0 (transparent) to 1 (opaque)
    // bm:    blend mode, one of the following:
    //          Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDodge, ColorBurn,
    //          HardLight, SoftLight, Difference, Exclusion, Hue, Saturation, Color, Luminosity
    function SetAlpha($alpha, $bm='Normal')
    {
        // set alpha for stroking (CA) and non-stroking (ca) operations
        $gs = $this->AddExtGState(array('ca'=>$alpha, 'CA'=>$alpha, 'BM'=>'/'.$bm));
        $this->SetExtGState($gs);
    }

    function AddExtGState($parms)
    {
        $n = count($this->extgstates)+1;
        $this->extgstates[$n]['parms'] = $parms;
        return $n;
    }

    function SetExtGState($gs)
    {
        $this->_out(sprintf('/GS%d gs', $gs));
    }

    function _enddoc()
    {
        if(!empty($this->extgstates) && $this->PDFVersion<'1.4')
            $this->PDFVersion='1.4';
            parent::_enddoc();
    }

    function _putextgstates()
    {
        for ($i = 1; $i <= count($this->extgstates); $i++)
        {
            $this->_newobj();
            $this->extgstates[$i]['n'] = $this->n;
            $this->_out('<</Type /ExtGState');
            $parms = $this->extgstates[$i]['parms'];
            $this->_out(sprintf('/ca %.3F', $parms['ca']));
            $this->_out(sprintf('/CA %.3F', $parms['CA']));
            $this->_out('/BM '.$parms['BM']);
            $this->_out('>>');
            $this->_out('endobj');
        }
    }

    function _putresourcedict()
    {
        parent::_putresourcedict();
        $this->_out('/ExtGState <<');
        foreach($this->extgstates as $k=>$extgstate)
            $this->_out('/GS'.$k.' '.$extgstate['n'].' 0 R');
            $this->_out('>>');
    }

    function _putresources()
    {
        $this->_putextgstates();
        parent::_putresources();
    }
}

# ==========================
$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);
        //Put the watermark
        $pdf->SetAlpha(0.5);
        $pdf->SetFont('Arial', 'i', 50);
        $pdf->SetTextColor(255, 192, 203);
        $pdf->RotatedText(20, 230, 'W A T E R M A R K', 45); 
        $pdf->AddPage();
    }
}
$pdf->Output(); //If you Leave blank then it should take default "I" i.e. Browser
//$pdf->Output("sampleUpdated.pdf", 'D'); //Download the file. open dialogue window in browser to save, not open with PDF browser viewer
//$pdf->Output("save_to_directory_path.pdf", 'F'); //save to a local file with the name given by filename (may include a path)
//$pdf->Output("sampleUpdated.pdf", 'I'); //I for "inline" to send the PDF to the browser
//$pdf->Output("", 'S'); //return the document as a string. filename is ignored.
?>

I added alphapdf plugin from fpdf to make text opacity lower then i add them inside of the loop.

chinmay235 commented 6 years ago

@puspabs Try to this pdf: chinmay235.pdf

This is not working for me; :(

siupud commented 6 years ago

Hello, it works with other pdf but not with the "chinmay235.pdf" Still a minor bug : on pdf with multiple page, the last page is not "watermarked"!? why The ultimate improvment can be that the script test the pdf to analyse if it's a pdf with scan image or not, to decide if the watermark go over or behind the original! Why this option, because for text or sheet music it's better to have the watermark behind: it's more clear thanks to both Best regards

puspabs commented 6 years ago

Hey.. I just realized that last page problem after posting comment. I did fix it. I'll send the code later. :)

siupud commented 6 years ago

@puspabs, If you have time to post the code for the last page..I know somebody who will be happy;-) Best regards