How to add watermark to existing PDF using php

Let us discuss about How to add watermark to existing PDF using php

  1. First you need to install FPDI library(using composer) OR download from git
  2. After that create a create a index.html and pdf.php File in your root directory
  3. Copy Bellow code in index.html File

Index.html

<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Add watermark to existing pdf using FPDF and FPDI</title>
  
<link rel='stylesheet' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- partial:index.partial.html -->
<div class="container-fluid itinfotech-builder">
    <h1>Add watermark to existing pdf using FPDF and FPDI</h1>
    <form name="watermark_to_pdf" id="watermark_to_pdf" action="pdf.php" method="post">
        <div class="row">
            <div class="col-sm-12 loc_form">
                <div class="row">
                    <div class="col-sm-4">
                        <div class="form-group">
                                <label for="title" class="control-label">Title</label>
                                <input required class="form-control" id="title" name="title" placeholder="Main title" value="Really incredible!!" maxlength="255" type="text" tabindex="2">
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group text-right">
                                <button id="genrate_pdf" name="genrate_pdf" value="I"
                                type="submit" class="btn btn-primary btn-block" tabindex="9"> preview</button>
                        </div>
                    </div>
                </div>
                <hr>
               
            </div>
        </div>
    </form>
</div>
<!-- partial -->
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

</body>
</html>

pdf.php

<?php
 function add_watermark($file, $x_axis, $y_axis, $op, $outdir) 
 {
     require_once($_SERVER["DOCUMENT_ROOT"].'/vendor/autoload.php');
     $pdf =  new \setasign\Fpdi\Fpdi();
    
     if (file_exists($file)){
         $pagecount = $pdf->setSourceFile($file);
        
     } else {
        return FALSE;
     }
     $tpl = $pdf->importPage(1);
     $pdf->addPage();
     $size = $pdf->getTemplateSize($tpl);
     
     $pdf->useTemplate($tpl, null, null, $size['width'], $size['height'], TRUE);
     //Put the watermark
     $data=$pdf->Image('custom.png', $x_axis, $y_axis, 0, 20, 'png');
     
     if ($outdir === TRUE){
        $pdf->Output();
     } else {
         return $pdf;
     }
     
 }
//  
 
if(count($_POST)>0 && !empty($_POST['title']) && !empty($_POST['genrate_pdf']))
{

$text = $_POST['title'];
$my_img = imagecreate( 500, 80 );                             //width & height
$background  = imagecolorallocate( $my_img, 240,   79,   83 );
$text_colour = imagecolorallocate( $my_img, 0, 0, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, $text, $text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );
header( "Content-type: image/png" );
imagepng( $my_img,'custom.png');
add_watermark("example12456.pdf", 80, 80, 90,TRUE);
}
?>

How to install FPDI library using composer

{
    "require": {
        "setasign/fpdi-tcpdf": "^2.0",
        "setasign/fpdf": "^1.8"
    }
}

Last do: composer update in terminal

Add a Comment

Your email address will not be published. Required fields are marked *