@d-zero/print 2.1.4 → 2.3.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/README.md +35 -7
- package/dist/cli.js +31 -3
- package/dist/modules/print-pdf.d.ts +4 -2
- package/dist/modules/print-pdf.js +23 -4
- package/dist/modules/print-png.d.ts +4 -1
- package/dist/modules/print-png.js +10 -10
- package/dist/print-child-process.d.ts +3 -1
- package/dist/print-child-process.js +3 -3
- package/dist/print-main-process.d.ts +3 -1
- package/dist/print-main-process.js +2 -0
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
|
|
5
5
|
- Puppeteerを実行してページのスクリーンショットを撮影します
|
|
6
6
|
- `png`、`pdf`、`note`の3つの形式で出力できます
|
|
7
|
-
-
|
|
7
|
+
- 複数のデバイスサイズでスクリーンショットを撮影可能(7種類のプリセット + カスタム設定)
|
|
8
|
+
- レスポンシブデザインの検証に最適
|
|
8
9
|
|
|
9
10
|
## CLI
|
|
10
11
|
|
|
11
12
|
```sh
|
|
12
|
-
npx @d-zero/print -f <listfile> [
|
|
13
|
-
|
|
14
|
-
npx @d-zero/print <url>... [--type <png|pdf|note>] [--limit <number>] [--debug]
|
|
13
|
+
npx @d-zero/print -f <listfile> [options]
|
|
14
|
+
npx @d-zero/print <url>... [options]
|
|
15
15
|
```
|
|
16
16
|
|
|
17
17
|
リストをファイルから読み込むか、URLを直接指定して実行します。
|
|
@@ -20,14 +20,42 @@ npx @d-zero/print <url>... [--type <png|pdf|note>] [--limit <number>] [--debug]
|
|
|
20
20
|
|
|
21
21
|
### オプション
|
|
22
22
|
|
|
23
|
-
- `-f, --
|
|
23
|
+
- `-f, --listfile <file>`: URLリストを持つファイルのパス
|
|
24
24
|
- `<url>`: 対象のURL(複数指定可能)
|
|
25
|
-
- `-t, --type <
|
|
26
|
-
- `png`: PNG
|
|
25
|
+
- `-t, --type <type>`: 出力形式(デフォルト: png)
|
|
26
|
+
- `png`: PNG画像(指定されたデバイスサイズで生成されます)
|
|
27
27
|
- `pdf`: PDFファイル(ブラウザの印刷機能を使用、Print CSSが適用されます)
|
|
28
28
|
- `note`: PNG画像のスクリーンショットに対してメモ欄付きのPDFファイルが生成されます
|
|
29
|
+
- `-d, --devices <devices>`: デバイスプリセット(カンマ区切り、デフォルト: desktop-compact,mobile)
|
|
29
30
|
- `--limit <number>`: 並列実行数の上限(デフォルト: 10)
|
|
30
31
|
- `--debug`: デバッグモード(デフォルト: false)
|
|
32
|
+
- `--verbose`: 詳細ログモード(デフォルト: false)
|
|
33
|
+
|
|
34
|
+
### 利用可能なデバイスプリセット
|
|
35
|
+
|
|
36
|
+
- `desktop`: 1400px幅
|
|
37
|
+
- `tablet`: 768px幅
|
|
38
|
+
- `mobile`: 375px幅(2倍解像度)
|
|
39
|
+
- `desktop-hd`: 1920px幅
|
|
40
|
+
- `desktop-compact`: 1280px幅
|
|
41
|
+
- `mobile-large`: 414px幅(3倍解像度)
|
|
42
|
+
- `mobile-small`: 320px幅(2倍解像度)
|
|
43
|
+
|
|
44
|
+
### 使用例
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
# デフォルトデバイス(desktop-compact, mobile)
|
|
48
|
+
npx @d-zero/print https://example.com
|
|
49
|
+
|
|
50
|
+
# カスタムデバイス指定
|
|
51
|
+
npx @d-zero/print https://example.com --devices desktop,tablet,mobile
|
|
52
|
+
|
|
53
|
+
# PDF出力
|
|
54
|
+
npx @d-zero/print -f urls.txt --type pdf
|
|
55
|
+
|
|
56
|
+
# ファイルから読み込み + デバイス指定
|
|
57
|
+
npx @d-zero/print -f urls.txt --devices desktop,mobile
|
|
58
|
+
```
|
|
31
59
|
|
|
32
60
|
#### URLリストのファイルフォーマット
|
|
33
61
|
|
package/dist/cli.js
CHANGED
|
@@ -1,27 +1,51 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { createCLI, parseCommonOptions } from '@d-zero/cli-core';
|
|
2
|
+
import { createCLI, parseCommonOptions, parseList } from '@d-zero/cli-core';
|
|
3
|
+
import { parseDevicesOption } from '@d-zero/puppeteer-page-scan';
|
|
3
4
|
import { print } from './print-main-process.js';
|
|
4
5
|
import { readConfig } from './read-config.js';
|
|
5
6
|
const { options, args, hasConfigFile } = createCLI({
|
|
6
7
|
aliases: {
|
|
7
8
|
f: 'listfile',
|
|
8
9
|
t: 'type',
|
|
10
|
+
d: 'devices',
|
|
11
|
+
T: 'timeout',
|
|
9
12
|
},
|
|
10
13
|
usage: [
|
|
11
14
|
'Usage:',
|
|
12
|
-
'\tprint -f <listfile> [
|
|
13
|
-
'\tprint <url>... [
|
|
15
|
+
'\tprint -f <listfile> [options]',
|
|
16
|
+
'\tprint <url>... [options]',
|
|
17
|
+
'',
|
|
18
|
+
'Options:',
|
|
19
|
+
'\t-f, --listfile <file> File containing URLs to print',
|
|
20
|
+
'\t-t, --type <type> Output type: png|pdf|note (default: png)',
|
|
21
|
+
'\t-d, --devices <devices> Device presets (comma-separated, default: desktop-compact,mobile)',
|
|
22
|
+
'\t-T, --timeout <ms> Request timeout in milliseconds (default: 30000)',
|
|
23
|
+
'\t--limit <number> Limit concurrent processes',
|
|
24
|
+
'\t--debug Enable debug mode',
|
|
25
|
+
'\t--verbose Enable verbose logging',
|
|
26
|
+
'',
|
|
27
|
+
'Available device presets:',
|
|
28
|
+
'\tdesktop, tablet, mobile, desktop-hd, desktop-compact, mobile-large, mobile-small',
|
|
29
|
+
'',
|
|
30
|
+
'Examples:',
|
|
31
|
+
'\tprint https://example.com',
|
|
32
|
+
'\tprint -f urls.txt --type pdf',
|
|
33
|
+
'\tprint https://example.com --devices desktop,mobile',
|
|
14
34
|
],
|
|
15
35
|
parseArgs: (cli) => ({
|
|
16
36
|
...parseCommonOptions(cli),
|
|
17
37
|
listfile: cli.listfile,
|
|
18
38
|
type: cli.type,
|
|
39
|
+
devices: cli.devices,
|
|
40
|
+
timeout: cli.timeout ? Number(cli.timeout) : undefined,
|
|
19
41
|
}),
|
|
20
42
|
validateArgs: (options, cli) => {
|
|
21
43
|
return !!(options.listfile?.length || cli._.length > 0);
|
|
22
44
|
},
|
|
23
45
|
});
|
|
24
46
|
const type = options.type === 'note' ? 'note' : options.type === 'pdf' ? 'pdf' : 'png';
|
|
47
|
+
const deviceNames = options.devices ? parseList(options.devices) : undefined;
|
|
48
|
+
const devices = parseDevicesOption(deviceNames);
|
|
25
49
|
if (hasConfigFile) {
|
|
26
50
|
const { urlList, hooks } = await readConfig(options.listfile);
|
|
27
51
|
await print(urlList, {
|
|
@@ -30,6 +54,8 @@ if (hasConfigFile) {
|
|
|
30
54
|
debug: options.debug,
|
|
31
55
|
verbose: options.verbose,
|
|
32
56
|
hooks,
|
|
57
|
+
devices,
|
|
58
|
+
timeout: options.timeout,
|
|
33
59
|
});
|
|
34
60
|
process.exit(0);
|
|
35
61
|
}
|
|
@@ -39,6 +65,8 @@ if (args.length > 0) {
|
|
|
39
65
|
limit: options.limit,
|
|
40
66
|
verbose: options.verbose,
|
|
41
67
|
debug: options.debug,
|
|
68
|
+
devices,
|
|
69
|
+
timeout: options.timeout,
|
|
42
70
|
});
|
|
43
71
|
process.exit(0);
|
|
44
72
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PageHook } from '@d-zero/puppeteer-page-scan';
|
|
1
|
+
import type { PageHook, Sizes } from '@d-zero/puppeteer-page-scan';
|
|
2
2
|
import type { Page } from 'puppeteer';
|
|
3
3
|
/**
|
|
4
4
|
*
|
|
@@ -7,5 +7,7 @@ import type { Page } from 'puppeteer';
|
|
|
7
7
|
* @param filePath
|
|
8
8
|
* @param update
|
|
9
9
|
* @param hooks
|
|
10
|
+
* @param devices
|
|
11
|
+
* @param timeout
|
|
10
12
|
*/
|
|
11
|
-
export declare function printPdf(page: Page, url: string, filePath: string, update: (log: string) => void, hooks?: readonly PageHook[]): Promise<void>;
|
|
13
|
+
export declare function printPdf(page: Page, url: string, filePath: string, update: (log: string) => void, hooks?: readonly PageHook[], devices?: Sizes, timeout?: number): Promise<void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { beforePageScan, pageScanListener } from '@d-zero/puppeteer-page-scan';
|
|
1
|
+
import { beforePageScan, pageScanListener, devicePresets, } from '@d-zero/puppeteer-page-scan';
|
|
2
2
|
/**
|
|
3
3
|
*
|
|
4
4
|
* @param page
|
|
@@ -6,18 +6,37 @@ import { beforePageScan, pageScanListener } from '@d-zero/puppeteer-page-scan';
|
|
|
6
6
|
* @param filePath
|
|
7
7
|
* @param update
|
|
8
8
|
* @param hooks
|
|
9
|
+
* @param devices
|
|
10
|
+
* @param timeout
|
|
9
11
|
*/
|
|
10
|
-
export async function printPdf(page, url, filePath, update, hooks) {
|
|
12
|
+
export async function printPdf(page, url, filePath, update, hooks, devices, timeout) {
|
|
13
|
+
// Use the first desktop device or fallback to desktop preset
|
|
14
|
+
const defaultWidth = devicePresets.desktop.width;
|
|
15
|
+
let pdfWidth = defaultWidth;
|
|
16
|
+
if (devices) {
|
|
17
|
+
// Find the first desktop-like device (width >= 1000) or use the first device
|
|
18
|
+
const desktopDevice = Object.values(devices).find((device) => device.width >= 1000);
|
|
19
|
+
if (desktopDevice) {
|
|
20
|
+
pdfWidth = desktopDevice.width;
|
|
21
|
+
}
|
|
22
|
+
else if (Object.values(devices).length > 0) {
|
|
23
|
+
const firstDevice = Object.values(devices)[0];
|
|
24
|
+
if (firstDevice) {
|
|
25
|
+
pdfWidth = firstDevice.width;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
11
29
|
await beforePageScan(page, url, {
|
|
12
30
|
name: 'pdf',
|
|
13
|
-
width:
|
|
31
|
+
width: pdfWidth,
|
|
14
32
|
listener: pageScanListener(update),
|
|
15
33
|
hooks,
|
|
34
|
+
timeout,
|
|
16
35
|
});
|
|
17
36
|
update('📄 Save as PDF');
|
|
18
37
|
await page.pdf({
|
|
19
38
|
path: filePath,
|
|
20
|
-
timeout: 30_000 * 10,
|
|
39
|
+
timeout: timeout ?? 30_000 * 10,
|
|
21
40
|
format: 'A4',
|
|
22
41
|
printBackground: true,
|
|
23
42
|
displayHeaderFooter: false,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Sizes } from '@d-zero/puppeteer-page-scan';
|
|
1
2
|
import type { PageHook } from '@d-zero/puppeteer-screenshot';
|
|
2
3
|
import type { Page } from 'puppeteer';
|
|
3
4
|
/**
|
|
@@ -8,5 +9,7 @@ import type { Page } from 'puppeteer';
|
|
|
8
9
|
* @param filePath
|
|
9
10
|
* @param update
|
|
10
11
|
* @param hooks
|
|
12
|
+
* @param devices
|
|
13
|
+
* @param timeout
|
|
11
14
|
*/
|
|
12
|
-
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>>;
|
|
15
|
+
export declare function printPng(page: Page, url: string, fileId: string, filePath: string, update: (log: string) => void, hooks?: readonly PageHook[], devices?: Sizes, timeout?: number): Promise<Record<string, import("@d-zero/puppeteer-screenshot").Screenshot>>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { devicePresets } from '@d-zero/puppeteer-page-scan';
|
|
1
2
|
import { screenshot, screenshotListener } from '@d-zero/puppeteer-screenshot';
|
|
2
3
|
/**
|
|
3
4
|
*
|
|
@@ -7,21 +8,20 @@ import { screenshot, screenshotListener } from '@d-zero/puppeteer-screenshot';
|
|
|
7
8
|
* @param filePath
|
|
8
9
|
* @param update
|
|
9
10
|
* @param hooks
|
|
11
|
+
* @param devices
|
|
12
|
+
* @param timeout
|
|
10
13
|
*/
|
|
11
|
-
export function printPng(page, url, fileId, filePath, update, hooks) {
|
|
14
|
+
export function printPng(page, url, fileId, filePath, update, hooks, devices, timeout) {
|
|
15
|
+
const defaultSizes = {
|
|
16
|
+
'desktop-compact': devicePresets['desktop-compact'],
|
|
17
|
+
mobile: devicePresets.mobile,
|
|
18
|
+
};
|
|
12
19
|
return screenshot(page, url, {
|
|
13
20
|
id: fileId,
|
|
14
21
|
path: filePath,
|
|
15
|
-
sizes:
|
|
16
|
-
desktop: {
|
|
17
|
-
width: 1280,
|
|
18
|
-
},
|
|
19
|
-
mobile: {
|
|
20
|
-
width: 375,
|
|
21
|
-
resolution: 2,
|
|
22
|
-
},
|
|
23
|
-
},
|
|
22
|
+
sizes: devices ?? defaultSizes,
|
|
24
23
|
listener: screenshotListener(update),
|
|
25
24
|
hooks,
|
|
25
|
+
timeout,
|
|
26
26
|
});
|
|
27
27
|
}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { PrintType } from './types.js';
|
|
2
|
-
import type { PageHook } from '@d-zero/puppeteer-page-scan';
|
|
2
|
+
import type { PageHook, Sizes } from '@d-zero/puppeteer-page-scan';
|
|
3
3
|
export type ChildProcessParams = {
|
|
4
4
|
dir: string;
|
|
5
5
|
type: PrintType;
|
|
6
6
|
hooks?: readonly PageHook[];
|
|
7
|
+
devices?: Sizes;
|
|
8
|
+
timeout?: number;
|
|
7
9
|
};
|
|
@@ -4,18 +4,18 @@ import { pngToPdf } from './modules/png-to-pdf.js';
|
|
|
4
4
|
import { printPdf } from './modules/print-pdf.js';
|
|
5
5
|
import { printPng } from './modules/print-png.js';
|
|
6
6
|
createChildProcess((param) => {
|
|
7
|
-
const { dir, type, hooks } = param;
|
|
7
|
+
const { dir, type, hooks, devices, timeout } = param;
|
|
8
8
|
return {
|
|
9
9
|
async eachPage({ page, id, url }, logger) {
|
|
10
10
|
const ext = type === 'pdf' ? 'pdf' : 'png';
|
|
11
11
|
const fileName = `${id}.${ext}`;
|
|
12
12
|
const filePath = path.resolve(dir, fileName);
|
|
13
13
|
if (type === 'pdf') {
|
|
14
|
-
await printPdf(page, url, filePath, logger, hooks);
|
|
14
|
+
await printPdf(page, url, filePath, logger, hooks, devices, timeout);
|
|
15
15
|
logger('🔚 Closing');
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
|
-
const result = await printPng(page, url, id, filePath, logger, hooks);
|
|
18
|
+
const result = await printPng(page, url, id, filePath, logger, hooks, devices, timeout);
|
|
19
19
|
if (type === 'png') {
|
|
20
20
|
logger('🔚 Closing');
|
|
21
21
|
return;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PrintType } from './types.js';
|
|
2
|
-
import type { PageHook } from '@d-zero/puppeteer-page-scan';
|
|
2
|
+
import type { PageHook, Sizes } from '@d-zero/puppeteer-page-scan';
|
|
3
3
|
import type { LaunchOptions } from 'puppeteer';
|
|
4
4
|
export interface PrintOptions {
|
|
5
5
|
readonly type?: PrintType;
|
|
@@ -7,6 +7,8 @@ export interface PrintOptions {
|
|
|
7
7
|
readonly debug?: boolean;
|
|
8
8
|
readonly verbose?: boolean;
|
|
9
9
|
readonly hooks?: readonly PageHook[];
|
|
10
|
+
readonly devices?: Sizes;
|
|
11
|
+
readonly timeout?: number;
|
|
10
12
|
}
|
|
11
13
|
/**
|
|
12
14
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@d-zero/print",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Print web pages to PDF or image files.",
|
|
5
5
|
"author": "D-ZERO",
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,16 +26,16 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@d-zero/cli-core": "1.1.1",
|
|
28
28
|
"@d-zero/html-distiller": "2.0.1",
|
|
29
|
-
"@d-zero/puppeteer-dealer": "0.5.
|
|
30
|
-
"@d-zero/puppeteer-page-scan": "4.0
|
|
31
|
-
"@d-zero/puppeteer-screenshot": "3.
|
|
29
|
+
"@d-zero/puppeteer-dealer": "0.5.4",
|
|
30
|
+
"@d-zero/puppeteer-page-scan": "4.2.0",
|
|
31
|
+
"@d-zero/puppeteer-screenshot": "3.2.0",
|
|
32
32
|
"@d-zero/readtext": "1.1.8",
|
|
33
33
|
"@d-zero/shared": "0.9.2",
|
|
34
34
|
"ansi-colors": "4.1.3",
|
|
35
35
|
"dayjs": "1.11.18",
|
|
36
36
|
"front-matter": "4.0.2",
|
|
37
37
|
"minimist": "1.2.8",
|
|
38
|
-
"puppeteer": "24.
|
|
38
|
+
"puppeteer": "24.18.0"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "00ab4d99dc17e8f09f1a84e3a02f702629b642fc"
|
|
41
41
|
}
|