0:00
uh hello guys uh in this video I will
0:02
show you how to create a PDF document
0:04
from a JSON file in NodeJS so we have
0:07
this input JSON file right here and from
0:11
this JSON file I will show you how to
0:16
file so we have this JSON file right
0:19
here so for this we need to install a
0:22
package here which is a PDF kit so just
0:26
install this command here by executing
0:28
this command npmi PDF kit i've already
0:31
installed this package so as I run this
0:34
file right here if you
0:39
see you will see on the left hand side
0:42
the JSON file will be converted to PDF
0:44
and this PDF file is created output pdf
0:49
and you can see all your data has been
0:53
converted and now I will show you step
0:55
by step how to actually do this so for
0:59
this you first of all need to import the
1:01
file system module which is a built-in
1:03
module then we import the actual package
1:07
which is your PDF kit so we simply
1:11
require it require PDF
1:14
kit and after requiring it we actually
1:17
read the input JSON file
1:21
for reading it we will first of all wrap
1:24
everything using JSON parse and then we
1:27
will read this file system dot read file
1:32
synchronously and we will basically pass
1:34
the name of the file which is located in
1:38
output.json and then we will create a
1:40
new PDF document by instantiating this
1:49
after doing this we actually can pipe
1:53
data by using file system dot
2:00
stream and inside this we specify the
2:03
output file name which will be
2:07
output.pdf and then we can control some
2:09
properties related to this PDF document
2:11
such as the phone size by using doc
2:14
phone size i can set this to 12 and then
2:18
dot text and then we can insert this
2:21
JSON inside this PDF json dot stringy
2:24
file JSON data null 2 so we insert this
2:30
JSON into the PDF and now we just need
2:32
to end this so dot end so this creates
2:36
your PDF file and then we can show a
2:38
notification message that uh PDF
2:41
generated successfully so very
2:44
systematic process guys first of all we
2:46
read the JSON file by using the file
2:48
system module then we created the new
2:50
instance of this then we pipe the data
2:55
create this output PDF then we can
2:57
control the phone size and insert the
3:00
JSON data simply end the file and show
3:03
the notification so if I run this now
3:06
deleting this and again running the
3:09
script right here you will see it is
3:12
saying that null is not
3:16
defined sorry this is null not
3:24
mull so you'll see your PDF generated
3:31
so in this easy way guys you can create
3:33
your PDF file from the JSON file using
3:36
PDF kit inside NodeJS and also check out
3:40
freemediatools.com which contains