Last active
June 19, 2017 10:11
-
-
Save teitei-tk/cba5cfb0780ef3161309d29b407e4390 to your computer and use it in GitHub Desktop.
This file contains 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
import { writeFile } from 'fs'; | |
import Chaldeas from 'chaldeas'; | |
async function main() { | |
// EntryPointのClassを生成。 | |
const chaldeas = Chaldeas.new(); | |
try { | |
// chromeの立ち上げと、Debugging Protocolへの接続 | |
const protocol = await chaldeas.fetchProtocol(); | |
// DevtoolsProtocolを取得 | |
// @see https://chromedevtools.github.io/devtools-protocol/tot/Page/ | |
// @see https://chromedevtools.github.io/devtools-protocol/tot/Network/ | |
const { Page, Network } = protocol; | |
// 有効化 | |
await Promise.all([ | |
Page.enable(), | |
Network.enable(), | |
]); | |
// githubのトップページを出力するのでURLを指定。 | |
await Page.navigate({ url: 'https://github.com' }); | |
// dom onload eventの着火 | |
await Page.loadEventFired(async () => { | |
// pdfを取得(base64 encode data) | |
const result = await Page.printToPDF(); | |
const fileName = (new Date()).getTime(); | |
// base64からデコードしてファイルとして保存。 | |
const buffer = Buffer.from(result.data, 'base64'); | |
writeFile(`${fileName}.pdf`, buffer, (e) => { | |
if (e) { | |
throw e; | |
} | |
}); | |
// chromeの終了とDebugging Protocolとの接続を切断。 | |
await chaldeas.terminate(); | |
}); | |
} catch (error) { | |
await chaldeas.terminate(); | |
console.error(error); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment