@d-zero/print 1.1.0 → 2.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/dist/cli.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  import minimist from 'minimist';
3
- import { print } from './print.js';
3
+ import { print } from './print-main-process.js';
4
4
  import { readConfig } from './read-config.js';
5
5
  const cli = minimist(process.argv.slice(2), {
6
6
  alias: {
@@ -1,3 +1,9 @@
1
- import type { Page } from '@d-zero/puppeteer-page';
2
1
  import type { Screenshot } from '@d-zero/puppeteer-screenshot';
2
+ import type { Page } from 'puppeteer';
3
+ /**
4
+ *
5
+ * @param page
6
+ * @param screenshots
7
+ * @param update
8
+ */
3
9
  export declare function pngToPdf(page: Page, screenshots: Record<string, Screenshot>, update: (log: string) => void): Promise<void>;
@@ -2,6 +2,12 @@ import { rm } from 'node:fs/promises';
2
2
  import path from 'node:path';
3
3
  import c from 'ansi-colors';
4
4
  import { printPdfWithNote } from './print-pdf-with-note.js';
5
+ /**
6
+ *
7
+ * @param page
8
+ * @param screenshots
9
+ * @param update
10
+ */
5
11
  export async function pngToPdf(page, screenshots, update) {
6
12
  for (const [sizeName, screenshot] of Object.entries(screenshots)) {
7
13
  if (!screenshot.filePath) {
@@ -1,3 +1,12 @@
1
- import type { Page } from '@d-zero/puppeteer-page';
2
1
  import type { Screenshot } from '@d-zero/puppeteer-screenshot';
2
+ import type { Page } from 'puppeteer';
3
+ /**
4
+ *
5
+ * @param page
6
+ * @param root0
7
+ * @param root0.id
8
+ * @param root0.filePath
9
+ * @param root0.url
10
+ * @param root0.title
11
+ */
3
12
  export declare function printPdfWithNote(page: Page, { id, filePath, url, title }: Screenshot): Promise<void>;
@@ -1,4 +1,13 @@
1
1
  import dayjs from 'dayjs';
2
+ /**
3
+ *
4
+ * @param page
5
+ * @param root0
6
+ * @param root0.id
7
+ * @param root0.filePath
8
+ * @param root0.url
9
+ * @param root0.title
10
+ */
2
11
  export async function printPdfWithNote(page, { id, filePath, url, title }) {
3
12
  if (!filePath) {
4
13
  throw new Error(`No file path (ID: ${id}): ${url}`);
@@ -1,3 +1,11 @@
1
- import type { Page } from '@d-zero/puppeteer-page';
2
1
  import type { PageHook } from '@d-zero/puppeteer-page-scan';
2
+ import type { Page } from 'puppeteer';
3
+ /**
4
+ *
5
+ * @param page
6
+ * @param url
7
+ * @param filePath
8
+ * @param update
9
+ * @param hooks
10
+ */
3
11
  export declare function printPdf(page: Page, url: string, filePath: string, update: (log: string) => void, hooks?: readonly PageHook[]): Promise<void>;
@@ -1,4 +1,12 @@
1
1
  import { beforePageScan, pageScanListener } from '@d-zero/puppeteer-page-scan';
2
+ /**
3
+ *
4
+ * @param page
5
+ * @param url
6
+ * @param filePath
7
+ * @param update
8
+ * @param hooks
9
+ */
2
10
  export async function printPdf(page, url, filePath, update, hooks) {
3
11
  await beforePageScan(page, url, {
4
12
  name: 'pdf',
@@ -1,3 +1,12 @@
1
- import type { Page } from '@d-zero/puppeteer-page';
2
1
  import type { PageHook } from '@d-zero/puppeteer-screenshot';
2
+ import type { Page } from 'puppeteer';
3
+ /**
4
+ *
5
+ * @param page
6
+ * @param url
7
+ * @param fileId
8
+ * @param filePath
9
+ * @param update
10
+ * @param hooks
11
+ */
3
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>>;
@@ -1,4 +1,13 @@
1
1
  import { screenshot, screenshotListener } from '@d-zero/puppeteer-screenshot';
2
+ /**
3
+ *
4
+ * @param page
5
+ * @param url
6
+ * @param fileId
7
+ * @param filePath
8
+ * @param update
9
+ * @param hooks
10
+ */
2
11
  export function printPng(page, url, fileId, filePath, update, hooks) {
3
12
  return screenshot(page, url, {
4
13
  id: fileId,
@@ -0,0 +1,7 @@
1
+ import type { PrintType } from './types.js';
2
+ import type { PageHook } from '@d-zero/puppeteer-page-scan';
3
+ export type ChildProcessParams = {
4
+ dir: string;
5
+ type: PrintType;
6
+ hooks?: readonly PageHook[];
7
+ };
@@ -0,0 +1,27 @@
1
+ import path from 'node:path';
2
+ import { createChildProcess } from '@d-zero/puppeteer-dealer';
3
+ import { pngToPdf } from './modules/png-to-pdf.js';
4
+ import { printPdf } from './modules/print-pdf.js';
5
+ import { printPng } from './modules/print-png.js';
6
+ createChildProcess((param) => {
7
+ const { dir, type, hooks } = param;
8
+ return {
9
+ async eachPage({ page, id, url }, logger) {
10
+ const ext = type === 'pdf' ? 'pdf' : 'png';
11
+ const fileName = `${id}.${ext}`;
12
+ const filePath = path.resolve(dir, fileName);
13
+ if (type === 'pdf') {
14
+ await printPdf(page, url, filePath, logger, hooks);
15
+ logger('🔚 Closing');
16
+ return;
17
+ }
18
+ const result = await printPng(page, url, id, filePath, logger, hooks);
19
+ if (type === 'png') {
20
+ logger('🔚 Closing');
21
+ return;
22
+ }
23
+ await pngToPdf(page, result, logger);
24
+ logger('🔚 Closing');
25
+ },
26
+ };
27
+ });
@@ -1,5 +1,6 @@
1
1
  import type { PrintType } from './types.js';
2
2
  import type { PageHook } from '@d-zero/puppeteer-page-scan';
3
+ import type { LaunchOptions } from 'puppeteer';
3
4
  export interface PrintOptions {
4
5
  readonly type?: PrintType;
5
6
  readonly limit?: number;
@@ -7,7 +8,12 @@ export interface PrintOptions {
7
8
  readonly verbose?: boolean;
8
9
  readonly hooks?: readonly PageHook[];
9
10
  }
11
+ /**
12
+ *
13
+ * @param urlList
14
+ * @param options
15
+ */
10
16
  export declare function print(urlList: readonly (string | {
11
17
  id: string | null;
12
18
  url: string;
13
- })[], options?: PrintOptions): Promise<void>;
19
+ })[], options?: PrintOptions & LaunchOptions): Promise<void>;
@@ -0,0 +1,30 @@
1
+ import { mkdir } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { createProcess, deal } from '@d-zero/puppeteer-dealer';
4
+ import c from 'ansi-colors';
5
+ /**
6
+ *
7
+ * @param urlList
8
+ * @param options
9
+ */
10
+ export async function print(urlList, options) {
11
+ const dir = path.resolve(process.cwd(), '.print');
12
+ await mkdir(dir, { recursive: true }).catch(() => { });
13
+ const type = options?.type ?? 'png';
14
+ await deal(urlList.map((url) => {
15
+ if (typeof url === 'string') {
16
+ return { id: null, url };
17
+ }
18
+ return url;
19
+ }), (_, done, total) => {
20
+ return `${c.bold.magenta('🎨 Print pages')} ${c.bgBlueBright(` ${type} `)} ${done}/${total}`;
21
+ }, () => {
22
+ return createProcess(path.resolve(import.meta.dirname, 'print-child-process.js'), {
23
+ dir,
24
+ type,
25
+ hooks: options?.hooks,
26
+ }, {
27
+ ...options,
28
+ });
29
+ });
30
+ }
@@ -1,3 +1,7 @@
1
+ /**
2
+ *
3
+ * @param filePath
4
+ */
1
5
  export declare function readConfig(filePath: string): Promise<{
2
6
  urlList: {
3
7
  id: string | null;
@@ -3,6 +3,10 @@ import path from 'node:path';
3
3
  import { readPageHooks } from '@d-zero/puppeteer-page-scan';
4
4
  import { toKvList } from '@d-zero/readtext/list';
5
5
  import fm from 'front-matter';
6
+ /**
7
+ *
8
+ * @param filePath
9
+ */
6
10
  export async function readConfig(filePath) {
7
11
  const fileContent = await fs.readFile(filePath, 'utf8');
8
12
  const content =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@d-zero/print",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "Print web pages to PDF or image files.",
5
5
  "author": "D-ZERO",
6
6
  "license": "MIT",
@@ -27,18 +27,16 @@
27
27
  "clean": "tsc --build --clean"
28
28
  },
29
29
  "dependencies": {
30
- "@d-zero/html-distiller": "1.0.1",
31
- "@d-zero/puppeteer-dealer": "0.2.0",
32
- "@d-zero/puppeteer-page-scan": "2.0.0",
33
- "@d-zero/puppeteer-screenshot": "2.0.0",
34
- "@d-zero/readtext": "1.1.1",
30
+ "@d-zero/html-distiller": "1.0.3",
31
+ "@d-zero/puppeteer-dealer": "0.4.0",
32
+ "@d-zero/puppeteer-page-scan": "4.0.0",
33
+ "@d-zero/puppeteer-screenshot": "3.0.1",
34
+ "@d-zero/readtext": "1.1.3",
35
35
  "ansi-colors": "4.1.3",
36
36
  "dayjs": "1.11.13",
37
37
  "front-matter": "4.0.2",
38
- "minimist": "1.2.8"
38
+ "minimist": "1.2.8",
39
+ "puppeteer": "24.9.0"
39
40
  },
40
- "devDependencies": {
41
- "@d-zero/puppeteer-page": "0.2.0"
42
- },
43
- "gitHead": "1eb1c03400580040119121e11ffb33acd876fe1b"
41
+ "gitHead": "4e9cc7b87e0fef91b6f2d4edfb66ca9134b2491b"
44
42
  }
package/dist/label.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import c from 'ansi-colors';
2
- export declare function label(str: string, color?: c.StyleFunction): string;
package/dist/label.js DELETED
@@ -1,4 +0,0 @@
1
- import c from 'ansi-colors';
2
- export function label(str, color = c.bgMagenta) {
3
- return color(` ${str} `);
4
- }
package/dist/pdf.d.ts DELETED
@@ -1,6 +0,0 @@
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 DELETED
@@ -1,74 +0,0 @@
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
- }
package/dist/print.js DELETED
@@ -1,39 +0,0 @@
1
- import { mkdir } from 'node:fs/promises';
2
- import path from 'node:path';
3
- import { deal } from '@d-zero/puppeteer-dealer';
4
- import c from 'ansi-colors';
5
- import { pngToPdf } from './png-to-pdf.js';
6
- import { printPdf } from './print-pdf.js';
7
- import { printPng } from './print-png.js';
8
- export async function print(urlList, options) {
9
- const dir = path.resolve(process.cwd(), '.print');
10
- await mkdir(dir, { recursive: true }).catch(() => { });
11
- const type = options?.type ?? 'png';
12
- const hooks = options?.hooks;
13
- await deal(urlList.map((url) => {
14
- if (typeof url === 'string') {
15
- return { id: null, url };
16
- }
17
- return url;
18
- }), (_, done, total) => {
19
- return `${c.bold.magenta('🎨 Print pages')} ${c.bgBlueBright(` ${type} `)} ${done}/${total}`;
20
- }, {
21
- async deal(page, id, url, logger) {
22
- const ext = type === 'pdf' ? 'pdf' : 'png';
23
- const fileName = `${id}.${ext}`;
24
- const filePath = path.resolve(dir, fileName);
25
- if (type === 'pdf') {
26
- await printPdf(page, url, filePath, logger, hooks);
27
- logger('🔚 Closing');
28
- return;
29
- }
30
- const result = await printPng(page, url, id, filePath, logger, hooks);
31
- if (type === 'png') {
32
- logger('🔚 Closing');
33
- return;
34
- }
35
- await pngToPdf(page, result, logger);
36
- logger('🔚 Closing');
37
- },
38
- }, options);
39
- }
@@ -1,2 +0,0 @@
1
- import type { Listener } from '@d-zero/puppeteer-screenshot';
2
- export declare function scanListener(update: (log: string) => void): Listener;
@@ -1,43 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- import type { ScreenshotListener } from '@d-zero/puppeteer-screenshot';
2
- export declare function screenshotListener(update: (log: string) => void): ScreenshotListener;
@@ -1,43 +0,0 @@
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
- }