NodeJs Tutorials
- NodeJs
- Install NodeJs
- Why use NodeJs
- NodeJs Process Model
- create First Application
- Run NodeJs Application
- Node.js Console
- Node.Js Modules
- URL Modules
- node.js Callback
- Node.js Events
- Upload Files
- Upload single & Multiple Files
- NodeJs File System
- NodeJs Email
- Debugging NodeJs
- .ENV
- NodeJs Mysql
- Helpers
- encription and decription in nodeJs
- Query string
- Date & Time
- Express Js
- Template Engine with Express
- MVC Pattern in Node.Js
- list of NPM Module
- Middleware
- Body Parser
- Render
- Nodemon module
- Morgan module
- Flash Message in ExpressJs
- Session
- Session store in database
- Cookies
- Helmet
- Multer
- Router: How To Use Routing In Node.Js
- App.Js
- express.json() and express.urlencoded()
- REST APIs in NodeJs
- Gloabal Objects
- Submit Form Data
- How to get Post Data in Node.js
- How to Get URL Parameters in Node.js
- How to create Node Project
- How to Insert Form Data Into the MySql Table Using Node.js
- How to fetch Data from MySQL database table using Node.js
- CRUD Example
- Await and Async
- Promises
- Login Example
- Password Encription
- How to validate Form (Form Validation) in Node.Js?
- Registration & Login form usingn Node.Js & MySql?
- Forgot & Reset Password
- File Upload in Node.Js with ExpressJs
- Resize Image Before Upload using Multer Sharp
- Upload multiple file using node.js with multer module
- Upload file using node.js with multer module
- Client IP Address
- REST API Downloading File in NodeJs
- Export and Download CSV From MySQL Database
- CRUD REST API
- CRUD REST API Example 2
- Enable HTTPS using Node
- How to send EMAIL using NodeJs?
- Dynamic dependent dropdown using NodeJs?
- Autocomplete Search
- Get and Send Data From Ajax Request
- Get and Post Data using Ajax
- Passport Authentication
- Node Js Data type
- Node Js Error
- Node Js Array Function
- Node Js String Function
- Puppeter Module
NodeJs Puppeter
Puppeteer is a Node.js library developed by Google that lets you control headless Chrome through the DevTools Protocol.
Features of Puppeteer are :
Puppeteer basically creates an instance of the browser and then manipulate the pages of the browser.
npm install puppeteer --save
Example
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.ittutorial.in/');
await browser.close();
})();
browser.newPage() is used to create a new page and then navigate to the URL provided in page.goto() as a parameter. And, finally browser.close() is used to close the whole running process.
Take screenshort
File Name : user.js
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.ittutorial.in/');
await page.screenshot({ path: 'GFG.png' });
await browser.close();
})();
page.screenshot method will take the screenshot of the page and save it with the filename GFG.png.
create pdf
File Name :
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.ittutorial.in/');
await page.pdf({ path: 'sana.pdf' });
await browser.close();
})();
page.pdf() will create the PDF of the given website and save it with the name sana.pdf.
get dimension
File Name :
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://www.ittutorial.in/');
const getDimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
};
});
console.log(getDimensions);
await browser.close();
})();
File Name :
const browser = await puppeteer.launch({ headless: false })
Download Pdf file using node js
res.download('./result.pdf');
res.download('temp//user/myfile.pdf');
############### OR ##################
res.sendfile('public/files/myfile.pdf');
############### OR ##################
fs.readFile('public/files/myfile.pdf',function(error,data){
if(error){
res.json({'status':'error',msg:err});
}else{
res.writeHead(200, {"Content-Type": "application/pdf"});
res.write(data);
res.end();
}
});
HOW TO DOWNLOAD PDF OF CURRENT PAGE USING PUPPETEER in NODE.JS
const puppeteer = require('puppeteer')
async function printPDF(url) {
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.goto(url, {waitUntil: 'networkidle0'});
const pdf = await page.pdf({ format: 'A4' });
await browser.close();
return pdf
})
HOW TO GENERATE PDF FROM MULTIPLE HTML FILES USING PUPPETEER ASYNCHRONOUSLY?-NODE.JS
const html_files = [
'example-1.html',
'example-2.html',
'example-3.html',
];
for (let i = 0, total = html_files.length; i < total; i++) {
await page.goto(html_files[i]);
await page.pdf({
path: `example-pdf-${i}.pdf`,
});
}