0:00
uh hello guys uh welcome to this uh
0:03
video so in this video I will show you
0:05
how to convert your HTML file into PDF
0:09
using puppeteer library inside uh uh
0:13
NodeJS so I've written this script here
0:15
which actually takes your index html
0:19
file with which can contain CSS code as
0:22
well so it actually looks like
0:27
uh if you open this file in the browser
0:30
it actually is a login form it looks
0:36
this and now I will just show you the
0:40
script which when I run the script right
0:42
here it actually converts your HTML to
0:45
PDF and uh you will see this message and
0:48
you will see this PDF file been created
0:50
in the same directory and you can see
0:53
puppeteer actually converts your HTML
0:57
so I will show you step by step i will
1:01
write the script and explain to you how
1:04
it is done so first of
1:06
all we require the file system module so
1:09
we require this module and then we also
1:14
puppeteer module and then we write a
1:21
function and inside this function we
1:24
actually open the browser using this
1:28
puppeteer it actually contains this
1:31
launch function we open the launch using
1:34
this launch function after that we open
1:39
page and for going to the page we use
1:41
this function here browser dot new page
1:44
it will open a new page in the browser
1:47
automatically using this puppeteer
1:49
library after opening the page here we
1:51
will convert all the HTML so we will
1:54
read the HTML file here by using the
1:57
file system module by using this
1:59
function read file sync and after that
2:02
here we will pass the file name which is
2:05
present in the same directory index html
2:08
and we will be reading this inside UTF8
2:11
encoding type and then we will basically
2:15
set the page content by using this
2:17
function page dot set content is equal
2:20
to HTML so we are using this function
2:24
here to set the HTML after that we just
2:26
need to convert this HTML into PDF so
2:30
we'll say that page dot PDF it contains
2:34
this function and here we specify the
2:37
path here wherever we need to save this
2:39
PDF file so we simply say output dot PDF
2:42
and then here the second option we
2:44
specify the format so A4 size the size
2:49
and then we simply close the browser by
2:52
using the close function and then we
2:55
simply say the notification in the
2:57
console log message PDF generated
3:00
successfully so this is actually the
3:01
step-by-step process and again if I run
3:05
the script you will see it
3:09
will it is saying that require is not a
3:12
function so just make sure that you put
3:14
a semicolon here just do this and also
3:20
write here as well so just after that
3:22
just put a semicolon and this will fix
3:25
this error and you will now see it will
3:28
take some time and then it will PDF
3:30
generated successfully so it actually
3:32
converted that HTML document which
3:34
contained CSS code as well to the PDF
3:37
document so in this easy way guys you
3:39
can actually convert your HTML to PDF in
3:43
NodeJS also check out my website
3:45
freemediatools.com which contains