@nowline/export-pdf 0.2.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 +190 -0
- package/README.md +89 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +119 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -0
- package/src/index.ts +182 -0
- package/src/svg-to-pdfkit.d.ts +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding any notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Lolay, Inc.
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @nowline/export-pdf
|
|
2
|
+
|
|
3
|
+
Vector PDF export for Nowline roadmaps. Uses
|
|
4
|
+
[`pdfkit`](https://github.com/foliojs/pdfkit) for the document and
|
|
5
|
+
[`@kittl/svg-to-pdfkit`](https://www.npmjs.com/package/@kittl/svg-to-pdfkit)
|
|
6
|
+
to embed the renderer's SVG as a PDF page.
|
|
7
|
+
|
|
8
|
+
**License:** Apache 2.0
|
|
9
|
+
**Part of:** [`lolay/nowline`](../../) monorepo
|
|
10
|
+
**Spec:** [`specs/handoffs/m2c.md`](../../specs/handoffs/m2c.md) § 4
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pnpm add @nowline/export-pdf @nowline/export-core @nowline/renderer
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { exportPdf } from '@nowline/export-pdf';
|
|
22
|
+
|
|
23
|
+
const pdf = await exportPdf(inputs, svg, {
|
|
24
|
+
pageSize: 'letter', // preset name, "WxHunit" custom, or "content"
|
|
25
|
+
orientation: 'auto', // 'portrait' | 'landscape' | 'auto'
|
|
26
|
+
marginPt: 36, // points (¼ inch). Use parseLength() for in/mm/cm.
|
|
27
|
+
fonts, // ResolvedFontPair from @nowline/export-core
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// `pdf` is a Uint8Array of PDF bytes.
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Options
|
|
34
|
+
|
|
35
|
+
| Option | Default | Notes |
|
|
36
|
+
|----------------|--------------------------------------|-------|
|
|
37
|
+
| `pageSize` | `'letter'` | Preset name (`letter`, `legal`, `tabloid`, `ledger`, `a1`–`a5`, `b3`–`b5`), `'content'` for auto-fit, or `WxHunit` for custom (e.g., `8.5x11in`, `210x297mm`). |
|
|
38
|
+
| `orientation` | `'auto'` | `'portrait'`, `'landscape'`, or `'auto'`. Auto picks the shorter-edge orientation that lets the model fit at ≥ 1:1. |
|
|
39
|
+
| `marginPt` | `36` (½ inch) | Margin in PDF points on every side. The CLI parses `--margin 0.5in` via `@nowline/export-core`'s unit converter. |
|
|
40
|
+
| `fonts` | `resolveFonts()` | Pre-resolved sans/mono pair. If omitted, the exporter calls the resolver itself. |
|
|
41
|
+
| `title` | roadmap title or `'Nowline Roadmap'` | PDF `Info /Title`. |
|
|
42
|
+
| `compress` | `true` | PDFKit `compress` option. Set `false` to make the raw PDF byte stream readable for tests. |
|
|
43
|
+
|
|
44
|
+
A roadmap whose content box exceeds `(page − 2 × margin)` at 1:1 is scaled
|
|
45
|
+
**uniformly down** to fit; we never scale up. This keeps a tiny roadmap
|
|
46
|
+
small on a Letter page rather than bloating it.
|
|
47
|
+
|
|
48
|
+
## Determinism
|
|
49
|
+
|
|
50
|
+
PDF determinism is fragile by default — most PDF libraries embed
|
|
51
|
+
`new Date()`, random `/ID` arrays, and timestamp-derived stream filters.
|
|
52
|
+
This exporter pins every source of variation:
|
|
53
|
+
|
|
54
|
+
- `info.CreationDate` and `info.ModDate` come from `inputs.today` (UTC),
|
|
55
|
+
never from `new Date()`. The CLI propagates `--today` to both the layout
|
|
56
|
+
and the PDF metadata.
|
|
57
|
+
- `pdfVersion: '1.7'` pins the PDF spec.
|
|
58
|
+
- `Producer` / `Creator` are explicit, version-pinned strings.
|
|
59
|
+
- `/ID` is auto-generated by PDFKit from `info` only (PDFKit ≥ 0.13), so a
|
|
60
|
+
fixed `CreationDate` yields a stable ID array.
|
|
61
|
+
- Fonts are registered by name with explicit byte buffers — glyph subsets
|
|
62
|
+
are byte-identical across hosts when the same `ResolvedFontPair` is
|
|
63
|
+
used.
|
|
64
|
+
|
|
65
|
+
The `pdfjs` round-trip test (`test/export-pdf.test.ts`) loads the output
|
|
66
|
+
back via `pdfjs-dist` and asserts that page count, dimensions, and visible
|
|
67
|
+
text match the source roadmap.
|
|
68
|
+
|
|
69
|
+
## Why embed SVG instead of walking PositionedRoadmap directly?
|
|
70
|
+
|
|
71
|
+
The original spec called for a per-emitter walk of `PositionedRoadmap` to
|
|
72
|
+
PDFKit primitives. We reuse the SVG renderer instead and embed its output
|
|
73
|
+
via `svg-to-pdfkit`. Trade-offs:
|
|
74
|
+
|
|
75
|
+
- **Pros**: every visual feature the SVG renderer ever supports works in
|
|
76
|
+
PDF for free; no double-implementation; ~150 LOC in this package vs.
|
|
77
|
+
~1500 LOC for a parallel emitter; PDF stays in lock-step with SVG even
|
|
78
|
+
as the renderer evolves (m4 themes, m4 partials, etc.).
|
|
79
|
+
- **Cons**: bugs in `svg-to-pdfkit` show up as PDF rendering bugs; we pin
|
|
80
|
+
the version (`@kittl/svg-to-pdfkit`, the actively-maintained fork) and
|
|
81
|
+
treat any divergence from the SVG as a bug.
|
|
82
|
+
|
|
83
|
+
The trade-off is documented in
|
|
84
|
+
[`specs/handoffs/m2c.md`](../../specs/handoffs/m2c.md) § 4.
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
Apache-2.0. Bundles the `@kittl/svg-to-pdfkit` and `pdfkit` runtime
|
|
89
|
+
dependencies (also Apache / MIT).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ExportInputs, PdfOrientation, PdfPageSize, ResolvedFontPair } from '@nowline/export-core';
|
|
2
|
+
import { type ResolvedPage } from '@nowline/export-core';
|
|
3
|
+
export interface PdfOptions {
|
|
4
|
+
pageSize?: PdfPageSize | string;
|
|
5
|
+
orientation?: PdfOrientation;
|
|
6
|
+
/** Page margin in PDF points. Default 36 (½ inch). */
|
|
7
|
+
marginPt?: number;
|
|
8
|
+
fonts?: ResolvedFontPair;
|
|
9
|
+
/** Author baked into the PDF Info dict. */
|
|
10
|
+
author?: string;
|
|
11
|
+
/** Title baked into the PDF Info dict. Defaults to roadmap title. */
|
|
12
|
+
title?: string;
|
|
13
|
+
/** Subject. Defaults to the source filename. */
|
|
14
|
+
subject?: string;
|
|
15
|
+
/** Producer string. Defaults to `nowline (m2c)`. */
|
|
16
|
+
producer?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Disable PDFKit deflate compression. Tests use this to inspect the Info
|
|
19
|
+
* dict / MediaBox / etc. without round-tripping through zlib. Production
|
|
20
|
+
* paths should leave compression enabled.
|
|
21
|
+
*/
|
|
22
|
+
compress?: boolean;
|
|
23
|
+
}
|
|
24
|
+
export declare function exportPdf(inputs: ExportInputs, svg: string, options?: PdfOptions): Promise<Uint8Array>;
|
|
25
|
+
export type { ResolvedPage };
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EACR,YAAY,EACZ,cAAc,EACd,WAAW,EACX,gBAAgB,EACnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAGH,KAAK,YAAY,EAIpB,MAAM,sBAAsB,CAAC;AAG9B,MAAM,WAAW,UAAU;IACvB,QAAQ,CAAC,EAAE,WAAW,GAAG,MAAM,CAAC;IAChC,WAAW,CAAC,EAAE,cAAc,CAAC;IAC7B,sDAAsD;IACtD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,gBAAgB,CAAC;IACzB,2CAA2C;IAC3C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,qEAAqE;IACrE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,gDAAgD;IAChD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,oDAAoD;IACpD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACtB;AASD,wBAAsB,SAAS,CAC3B,MAAM,EAAE,YAAY,EACpB,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,UAAe,GACzB,OAAO,CAAC,UAAU,CAAC,CAmErB;AAqCD,YAAY,EAAE,YAAY,EAAE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// PDF exporter — vector PDF via PDFKit + @kittl/svg-to-pdfkit.
|
|
2
|
+
//
|
|
3
|
+
// Spec: specs/handoffs/m2c.md § 4 "PDF — vector PDF via PDFKit".
|
|
4
|
+
//
|
|
5
|
+
// The handoff calls for a per-emitter walk of `PositionedRoadmap` rendered
|
|
6
|
+
// directly to PDFKit primitives. For m2c we instead reuse the existing SVG
|
|
7
|
+
// renderer and embed its output into PDFKit via `@kittl/svg-to-pdfkit`. The
|
|
8
|
+
// trade-off is documented in `packages/export-pdf/README.md`:
|
|
9
|
+
//
|
|
10
|
+
// - Pros: every visual detail the SVG renderer ever supports works in PDF
|
|
11
|
+
// for free; no double-implementation of the renderer; ~150 LOC vs
|
|
12
|
+
// ~1500 LOC.
|
|
13
|
+
// - Cons: we route through SVGtoPDF's parsing layer, so any rendering bug
|
|
14
|
+
// in svg-to-pdfkit shows up in our output. We pin the version and treat
|
|
15
|
+
// any divergence from the SVG renderer as a bug.
|
|
16
|
+
//
|
|
17
|
+
// Determinism contract:
|
|
18
|
+
// - PDFKit `info.CreationDate` / `ModDate`: pinned to `inputs.today` (UTC),
|
|
19
|
+
// never `new Date()`.
|
|
20
|
+
// - PDFKit auto-generated `/ID`: PDFKit derives the ID from `info` only
|
|
21
|
+
// (since 0.13), so a fixed CreationDate yields a stable ID.
|
|
22
|
+
// - `pdfVersion: '1.7'` pins the PDF spec.
|
|
23
|
+
// - Producer / Creator strings are explicit, version-stable.
|
|
24
|
+
// - Fonts: registered explicitly via PDFKit `registerFont(name, bytes,
|
|
25
|
+
// family?)` so glyph subsets are byte-identical across hosts.
|
|
26
|
+
import { PassThrough } from 'node:stream';
|
|
27
|
+
import SVGtoPDF from '@kittl/svg-to-pdfkit';
|
|
28
|
+
import { fitContent, parsePageSize, resolveFonts, resolvePage, validateMargin, } from '@nowline/export-core';
|
|
29
|
+
import PDFDocument from 'pdfkit';
|
|
30
|
+
export async function exportPdf(inputs, svg, options = {}) {
|
|
31
|
+
const resolved = await resolveOptions(options);
|
|
32
|
+
const page = resolvePage({
|
|
33
|
+
pageSize: resolved.pageSize,
|
|
34
|
+
orientation: resolved.orientation,
|
|
35
|
+
contentWidthPt: inputs.model.width,
|
|
36
|
+
contentHeightPt: inputs.model.height,
|
|
37
|
+
marginPt: resolved.marginPt,
|
|
38
|
+
});
|
|
39
|
+
validateMargin(resolved.marginPt, page);
|
|
40
|
+
const fit = fitContent({
|
|
41
|
+
page,
|
|
42
|
+
contentWidthPt: inputs.model.width,
|
|
43
|
+
contentHeightPt: inputs.model.height,
|
|
44
|
+
marginPt: resolved.marginPt,
|
|
45
|
+
});
|
|
46
|
+
const today = inputs.today ?? new Date(Date.UTC(2026, 0, 5)); // deterministic Monday default
|
|
47
|
+
const creationDate = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate(), 0, 0, 0, 0));
|
|
48
|
+
const doc = new PDFDocument({
|
|
49
|
+
autoFirstPage: false,
|
|
50
|
+
compress: options.compress ?? true,
|
|
51
|
+
pdfVersion: '1.7',
|
|
52
|
+
info: {
|
|
53
|
+
Title: options.title ?? (inputs.model.header.title || 'Nowline Roadmap'),
|
|
54
|
+
Author: options.author ?? inputs.model.header.author ?? '',
|
|
55
|
+
Subject: options.subject ?? inputs.sourcePath,
|
|
56
|
+
Producer: options.producer ?? 'nowline (m2c)',
|
|
57
|
+
Creator: options.producer ?? 'nowline (m2c)',
|
|
58
|
+
CreationDate: creationDate,
|
|
59
|
+
ModDate: creationDate,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
// Register the resolved font pair once, by family name. PDFKit accepts
|
|
63
|
+
// ArrayBuffer / Buffer / Uint8Array directly. Fontkit handles VF detection
|
|
64
|
+
// internally; the bytes are embedded as-is.
|
|
65
|
+
const sansBuffer = bufferOf(resolved.fonts.sans.bytes);
|
|
66
|
+
const monoBuffer = bufferOf(resolved.fonts.mono.bytes);
|
|
67
|
+
doc.registerFont('Sans', sansBuffer, resolved.fonts.sans.face);
|
|
68
|
+
doc.registerFont('Mono', monoBuffer, resolved.fonts.mono.face);
|
|
69
|
+
doc.font('Sans');
|
|
70
|
+
doc.addPage({
|
|
71
|
+
size: [page.widthPt, page.heightPt],
|
|
72
|
+
margin: 0,
|
|
73
|
+
});
|
|
74
|
+
SVGtoPDF(doc, svg, fit.offsetX, fit.offsetY, {
|
|
75
|
+
width: inputs.model.width * fit.factor,
|
|
76
|
+
height: inputs.model.height * fit.factor,
|
|
77
|
+
preserveAspectRatio: 'xMidYMid meet',
|
|
78
|
+
useCSS: false,
|
|
79
|
+
assumePt: false,
|
|
80
|
+
fontCallback: (family) => {
|
|
81
|
+
// Map any sans-ish family to "Sans", mono-ish to "Mono".
|
|
82
|
+
if (/mono|courier|consolas|menlo|monaco/i.test(family))
|
|
83
|
+
return 'Mono';
|
|
84
|
+
return 'Sans';
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
return await streamToBytes(doc);
|
|
88
|
+
}
|
|
89
|
+
function streamToBytes(doc) {
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
const chunks = [];
|
|
92
|
+
const stream = new PassThrough();
|
|
93
|
+
stream.on('data', (chunk) => chunks.push(chunk));
|
|
94
|
+
stream.on('end', () => {
|
|
95
|
+
const total = Buffer.concat(chunks);
|
|
96
|
+
resolve(new Uint8Array(total.buffer, total.byteOffset, total.byteLength));
|
|
97
|
+
});
|
|
98
|
+
stream.on('error', reject);
|
|
99
|
+
doc.pipe(stream);
|
|
100
|
+
doc.end();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
async function resolveOptions(options) {
|
|
104
|
+
const pageSize = typeof options.pageSize === 'string'
|
|
105
|
+
? parsePageSize(options.pageSize)
|
|
106
|
+
: (options.pageSize ?? { kind: 'preset', name: 'letter' });
|
|
107
|
+
const orientation = options.orientation ?? 'auto';
|
|
108
|
+
const marginPt = options.marginPt ?? 36;
|
|
109
|
+
const fonts = options.fonts ?? (await resolveFontsFor());
|
|
110
|
+
return { pageSize, orientation, marginPt, fonts };
|
|
111
|
+
}
|
|
112
|
+
async function resolveFontsFor() {
|
|
113
|
+
const result = await resolveFonts();
|
|
114
|
+
return { sans: result.sans, mono: result.mono };
|
|
115
|
+
}
|
|
116
|
+
function bufferOf(bytes) {
|
|
117
|
+
return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,EAAE;AACF,iEAAiE;AACjE,EAAE;AACF,2EAA2E;AAC3E,2EAA2E;AAC3E,4EAA4E;AAC5E,8DAA8D;AAC9D,EAAE;AACF,4EAA4E;AAC5E,sEAAsE;AACtE,iBAAiB;AACjB,4EAA4E;AAC5E,4EAA4E;AAC5E,qDAAqD;AACrD,EAAE;AACF,wBAAwB;AACxB,8EAA8E;AAC9E,0BAA0B;AAC1B,0EAA0E;AAC1E,gEAAgE;AAChE,6CAA6C;AAC7C,+DAA+D;AAC/D,yEAAyE;AACzE,kEAAkE;AAElE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,QAAQ,MAAM,sBAAsB,CAAC;AAO5C,OAAO,EACH,UAAU,EACV,aAAa,EAEb,YA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AED,KAAK,UAAU,cAAc,CAAC,OAAmB;IAC7C,MAAM,QAAQ,GACV,OAAO,OAAO,CAAC,QAAQ,KAAK,QAAQ;QAChC,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC,QAAQ,CAAC;QACjC,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,IAAI,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,CAAC;IACnE,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,MAAM,CAAC;IAClD,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,EAAE,CAAC;IACxC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,CAAC,MAAM,eAAe,EAAE,CAAC,CAAC;IACzD,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AACtD,CAAC;AAED,KAAK,UAAU,eAAe;IAC1B,MAAM,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;IACpC,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;AACpD,CAAC;AAED,SAAS,QAAQ,CAAC,KAAiB;IAC/B,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;AACzE,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nowline/export-pdf",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Nowline PDF exporter — vector PDF via PDFKit + svg-to-pdfkit",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist/",
|
|
17
|
+
"src/"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@kittl/svg-to-pdfkit": "^0.1.12",
|
|
21
|
+
"pdfkit": "^0.15.0",
|
|
22
|
+
"@nowline/export-core": "0.2.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^22.0.0",
|
|
26
|
+
"@types/pdfkit": "^0.13.4",
|
|
27
|
+
"langium": "~4.2.2",
|
|
28
|
+
"typescript": "~5.7.0",
|
|
29
|
+
"vitest": "^3.1.0",
|
|
30
|
+
"@nowline/layout": "0.2.0",
|
|
31
|
+
"@nowline/core": "0.2.0",
|
|
32
|
+
"@nowline/renderer": "0.2.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc -b tsconfig.json",
|
|
36
|
+
"watch": "tsc -b tsconfig.json --watch",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"test:watch": "vitest"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// PDF exporter — vector PDF via PDFKit + @kittl/svg-to-pdfkit.
|
|
2
|
+
//
|
|
3
|
+
// Spec: specs/handoffs/m2c.md § 4 "PDF — vector PDF via PDFKit".
|
|
4
|
+
//
|
|
5
|
+
// The handoff calls for a per-emitter walk of `PositionedRoadmap` rendered
|
|
6
|
+
// directly to PDFKit primitives. For m2c we instead reuse the existing SVG
|
|
7
|
+
// renderer and embed its output into PDFKit via `@kittl/svg-to-pdfkit`. The
|
|
8
|
+
// trade-off is documented in `packages/export-pdf/README.md`:
|
|
9
|
+
//
|
|
10
|
+
// - Pros: every visual detail the SVG renderer ever supports works in PDF
|
|
11
|
+
// for free; no double-implementation of the renderer; ~150 LOC vs
|
|
12
|
+
// ~1500 LOC.
|
|
13
|
+
// - Cons: we route through SVGtoPDF's parsing layer, so any rendering bug
|
|
14
|
+
// in svg-to-pdfkit shows up in our output. We pin the version and treat
|
|
15
|
+
// any divergence from the SVG renderer as a bug.
|
|
16
|
+
//
|
|
17
|
+
// Determinism contract:
|
|
18
|
+
// - PDFKit `info.CreationDate` / `ModDate`: pinned to `inputs.today` (UTC),
|
|
19
|
+
// never `new Date()`.
|
|
20
|
+
// - PDFKit auto-generated `/ID`: PDFKit derives the ID from `info` only
|
|
21
|
+
// (since 0.13), so a fixed CreationDate yields a stable ID.
|
|
22
|
+
// - `pdfVersion: '1.7'` pins the PDF spec.
|
|
23
|
+
// - Producer / Creator strings are explicit, version-stable.
|
|
24
|
+
// - Fonts: registered explicitly via PDFKit `registerFont(name, bytes,
|
|
25
|
+
// family?)` so glyph subsets are byte-identical across hosts.
|
|
26
|
+
|
|
27
|
+
import { PassThrough } from 'node:stream';
|
|
28
|
+
import SVGtoPDF from '@kittl/svg-to-pdfkit';
|
|
29
|
+
import type {
|
|
30
|
+
ExportInputs,
|
|
31
|
+
PdfOrientation,
|
|
32
|
+
PdfPageSize,
|
|
33
|
+
ResolvedFontPair,
|
|
34
|
+
} from '@nowline/export-core';
|
|
35
|
+
import {
|
|
36
|
+
fitContent,
|
|
37
|
+
parsePageSize,
|
|
38
|
+
type ResolvedPage,
|
|
39
|
+
resolveFonts,
|
|
40
|
+
resolvePage,
|
|
41
|
+
validateMargin,
|
|
42
|
+
} from '@nowline/export-core';
|
|
43
|
+
import PDFDocument from 'pdfkit';
|
|
44
|
+
|
|
45
|
+
export interface PdfOptions {
|
|
46
|
+
pageSize?: PdfPageSize | string;
|
|
47
|
+
orientation?: PdfOrientation;
|
|
48
|
+
/** Page margin in PDF points. Default 36 (½ inch). */
|
|
49
|
+
marginPt?: number;
|
|
50
|
+
fonts?: ResolvedFontPair;
|
|
51
|
+
/** Author baked into the PDF Info dict. */
|
|
52
|
+
author?: string;
|
|
53
|
+
/** Title baked into the PDF Info dict. Defaults to roadmap title. */
|
|
54
|
+
title?: string;
|
|
55
|
+
/** Subject. Defaults to the source filename. */
|
|
56
|
+
subject?: string;
|
|
57
|
+
/** Producer string. Defaults to `nowline (m2c)`. */
|
|
58
|
+
producer?: string;
|
|
59
|
+
/**
|
|
60
|
+
* Disable PDFKit deflate compression. Tests use this to inspect the Info
|
|
61
|
+
* dict / MediaBox / etc. without round-tripping through zlib. Production
|
|
62
|
+
* paths should leave compression enabled.
|
|
63
|
+
*/
|
|
64
|
+
compress?: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface ResolvedPdfOptions {
|
|
68
|
+
pageSize: PdfPageSize;
|
|
69
|
+
orientation: PdfOrientation;
|
|
70
|
+
marginPt: number;
|
|
71
|
+
fonts: ResolvedFontPair;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function exportPdf(
|
|
75
|
+
inputs: ExportInputs,
|
|
76
|
+
svg: string,
|
|
77
|
+
options: PdfOptions = {},
|
|
78
|
+
): Promise<Uint8Array> {
|
|
79
|
+
const resolved = await resolveOptions(options);
|
|
80
|
+
|
|
81
|
+
const page = resolvePage({
|
|
82
|
+
pageSize: resolved.pageSize,
|
|
83
|
+
orientation: resolved.orientation,
|
|
84
|
+
contentWidthPt: inputs.model.width,
|
|
85
|
+
contentHeightPt: inputs.model.height,
|
|
86
|
+
marginPt: resolved.marginPt,
|
|
87
|
+
});
|
|
88
|
+
validateMargin(resolved.marginPt, page);
|
|
89
|
+
|
|
90
|
+
const fit = fitContent({
|
|
91
|
+
page,
|
|
92
|
+
contentWidthPt: inputs.model.width,
|
|
93
|
+
contentHeightPt: inputs.model.height,
|
|
94
|
+
marginPt: resolved.marginPt,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const today = inputs.today ?? new Date(Date.UTC(2026, 0, 5)); // deterministic Monday default
|
|
98
|
+
const creationDate = new Date(
|
|
99
|
+
Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate(), 0, 0, 0, 0),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const doc = new PDFDocument({
|
|
103
|
+
autoFirstPage: false,
|
|
104
|
+
compress: options.compress ?? true,
|
|
105
|
+
pdfVersion: '1.7',
|
|
106
|
+
info: {
|
|
107
|
+
Title: options.title ?? (inputs.model.header.title || 'Nowline Roadmap'),
|
|
108
|
+
Author: options.author ?? inputs.model.header.author ?? '',
|
|
109
|
+
Subject: options.subject ?? inputs.sourcePath,
|
|
110
|
+
Producer: options.producer ?? 'nowline (m2c)',
|
|
111
|
+
Creator: options.producer ?? 'nowline (m2c)',
|
|
112
|
+
CreationDate: creationDate,
|
|
113
|
+
ModDate: creationDate,
|
|
114
|
+
} as PDFKit.DocumentInfo & { CreationDate?: Date; ModDate?: Date },
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Register the resolved font pair once, by family name. PDFKit accepts
|
|
118
|
+
// ArrayBuffer / Buffer / Uint8Array directly. Fontkit handles VF detection
|
|
119
|
+
// internally; the bytes are embedded as-is.
|
|
120
|
+
const sansBuffer = bufferOf(resolved.fonts.sans.bytes);
|
|
121
|
+
const monoBuffer = bufferOf(resolved.fonts.mono.bytes);
|
|
122
|
+
doc.registerFont('Sans', sansBuffer, resolved.fonts.sans.face);
|
|
123
|
+
doc.registerFont('Mono', monoBuffer, resolved.fonts.mono.face);
|
|
124
|
+
doc.font('Sans');
|
|
125
|
+
|
|
126
|
+
doc.addPage({
|
|
127
|
+
size: [page.widthPt, page.heightPt],
|
|
128
|
+
margin: 0,
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
SVGtoPDF(doc, svg, fit.offsetX, fit.offsetY, {
|
|
132
|
+
width: inputs.model.width * fit.factor,
|
|
133
|
+
height: inputs.model.height * fit.factor,
|
|
134
|
+
preserveAspectRatio: 'xMidYMid meet',
|
|
135
|
+
useCSS: false,
|
|
136
|
+
assumePt: false,
|
|
137
|
+
fontCallback: (family: string) => {
|
|
138
|
+
// Map any sans-ish family to "Sans", mono-ish to "Mono".
|
|
139
|
+
if (/mono|courier|consolas|menlo|monaco/i.test(family)) return 'Mono';
|
|
140
|
+
return 'Sans';
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
return await streamToBytes(doc);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function streamToBytes(doc: PDFKit.PDFDocument): Promise<Uint8Array> {
|
|
148
|
+
return new Promise((resolve, reject) => {
|
|
149
|
+
const chunks: Buffer[] = [];
|
|
150
|
+
const stream = new PassThrough();
|
|
151
|
+
stream.on('data', (chunk: Buffer) => chunks.push(chunk));
|
|
152
|
+
stream.on('end', () => {
|
|
153
|
+
const total = Buffer.concat(chunks);
|
|
154
|
+
resolve(new Uint8Array(total.buffer, total.byteOffset, total.byteLength));
|
|
155
|
+
});
|
|
156
|
+
stream.on('error', reject);
|
|
157
|
+
doc.pipe(stream);
|
|
158
|
+
doc.end();
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function resolveOptions(options: PdfOptions): Promise<ResolvedPdfOptions> {
|
|
163
|
+
const pageSize: PdfPageSize =
|
|
164
|
+
typeof options.pageSize === 'string'
|
|
165
|
+
? parsePageSize(options.pageSize)
|
|
166
|
+
: (options.pageSize ?? { kind: 'preset', name: 'letter' });
|
|
167
|
+
const orientation = options.orientation ?? 'auto';
|
|
168
|
+
const marginPt = options.marginPt ?? 36;
|
|
169
|
+
const fonts = options.fonts ?? (await resolveFontsFor());
|
|
170
|
+
return { pageSize, orientation, marginPt, fonts };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function resolveFontsFor(): Promise<ResolvedFontPair> {
|
|
174
|
+
const result = await resolveFonts();
|
|
175
|
+
return { sans: result.sans, mono: result.mono };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function bufferOf(bytes: Uint8Array): Buffer {
|
|
179
|
+
return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export type { ResolvedPage };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Ambient declaration for the untyped @kittl/svg-to-pdfkit fork. The original
|
|
2
|
+
// `svg-to-pdfkit` ships its own d.ts but the @kittl fork (the actively
|
|
3
|
+
// maintained version we depend on) does not. Mirror the surface we use.
|
|
4
|
+
|
|
5
|
+
declare module '@kittl/svg-to-pdfkit' {
|
|
6
|
+
interface SVGtoPDFOptions {
|
|
7
|
+
width?: number;
|
|
8
|
+
height?: number;
|
|
9
|
+
preserveAspectRatio?: string;
|
|
10
|
+
useCSS?: boolean;
|
|
11
|
+
assumePt?: boolean;
|
|
12
|
+
precision?: number;
|
|
13
|
+
fontCallback?: (family: string, isBold: boolean, isItalic: boolean) => string;
|
|
14
|
+
imageCallback?: (link: string) => string;
|
|
15
|
+
documentCallback?: (doc: PDFKit.PDFDocument) => PDFKit.PDFDocument;
|
|
16
|
+
warningCallback?: (message: string) => void;
|
|
17
|
+
colorCallback?: (color: number[] | string) => number[] | string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function SVGtoPDF(
|
|
21
|
+
doc: PDFKit.PDFDocument,
|
|
22
|
+
svg: string,
|
|
23
|
+
x?: number,
|
|
24
|
+
y?: number,
|
|
25
|
+
options?: SVGtoPDFOptions,
|
|
26
|
+
): void;
|
|
27
|
+
|
|
28
|
+
export default SVGtoPDF;
|
|
29
|
+
}
|