-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit_run.js
58 lines (57 loc) · 1.92 KB
/
unit_run.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const fs = require('fs');
const assert = require('assert');
const puppeteer = require('puppeteer');
const { basename } = require('path');
const { exit } = require('process');
(async() => {
const url = process.argv[2];
if (!url) {
console.log('node unit_run.js http://localhost:2525/ZIZAIS01_G001/ZIZAIS01_G001.html のようにURLを指定してください')
exit(1);
}
let args = [
'--no-sandbox',
'--disable-setuid-sandbox',
'--window-size=800,450'
];
const browser = await puppeteer.launch({
headless: true,
args: args,
defaultViewport: {
width: 800,
height: 450
}
});
const page = await browser.newPage();
console.log('PDF変換を開始します')
const dateStr = new Date()
.toISOString()
.replace(/[^0-9]/g, '')
.slice(0, -5);
const dirpath = 'out/' + dateStr;
fs.mkdirSync(dirpath, { recursive: true });
await page.goto(url, {
waitUntil: "networkidle0"
});
await page.waitForFunction(() => {
console.log(window.location.href)
return window.document.readyState === 'interactive' || window.document.readyState === 'complete'
})
await page.addStyleTag({content: ' kbd:has(img) {background: white; box-shadow: none; border-radius: 0px; border: 1px solid #999;} @page{size:auto !important}'})
const width = await page.evaluate(() => document.body.scrollWidth);
let height = await page.evaluate(() => document.body.scrollHeight);
console.log(height);
// PDF作成処理
await page.pdf({
path: dirpath + '/' + basename(url).split('.')[0] + '.pdf',
printBackground: true,
margin: { top: '37px', right: '37px', bottom: '37px', left: '37px' },
width: 793,
height: height + 2000,
printBackground: true,
});
process.stdout.write(`\rPDF変換中…`)
process.stdout.write("\n");
console.log('PDF変換が完了しました')
browser.close();
})();