pdfkit 0.19.1 → 0.20.1

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.
Files changed (58) hide show
  1. package/.yarn/install-state.gz +0 -0
  2. package/CHANGELOG.md +25 -0
  3. package/README.md +97 -25
  4. package/js/output.cjs +53 -0
  5. package/js/output.mjs +50 -0
  6. package/js/pdfkit.browser.js +7524 -0
  7. package/js/pdfkit.browser.js.map +1 -0
  8. package/js/pdfkit.browser.mjs +7344 -0
  9. package/js/pdfkit.browser.mjs.map +1 -0
  10. package/js/pdfkit.browser.old.js +7517 -0
  11. package/js/pdfkit.browser.old.mjs +7342 -0
  12. package/js/pdfkit.js +1419 -487
  13. package/js/pdfkit.js.map +1 -1
  14. package/js/{pdfkit.es.js → pdfkit.node.mjs} +1410 -488
  15. package/js/pdfkit.node.mjs.map +1 -0
  16. package/js/pdfkit.old.js +7276 -0
  17. package/js/pdfkit.standalone.js +23432 -35943
  18. package/js/standard-fonts/Courier.cjs +17 -0
  19. package/js/standard-fonts/Courier.mjs +15 -0
  20. package/js/standard-fonts/CourierBold.cjs +17 -0
  21. package/js/standard-fonts/CourierBold.mjs +15 -0
  22. package/js/standard-fonts/CourierBoldOblique.cjs +17 -0
  23. package/js/standard-fonts/CourierBoldOblique.mjs +15 -0
  24. package/js/standard-fonts/CourierOblique.cjs +17 -0
  25. package/js/standard-fonts/CourierOblique.mjs +15 -0
  26. package/js/standard-fonts/Helvetica.cjs +17 -0
  27. package/js/standard-fonts/Helvetica.mjs +15 -0
  28. package/js/standard-fonts/HelveticaBold.cjs +17 -0
  29. package/js/standard-fonts/HelveticaBold.mjs +15 -0
  30. package/js/standard-fonts/HelveticaBoldOblique.cjs +17 -0
  31. package/js/standard-fonts/HelveticaBoldOblique.mjs +15 -0
  32. package/js/standard-fonts/HelveticaOblique.cjs +17 -0
  33. package/js/standard-fonts/HelveticaOblique.mjs +15 -0
  34. package/js/standard-fonts/Symbol.cjs +15 -0
  35. package/js/standard-fonts/Symbol.mjs +13 -0
  36. package/js/standard-fonts/TimesBold.cjs +17 -0
  37. package/js/standard-fonts/TimesBold.mjs +15 -0
  38. package/js/standard-fonts/TimesBoldItalic.cjs +17 -0
  39. package/js/standard-fonts/TimesBoldItalic.mjs +15 -0
  40. package/js/standard-fonts/TimesItalic.cjs +17 -0
  41. package/js/standard-fonts/TimesItalic.mjs +15 -0
  42. package/js/standard-fonts/TimesRoman.cjs +17 -0
  43. package/js/standard-fonts/TimesRoman.mjs +15 -0
  44. package/js/standard-fonts/ZapfDingbats.cjs +15 -0
  45. package/js/standard-fonts/ZapfDingbats.mjs +13 -0
  46. package/js/standard-fonts/chunks/standardGlyphNames-DNHAb7rp.cjs +5 -0
  47. package/js/standard-fonts/chunks/standardGlyphNames-w3QlG7PW.mjs +3 -0
  48. package/package.json +110 -28
  49. package/rollup.config.mjs +126 -57
  50. package/tools/README.md +124 -0
  51. package/tools/afm-converter.js +317 -0
  52. package/tools/asset-plugin.mjs +17 -0
  53. package/tools/binary-plugin.mjs +18 -0
  54. package/tools/convert-afm-parsed.js +8 -0
  55. package/tools/convert-afm-runtime.js +8 -0
  56. package/vitest.config.js +2 -0
  57. package/js/pdfkit.es.js.map +0 -1
  58. package/js/virtual-fs.js +0 -49
Binary file
package/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  ### Unreleased
4
4
 
