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.
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');
The following example code adds watermark text to the existing PDF file using PHP.
imagefontheight()
and imagefontwidth()
function.imagecreatetruecolor()
function.imagecolorallocate()
function.imagepng()
function.setSourceFile()
method.importPage()
, getTemplateSize()
, addPage()
, useTemplate()
, and Image()
methods of Fpdi class.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($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 settings
$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);
$op = !empty($opacity)?$opacity:100;
if ( ($op < 0) OR ($op >100) ){
$op = 100;
}
// Create watermark image
imagecopymerge($blank, $img, 0, 0, 0, 0, $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($tpl, 1, 1, $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_final, 0, 0, 'png');
}
@unlink($name.'.png');
// Output PDF with watermark
$pdf->Output();
The following example code adds a watermark image to an existing PDF file using PHP.
setSourceFile()
method.importPage()
, getTemplateSize()
, addPage()
, useTemplate()
, and Image()
methods of Fpdi class.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($tpl, 1, 1, $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_final, 0, 0, 'png');
}
// Output PDF with watermark
$pdf->Output();
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
Amazing tutotial! I was wondering how I can achieve the same but then for multiple pdf files?