Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

How to Place watermark on pdf using PHP

when i place it overlap on the pdf text.. i want to put in the backgroud of pdf file..that should be transparent

user-image
Question ajoutée par Faizan Ahmad , Software Engineer , cardekho.com
Date de publication: 2013/04/23
Abdullah Akram
par Abdullah Akram , Full Stack Developer , Ertekaz Technology

Example using FPDF and the FPDI Classes: function PlaceWatermark($file, $text, $xxx, $yyy, $op, $outdir) { require_once('fpdf.php'); require_once('fpdi.php'); $name = uniqid(); $font_size = 5; $ts=explode("\n",$text); $width=0; foreach ($ts as $k=>$string) { $width=max($width,strlen($string)); } $width = imagefontwidth($font_size)*$width; $height = imagefontheight($font_size)*count($ts); $el=imagefontheight($font_size); $em=imagefontwidth($font_size); $img = imagecreatetruecolor($width,$height); // Background color $bg = imagecolorallocate($img, 255, 255, 255); imagefilledrectangle($img, 0, 0,$width ,$height , $bg); // Font color $color = imagecolorallocate($img, 0, 0, 0); foreach ($ts as $k=>$string) { $len = strlen($string); $ypos = 0; for($i=0;$i<$len;$i++){ $xpos = $i * $em; $ypos = $k * $el; imagechar($img, $font_size, $xpos, $ypos, $string, $color); $string = substr($string, 1); } } imagecolortransparent($img, $bg); $blank = imagecreatetruecolor($width, $height); $tbg = imagecolorallocate($blank, 255, 255, 255); imagefilledrectangle($blank, 0, 0,$width ,$height , $tbg); imagecolortransparent($blank, $tbg); if ( ($op < 0) OR ($op >100) ){ $op = 100; } imagecopymerge($blank, $img, 0, 0, 0, 0, $width, $height, $op); imagepng($blank,$name.".png"); // Created Watermark Image $pdf = new FPDI(); if (file_exists("./".$file)){ $pagecount = $pdf->setSourceFile($file); } else { return FALSE; } $tpl = $pdf->importPage(1); $pdf->addPage(); $pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE); //Put the watermark $pdf->Image($name.'.png', $xxx, $yyy, 0, 0, 'png'); if ($outdir === TRUE){ return $pdf->Output(); } else { return $pdf; } } PlaceWatermark("filename.pdf", "This is a lazy, but still simple test\n This should stand on a new line!", 30, 120, 100,TRUE); Usage: PlaceWatermark($filename, $text, $x, $y, $opacity, $directoutput); $filename – The path of the PDF in which you want to put the Watermark $text – The Watermark text you want to add $x – x coordinate where you want to put the Watermark $y – y coordinate where you want to put the Watermark $opacity – Opacity of the text $directoutput – If TRUE function will output a PDF File, else it will return the $pdf Note:- the above code is for only one page.You can add watermark to more pages using the loop mentioned below. for($i=1; $i <= $pagecount; $i++) { $tpl = $pdf->importPage($i); $pdf->addPage(); $pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE); //Put the watermark $pdf->Image($name.'.png', $xxx, $yyy, 0, 0, 'png');} Modify it according to your wish...n cheers!!! :)

Muhammad Majid Saleem
par Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

Here is solution for your problem: Article URL: http://sanjoyinfoworld.blogspot.com/2012/08/create-watermark-for-pdf-using-php.html Library Manual: http://mpdf1.com/manual/index.php?tid=242&searchstring=Watermarks

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?