Node.js Poppler & PPTXGen.js Example to Export All Pages of PDF to Powerpoint (.PPTX) File in CMD
Jan 9, 2025
Buy Premium Scripts and Apps Here:
https://procodestore.com/
Show More Show Less View Video Transcript
0:02
uh hello guys welcome to this video so
0:04
in this video we will actually uh
0:06
building out a very simple application
0:09
which allows you to convert your PDF
0:11
document into Microsoft PowerPoint pptx
0:15
file so in many cases guys if you want
0:18
to build out a presentation you should
0:20
be want to uh directly convert your PDF
0:25
document that you have to that
0:28
PowerPoint presentation so what I do in
0:30
this Pro in this tutorial I'll show you
0:32
a very simple node J script that I
0:34
developed and I'm using two external
0:37
packages right here the very first
0:39
package it will first of all do it will
0:42
take the screenshot of each individual
0:44
page in the PDF document using a
0:47
open-source python Library call as
0:49
popular p o p l e r and we have a nodejs
0:54
rapper Library called as PDF DP which
0:57
allows to take screenshots of each IND
1:00
visual pages in PDF document and then we
1:02
will use ppts generation. JS which
1:05
allows you to create power topoint
1:07
presentations in programmatically using
1:10
JavaScript it's a JavaScript library so
1:13
this is actually the nodejs script that
1:14
you see right here guys so first of all
1:17
we are basically providing our PDF file
1:20
here we are taking this example it
1:22
contains four pages in The PDF document
1:24
let me open it in the browser if I open
1:27
it you will see that this is the actual
1:30
first page second page third page and
1:32
fourth page so if you want to directly
1:34
convert this PDF file to pptx file what
1:37
it you you will do right here let me run
1:40
this file let me run this uh node
1:44
index.js this is actually the
1:47
application so node index 2. JS so you
1:50
will basically see guys first of all it
1:52
will actually take the screenshot of
1:54
each individual page so you can see that
1:57
one two so we have taken this example
2:00
sample. PDF it contains two pages and
2:03
then it created this presentation pptx
2:05
file so if I directly open it my
2:08
Microsoft PowerPoint so you can see that
2:11
now it is actually converted into a pptx
2:14
file this is the first slide this is a
2:16
second slide so let's suppose I change
2:22
the path here to 1.pdf so what it will
2:27
do right here it will directly append
2:29
that
2:30
again you will see it will take all
2:33
these screenshots and now our
2:36
presentation file presentation. ppts
2:38
file if I open this now just close it
2:42
again open
2:45
it so it will now have more
2:49
slides you will see this is the first
2:52
slide this is a second slide third slide
2:55
fourth slide fifth slide and sixth slide
2:58
so in this way guys you can direct
2:59
directly convert your PDF document into
3:02
pptx so for doing this we are using
3:05
popular if you directly write on Google
3:08
it's the open-source uh python library
3:11
for rendering the port PDF documents and
3:14
it supports taking the screenshot of
3:16
each individual page as images so if you
3:18
want to convert your PDF pages to images
3:21
we will use this Library first of all
3:23
and the second Library we are using is
3:25
pptx generation. JS it's a JavaScript
3:28
library and this is their official
3:31
documentation you can create PowerPoint
3:33
presentation programmatically using
3:35
JavaScript so we will be using it inside
3:37
nodejs so there is this package right
3:40
here which allows you to use this and
3:42
just go to npmjs.com the first one we
3:45
are using PDF popular this is actual
3:49
package for using popular JS in nodejs
3:52
so 2,000 weekly downloads the second
3:55
library is pptx Generation JS this is
3:59
actually the library package which
4:01
allows you to use this library in
4:05
nodejs so you can see that so it's a
4:08
pretty vast Library 52,000 weekly
4:11
downloads are there you can make awesome
4:13
little PowerPoint presentations using
4:15
nodejs using this package so I will just
4:18
show you a very simple example how you
4:21
can directly convert your PDF document
4:23
into pptx so let's start this tutorial
4:26
so just install these two packages now
4:29
now I will go back to my file
4:32
and I will simply first of all require
4:35
all the packages which are required for
4:37
this so PDF popular I will require
4:48
it so after requiring that we also need
4:51
to require pptx
4:53
generation so we need to require this
4:55
also pptx generation
4:58
JS then we need to install the file
5:01
system and the path module which is
5:02
built-in module so file system and the
5:05
path
5:11
module so after that guys we need to uh
5:15
just uh provide the output directory
5:17
where the output directory will be
5:19
created so I will create this directory
5:22
output images where it will actually
5:24
take the screenshot of each PDF document
5:26
and create this directory
5:28
programmatically and also need to
5:30
provide the PDF path this will be the
5:32
input file it is present in the same
5:34
directory if you see 1. PDF so I will
5:37
change to 1.pdf like this and output
5:40
directory will be created right here in
5:42
my project directory so right here so
5:45
now we have this simple if condition
5:47
that if this directory doesn't exist
5:50
file system has this method
5:52
exist we will say output
5:56
directory then we will create this
5:58
directory by using make directory Dory
5:59
sync output
6:01
directory so it will create the
6:03
directory first of all guys we have two
6:06
functions right here first function will
6:08
be responsible for converting your PDF
6:10
pages to images this function we will
6:14
create this it will be a Asing
6:16
function which will expect the PDF path
6:19
and the output directory to be passed as
6:24
argument and uh the second function will
6:27
be responsible for creating the
6:30
presentation from those images so we'll
6:32
be creating slides after we take
6:35
screenshot of each individual page from
6:37
the PDF document and this will be the
6:39
responsible output directory where the
6:41
pages where the images are stored and
6:44
then the pptx file so this file will get
6:47
created automatically after we execute
6:49
this so right here we need to actually
6:52
make a main function where we will
6:54
actually call these two functions so
6:56
this main function will be right here
7:01
it will be a async function and right
7:04
here we will await we'll call this first
7:07
first function convert PDF to images we
7:09
pass the PDF path and the output
7:12
directory and then we will call the
7:14
second function create P pptx from
7:16
images and then you'll pass the output
7:18
directory and just give it a file name
7:22
this file will create created
7:23
presentation pptx we are calling these
7:26
two functions guys
7:29
now we need to create them so the very
7:31
first function I discussed this guys in
7:33
the last video if you didn't so I
7:36
basically showed you this uh make the
7:38
this video which allows you to take
7:41
screenshot of your PDF pages I covered
7:45
this in the last video so I will not
7:47
explain you in detail format is PNG
7:50
output directory will be the output
7:52
directory past output
7:54
prefix pa. base
7:57
name this is just for the convention
8:00
all your images will be named right here
8:02
pa. extension name PDF PA like this and
8:07
then the page is null that's all so
8:10
after you put this options we now need
8:12
to have a try catch
8:15
block so if any sort of error take place
8:18
we can console log the error message so
8:20
inside this we can simply call the PDF
8:22
popular Library it contains a method
8:25
called as convert we'll pass the input
8:28
file PDF file the options that's all so
8:31
then you can console log message that
8:33
your PDF converted to images
8:38
successfully so this function will be
8:41
executed first guys it will take the
8:44
screenshot of each individual page in
8:46
the PDF document it will create this
8:49
directory if I just execute this node
8:52
index.js you will see on the left hand
8:54
side a directory will be created output
8:57
uncore images you will see that so it
9:00
has taken the screenshots and save it as
9:03
a PNG file now we need to actually use
9:05
these screenshots PNG images and create
9:08
a presentation file pptx file using the
9:11
pptx Gen Library so right in the
9:14
definition of this function we will
9:16
instantiate pptx gen so we are importing
9:21
this library with the very top you will
9:22
see that we are instantiating the
9:24
Constructor of this library and then
9:27
after that we need to actually Loop
9:29
through all the
9:31
images that we there in the output
9:34
directory so right here we will use some
9:36
logic right here to
9:38
actually first of all get access to that
9:41
we'll read the directory read directory
9:44
sync and you'll basically provide the
9:46
path here which is output directory and
9:48
then we will filter all the images we
9:50
will use a filter
9:52
method and we will filter all the files
9:55
which ends with
9:59
do PNG so we only need the files which
10:02
are ending with PNG so we will get
10:04
access to this array now we can simply
10:07
Loop through it by using for each Loop
10:10
and for each image
10:16
file like this we will actually create a
10:20
slide create a slide in pptx so now to
10:24
create a slide we have this function
10:27
pptx add slide
10:30
in this Library add slide and after that
10:33
now to add the slide as image file image
10:37
path we will get the image path path.
10:39
joint output directory and the image
10:45
file slide. add image we will add this
10:48
image to the slide and here we need to
10:51
pass the path of the file which is
10:53
stored in the image path x coordinate is
10:57
0.5 y coordinate you can just modify
11:00
these values accordingly if you want to
11:01
set different positions but this is the
11:04
default values Y is
11:06
0.5 width will be
11:09
8
11:11
height these are all customizable guys
11:14
you can change the values accordingly if
11:16
you want to have a different
11:18
result that's
11:20
all after you do this it is having in
11:23
this Loop right here it will add all the
11:25
images as different slides after it is
11:28
over we can simply write the p uh pptx
11:31
file so pptx dot it contains this
11:34
function write file and here we need to
11:37
pass the file name as an option whatever
11:40
file name is
11:45
passed you will see that pptx file we
11:49
are passing this file name you will see
11:51
that pptx
11:53
file so this returns a promise we can
11:56
handle this using dot then it will the
11:59
file name will be returned to us and we
12:01
can simply console log the message to
12:03
the user that your file has been
12:13
saved pptx file
12:17
saved so we can also handle if any sort
12:22
of error takes place by using do catch
12:24
as well that's all this is actually the
12:26
application guys first of all we
12:28
actually take the screenshot and then we
12:31
add these screenshots so let me just run
12:35
this once again you will see uh
12:39
presentation let me delete that
12:42
presentation.
12:44
pptx let me also change the file name
12:48
just to cross check result. pptx so if I
12:51
change this now you will see on the left
12:53
hand side first this folder will be
12:56
created it will have these screenshots
12:58
and now result. pptx if I try to open
13:01
this presentation file in Microsoft
13:04
PowerPoint you will see that it now it
13:06
contains four slides first image of the
13:09
PDF document second image third page
13:12
fourth page so in this way guys you can
13:15
actually convert your whole PDF document
13:17
into a Microsoft PowerPoint presentation
13:20
thank you very much for watching this
13:22
video If you like this video then please
13:24
hit that like button subscribe the
13:25
channel as well and I will be seeing you
13:27
in the next live stream and until then
13:29
thank you very much
