Skip to content

Instantly share code, notes, and snippets.

@manan-gup
Created February 21, 2022 20:34
Show Gist options
  • Save manan-gup/738fac5decc3e864676cf61f720567df to your computer and use it in GitHub Desktop.
Save manan-gup/738fac5decc3e864676cf61f720567df to your computer and use it in GitHub Desktop.
Script to save a DOM element as a PDF to disk while retaining the styles
import path from "path";
import puppeteer from "puppeteer";
async function main() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(
"https://www.reddit.com/r/underlords/comments/mlb93c/6_4_2021_meta_builds_tier_list_by_dani/",
{ waitUntil: "networkidle0" }
);
const element = await page.$("#t3_mlb93c");
await page.evaluate((el) => {
document.body.innerHTML = `
<div>
${el.outerHTML}
</div>
`;
}, element);
await page.pdf({
path: path.join(process.cwd(), "underlordsTierList.pdf"),
printBackground: true,
format: "A4",
});
await browser.close();
}
await main();
process.exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment