Add Watermark to Existing PDF using PHP

PDF (Portable Document Format) is a popular file format to work with text/graphics content. Generally, the PDF file is used to share text/image data for offline use. In the web application, a PDF file is created with HTML content to make dynamic data available for download. We can easily create PDF document dynamically and convert HTML content to PDF using PHP.

In a PDF document, a watermark is used to make it unique by adding text/image/stamp over pages. If you using a PHP library such as Dompdf to create PDF document, the watermark can be added at the time of PDF generation. But, in this case, you cannot add a watermark to an existing PDF document. In this tutorial, we will show you how to add watermark to existing PDF document using PHP.

In this example script, we will use FPDF and FPDI libraries to add watermark images to PDF with PHP. You can add text or image as a watermark to existing PDF document using PHP.

Install FPDF and FPDI Library

Run the following command to install FPDF and FPDI library at once using composer.

composer require setasign/fpdi-fpdf

Note that: You don’t need to download the FPDF-FPDI library separately, all the required files are included in the source code.

Include autoloader to load FPDI library and helper functions in the PHP script.

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

Add Watermark to PDF (text)

The following example code adds watermark text to the existing PDF file using PHP.

  • Specify source PDF file and watermark text.
  • Configure text font, weight, and height using PHP imagefontheight() and imagefontwidth() function.
  • Create blank image with imagecreatetruecolor() function.
  • Set background and font color with imagecolorallocate() function.
  • Create image with imagepng() function.
  • Initialize Fpdi class and set the source PDF file using setSourceFile() method.
  • Add watermark to PDF pages using importPage(), getTemplateSize(), addPage(), useTemplate(), and Image() methods of Fpdi class.
  • Render the generated PDF using the Output() function of Fpdi class.
<?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)

The following example code adds a watermark image to an existing PDF file using PHP.

  • Specify source PDF file and watermark image.
  • Initialize Fpdi class and set the source PDF file using setSourceFile() method.
  • Add watermark image to PDF pages using importPage(), getTemplateSize(), addPage(), useTemplate(), and Image() methods of Fpdi class.
  • Render the generated PDF with watermark using the Output() function of Fpdi class.
<?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

By default, the Output() method will render the PDF file on the browser. You can use parameters in the Output() method to customize the PDF output.
First Parameter:

  • I – (default) Output PDF to browser.
  • D – Download PDF file.
  • F – Save PDF to the local file.

Second Parameter:
Specify the file name of the PDF to download.

// 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');

Create PDF with Watermark in PHP using Dompdf

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

1 Comment

  1. Diedie Said...

Leave a reply

keyboard_double_arrow_up