loading

How to Add Watermark to Existing PDF using PHP

How to Add Watermark to Existing PDF using PHP

How to Add Watermark to Existing PDF using PHP

0 Sales

Free

PDF (Portable Document Format) is a widely used file format for working with text and images. In general, PDF files are used to transfer text/image data for offline listening. To make dynamic data downloadable, a PDF file containing HTML information is produced in the online application. Using PHP, we can simply produce PDF documents dynamically and transform HTML information to PDF.

 

A watermark is used to render a PDF file distinctive by placing text/image/stamp over pages. If you use a PHP library to produce PDF documents, such as Dompdf, the watermark may be inserted during the PDF generating process. However, you cannot apply a watermark to an old PDF document in this scenario. In this article, we'll teach you how to use PHP to add a watermark to an existing PDF document.

 

In this sample script, we will add watermark pictures to PDF using PHP and the FPDF and FPDI libraries. Using PHP, you may add a text or picture watermark to an existing PDF document.

 

Install FPDF and FPDI Library

 

To install the FPDF and FPDI libraries at the same time, use the composer command.

composer require setasign/fpdi-fpdf

 

It is important to note that you do not need to install the FPDF-FPDI library individually; all of the necessary files are contained in the source code.

 

Also include autoloader in the PHP script to load the FPDI library and auxiliary functions.

// Load Fpdi library 
use setasign\Fpdi\Fpdi; 
require_once('vendor/autoload.php');

 

Add Watermark to PDF (text)

 

Using PHP, the following sample code adds signature text to an existing PDF file.

 

- Specify the source PDF file and the text to be watermarked.


- Using the PHP imagefonttheight() and imagefontwidth() functions, you may change the text font, weight, and height.


- With the imagecreatetruecolor() method, you may create a blank image.


- With the imagecolorallocate() method, you may change the colour of the background and font.


- Use the imagepng() method to generate an image.


- Set the source PDF file using the setSourceFile() function after initialising the Fpdi class.


- Watermark PDF pages with the Fpdi class's importPage(), getTemplateSize(), addPage(), useTemplate(), and Image() methods.


- Render the resulting PDF using the Fpdi class's Output() method.

 

<?php 

// Source file and watermark config 
$file 'documents/Proposal.pdf'; 
$text 'CodexWorld.com'; 
 
// Text font settings 
$name uniqid(); 
$font_size 5; 
$opacity 100; 
$ts explode("\n"$text); 
$width 0; 
foreach($ts as $k=>$string){ 
    $width max($widthstrlen($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($img255255255); 
imagefilledrectangle($img00$width$height$bg); 
 
// Font color settings 
$color imagecolorallocate($img000); 
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($string1);       
    } 
} 
imagecolortransparent($img$bg); 
$blank imagecreatetruecolor($width$height); 
$tbg imagecolorallocate($blank255255255); 
imagefilledrectangle($blank00$width$height$tbg); 
imagecolortransparent($blank$tbg); 
$op = !empty($opacity)?$opacity:100; 
if ( ($op 0) OR ($op >100) ){ 
    $op 100; 
} 
 
// Create watermark image 
imagecopymerge($blank$img0000$width$height$op); 
imagepng($blank$name.".png"); 
 
// Set source PDF file 
$pdf = new Fpdi(); 
if(file_exists("./".$file)){ 
    $pagecount $pdf->setSourceFile($file); 
}else{ 
    die('Source PDF not found!'); 
} 
 
// Add watermark to PDF pages 
for($i=1;$i<=$pagecount;$i++){ 
    $tpl $pdf->importPage($i); 
    $size $pdf->getTemplateSize($tpl); 
    $pdf->addPage(); 
    $pdf->useTemplate($tpl11$size['width'], $size['height'], TRUE); 
     
    //Put the watermark 
    $xxx_final = ($size['width']-50); 
    $yyy_final = ($size['height']-25); 
    $pdf->Image($name.'.png'$xxx_final$yyy_final00'png'); 
} 
@unlink($name.'.png'); 
 
// Output PDF with watermark 
$pdf->Output();

 

Add Watermark to PDF (image)

 

Using PHP, the below shown code adds a watermark image to an already PDF file.

 

- Choose a source PDF file and a watermark image.


- Set the source PDF file using the setSourceFile() function after initialising the Fpdi class.


- Use the Fpdi class's importPage(), getTemplateSize(), addPage(), useTemplate(), and Image() methods to add a watermark image to PDF pages.


- Using the Fpdi class's Output() function, render the resulting PDF with a watermark.

 

<?php 

// Source file and watermark config 
$file 'documents/Proposal.pdf'; 
$text_image 'images/codexworld-logo.png'; 
 
// Set source PDF file 
$pdf = new Fpdi(); 
if(file_exists("./".$file)){ 
    $pagecount $pdf->setSourceFile($file); 
}else{ 
    die('Source PDF not found!'); 
} 
 
// Add watermark image to PDF pages 
for($i=1;$i<=$pagecount;$i++){ 
    $tpl $pdf->importPage($i); 
    $size $pdf->getTemplateSize($tpl); 
    $pdf->addPage(); 
    $pdf->useTemplate($tpl11$size['width'], $size['height'], TRUE); 
     
    //Put the watermark 
    $xxx_final = ($size['width']-60); 
    $yyy_final = ($size['height']-25); 
    $pdf->Image($text_image$xxx_final$yyy_final00'png'); 
} 
 
// Output PDF with watermark 
$pdf->Output();

 

Output PDF with Watermark

 

The Output() function will show the PDF file on the browser by default. To customise the PDF output, utilise arguments in the Output() function.


The first parameter is:

 

I – (By default) Send PDF to browser.


D – Save the PDF file.


F – Save the PDF to a local file.

 

Second, specify the file name of the PDF to be downloaded.

 

// Output to browser 
$pdf->Output(); 
 
// Download PDF file 
$pdf->Output('D''my-document.pdf'); 
 
// Save PDF to local file 
$pdf->Output('F''my-document.pdf');

 

 

LICENSE OF USE

You can use it for personal or commercial projects. You can't resell it partially or in this form.

PRODUCT INFO

Create Date : Jan 20, 2022

Updated Date : Jan 23, 2022

Ratings

Comments : 0

Downloads : 0