Created
July 23, 2022 06:50
-
-
Save hyunbinseo/46598d16fb4ee8859b6bba2ad2ea754c to your computer and use it in GitHub Desktop.
Convert multiple URLs into PDF using puppeteer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const puppeteer = require('puppeteer'); // [email protected] | |
const urls = [ | |
'https://en.wikipedia.org/wiki/TypeScript', | |
'https://en.wikipedia.org/wiki/JavaScript', | |
]; | |
const urlCount = urls.length; | |
const delay = 5000; // Delay between each save | |
(async () => { | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
// Set multiple cookies (domain required) | |
await page.setCookie(...[ | |
{ | |
name: 'name', | |
value: 'value', | |
domain: '.wikipedia.org', | |
}, | |
]); | |
const savePageAsPDF = async (url) => { | |
await page.goto(url, { waitUntil: 'networkidle2' }); | |
await page.evaluate(() => { | |
// Remove HTML elements | |
[ | |
'header', | |
'progress', | |
'footer', | |
].forEach((selector) => { | |
const element = document.querySelector(selector); | |
if (element) element.parentNode.removeChild(element); | |
}); | |
}); | |
const title = await page.title(); // Website title | |
// Save as PDF | |
await page.pdf({ | |
path: `${title}.pdf`, | |
format: 'a4', | |
margin: { top: '1.27cm', left: '1.27cm', right: '1.27cm', bottom: '1.27cm' }, | |
}); | |
return title; | |
}; | |
for (const [index, url] of urls.entries()) { | |
console.log(`Saving ${index + 1} / ${urlCount}`); | |
const title = await savePageAsPDF(url); | |
console.log(`Saved '${title}'`); | |
if (index + 1 !== urlCount) { | |
console.log(`Waiting for ${delay}ms`); | |
await new Promise((resolve) => setTimeout(resolve, delay)); | |
} else { | |
console.log('Batch completed'); | |
} | |
} | |
await browser.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment