Here I will Guide how to Print Web To PDF using FREE Resources.
There are 2 things needed to be installed.
1.
nodejs , seek for "Installation Steps" and follow everything.
2. puppeteer, first you need to open "Command Prompt" then type
mkdir puppeteer
cd puppeteer
npm install puppeteer
3. Restart ur PC.
4. copy paste the following code and paste it in a notepad then save it as "nit.js" in the puppeteer folder that you have created.
const puppeteer = require('puppeteer'); //library of the core
(async () => {
console.log("1/4 opening browser");
const browser = await puppeteer.launch({ headless: true });
console.log("2/4 opening page");
const page = await browser.newPage();
await page.goto(`https://www.google.com`, {waitUntil: 'load',timeout: 0});
console.log("3/4 printing page");
await page.pdf({path: 'hn.pdf', format: 'A4'});
await browser.close();
console.log("4/4 completed");
})();
5. Open your command prompt and enter the following.
cd puppeteer
node nit.js
Bulk Solution.
1. Copy paste the following to a notepad and save it as "test.csv" in the puppeteer folder.
https://www.google.com,https://www.youtube.com,https://www.facebook.com
2. copy paste the following code and paste it in a notepad then save it as "nit.js" in the puppeteer folder that you have created.
const puppeteer = require('puppeteer'); //library of the core
const fs = require('fs');
// library of file read
// read from csv file and convert to data
var data = fs.readFileSync('test.csv')
.toString() // convert Buffer to string
.split('\n') // split string to lines
.map(e => e.trim()) // remove white spaces for each line
.map(e => e.split(',').map(e => e.trim())); // split each line to array
var arr=data[0];
printpdf(arr);
async function printpdf(arr) {
for (let i = 0; i < arr.length; i++) {
const url = arr[i];
try {
console.log("1/4 opening browser");
const browser = await puppeteer.launch({ headless: true });
console.log("2/4 opening page");
const page = await browser.newPage();
await page.goto(`${url}`, {waitUntil: 'load',timeout: 0});
console.log("3/4 printing "+i+" out of "+ (arr.length-1));
const pdf = await page.pdf({path: i+'.pdf', format: 'A4' });
await browser.close();
console.log("4/4 closing page");
} catch(err) {
console.log(err);
}
}
console.log("Completed");
}
5. Open your command prompt and enter the following then pdf will be generated in the same folder.
cd puppeteer
node nit.js
Want To know more?
Leave a message to us via the messenger
Very useful information for new developers.
ReplyDeleteFor more useful information about Biotechnology visit BioTechBug