5
+ ### [v0.20.1] - 2026-08-23
6
+
7
+ - Add a Node ESM build so `import 'pdfkit'` in Node resolves to the Node build (real file system, native zlib, Node streams, self-registering standard fonts) instead of the browser bundle
8
+
9
+ ### [v0.20.0] - 2026-08-23
10
+
11
+ - [BREAKING CHANGE] Remove the virtual file system (`pdfkit/virtual-fs`). Browser builds no longer depend on `fs`: use `registerFile` to register `Uint8Array` data under a path, pass a `Uint8Array` or `ArrayBuffer` directly to `registerFont`, `image` and `file`, or pass a data URL directly to `image` and `file`
12
+ - Add `registerFile(path, data, options)` to globally register in-memory files in Node and browsers, with optional `birthtime` and `ctime` metadata. Passing `undefined` as data unregisters the path
13
+ - [BREAKING CHANGE] Export `PDFDocument`, `LineWrapper` and `registerFile` as named exports from the main Node and browser entry points while preserving the default `PDFDocument` export
14
+ - Add experimental `toBlob(document)` and `toBytes(document)` output helpers under `pdfkit/output`
15
+ - Accept already-parsed fontkit `Font` instances in `doc.font()` and `registerFont`
16
+ - Load the PDF/A ICC profile from disk only when needed in Node, while continuing to bundle it in browser builds
17
+ - Add tools to convert raw AFM standard-font definitions into parsed or compact runtime JavaScript modules
18
+ - [BREAKING CHANGE] Use generated standard-font data instead of parsing raw AFM definitions at runtime. Node loads font metrics lazily; browser applications must import each font they use from `pdfkit/standard-fonts/*` and register it with `registerStdFonts()`
19
+ - [BREAKING CHANGE] Restrict AcroForm options to documented mappings and explicit escape hatches.
20
+ - [BREAKING CHANGE] Stop automatically uppercasing annotation option keys.
21
+ - [BREAKING CHANGE] Throw from `addNamedEmbeddedFile` when no ref is given, instead of writing an unparseable `undefined` token into the `/EmbeddedFiles` name tree
22
+ - Do not mutate options passed to `doc.annotate()` and its convenience methods (link, note, strike, lineAnnotation, rectAnnotation, ellipseAnnotation, textAnnotation, fileAnnotation)
23
+ - Persist font options when adding a new page. Fixes #1739
24
+ - Use `Uint8Array` instead of Node's `Buffer` internally
25
+ - Fix `date` text field formatting emitting invalid JavaScript, so the format was never applied. Fixes #1546
26
+ - Fix `indentAllLines` applying the indent again on every paragraph and every page break, and keep it applied across continued text. Fixes #1606
27
+ - Fix a hole in a sparse array being skipped entirely, which shifted every later entry down one
28
+ - Encrypt strings inside name trees. Fixes #1513
29
+
5
30
  ### [v0.19.1] - 2026-06-10
6
31
 
