@d-zero/print 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +97 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +28 -0
- package/dist/label.d.ts +2 -0
- package/dist/label.js +4 -0
- package/dist/pdf.d.ts +6 -0
- package/dist/pdf.js +74 -0
- package/dist/png-to-pdf.d.ts +3 -0
- package/dist/png-to-pdf.js +32 -0
- package/dist/print-pdf-with-note.d.ts +3 -0
- package/dist/print-pdf-with-note.js +47 -0
- package/dist/print-pdf.d.ts +3 -0
- package/dist/print-pdf.js +18 -0
- package/dist/print-png.d.ts +3 -0
- package/dist/print-png.js +18 -0
- package/dist/print.d.ts +12 -0
- package/dist/print.js +65 -0
- package/dist/read-config.d.ts +7 -0
- package/dist/read-config.js +21 -0
- package/dist/scan-listener.d.ts +2 -0
- package/dist/scan-listener.js +43 -0
- package/dist/screenshot-listener.d.ts +2 -0
- package/dist/screenshot-listener.js +43 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 D-ZERO Co., Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# `@d-zero/print`
|
|
2
|
+
|
|
3
|
+
ウェブサイトのスクリーンショットを撮影するツールです。
|
|
4
|
+
|
|
5
|
+
- Puppeteerを実行してページのスクリーンショットを撮影します
|
|
6
|
+
- `png`、`pdf`、`note`の3つの形式で出力できます
|
|
7
|
+
- スクリーンショットはデスクトップとモバイルの2つのサイズでそれぞれ撮影します
|
|
8
|
+
|
|
9
|
+
## CLI
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npx @d-zero/print -f <listfile> [--type <png|pdf|note>] [--limit <number>] [--debug]
|
|
13
|
+
|
|
14
|
+
npx @d-zero/print <url>... [--type <png|pdf|note>] [--limit <number>] [--debug]
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
リストをファイルから読み込むか、URLを直接指定して実行します。
|
|
18
|
+
|
|
19
|
+
実行した結果は`.print`ディレクトリに保存されます。
|
|
20
|
+
|
|
21
|
+
### オプション
|
|
22
|
+
|
|
23
|
+
- `-f, --file <filepath>`: URLリストを持つファイルのパス
|
|
24
|
+
- `<url>`: 対象のURL(複数指定可能)
|
|
25
|
+
- `-t, --type <png|pdf|note>`: 出力形式(デフォルト: png)
|
|
26
|
+
- `png`: PNG画像(モバイルとデスクトップの2つが生成されます)
|
|
27
|
+
- `pdf`: PDFファイル(ブラウザの印刷機能を使用、Print CSSが適用されます)
|
|
28
|
+
- `note`: PNG画像のスクリーンショットに対してメモ欄付きのPDFファイルが生成されます
|
|
29
|
+
- `--limit <number>`: 並列実行数の上限(デフォルト: 10)
|
|
30
|
+
- `--debug`: デバッグモード(デフォルト: false)
|
|
31
|
+
|
|
32
|
+
#### URLリストのファイルフォーマット
|
|
33
|
+
|
|
34
|
+
```txt
|
|
35
|
+
https://example.com
|
|
36
|
+
https://example.com/a
|
|
37
|
+
https://example.com/b
|
|
38
|
+
ID:ABC https://example.com/c
|
|
39
|
+
ID:XYZ https://example.com/xyz/001
|
|
40
|
+
# コメント
|
|
41
|
+
# https://example.com/d
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
URLの手前に任意のIDを付与することで、出力ファイル名にIDが含まれます。ホワイトスペースで区切ることでIDとURLを分けることができます。
|
|
45
|
+
IDが指定されていない場合は、連番がIDとして使用されます。連番は1から始まり、3桁のゼロパディングされた数字として出力されます。空行を除いた行数が連番として使用されます。
|
|
46
|
+
|
|
47
|
+
`#`で始まる行はコメントとして無視されます。
|
|
48
|
+
|
|
49
|
+
## ページフック
|
|
50
|
+
|
|
51
|
+
[Frontmatter](https://jekyllrb.com/docs/front-matter/)の`hooks`に配列としてスクリプトファイルのパスを渡すと、ページを開いた後(厳密にはPuppetterの`waitUntil: 'networkidle0'`のタイミング直後)にそれらのスクリプトを実行します。スクリプトは配列の順番通りに逐次実行されます。
|
|
52
|
+
|
|
53
|
+
```txt
|
|
54
|
+
---
|
|
55
|
+
hooks:
|
|
56
|
+
- ./hook1.cjs
|
|
57
|
+
- ./hook2.mjs
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
https://example.com
|
|
61
|
+
https://example.com/a
|
|
62
|
+
︙
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
フックスクリプトは、以下のようにエクスポートされた関数を持つモジュールとして定義します。
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
/**
|
|
69
|
+
* @type {import('@d-zero/print').PageHook}
|
|
70
|
+
*/
|
|
71
|
+
export default async function (page, { name, width, resolution, log }) {
|
|
72
|
+
// 非同期処理可能
|
|
73
|
+
// page: PuppeteerのPageオブジェクト
|
|
74
|
+
// name: サイズ名('desktop' | 'mobile')
|
|
75
|
+
// width: ウィンドウ幅
|
|
76
|
+
// resolution: 解像度
|
|
77
|
+
// log: ロガー
|
|
78
|
+
|
|
79
|
+
// ログイン処理の例
|
|
80
|
+
log('login');
|
|
81
|
+
await page.type('#username', 'user');
|
|
82
|
+
await page.type('#password', 'pass');
|
|
83
|
+
await page.click('button[type="submit"]');
|
|
84
|
+
await page.waitForNavigation();
|
|
85
|
+
log('login done');
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
例のように、ページにログインする処理をフックスクリプトに記述することで、ユーザー認証が必要なページのスクリーンショットを撮影することができます。
|
|
90
|
+
|
|
91
|
+
## 認証
|
|
92
|
+
|
|
93
|
+
### Basic認証
|
|
94
|
+
|
|
95
|
+
Basic認証が必要なページの場合はURLにユーザー名とパスワードを含めます。
|
|
96
|
+
|
|
97
|
+
例: `https://user:pass@example.com`
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import minimist from 'minimist';
|
|
3
|
+
import { print } from './print.js';
|
|
4
|
+
import { readConfig } from './read-config.js';
|
|
5
|
+
const cli = minimist(process.argv.slice(2), {
|
|
6
|
+
alias: {
|
|
7
|
+
f: 'listfile',
|
|
8
|
+
t: 'type',
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
const limit = cli.limit ? Number.parseInt(cli.limit) : undefined;
|
|
12
|
+
const debug = !!cli.debug;
|
|
13
|
+
const type = cli.type === 'note' ? 'note' : cli.type === 'pdf' ? 'pdf' : 'png';
|
|
14
|
+
if (cli.listfile?.length) {
|
|
15
|
+
const { urlList, hooks } = await readConfig(cli.listfile);
|
|
16
|
+
await print(urlList, { type, limit, debug, hooks });
|
|
17
|
+
process.exit(0);
|
|
18
|
+
}
|
|
19
|
+
if (cli._.length > 0) {
|
|
20
|
+
await print(cli._, { type, limit, debug });
|
|
21
|
+
process.exit(0);
|
|
22
|
+
}
|
|
23
|
+
process.stderr.write([
|
|
24
|
+
'Usage:',
|
|
25
|
+
'\tprint -f <listfile> [--type <png|pdf|note>] [--limit <number>] [--debug]',
|
|
26
|
+
'\tprint <url>... [--type <png|pdf|note>] [--limit <number>] [--debug]',
|
|
27
|
+
].join('\n') + '\n');
|
|
28
|
+
process.exit(1);
|
package/dist/label.d.ts
ADDED
package/dist/label.js
ADDED
package/dist/pdf.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Screenshot } from '@d-zero/puppeteer-screenshot';
|
|
2
|
+
import type { Browser } from 'puppeteer';
|
|
3
|
+
export declare function pngToPdf(browser: Browser, screenshots: Record<string, Screenshot>, options?: {
|
|
4
|
+
note?: boolean;
|
|
5
|
+
update?: (log: string) => void;
|
|
6
|
+
}): Promise<void>;
|
package/dist/pdf.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { rm } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import dayjs from 'dayjs';
|
|
4
|
+
import { label } from './label.js';
|
|
5
|
+
export async function pngToPdf(browser, screenshots, options) {
|
|
6
|
+
const page = await browser.newPage();
|
|
7
|
+
page.setDefaultNavigationTimeout(0);
|
|
8
|
+
const note = options?.note ?? true;
|
|
9
|
+
const datetime = dayjs().format('YYYY-MM-DD HH:mm');
|
|
10
|
+
for (const [sizeName, { id, filePath, url, title }] of Object.entries(screenshots)) {
|
|
11
|
+
if (!filePath) {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
const update = (log) => options?.update?.(`${label(sizeName)}: ${log}`);
|
|
15
|
+
update(`🖼 Open an image file`);
|
|
16
|
+
await page.goto(`file://${filePath}`, {
|
|
17
|
+
waitUntil: 'networkidle0',
|
|
18
|
+
timeout: 5 * 60 * 1000,
|
|
19
|
+
});
|
|
20
|
+
let noteSettings = {};
|
|
21
|
+
if (note) {
|
|
22
|
+
await page.evaluate(() => {
|
|
23
|
+
const style = document.createElement('style');
|
|
24
|
+
style.textContent = `
|
|
25
|
+
html, body { height: auto !important; }
|
|
26
|
+
html, body { margin: 0; padding: 0; }
|
|
27
|
+
img { width: 100% !important; height: auto !important; }
|
|
28
|
+
`;
|
|
29
|
+
document.body.setAttribute('style', 'margin: 0; padding: 0;');
|
|
30
|
+
document.head.append(style);
|
|
31
|
+
});
|
|
32
|
+
noteSettings = {
|
|
33
|
+
displayHeaderFooter: true,
|
|
34
|
+
margin: { top: '1.3cm', bottom: '1cm', left: '1cm', right: '5cm' },
|
|
35
|
+
headerTemplate: `
|
|
36
|
+
<div style="font-size: 2mm; width: 100%; display: flex; justify-content: space-between; margin: 0 1cm; font-family: sans-serif;">
|
|
37
|
+
<div>
|
|
38
|
+
<div>Title: ${title}</div>
|
|
39
|
+
<div style="font-size: 0.6em">Printed: ${datetime}</div>
|
|
40
|
+
</div>
|
|
41
|
+
<div>
|
|
42
|
+
<div style="font-size: 3mm; margin-bottom: 1mm; text-align: right;">[ID: ${id}]</div>
|
|
43
|
+
<div style="text-align: right; background-color: #000; color: #fff;">Note:</div>
|
|
44
|
+
</div>
|
|
45
|
+
</div>
|
|
46
|
+
`,
|
|
47
|
+
footerTemplate: `
|
|
48
|
+
<div style="font-size: 2mm; width: 100%; display: flex; justify-content: space-between; margin: 0 1cm; font-family: monospace;">
|
|
49
|
+
<div>
|
|
50
|
+
<span>[ID: ${id}] ${url}</span>
|
|
51
|
+
</div>
|
|
52
|
+
<div>
|
|
53
|
+
<span class="pageNumber"></span>/<span class="totalPages"></span>
|
|
54
|
+
</div>
|
|
55
|
+
</div>
|
|
56
|
+
`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const dir = path.dirname(filePath);
|
|
60
|
+
const fileName = path.basename(filePath, path.extname(filePath));
|
|
61
|
+
const pdfPath = path.resolve(dir, `${fileName}.pdf`);
|
|
62
|
+
update('📝 Print as a PDF%dots%');
|
|
63
|
+
await page.pdf({
|
|
64
|
+
path: pdfPath,
|
|
65
|
+
timeout: 30_000 * 10, // Default * 10
|
|
66
|
+
format: 'A4',
|
|
67
|
+
printBackground: true,
|
|
68
|
+
margin: { top: '1cm', bottom: '1cm', left: '1cm', right: '1cm' },
|
|
69
|
+
...noteSettings,
|
|
70
|
+
});
|
|
71
|
+
update('🚮 Remove the screenshot image');
|
|
72
|
+
await rm(filePath);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { rm } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import c from 'ansi-colors';
|
|
4
|
+
import { printPdfWithNote } from './print-pdf-with-note.js';
|
|
5
|
+
export async function pngToPdf(browser, screenshots, update) {
|
|
6
|
+
const page = await browser.newPage();
|
|
7
|
+
page.setDefaultNavigationTimeout(0);
|
|
8
|
+
for (const [sizeName, screenshot] of Object.entries(screenshots)) {
|
|
9
|
+
if (!screenshot.filePath) {
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
const sizeLabel = c.bgMagenta(` ${sizeName} `);
|
|
13
|
+
update(`🖼 Open an image file`);
|
|
14
|
+
await page.goto(`file://${screenshot.filePath}`, {
|
|
15
|
+
waitUntil: 'networkidle0',
|
|
16
|
+
timeout: 5 * 60 * 1000,
|
|
17
|
+
});
|
|
18
|
+
const dir = path.dirname(screenshot.filePath);
|
|
19
|
+
const fileName = path.basename(screenshot.filePath, path.extname(screenshot.filePath));
|
|
20
|
+
const pdfPath = path.resolve(dir, `${fileName}.pdf`);
|
|
21
|
+
update(`${sizeLabel} 📝 Print as a PDF%dots%`);
|
|
22
|
+
await printPdfWithNote(page, { ...screenshot, filePath: pdfPath });
|
|
23
|
+
update(`${sizeLabel} 🚮 Remove the screenshot image`);
|
|
24
|
+
await rm(screenshot.filePath).catch((error) => {
|
|
25
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') {
|
|
26
|
+
// Ignore the error if the file does not exist
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
throw error;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
export async function printPdfWithNote(page, { id, filePath, url, title }) {
|
|
3
|
+
if (!filePath) {
|
|
4
|
+
throw new Error(`No file path (ID: ${id}): ${url}`);
|
|
5
|
+
}
|
|
6
|
+
const datetime = dayjs().format('YYYY-MM-DD HH:mm');
|
|
7
|
+
await page.evaluate(() => {
|
|
8
|
+
const style = document.createElement('style');
|
|
9
|
+
style.textContent = `
|
|
10
|
+
html, body { height: auto !important; }
|
|
11
|
+
html, body { margin: 0; padding: 0; }
|
|
12
|
+
img { width: 100% !important; height: auto !important; }
|
|
13
|
+
`;
|
|
14
|
+
document.body.setAttribute('style', 'margin: 0; padding: 0;');
|
|
15
|
+
document.head.append(style);
|
|
16
|
+
});
|
|
17
|
+
await page.pdf({
|
|
18
|
+
path: filePath,
|
|
19
|
+
timeout: 30_000 * 10, // Default * 10
|
|
20
|
+
format: 'A4',
|
|
21
|
+
printBackground: true,
|
|
22
|
+
displayHeaderFooter: true,
|
|
23
|
+
margin: { top: '1.3cm', bottom: '1cm', left: '1cm', right: '5cm' },
|
|
24
|
+
headerTemplate: `
|
|
25
|
+
<div style="font-size: 2mm; width: 100%; display: flex; justify-content: space-between; margin: 0 1cm; font-family: sans-serif;">
|
|
26
|
+
<div>
|
|
27
|
+
<div>Title: ${title}</div>
|
|
28
|
+
<div style="font-size: 0.6em">Printed: ${datetime}</div>
|
|
29
|
+
</div>
|
|
30
|
+
<div>
|
|
31
|
+
<div style="font-size: 3mm; margin-bottom: 1mm; text-align: right;">[ID: ${id}]</div>
|
|
32
|
+
<div style="text-align: right; background-color: #000; color: #fff;">Note:</div>
|
|
33
|
+
</div>
|
|
34
|
+
</div>
|
|
35
|
+
`,
|
|
36
|
+
footerTemplate: `
|
|
37
|
+
<div style="font-size: 2mm; width: 100%; display: flex; justify-content: space-between; margin: 0 1cm; font-family: monospace;">
|
|
38
|
+
<div>
|
|
39
|
+
<span>[ID: ${id}] ${url}</span>
|
|
40
|
+
</div>
|
|
41
|
+
<div>
|
|
42
|
+
<span class="pageNumber"></span>/<span class="totalPages"></span>
|
|
43
|
+
</div>
|
|
44
|
+
</div>
|
|
45
|
+
`,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { beforePageScan } from '@d-zero/puppeteer-page-scan';
|
|
2
|
+
import { screenshotListener } from '@d-zero/puppeteer-screenshot';
|
|
3
|
+
export async function printPdf(page, url, filePath, update, hooks) {
|
|
4
|
+
await beforePageScan(page, url, {
|
|
5
|
+
name: 'pdf',
|
|
6
|
+
width: 1400,
|
|
7
|
+
listener: screenshotListener(update),
|
|
8
|
+
hooks,
|
|
9
|
+
});
|
|
10
|
+
update('📄 Save as PDF');
|
|
11
|
+
await page.pdf({
|
|
12
|
+
path: filePath,
|
|
13
|
+
timeout: 30_000 * 10,
|
|
14
|
+
format: 'A4',
|
|
15
|
+
printBackground: true,
|
|
16
|
+
displayHeaderFooter: false,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { PageHook } from '@d-zero/puppeteer-screenshot';
|
|
2
|
+
import type { Page } from 'puppeteer';
|
|
3
|
+
export declare function printPng(page: Page, url: string, fileId: string, filePath: string, update: (log: string) => void, hooks?: readonly PageHook[]): Promise<Record<string, import("@d-zero/puppeteer-screenshot").Screenshot>>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { screenshot, screenshotListener } from '@d-zero/puppeteer-screenshot';
|
|
2
|
+
export function printPng(page, url, fileId, filePath, update, hooks) {
|
|
3
|
+
return screenshot(page, url, {
|
|
4
|
+
id: fileId,
|
|
5
|
+
path: filePath,
|
|
6
|
+
sizes: {
|
|
7
|
+
desktop: {
|
|
8
|
+
width: 1280,
|
|
9
|
+
},
|
|
10
|
+
mobile: {
|
|
11
|
+
width: 375,
|
|
12
|
+
resolution: 2,
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
listener: screenshotListener(update),
|
|
16
|
+
hooks,
|
|
17
|
+
});
|
|
18
|
+
}
|
package/dist/print.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PrintType } from './types.js';
|
|
2
|
+
import type { PageHook } from '@d-zero/puppeteer-page-scan';
|
|
3
|
+
export interface PrintOptions {
|
|
4
|
+
readonly type?: PrintType;
|
|
5
|
+
readonly limit?: number;
|
|
6
|
+
readonly debug?: boolean;
|
|
7
|
+
readonly hooks?: readonly PageHook[];
|
|
8
|
+
}
|
|
9
|
+
export declare function print(urlList: readonly (string | {
|
|
10
|
+
id: string | null;
|
|
11
|
+
url: string;
|
|
12
|
+
})[], options?: PrintOptions): Promise<void>;
|
package/dist/print.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { mkdir } from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { deal } from '@d-zero/dealer';
|
|
4
|
+
import c from 'ansi-colors';
|
|
5
|
+
import puppeteer from 'puppeteer';
|
|
6
|
+
import { pngToPdf } from './png-to-pdf.js';
|
|
7
|
+
import { printPdf } from './print-pdf.js';
|
|
8
|
+
import { printPng } from './print-png.js';
|
|
9
|
+
export async function print(urlList, options) {
|
|
10
|
+
const browser = await puppeteer.launch({
|
|
11
|
+
headless: true,
|
|
12
|
+
args: [
|
|
13
|
+
//
|
|
14
|
+
'--lang=ja',
|
|
15
|
+
'--no-zygote',
|
|
16
|
+
'--ignore-certificate-errors',
|
|
17
|
+
],
|
|
18
|
+
});
|
|
19
|
+
const dir = path.resolve(process.cwd(), '.print');
|
|
20
|
+
await mkdir(dir, { recursive: true }).catch(() => { });
|
|
21
|
+
const type = options?.type ?? 'png';
|
|
22
|
+
const hooks = options?.hooks;
|
|
23
|
+
await deal(urlList.map((url) => {
|
|
24
|
+
if (typeof url === 'string') {
|
|
25
|
+
return { id: null, url };
|
|
26
|
+
}
|
|
27
|
+
return url;
|
|
28
|
+
}), ({ id, url }, update, index) => {
|
|
29
|
+
return async () => {
|
|
30
|
+
const page = await browser.newPage();
|
|
31
|
+
page.setDefaultNavigationTimeout(0);
|
|
32
|
+
await page.setExtraHTTPHeaders({
|
|
33
|
+
'Accept-Language': 'ja-JP',
|
|
34
|
+
});
|
|
35
|
+
const fileId = id || index.toString().padStart(3, '0');
|
|
36
|
+
const ext = type === 'pdf' ? 'pdf' : 'png';
|
|
37
|
+
const fileName = `${fileId}.${ext}`;
|
|
38
|
+
const filePath = path.resolve(dir, fileName);
|
|
39
|
+
const lineHeader = `%braille% ${c.bgWhite(` ${fileId} `)} ${c.gray(url)}: `;
|
|
40
|
+
update(`${lineHeader}🔗 Open%dots%`);
|
|
41
|
+
if (type === 'pdf') {
|
|
42
|
+
await printPdf(page, url, filePath, (log) => update(lineHeader + log), hooks);
|
|
43
|
+
update(`${lineHeader}🔚 Closing`);
|
|
44
|
+
await page.close();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const result = await printPng(page, url, fileId, filePath, (log) => update(lineHeader + log), hooks);
|
|
48
|
+
if (type === 'png') {
|
|
49
|
+
update(`${lineHeader}🔚 Closing`);
|
|
50
|
+
await page.close();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
await pngToPdf(browser, result, (log) => update(lineHeader + log));
|
|
54
|
+
update(`${lineHeader}🔚 Closing`);
|
|
55
|
+
await page.close();
|
|
56
|
+
};
|
|
57
|
+
}, {
|
|
58
|
+
limit: options?.limit,
|
|
59
|
+
debug: options?.debug,
|
|
60
|
+
header(_, done, total) {
|
|
61
|
+
return `${c.bold.magenta('🎨 Print pages')} ${c.bgBlueBright(` ${type} `)} ${done}/${total}`;
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
await browser.close();
|
|
65
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { readPageHooks } from '@d-zero/puppeteer-page-scan';
|
|
4
|
+
import { toKvList } from '@d-zero/readtext/list';
|
|
5
|
+
import fm from 'front-matter';
|
|
6
|
+
export async function readConfig(filePath) {
|
|
7
|
+
const fileContent = await fs.readFile(filePath, 'utf8');
|
|
8
|
+
const content =
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
fm(fileContent);
|
|
11
|
+
const urlList = toKvList(content.body).map((kv) => ({
|
|
12
|
+
id: kv.value ? kv.key : null,
|
|
13
|
+
url: kv.value || kv.key,
|
|
14
|
+
}));
|
|
15
|
+
const baseDir = path.dirname(filePath);
|
|
16
|
+
const hooks = await readPageHooks(content.attributes?.hooks ?? [], baseDir);
|
|
17
|
+
return {
|
|
18
|
+
urlList,
|
|
19
|
+
hooks,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { label } from './label.js';
|
|
3
|
+
export function scanListener(update) {
|
|
4
|
+
return (phase, data) => {
|
|
5
|
+
const sizeLabel = label(data.name);
|
|
6
|
+
switch (phase) {
|
|
7
|
+
case 'setViewport': {
|
|
8
|
+
const { width } = data;
|
|
9
|
+
update(`${sizeLabel} ↔️ Change viewport size to ${width}px`);
|
|
10
|
+
break;
|
|
11
|
+
}
|
|
12
|
+
case 'load': {
|
|
13
|
+
const { type } = data;
|
|
14
|
+
update(`${sizeLabel} %earth% ${type === 'open' ? 'Open' : 'Reload'} page`);
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
case 'hook': {
|
|
18
|
+
const { message } = data;
|
|
19
|
+
update(`${sizeLabel} ${message}`);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case 'scroll': {
|
|
23
|
+
update(`${sizeLabel} %propeller% Scroll the page`);
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case 'screenshotStart': {
|
|
27
|
+
update(`${sizeLabel} 📸 Take a screenshot`);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case 'screenshotSaving': {
|
|
31
|
+
const { path: filePath } = data;
|
|
32
|
+
const name = path.basename(filePath);
|
|
33
|
+
update(`${sizeLabel} 🖼 Save a file ${name}`);
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case 'screenshotError': {
|
|
37
|
+
const { error } = data;
|
|
38
|
+
update(`${sizeLabel} ❌️ ${error.message}`);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { label } from './label.js';
|
|
3
|
+
export function screenshotListener(update) {
|
|
4
|
+
return (phase, data) => {
|
|
5
|
+
const sizeLabel = label(data.name);
|
|
6
|
+
switch (phase) {
|
|
7
|
+
case 'setViewport': {
|
|
8
|
+
const { width } = data;
|
|
9
|
+
update(`${sizeLabel} ↔️ Change viewport size to ${width}px`);
|
|
10
|
+
break;
|
|
11
|
+
}
|
|
12
|
+
case 'load': {
|
|
13
|
+
const { type } = data;
|
|
14
|
+
update(`${sizeLabel} %earth% ${type === 'open' ? 'Open' : 'Reload'} page`);
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
case 'hook': {
|
|
18
|
+
const { message } = data;
|
|
19
|
+
update(`${sizeLabel} ${message}`);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case 'scroll': {
|
|
23
|
+
update(`${sizeLabel} %propeller% Scroll the page`);
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case 'screenshotStart': {
|
|
27
|
+
update(`${sizeLabel} 📸 Take a screenshot`);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case 'screenshotSaving': {
|
|
31
|
+
const { path: filePath } = data;
|
|
32
|
+
const name = path.basename(filePath);
|
|
33
|
+
update(`${sizeLabel} 🖼 Save a file ${name}`);
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case 'screenshotError': {
|
|
37
|
+
const { error } = data;
|
|
38
|
+
update(`${sizeLabel} ❌️ ${error.message}`);
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@d-zero/print",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Print web pages to PDF or image files.",
|
|
5
|
+
"author": "D-ZERO",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"private": false,
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/print.js",
|
|
15
|
+
"types": "./dist/print.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"bin": {
|
|
19
|
+
"print": "./dist/cli.js"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"clean": "tsc --build --clean"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@d-zero/dealer": "1.1.0",
|
|
30
|
+
"@d-zero/html-distiller": "1.0.0",
|
|
31
|
+
"@d-zero/puppeteer-page-scan": "1.0.0",
|
|
32
|
+
"@d-zero/puppeteer-screenshot": "1.2.0",
|
|
33
|
+
"@d-zero/readtext": "1.1.0",
|
|
34
|
+
"ansi-colors": "4.1.3",
|
|
35
|
+
"dayjs": "1.11.13",
|
|
36
|
+
"front-matter": "4.0.2",
|
|
37
|
+
"minimist": "1.2.8",
|
|
38
|
+
"puppeteer": "23.5.0"
|
|
39
|
+
},
|
|
40
|
+
"gitHead": "e8f65086bf7c316dda6667f1173da8585a5ef19c"
|
|
41
|
+
}
|