Created
February 19, 2019 04:32
-
-
Save codeskyblue/d6ec253da48efdc1520fbd147bba9a69 to your computer and use it in GitHub Desktop.
Convert html file into image using Google 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'); | |
const resolve = require("path").resolve; | |
// node html2image.js -i input.html -o output.png | |
(async () => { | |
var program = require("commander"); | |
program | |
.version("0.1.0") | |
.option("-i, --input <path>", "input html") | |
.option("-o, --output <path>", "output image file") | |
.parse(process.argv) | |
const htmlPath = resolve(program.input) | |
console.log(htmlPath) | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
await page.goto('file://' +resolve(program.input)); | |
await page.screenshot({ | |
path: program.output | |
}); | |
await browser.close(); | |
})(); |
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
{ | |
"dependencies": { | |
"commander": "^2.19.0", | |
"puppeteer": "^1.12.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment