Generate PDF using node.js

🙋‍♂️ Shubham Verma    🗓 March 28, 2018


Generate PDF using node.js


In this article, we will learn how we can generate PDF using nodejs.

To generate PDF we need a HTML page and using that page we will generate the same design in pdf. The HTML design would be a plateform to generate the PDF.

To create PDF you need to make HTML content of that particular, We will generate PDF of same HTML content.

So let's start generating PDF using node.js

I am using nodejs and I want to generate PDF on specific route ( end point/API ). It means I will hit that URL in my browser then it will generate the PDF and give us that pdf. we will use following npm modules:

ejs, fs and html-pdf

Create a package.json file and install the dependencies. you can take the reference of how you can create a package.json file from here

Install the above dependencies using npm:

npm install ejs fs html-pdf --save

Step 1: Create a server in nodejs:

Create a file with name "app.js" and paste the below code:


                        // app.js: Generate PDF using node.js
                    
                        const express = require('express');
                        const app = express();
                        const utils = require('./pdfGenerator')
                        const port = 3100;
                    
                        app.use(express.static(__dirname + '/'));
                    
                        app.get('/', (req, res) => {
                          utils.createPdf()
                            .then((htmlString) => {
                              res.redirect("http://localhost:3100/mypdf.pdf");
                            });
                        });
                    
                        app.listen(port, () => console.log(`App listening to port ${port}`));
                    

Step 2: Write the HTML file to generate the pdf of that file:

Let's create a HTML file with name "example.html", and we will generate the pdf of html file later

Now create a file example.html in same directory and write the below code:


Step 3: Write the logic to generate pdf:

Let's write the logic to create the PDF, to do this create a file "pdfGenerator.js" and write the below code:

pdfGenerator.js


Step 4: Run the app :

To get the result, we need to run the app using below command:

node app.js

Now goto the Browser, and Hit URL "localhost:3100/" and see the result.

In your app folder you can see the pdf is generated of the given html file.

Create PDF using in your nodejs app: idkblogs.com

Source: idkblogs.com

Conclusion:

In this article, we learned how we can create PDF file in nodejs.



Support our IDKBlogs team

Creating quality content takes time and resources, and we are committed to providing value to our readers. If you find my articles helpful or informative, please consider supporting us financially.

Any amount (10, 20, 50, 100, ....), no matter how small, will help us continue to produce high-quality content.

Thank you for your support!




Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.