@spinajs/queue-templates-pdf 2.0.490 → 2.0.494

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 CHANGED
@@ -1,101 +1,101 @@
1
- # @spinajs/queue-templates-pdf
2
-
3
- A SpinaJS queue job that renders a PDF template in an **isolated worker process**
4
- and reports render progress (percent + phase + message) back to the queue.
5
-
6
- All heavy, crash-prone work — Chromium, template compilation, upload — runs in a
7
- forked worker. The job in the queue consumer only supervises it, so a hung or
8
- crashing render can never take down the worker, and memory is reclaimed per render.
9
-
10
- ## Installation
11
-
12
- ```bash
13
- npm install @spinajs/queue-templates-pdf
14
- ```
15
-
16
- Requires a configured queue (transport + DB for the job model) and at least one
17
- `@spinajs/fs` provider for the output.
18
-
19
- ## Usage
20
-
21
- ```ts
22
- import { scheduleRenderPdf } from '@spinajs/queue-templates-pdf';
23
-
24
- // render now
25
- await scheduleRenderPdf({
26
- input: 'invoices/invoice.pug', // a template (template: true) or raw HTML
27
- template: true,
28
- model: { total: 42 },
29
- lang: 'en',
30
- output: { provider: 's3', path: 'invoices/2026/1.pdf' }, // any @spinajs/fs provider
31
- pdfOptions: { format: 'A4', printBackground: true },
32
- });
33
-
34
- // render in 5 minutes
35
- await scheduleRenderPdf(payload, { delay: 5 * 60 * 1000 });
36
-
37
- // recurring: every day at 06:00
38
- await scheduleRenderPdf(payload, { cron: '0 6 * * *' });
39
- ```
40
-
41
- The job must be consumed by a queue worker:
42
-
43
- ```ts
44
- import { RenderPdfJob } from '@spinajs/queue-templates-pdf';
45
- const queue = await DI.resolve(QueueService);
46
- await queue.consume(RenderPdfJob);
47
- ```
48
-
49
- ## Output
50
-
51
- `output: { provider, path }` names a configured `@spinajs/fs` provider (local,
52
- temp, S3, ...) and a path relative to it. The worker renders to a local temp file
53
- and uploads it to that provider, so the same job works for local disk or object
54
- storage without change.
55
-
56
- ## Progress
57
-
58
- The worker streams progress; the supervisor forwards it to the queue job model:
59
-
60
- - **percent** 0–100 (phase-weighted),
61
- - **phase** — `starting` → `preparing` → `loading` → `rendering` → `done` (or `failed`),
62
- - **message** — e.g. `Loading resources (12 done, 3 pending)`.
63
-
64
- With `@spinajs/queue-http-progress` this is exposed at:
65
-
66
- ```
67
- GET jobs/v1/:jobId/status
68
- -> { jobId, progress, phase, message, status, createdAt }
69
- ```
70
-
71
- `scheduleRenderPdf` (and `RenderPdfJob.emit`) return the generated `jobId`, so you
72
- can hand it straight to the status endpoint:
73
-
74
- ```ts
75
- const jobId = await scheduleRenderPdf(payload);
76
- // later: GET jobs/v1/${jobId}/status
77
- ```
78
-
79
- ## Worker configuration
80
-
81
- The worker re-bootstraps the app configuration by default (it runs in the app's
82
- config environment: same config sources / cwd, so it resolves the same
83
- `templates.pdf` launch args and fs providers). For environments where that is not
84
- possible, pass an inline `config` object in the payload and the worker will
85
- bootstrap from it instead.
86
-
87
- ## How it works
88
-
89
- ```
90
- scheduleRenderPdf ─► RenderPdfJob (queue consumer)
91
- │ fork()
92
-
93
- render-worker (separate process)
94
- ├─ bootstrap config/DI
95
- ├─ Templates.render (template mode) ─► HTML
96
- ├─ PdfRenderer.renderHtmlToFile ──── onProgress ─┐
97
- └─ fs provider.upload(tmp → output.path) │
98
- ▲ │
99
- progress(percent, {phase, message}) ◄── IPC ◄─────────────────────-─┘
100
- (persisted to the job model, exposed over HTTP)
101
- ```
1
+ # @spinajs/queue-templates-pdf
2
+
3
+ A SpinaJS queue job that renders a PDF template in an **isolated worker process**
4
+ and reports render progress (percent + phase + message) back to the queue.
5
+
6
+ All heavy, crash-prone work — Chromium, template compilation, upload — runs in a
7
+ forked worker. The job in the queue consumer only supervises it, so a hung or
8
+ crashing render can never take down the worker, and memory is reclaimed per render.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ npm install @spinajs/queue-templates-pdf
14
+ ```
15
+
16
+ Requires a configured queue (transport + DB for the job model) and at least one
17
+ `@spinajs/fs` provider for the output.
18
+
19
+ ## Usage
20
+
21
+ ```ts
22
+ import { scheduleRenderPdf } from '@spinajs/queue-templates-pdf';
23
+
24
+ // render now
25
+ await scheduleRenderPdf({
26
+ input: 'invoices/invoice.pug', // a template (template: true) or raw HTML
27
+ template: true,
28
+ model: { total: 42 },
29
+ lang: 'en',
30
+ output: { provider: 's3', path: 'invoices/2026/1.pdf' }, // any @spinajs/fs provider
31
+ pdfOptions: { format: 'A4', printBackground: true },
32
+ });
33
+
34
+ // render in 5 minutes
35
+ await scheduleRenderPdf(payload, { delay: 5 * 60 * 1000 });
36
+
37
+ // recurring: every day at 06:00
38
+ await scheduleRenderPdf(payload, { cron: '0 6 * * *' });
39
+ ```
40
+
41
+ The job must be consumed by a queue worker:
42
+
43
+ ```ts
44
+ import { RenderPdfJob } from '@spinajs/queue-templates-pdf';
45
+ const queue = await DI.resolve(QueueService);
46
+ await queue.consume(RenderPdfJob);
47
+ ```
48
+
49
+ ## Output
50
+
51
+ `output: { provider, path }` names a configured `@spinajs/fs` provider (local,
52
+ temp, S3, ...) and a path relative to it. The worker renders to a local temp file
53
+ and uploads it to that provider, so the same job works for local disk or object
54
+ storage without change.
55
+
56
+ ## Progress
57
+
58
+ The worker streams progress; the supervisor forwards it to the queue job model:
59
+
60
+ - **percent** 0–100 (phase-weighted),
61
+ - **phase** — `starting` → `preparing` → `loading` → `rendering` → `done` (or `failed`),
62
+ - **message** — e.g. `Loading resources (12 done, 3 pending)`.
63
+
64
+ With `@spinajs/queue-http-progress` this is exposed at:
65
+
66
+ ```
67
+ GET jobs/v1/:jobId/status
68
+ -> { jobId, progress, phase, message, status, createdAt }
69
+ ```
70
+
71
+ `scheduleRenderPdf` (and `RenderPdfJob.emit`) return the generated `jobId`, so you
72
+ can hand it straight to the status endpoint:
73
+
74
+ ```ts
75
+ const jobId = await scheduleRenderPdf(payload);
76
+ // later: GET jobs/v1/${jobId}/status
77
+ ```
78
+
79
+ ## Worker configuration
80
+
81
+ The worker re-bootstraps the app configuration by default (it runs in the app's
82
+ config environment: same config sources / cwd, so it resolves the same
83
+ `templates.pdf` launch args and fs providers). For environments where that is not
84
+ possible, pass an inline `config` object in the payload and the worker will
85
+ bootstrap from it instead.
86
+
87
+ ## How it works
88
+
89
+ ```
90
+ scheduleRenderPdf ─► RenderPdfJob (queue consumer)
91
+ │ fork()
92
+
93
+ render-worker (separate process)
94
+ ├─ bootstrap config/DI
95
+ ├─ Templates.render (template mode) ─► HTML
96
+ ├─ PdfRenderer.renderHtmlToFile ──── onProgress ─┐
97
+ └─ fs provider.upload(tmp → output.path) │
98
+ ▲ │
99
+ progress(percent, {phase, message}) ◄── IPC ◄─────────────────────-─┘
100
+ (persisted to the job model, exposed over HTTP)
101
+ ```