Skip to content

Instantly share code, notes, and snippets.

@teitei-tk
Last active June 19, 2017 10:11
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 teitei-tk/cba5cfb0780ef3161309d29b407e4390 to your computer and use it in GitHub Desktop.
Save teitei-tk/cba5cfb0780ef3161309d29b407e4390 to your computer and use it in GitHub Desktop.
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