7
32
  - Fix RGB JPEG embedded as DeviceGray (0.19.0 regression) (#1734)
package/README.md CHANGED
@@ -69,8 +69,13 @@ yarn add pdfkit
69
69
 
70
70
  ## Example
71
71
 
72
+ Both `const PDFDocument = require('pdfkit')` and
73
+ `import PDFDocument from 'pdfkit'` remain supported for backward compatibility.
74
+ New code should prefer the named `PDFDocument` export, which will make a future
75
+ migration to an ESM-only package more straightforward.
76
+
72
77
  ```javascript
73
- const PDFDocument = require('pdfkit');
78
+ const { PDFDocument } = require('pdfkit');
74
79
  const fs = require('fs');
75
80
 
76
81
  // Create a document
@@ -139,46 +144,113 @@ There are three ways to use PDFKit in the browser:
139
144
  - Use [webpack](https://webpack.js.org/). See [complete example](https://github.com/foliojs/pdfkit/blob/master/examples/webpack).
140
145
  - Use prebuilt version. Distributed as `pdfkit.standalone.js` file in the [releases](https://github.com/foliojs/pdfkit/releases) or in the package `js` folder.
141
146
 
142
- In addition to PDFKit, you'll need somewhere to stream the output to. HTML5 has a
143
- [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) object which can be used to store binary data, and
144
- get URLs to this data in order to display PDF output inside an iframe, or upload to a server, etc. In order to
145
- get a Blob from the output of PDFKit, you can use the [blob-stream](https://github.com/devongovett/blob-stream)
146
- module.
147
+ In addition to PDFKit, you'll need to collect its output. Browsers provide a
148
+ [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) object for storing
149
+ binary data and creating URLs that can be displayed in an iframe, downloaded or
150
+ uploaded.
151
+
152
+ ### Experimental output helpers
147
153
 
148
- The following example uses Browserify or webpack to load `PDFKit` and `blob-stream`. See [here](https://codepen.io/blikblum/pen/gJNWMg?editors=1010) and [here](https://codepen.io/blikblum/pen/YboVNq?editors=1010) for examples
149
- of prebuilt version usage.
154
+ PDFKit provides experimental `toBlob` and `toBytes` helpers from
155
+ `pdfkit/output`. These functions may change before they are stabilized. Call the
156
+ selected helper before ending the document so it receives the complete output.
157
+
158
+ Use `toBlob` when displaying, downloading or uploading the PDF in a browser:
150
159
 
151
160
  ```javascript
152
- // require dependencies
153
- const PDFDocument = require('pdfkit');
154
- const blobStream = require('blob-stream');
161
+ import { PDFDocument, registerStdFonts } from 'pdfkit';
162
+ import Helvetica from 'pdfkit/standard-fonts/Helvetica';
163
+ import HelveticaBold from 'pdfkit/standard-fonts/HelveticaBold';
164
+ import { toBlob } from 'pdfkit/output';
155
165
 
156
- // create a document the same way as above
166
+ registerStdFonts(Helvetica, HelveticaBold);
157
167
  const doc = new PDFDocument();
168
+ const output = toBlob(doc);
169
+
170
+ // Add your content to the document here, as usual.
171
+
172
+ doc.end();
173
+ const blob = await output;
174
+ const url = URL.createObjectURL(blob);
175
+ iframe.src = url;
176
+
177
+ // Revoke the URL when the iframe no longer needs the PDF.
178
+ // URL.revokeObjectURL(url);
179
+ ```
158
180
 
159
- // pipe the document to a blob
160
- const stream = doc.pipe(blobStream());
181
+ Use `toBytes` instead when a binary API, worker or parser needs one contiguous
182
+ `Uint8Array`:
161
183
 
162
- // add your content to the document here, as usual
184
+ ```javascript
185
+ import { toBytes } from 'pdfkit/output';
163
186
 
164
- // get a blob when you are done
187
+ const output = toBytes(doc);
165
188
  doc.end();
166
- stream.on('finish', function() {
167
- // get a blob you can do whatever you like with
168
- const blob = stream.toBlob('application/pdf');
189
+ const bytes = await output;
190
+ ```
191
+
192
+ The stable, dependency-free alternative is to collect the document's
193
+ `Uint8Array` chunks using its events and construct the Blob directly:
194
+
195
+ ```javascript
196
+ const chunks = [];
169
197
 
170
- // or get a blob URL for display in the browser
171
- const url = stream.toBlobURL('application/pdf');
198
+ doc.on('data', chunk => chunks.push(chunk));
199
+ doc.on('end', () => {
200
+ const blob = new Blob(chunks, { type: 'application/pdf' });
201
+ const url = URL.createObjectURL(blob);
172
202
  iframe.src = url;
173
203
  });
204
+
205
+ // Add your content to the document here, as usual.
206
+ doc.end();
174
207
  ```
175
208
 
176
209
  You can see an interactive in-browser demo of PDFKit [here](http://pdfkit.org/demo/browser.html).
177
210
 
178
- Note that in order to Browserify a project using PDFKit, you need to install the `brfs` module,
179
- which is used to load built-in font data into the package. It is listed as a `devDependency` in
180
- PDFKit's `package.json`, so it isn't installed by default for Node users.
181
- If you forget to install it, Browserify will print an error message.
211
+ The browser build has no access to the file system. You can register a
212
+ `Uint8Array` under a path before passing that exact path to `registerFont`,
213
+ `image` or `file`:
214
+
215
+ ```javascript
216
+ import { PDFDocument, registerFile } from 'pdfkit';
217
+
218
+ const response = await fetch('/fonts/Roboto-Regular.ttf');
219
+ const fontData = new Uint8Array(await response.arrayBuffer());
220
+
221
+ registerFile('fonts/Roboto-Regular.ttf', fontData);
222
+
223
+ const doc = new PDFDocument();
224
+
225
+ // register an alias for the font path
226
+ doc.registerFont('Roboto', 'fonts/Roboto-Regular.ttf');
227
+ // or use the path directly
228
+ doc.font('fonts/Roboto-Regular.ttf');
229
+
230
+ // Optionally unregister the path when it is no longer needed.
231
+ registerFile('fonts/Roboto-Regular.ttf', undefined);
232
+ ```
233
+
234
+ Registration is global to the loaded PDFKit module. Registering the same path
235
+ again replaces its data. In Node, registered data takes precedence over a file at
236
+ the same path; unregistering it restores normal file system lookup. Both APIs
237
+ are available as named exports from the CommonJS entry point:
238
+
239
+ ```javascript
240
+ const { PDFDocument, registerFile } = require('pdfkit');
241
+
242
+ registerFile('files/example.txt', new Uint8Array([1, 2, 3]), {
243
+ birthtime: new Date('2020-01-02T03:04:05Z'),
244
+ ctime: new Date('2021-02-03T04:05:06Z'),
245
+ });
246
+ registerFile('files/example.txt', undefined);
247
+ ```
248
+
249
+ `registerFile` accepts only a `Uint8Array` or `undefined`. You can still pass a
250
+ `Uint8Array` or `ArrayBuffer` directly to `registerFont`, `image` and `file`, and
251
+ you can pass a data URL directly to `image` and `file`. An unregistered file path
252
+ throws in the browser. The optional `birthtime` and `ctime` values must be valid
253
+ `Date` objects; either omitted value defaults to the time of registration.
182
254
 
183
255
  ## Documentation
184
256
 
package/js/output.cjs ADDED
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ const collect = document => new Promise((resolve, reject) => {
4
+ const chunks = [];
5
+ let length = 0;
6
+ const cleanup = () => {
7
+ document.off('data', onData);
8
+ document.off('end', onEnd);
9
+ document.off('error', onError);
10
+ };
11
+ const onData = chunk => {
12
+ chunks.push(chunk);
13
+ length += chunk.byteLength;
14
+ };
15
+ const onEnd = () => {
16
+ cleanup();
17
+ resolve({
18
+ chunks,
19
+ length
20
+ });
21
+ };
22
+ const onError = error => {
23
+ cleanup();
24
+ reject(error);
25
+ };
26
+ document.on('data', onData);
27
+ document.on('end', onEnd);
28
+ document.on('error', onError);
29
+ });
30
+ async function toBlob(document) {
31
+ const {
32
+ chunks
33
+ } = await collect(document);
34
+ return new Blob(chunks, {
35
+ type: 'application/pdf'
36
+ });
37
+ }
38
+ async function toBytes(document) {
39
+ const {
40
+ chunks,
41
+ length
42
+ } = await collect(document);
43
+ const bytes = new Uint8Array(length);
44
+ let offset = 0;
45
+ for (const chunk of chunks) {
46
+ bytes.set(chunk, offset);
47
+ offset += chunk.byteLength;
48
+ }
49
+ return bytes;
50
+ }
51
+
52
+ exports.toBlob = toBlob;
53
+ exports.toBytes = toBytes;
package/js/output.mjs ADDED
@@ -0,0 +1,50 @@
1
+ const collect = document => new Promise((resolve, reject) => {
2
+ const chunks = [];
3
+ let length = 0;
4
+ const cleanup = () => {
5
+ document.off('data', onData);
6
+ document.off('end', onEnd);
7
+ document.off('error', onError);
8
+ };
9
+ const onData = chunk => {
10
+ chunks.push(chunk);
11
+ length += chunk.byteLength;
12
+ };
13
+ const onEnd = () => {
14
+ cleanup();
15
+ resolve({
16
+ chunks,
17
+ length
18
+ });
19
+ };
20
+ const onError = error => {
21
+ cleanup();
22
+ reject(error);
23
+ };
24
+ document.on('data', onData);
25
+ document.on('end', onEnd);
26
+ document.on('error', onError);
27
+ });
28
+ async function toBlob(document) {
29
+ const {
30
+ chunks
31
+ } = await collect(document);
32
+ return new Blob(chunks, {
33
+ type: 'application/pdf'
34
+ });
35
+ }
36
+ async function toBytes(document) {
37
+ const {
38
+ chunks,
39
+ length
40
+ } = await collect(document);
41
+ const bytes = new Uint8Array(length);
42
+ let offset = 0;
43
+ for (const chunk of chunks) {
44
+ bytes.set(chunk, offset);
45
+ offset += chunk.byteLength;
46
+ }
47
+ return bytes;
48
+ }
49
+
50
+ export { toBlob, toBytes };