PHP FPDF Script to Add Watermark PNG & JPG Image to All Pages of PDF Document in Browser
Dec 21, 2025
Buy the full source code of the application here:
https://procodestore.com/index.php/product/php-fpdf-script-to-add-watermark-png-jpg-image-to-all-pages-of-pdf-document-in-browser/
Show More Show Less View Video Transcript
0:01
Uh hello friends, welcome to this live
0:03
stream uh of this coding section. So
0:07
here basically we will be building a PHP
0:09
script which allows you to add watermark
0:13
uh logo image to all the pages of the
0:16
PDF document and uh we will be using the
0:19
library called as FPDF library which is
0:22
a open-source PHP library. If you just
0:25
type here FPDF PHP the very first link
0:29
which comes right here it's a PHP
0:31
library and uh
0:34
it's a basically a class which allows
0:36
you to generate PDFs on the server side
0:38
in PHP and uh it works without the PDF
0:43
lib library. So this is a FPDF library
0:47
that we have using right here in the
0:49
application and this is our script right
0:51
here index dot php. So let me show you
0:54
the input file right here. You will see
0:57
this is actually the input file. If I
0:58
open this in the browser but this is
1:01
actually the input file that we are
1:03
taking. 11 pages are there in the
1:05
document of PDF. And now I need to add
1:08
the watermark image to each and every
1:12
page in the PDF document. So I can
1:14
simply go to the script and write here
1:18
just here you need to provide the input
1:21
PDF path here. So I just need to write
1:23
here input PDF here. So just replace
1:25
your path right here. So I will just
1:27
simply replace the path here. And then
1:29
the logo.png. So whatever image that you
1:32
want to add right here. So my image is
1:34
present in the same folder. So you will
1:36
see logo.png.
1:38
If I show you this is actually a YouTube
1:41
transparent logo. So if I want to, you
1:43
can add your own company logo. So this
1:45
is logo.png. And then this is actually
1:48
the output file name. So this file will
1:51
get created in the same directory
1:52
whenever you run this PHP script. So you
1:55
can even control the opacity as well of
1:57
the watermark. So opacity is nothing but
2:00
the intensity of the watermark which is
2:03
there in the PDF document. So currently
2:05
the default value is 20. You can change
2:07
it to 50 or 10 as well. Let me stick to
2:11
the default value which is 20. So if I
2:14
just run this PHP script, as you all
2:16
know PHP is run by Apache server. So you
2:19
need to have this exam control panel
2:21
installed on your system. So I've
2:23
already installed this. If you just go
2:26
to Google just type here download exam
2:28
control panel.
2:31
So it's a crossplatform software. It's
2:33
available for Windows, Linux and Mac as
2:36
well. So just need to install for your
2:38
respective operating system. So I've
2:40
already installed it. So you just need
2:42
to start the Apache web server. Simply
2:44
will start it on localhost 80 port
2:46
number. So you just need to go to so by
2:49
default whenever you install examp
2:58
docs folder and uh
3:01
so I've given the overall project in the
3:03
description of the video you can
3:05
directly purchase it and after you
3:06
purchase it you will actually get a zip
3:08
file from Google drive and after you
3:11
extract the zip file inside this exam
3:13
folder you will hdoc folder
3:17
So you can simply open this application
3:19
in the browser by simply typing here
3:21
localhost PDF watermark.
3:25
So it will
3:27
nothing will be displayed in the
3:28
browser. But if you now see a a file
3:30
will be created output PDF. So you will
3:33
see that this output PDF file is
3:35
created. And uh
3:39
if you open it inside the browser
3:43
so you will see each and every page will
3:45
have this watermark of YouTube here. So
3:48
you will see that this is a second page,
3:50
third page, fourth page. So let me
3:53
modify some options right here and rerun
3:57
this PHP script. Let me change this
3:59
opacity to so you can choose the value
4:01
from 0 to 100. Let me increase the
4:04
intensity to 50 here. opacity and uh
4:09
again if I run this
4:14
so it will run and you will see output
4:16
PDF will get created.
4:19
This time you will see the intensity of
4:21
the watermark is increased. So you can
4:23
now clearly see the watermark. So
4:26
default value was good. So 50 you can
4:28
control it opacity accordingly.
4:32
So each and every page the water wall
4:35
will appear at the same location at the
4:37
center of the page you will see that. So
4:40
it's very useful guys in many situations
4:42
if you have a large PDF document if you
4:44
want to add your own company logo to
4:45
each and every page in the PDF document
4:47
then it will be a good project you can
4:50
purchase it. The link is given from my
4:52
website procordstore.com.
4:54
So you will get a zip zip file right
4:56
here and this will be the directory
4:58
structure you will get after you extract
5:00
the zip file and you just need to run
5:02
this index.php script right here in the
5:05
browser and uh just need to replace your
5:07
input file output file and the logo file
5:10
that you want to do it. Let me now
5:12
change the logo right here. If I pick
5:13
any other logo and download it from the
5:16
internet.
5:18
So make sure it should have the size of
5:20
the logo must be small so that it
5:23
appears correctly in the PDF document.
5:26
So you can just
5:29
you can take the logo in PNG or JPG. So
5:32
no problem in extension. Both the
5:34
extensions are supported. So
5:38
so make sure that you take a transparent
5:41
logo. Just
5:44
so here you just need to save this here.
5:48
So you will see that no code.jpg is
5:50
stored right here.
6:00
So make sure that the file is
6:03
transparent. And
6:12
so if I change this here uh logo dot jpg
6:17
if I pick this logo right here
6:21
and run this.
6:24
So you will get this error right here
6:25
whenever you have changed the extension
6:27
from JPG to PNG or PNG to JPG. Just
6:30
inside the source code, you just need to
6:32
make slight adjustment right here. You
6:35
first of all just need to go to this
6:37
line number
6:39
uh
6:41
33 line number. You will see this is
6:43
PNG. So just if you are changing the
6:45
extension so just change it to JPG.
6:50
Just make this slide adjustment and then
6:52
run the code right here and it will
6:53
work.
6:55
So you will see this output PDF file is
6:57
created and there is this Facebook logo
7:02
is successfully added at the center of
7:04
the page of each page.
7:08
So this is the script guys if you are
7:10
interested the script is given the full
7:14
source code is you can purchase it from
7:16
my website procordstore.com and
7:20
you will get the zip file automatically
7:22
whenever you purchase from Google drive
7:23
you will be redirected to and you just
7:26
need to extract it as I told you how to
7:29
configure exam check start the Apache
7:32
server and you can even use this code
7:34
deploy this application to a real domain
7:37
name as well and on advertisement
7:40
revenue as well. So it's a specific tool
7:42
website which adds watermark to the PDF
7:46
document pages. So
7:50
so let me just tell you some more
7:52
information about the code. So I already
7:55
told you that we are actually using this
7:57
fpdf.tphp
7:59
library. So if you just don't know about
8:01
this library s just type here. It's a
8:04
opensource library. You can read the
8:07
documentation more about this. So it
8:09
allows you to generate PDF, add
8:11
watermarks, add different kinds of
8:14
shapes to your PDF document. So we are
8:17
actually using it as a base library and
8:19
on top of that we are actually modifying
8:21
it some. I just made this specific file
8:24
here for you alpha pdf.php php and
8:27
requiring it at the top of the script.
8:29
Then we are providing the input file,
8:32
the watermark image, the output file,
8:34
the opacity. All these things you can
8:37
customize it, control it according to
8:39
your own choice. And then we are
8:40
instantiating a new constructor of this
8:42
alpha PDF file right here. And we have
8:44
this if condition right here. We are
8:46
setting the source file here, passing
8:48
the input file name right here in a try
8:50
catch block. So if any sort of error
8:52
take place, it can be cached in this
8:54
exception. And we are displaying if any
8:57
sort of error take place. It's a good
8:59
good practice in coding. You should have
9:01
to have try catch block
9:04
and then we have in iterating through
9:07
each page here of the PDF document using
9:09
a for loop. So it will repeat. So if you
9:12
have a lot of pages in the PDF document,
9:14
so this for loop will be very
9:16
beneficial. We are actually importing
9:18
the page by using the import page method
9:20
of FPDF. And then we are getting the
9:22
template size. We are getting the size
9:25
of each page using this method. get
9:27
template size. Then we are adding this
9:30
page and then using this template by
9:32
using the use template method. And then
9:34
we are setting the opacity by using this
9:36
method set alpha. And then calculating
9:39
these co coordinates which is x
9:41
coordinate, y-coordinate, the width and
9:43
the height. And then we are setting this
9:45
watermark image using this method called
9:47
as image. And that's all. And lastly, we
9:50
are saving this output file using the
9:52
output method which is built. These are
9:54
all built-in methods of this library and
9:58
on top of that I made this file
10:00
alpha.pdf.php
10:02
which you will get after you make the
10:04
payment. So you will let me show you
10:06
some other file right here bookp. So you
10:10
can see it now it contains 530
10:13
541 pages. So now let me take this
10:16
example. So it contains a lot of pages.
10:19
Let me book PDF
10:23
book output PDF. So if I change this
10:26
opacity to 30 and let me change the logo
10:31
to back to the original logo which was
10:33
logo.png.
10:39
So again run this. So you can see that
10:41
the size is not a you'll see that you
10:44
again need to change it because you
10:46
changed this watermark image to PNG. So
10:48
again go to the line 33 and just here
10:51
change it to PNG. So you will see that
10:53
in this book PDF it it is almost 538
10:56
pages. So again if you run this
10:59
application it will run instantly in 2
11:02
seconds all your watermark will be added
11:05
to all the 538 pages of the PDF document
11:07
and a new file will be created and you
11:10
will see that each page is having this
11:13
watermark. All the 538 pages is
11:16
instantly been watermarked with this
11:19
simple image. So you no need to use
11:22
third party websites or third party
11:24
softwares. You can build your own
11:26
application in PHP. And uh if you're
11:29
interested guys this can purchase this
11:32
project. And thank you very much for
11:34
watching this live stream and I will be
11:37
seeing you in the next live stream.
11:39
Until then thank you very much.
