Skip to content

Instantly share code, notes, and snippets.

@zhangyuan
Last active October 19, 2017 02:15
Show Gist options
  • Save zhangyuan/e9ef7bcf9264ce7df08f9cf9d5ed4790 to your computer and use it in GitHub Desktop.
Save zhangyuan/e9ef7bcf9264ce7df08f9cf9d5ed4790 to your computer and use it in GitHub Desktop.
Save web page to PDF with selenium and PhantomJS.
// npm install selenium-webdriver phantomjs-prebuilt --save
const webdriver = require('selenium-webdriver')
async function render(driver, filename) {
return await driver.executePhantomJS(`this.render("${filename}")`);
}
const main = async () => {
const customPhantom = webdriver.Capabilities.phantomjs();
const driver = new webdriver.Builder()
.withCapabilities(customPhantom)
.build()
await driver.manage().window().setSize(1200, 300);
await driver.get('https://github.com')
await render(driver, './page.pdf');
driver.quit();
};
(async () => {
await main();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment