Node.js PDF-LIB Tutorial to Export All Images in Folder to PDF Document Using Sharp Library
2K views
Jun 7, 2025
Buy the full source code of application here: https://codingshiksha.com/node/node-js-pdf-lib-tutorial-to-export-all-images-in-folder-to-pdf-document-using-sharp-library-in-terminal/ Hi Join the official discord server to resolve doubts here: https://discord.gg/cRnjhk6nzW Visit my Online Free Media Tool Website https://freemediatools.com/ Buy Premium Scripts and Apps Here: https://procodestore.com/
View Video Transcript
0:00
friends welcome to this video so in this
0:02
video uh I will show you a NodeJS script
0:05
which will export all the images which
0:08
is present in the folder and it will
0:09
convert it into a PDF document so it's a
0:13
NodeJS script here so I've written the
0:15
script here it uses two libraries sharp
0:19
which is image processing library and
0:21
secondly we are using the PDF lib
0:23
library which is a PDF processing
0:25
library if you want to create PDF
0:28
documents we need PDF label
0:30
library so this is actually the NodeJS
0:34
script here and if I try to go to the
0:36
terminal
0:37
here and if I execute this script
0:41
here if I just say node
0:45
index.js so just see we have all these
0:48
images present in this folder if you see
0:50
in the images folder we have all these
0:52
images present
0:56
so we have a bunch of images present
0:58
right
1:00
here so as I execute the script here you
1:03
will see it will actually create a
1:06
output PDF
1:08
file right in the left hand side so if I
1:12
execute this you will see it will now
1:14
create this output dot PDF
1:19
file and all the images that you are
1:22
seeing in this folder is successfully
1:24
converted in the PDF file so if I try to
1:27
show the PDF file here so on each page
1:31
you will see each image is
1:33
appearing so all the images that were
1:36
there in the folder is successfully
1:38
converted to the PDF file you will see
1:41
that so I can even show it right here so
1:45
this is your PDF file which get created
1:50
so all the source code is given in the
1:53
description of this video so now first
1:55
of all you need to
1:58
actually install these two modules here
2:01
which is uh first of all the image
2:04
processing library which is
2:07
sharp it's a high performance NodeJS
2:11
library for working with images so the
2:13
command is simple you simply install
2:16
this it's having 11 billion downloads
2:19
and secondly we also need to install the
2:22
PDF lib package which is used for
2:27
generating the PDF
2:30
files here you can use this package to
2:33
create and modify PDF documents so the
2:36
command is
2:38
simple so after that we will simply
2:41
create this NodeJS script here so just
2:45
create a index.js JS file and right
2:48
here first of all we need to import the
2:51
necessary packages so we will require
2:54
the file system
2:57
module and then we also need to require
3:00
the path
3:04
module and then we will import this
3:07
sharp package here so we'll require the
3:10
shar
3:12
package and then we also need to import
3:15
the PDF lib
3:17
package from this we just need to import
3:20
this PDF document which will be coming
3:25
from this PDF live
3:30
package so you can see we imported all
3:33
these packages so some comments are
3:35
there you'll see where are you from so
3:37
I'm from
3:38
India so if you're watching it for the
3:41
very first time then please hit that
3:42
like button and subscribe the channel as
3:44
well so right here you can see all these
3:47
images are present in this folder so
3:49
we'll try to export all these images in
3:51
the
3:53
PDF
3:54
document so right
3:57
here we will simply tell the images
4:03
folder so here we will simply
4:06
tell this folder
4:09
is then we will also say the output
4:14
PDF what will be the name so here you
4:17
can simply say the output PDF must have
4:20
this name of output
4:23
PDF so after doing this now you will
4:27
tell which extensions are supported so
4:30
here you can declare
4:32
a array here and here you can simply
4:35
tell which extensions are supported so
4:37
here all these extensions of images are
4:40
supported and then after that we will
4:45
write a simple function which will be a
4:48
async
4:49
function which will be responsible for
4:51
reading each image file which is present
4:54
in this folder and then we will convert
4:56
this to PDF document
5:00
for doing this first of all we need to
5:04
read the folder for reading it we use
5:07
the file system module and it contains
5:09
this function which is
5:12
read
5:14
directory and here you simply pass the
5:18
the folder name which is images folder
5:21
so we are reading this
5:24
folder and uh then it also have this
5:27
method which is filter and here we'll be
5:30
applying the filter that only the images
5:32
that we
5:34
require so here we will pass this
5:37
supported
5:38
extensions includes
5:43
function
5:45
so so exactly here we are simply telling
5:49
we only need the files which are images
5:55
so after doing this now we'll get the
5:57
list of files that we want to
6:00
convert
6:02
so after that we now need to create a
6:04
PDF document so we simply say PDF doc
6:08
and for this we simply use this
6:11
constructor PDF document and it contains
6:14
a function of create to create a new PDF
6:17
document so it contains this reference
6:19
right here after that we just need to
6:22
loop through all the images for looping
6:26
through we are using a for loop const
6:28
file of files so here we are looping
6:32
through all the files which are present
6:33
in this folder one by one
6:41
so after that we will simply create
6:46
this we will get access to each image
6:50
image
6:51
path path dot
6:54
join images folder and actual file after
6:59
that we also get the extension by using
7:02
the path module extension
7:05
name and we convert this into lower case
7:09
it contains this
7:11
function after that we will read this
7:13
image for reading the image we use the
7:16
sharp library we convert this into
7:18
buffer for doing this we simply use the
7:22
sharp
7:24
module and we get this
7:27
image
7:32
image first of all we convert this we
7:35
read the image file we convert this into
7:37
a
7:40
buffer so we read this image we get the
7:43
buffer right here after that we will
7:45
read this buffer using the sharp library
7:48
you resize it so this sharp module is
7:52
used for resizing the
7:56
image so here we use this module of
8:00
sharp and here we specify the image
8:06
buffer and for resizing it it contains a
8:09
function right here which is resize and
8:11
here you'll be resizing the image you
8:13
can give any width 595 and
8:17
height of 8 or
8:20
42 so
8:23
before exporting to PDF we are simply
8:25
resizing the image like
8:28
this and here you can even give it a
8:31
background as well if you want here we
8:35
are giving a background of white so
8:38
after that
8:40
we convert this all these images into
8:43
JPG as well so all these functions are
8:46
available in this library of
8:49
sharp so after resizing it we are
8:52
converting all these images to JPG and
8:54
then converting it to buffer and once
8:56
all these images are converted now we
8:59
can actually embed these images
9:06
so we create a variable right
9:10
here embedded image so here we will
9:13
simply compare that if it's a PNG file
9:16
then we will use a different
9:19
function so there are two functions
9:22
available right here in this
9:25
library embed PNG and embed JPG so here
9:30
we simply compare in the if statement
9:32
right if the extension is PNG then we
9:36
use the embed PNG method and if it's a
9:39
JPG image then we use the embed JPG so
9:42
these are two different methods
9:43
available in this uh PDF lib
9:47
library so we are simply comparing it
9:50
and then embedding it so once the image
9:52
is embedded now we just need to
9:57
simply create a PDF file for creating
10:00
the PDF file we use this add page
10:07
method so then we are simply using these
10:09
PDF
10:12
lib method here to add a new page and
10:16
then scale it so all the script is given
10:19
in the description of the video you can
10:21
check out my blog post where I've given
10:23
all this script so we adding a new page
10:26
then we are embedding this image scaling
10:28
it to one and lastly we are simply
10:31
console logging a simple line that your
10:33
image has been
10:36
added so after we do this now we simply
10:39
need to save this PDF file at that
10:41
location for saving it we use the save
10:43
function so this needs to be outside
10:46
this loop
10:47
so we simply use the save function to
10:50
save the PDF document
10:54
and and then we console log a simple
10:57
line that your PDF file has been saved
11:02
so now once again if I delete this PDF
11:09
file and go to the terminal and
11:13
just execute this you will see it
11:18
will all the source code is given in the
11:21
description
11:27
so if I execute the script here you'll
11:30
see it will go one by one and then it
11:32
will create this output PDF file so your
11:36
PDF file has been successfully created
11:39
so each image is taking the full page of
11:41
the PDF file so you can see that so in
11:44
this easy way you can actually convert
11:47
all your images into PDF
11:50
so thank you very much for watching this
11:52
video please hit that like button
11:54
subscribe to the channel as
11:56
well and I will be seeing you in the
11:59
next video
#Programming
#Scripting Languages