Node.js ExcelJS Tutorial to Export Excel File to PDF Document Using PDF-LIB Library in Terminal
Jun 7, 2025
Buy the full source code of application here:
https://codingshiksha.com/node/node-js-exceljs-tutorial-to-export-excel-file-to-pdf-document-using-pdf-lib-library/
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/
Show More Show Less View Video Transcript
4:14
uh
4:22
hello uh now the sound is coming or not
4:25
can you tell me in the comment section i
4:27
turn on the microphone
4:42
uh now the sound is fixed here so in
4:45
this tutorial we'll be looking at how to
4:46
export your Excel file into PDF document
4:49
so I just written this NodeJS script
4:53
here so if I execute the script here you
4:55
will see node index.js
4:58
JS you will see it will export this
5:02
Excel file converted to PDF successfully
5:04
so you can see we have this Excel
5:07
file and if I see you in the you can see
5:11
it has created this PDF file file PDF
5:14
and if I try to open this file here you
5:16
will see that Excel file has been
5:19
successfully converted to PDF and you
5:21
will see this uh table is successfully
5:25
inserted inside the PDF document so so
5:29
this is sheet one we got all the records
5:33
so all the source code is given in the
5:34
description of this video you can check
5:36
out my blog here which is present on
5:38
codingsure.com so I've given the link in
5:41
the description of the video so this is
5:42
the complete script so now I will show
5:46
you how to
5:47
actually code this so just create a
5:50
simple index js and
5:52
uh first of all you need to require all
5:55
the modules which are required by this
5:59
application so one by one we will
6:01
require it so
6:03
import Excel
6:07
JS we need to import this module and
6:10
also for this we also need the file
6:12
system module as well and then we'll be
6:16
importing the PDF lib module which has
6:19
PDF document RGB and then we also have
6:24
standard fonts which will all be coming
6:27
from this PDF lib
6:31
library so first of all we imported all
6:34
these packages as you can
6:36
see and then after that we just need to
6:39
define a simple function which will be
6:41
responsible for converting your Excel
6:43
file into PDF so you define this
6:46
function convert
6:53
Excel to
6:57
PDF so it will be accepting two
7:00
arguments here first of all your Excel
7:03
file and secondly your PDF
7:08
file so inside this function we'll be
7:11
writing this code here so now we just
7:13
need to call this function
7:15
[Music]
7:17
so now to use this we simply
7:20
provide the Excel file which is present
7:24
in the same
7:25
directory and then the output file
7:29
path which is file PDF and then we
7:33
simply call this function so we simply
7:36
say convert Excel file to PDF and we
7:40
provide this two arguments here which is
7:42
Excel file and the PDF
7:47
file so here we are simply calling this
7:49
function so this async function right
7:55
here so as this is a async function it
7:58
returns a promise we can handle this dot
8:04
then so we just give it a notification
8:07
that Excel file converted to PDF
8:11
successfully so now we just need to
8:12
define this function so inside this
8:15
function it's very simple uh it's little
8:18
bit complicated first of all we need to
8:20
read the Excel file for reading the
8:22
Excel file we'll be using the ExcelJS
8:26
module for reading it we simply define
8:29
this constructor ExcelJS it contains
8:32
this function
8:34
workbook after that we simply say
8:36
workbook Excel is and it contains this
8:39
function read
8:41
file so first of all we actually read
8:44
the Excel
8:45
file so the path is passed as an
8:49
argument so we are using this function
8:50
read file to actually read the Excel
8:53
file and now we actually define a new
8:57
PDF
8:59
document so this for this we are using
9:02
this PDF lib library it's using a create
9:06
function to create a new PDF document
9:08
and storing this reference inside this
9:10
variable so we are actually using this
9:13
PDF lib library from this we are using
9:16
this after creating a new PDF document
9:19
we will be
9:23
actually you can have a custom font here
9:26
for this we'll be using this Helvetica
9:29
font and then we will add a new page for
9:33
adding a new page we actually use this
9:35
function of PDF lib which is PDF doc and
9:40
it contains this function which is add
9:42
page it will add a new page in the PDF
9:44
document and then we specify the width
9:47
and the
9:48
height so for this we get the size of
9:52
the by using page.get
9:56
size so after that we also calculate the
10:01
y which is height minus 50
10:05
ycoordinate so this is the mathematical
10:08
formula and then we convert the excel
10:11
file into pdf for this we use this
10:13
function which is
10:16
workbook so here we simply loop through
10:20
each record which is present in the
10:22
Excel file if you see this is our Excel
10:24
file we're taking an example so we have
10:26
these much of records here so we need to
10:28
loop through each record for looping
10:31
through we need to
10:35
uh first of all as we define this
10:38
workbook here by using ExcelJS so we are
10:41
using this object right
10:45
here
10:49
so all the source code is given in the
10:51
description of the video guys so this is
10:53
little bit complicated let me just paste
10:55
it the full code is given in the
10:58
description so this is I pasted this
11:02
code here this is responsible so we are
11:04
looping through each record here which
11:06
is using this each sheet function and we
11:10
are just also inserting the sheet name
11:13
as well so we can get the sheet name by
11:15
using this property worksheet dot name
11:17
after that we are using the draw text
11:20
method for drawing the text in the PDF
11:23
document so after we do this we simply
11:26
need to save the PDF file so for saving
11:29
it we simply say
11:31
PDF bytes and we simply use this
11:35
function PDF doc dots save this will
11:39
save the PDF file and after that we just
11:43
need to save it at
11:47
the use the write file function so this
11:50
will actually save at at that particular
11:52
location so which we have given which is
11:55
PDF file
12:01
path uh that's all that we need to do
12:04
and if I again delete this file
12:12
here and if I go to the terminal once
12:15
again if I execute this script you will
12:19
see it will Excel.js.Workbook is not a
12:23
constructor so this
12:25
is let me fix this error uh this error
12:28
is coming right
12:34
here new
12:36
ExcelJS sorry W will be capital so just
12:39
fix this W will be
12:41
capital so this this was the problem
12:44
here so W needs to be capital so again
12:46
if you fix this again if you execute
12:50
this you will see Excel file converted
12:53
to PDF and you get the PDF file and all
12:57
this Excel file is successfully exported
12:59
to PDF document so this is a very easy
13:03
way by which you can export your Excel
13:05
file into PDF you need these two modules
13:08
PDF lib and ExcelJS
13:11
so all the source code is given in the
13:13
description of the video and thank you
13:16
very much for watching this video and
13:18
also subscribe the channel and thank you
13:21
very much for watching this live stream
