Skip to content

Instantly share code, notes, and snippets.

@dannyockilson
Created July 11, 2019 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannyockilson/9f3f31b2dd6431d42b949f69d065f406 to your computer and use it in GitHub Desktop.
Save dannyockilson/9f3f31b2dd6431d42b949f69d065f406 to your computer and use it in GitHub Desktop.
Puppeteer for html to pdf
#!/bin/node
const puppeteer = require('puppeteer');
const join = require('path').join;
const url = process.argv[2] || 'http://localhost:4000/sponsorship-opportunities';
const filename = process.argv[3] || 'sponsor-pack.pdf';
(async () => {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await page.pdf({path: join(process.cwd(), filename), format: 'A4'})
await browser.close();
process.exit(0);
} catch(e) {
process.exit(1);
}
})()
{
"name": "pdf-generator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"puppeteer": "^1.17.0"
}
}
@dannyockilson
Copy link
Author

I just stick them in a folder somewhere, run npm i then node index.js https://url-to-turn-to-pdf name-of-output-file.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment