pdf-codec 5.3.32 → 5.5.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 +21 -1
- package/dist/annotations.d.cts +1 -1
- package/dist/annotations.d.ts +1 -1
- package/dist/attachments.d.cts +1 -1
- package/dist/attachments.d.ts +1 -1
- package/dist/cmap.d.cts +1 -1
- package/dist/cmap.d.ts +1 -1
- package/dist/codec.d.cts +10 -0
- package/dist/codec.d.ts +10 -0
- package/dist/content-read.d.cts +1 -1
- package/dist/content-read.d.ts +1 -1
- package/dist/diagnostics-BkG_Ej2x.d.cts +32 -0
- package/dist/diagnostics-BkG_Ej2x.d.ts +32 -0
- package/dist/diagnostics.cjs +17 -0
- package/dist/diagnostics.d.cts +2 -2
- package/dist/diagnostics.d.ts +2 -2
- package/dist/diagnostics.js +17 -1
- package/dist/document.d.cts +1 -1
- package/dist/document.d.ts +1 -1
- package/dist/encrypt.d.cts +1 -1
- package/dist/encrypt.d.ts +1 -1
- package/dist/filters.d.cts +1 -1
- package/dist/filters.d.ts +1 -1
- package/dist/font-read.d.cts +1 -1
- package/dist/font-read.d.ts +1 -1
- package/dist/form.d.cts +1 -1
- package/dist/form.d.ts +1 -1
- package/dist/images-read.d.cts +1 -1
- package/dist/images-read.d.ts +1 -1
- package/dist/index.cjs +6 -0
- package/dist/index.d.cts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +5 -4
- package/dist/interpret-types.d.cts +1 -1
- package/dist/interpret-types.d.ts +1 -1
- package/dist/layout.cjs +27 -2
- package/dist/layout.d.cts +31 -1
- package/dist/layout.d.ts +31 -1
- package/dist/layout.js +26 -3
- package/dist/names.d.cts +1 -1
- package/dist/names.d.ts +1 -1
- package/dist/navigation.d.cts +1 -1
- package/dist/navigation.d.ts +1 -1
- package/dist/optional-content.d.cts +1 -1
- package/dist/optional-content.d.ts +1 -1
- package/dist/parse.d.cts +1 -1
- package/dist/parse.d.ts +1 -1
- package/dist/predictors.d.cts +1 -1
- package/dist/predictors.d.ts +1 -1
- package/dist/raster-text.d.cts +1 -1
- package/dist/raster-text.d.ts +1 -1
- package/dist/raster.d.cts +1 -1
- package/dist/raster.d.ts +1 -1
- package/dist/read-convert.d.cts +1 -1
- package/dist/read-convert.d.ts +1 -1
- package/dist/read.cjs +44 -17
- package/dist/read.d.cts +32 -3
- package/dist/read.d.ts +32 -3
- package/dist/read.js +45 -18
- package/dist/structure.cjs +13 -3
- package/dist/structure.d.cts +11 -2
- package/dist/structure.d.ts +11 -2
- package/dist/structure.js +13 -3
- package/dist/write-skipped-surfaces.cjs +16 -0
- package/dist/write-skipped-surfaces.d.cts +12 -0
- package/dist/write-skipped-surfaces.d.ts +12 -0
- package/dist/write-skipped-surfaces.js +15 -0
- package/dist/write.cjs +17 -0
- package/dist/write.d.cts +9 -1
- package/dist/write.d.ts +9 -1
- package/dist/write.js +17 -1
- package/dist/xref.d.cts +1 -1
- package/dist/xref.d.ts +1 -1
- package/package.json +1 -1
- package/dist/diagnostics-C9FxfZ6H.d.cts +0 -22
- package/dist/diagnostics-C9FxfZ6H.d.ts +0 -22
package/dist/read.js
CHANGED
|
@@ -11,14 +11,30 @@ import { readAcroForm } from "./form.js";
|
|
|
11
11
|
import { readXmpMetadata } from "./xmp.js";
|
|
12
12
|
import { readOptionalContent } from "./optional-content.js";
|
|
13
13
|
import { readStructure } from "./structure.js";
|
|
14
|
-
import "./layout.js";
|
|
15
|
-
import { NOOP_DIAGNOSTIC_SINK, PdfParseError } from "./diagnostics.js";
|
|
14
|
+
import { LayoutDocumentSurfaceSchema } from "./layout.js";
|
|
15
|
+
import { NOOP_DIAGNOSTIC_SINK, PdfPageSelectionError, PdfParseError } from "./diagnostics.js";
|
|
16
16
|
import { openPdfDocument } from "./document.js";
|
|
17
17
|
import { throwIfAborted } from "./util/abort.js";
|
|
18
18
|
import { createFontResolver } from "./font-read.js";
|
|
19
19
|
import { t as interpretContentStream } from "./interpret---0lZPlv.js";
|
|
20
20
|
import { createDestinationRegistry, readOutline } from "./navigation.js";
|
|
21
21
|
//#region src/read.ts
|
|
22
|
+
function skippedSurfacesOf(surfaces) {
|
|
23
|
+
return LayoutDocumentSurfaceSchema.options.filter((surface) => surfaces?.[surface] === false);
|
|
24
|
+
}
|
|
25
|
+
const UNREAD_DESTINATION_REGISTRY = {
|
|
26
|
+
entries: [],
|
|
27
|
+
intern: () => void 0
|
|
28
|
+
};
|
|
29
|
+
function resolvePageSelection(selection, pageCount) {
|
|
30
|
+
const resolved = /* @__PURE__ */ new Set();
|
|
31
|
+
for (const requested of selection) {
|
|
32
|
+
const index = requested < 0 ? pageCount + requested : requested;
|
|
33
|
+
if (!Number.isInteger(requested) || index < 0 || index >= pageCount) throw new PdfPageSelectionError(requested, pageCount);
|
|
34
|
+
resolved.add(index);
|
|
35
|
+
}
|
|
36
|
+
return resolved;
|
|
37
|
+
}
|
|
22
38
|
const PDF_HEADER_BYTES = new TextEncoder().encode("%PDF-");
|
|
23
39
|
const HEADER_SEARCH_WINDOW = 1024;
|
|
24
40
|
const DEFAULT_PAGE_WIDTH_PT = 612;
|
|
@@ -44,21 +60,28 @@ function readPdf(bytes, options) {
|
|
|
44
60
|
const images = {};
|
|
45
61
|
const imageIdCache = /* @__PURE__ */ new Map();
|
|
46
62
|
const pageDicts = doc.pages();
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
const
|
|
53
|
-
const
|
|
63
|
+
const selection = options?.pages === void 0 ? void 0 : resolvePageSelection(options.pages, pageDicts.length);
|
|
64
|
+
const skippedSurfaces = skippedSurfacesOf(options?.surfaces);
|
|
65
|
+
const reads = (surface) => !skippedSurfaces.includes(surface);
|
|
66
|
+
const destinationRegistry = reads("destinations") ? createDestinationRegistry(doc.catalog, doc, (obj) => doc.pageIndex(obj), sink) : UNREAD_DESTINATION_REGISTRY;
|
|
67
|
+
const outline = reads("outline") ? readOutline(doc.catalog, destinationRegistry, doc, sink) : void 0;
|
|
68
|
+
const attachments = selection === void 0 ? readAttachments(doc.catalog, pageDicts, doc, sink) : [];
|
|
69
|
+
const optionalContent = reads("layers") ? readOptionalContent(doc.catalog, doc, sink) : void 0;
|
|
70
|
+
const form = reads("form") ? readAcroForm(doc.catalog, doc, (obj) => doc.pageIndex(obj), sink) : void 0;
|
|
71
|
+
const structure = reads("structure") ? readStructure(doc.catalog, pageDicts, (index) => selection === void 0 || selection.has(index), doc, sink) : void 0;
|
|
72
|
+
const source = reads("residue") ? readDocumentResidue(doc, sink) : void 0;
|
|
54
73
|
const pageBoxRows = [];
|
|
55
|
-
const pages = pageDicts.
|
|
74
|
+
const pages = pageDicts.flatMap((pageDict, index) => {
|
|
75
|
+
if (selection !== void 0 && !selection.has(index)) return [];
|
|
56
76
|
throwIfAborted(signal);
|
|
57
|
-
const read = readPage(index, pageDict, doc, fontResolver, images, imageIdCache, destinationRegistry, optionalContent
|
|
77
|
+
const read = readPage(index, pageDict, doc, fontResolver, images, imageIdCache, destinationRegistry, optionalContent?.layerNameOf ?? noLayerName, structure?.ownerOf ?? noStructureOwner, sink);
|
|
58
78
|
if (read.boxRow !== void 0) pageBoxRows.push(read.boxRow);
|
|
59
|
-
return read.page
|
|
79
|
+
return [selection === void 0 ? read.page : {
|
|
80
|
+
...read.page,
|
|
81
|
+
index
|
|
82
|
+
}];
|
|
60
83
|
});
|
|
61
|
-
if (pageBoxRows.length > 0) source["page-boxes"] = {
|
|
84
|
+
if (source !== void 0 && pageBoxRows.length > 0) source["page-boxes"] = {
|
|
62
85
|
format: "pdf",
|
|
63
86
|
xml: serializeObjectToText(pdfArray(pageBoxRows))
|
|
64
87
|
};
|
|
@@ -66,16 +89,20 @@ function readPdf(bytes, options) {
|
|
|
66
89
|
formatVersion: 1,
|
|
67
90
|
metadata: readMetadata(doc.trailer, doc, doc.catalog, sink),
|
|
68
91
|
pages,
|
|
92
|
+
...selection !== void 0 ? { pageCount: pageDicts.length } : {},
|
|
69
93
|
images,
|
|
70
94
|
...destinationRegistry.entries.length > 0 ? { destinations: [...destinationRegistry.entries] } : {},
|
|
71
|
-
...outline.length > 0 ? { outline } : {},
|
|
95
|
+
...outline !== void 0 && outline.length > 0 ? { outline } : {},
|
|
72
96
|
...attachments.length > 0 ? { attachments } : {},
|
|
73
|
-
...optionalContent.layers.length > 0 ? { layers: [...optionalContent.layers] } : {},
|
|
74
|
-
...form.length > 0 ? { form } : {},
|
|
75
|
-
...structure.tree.length > 0 ? { structure: [...structure.tree] } : {},
|
|
76
|
-
...Object.keys(source).length > 0 ? { source } : {}
|
|
97
|
+
...optionalContent !== void 0 && optionalContent.layers.length > 0 ? { layers: [...optionalContent.layers] } : {},
|
|
98
|
+
...form !== void 0 && form.length > 0 ? { form } : {},
|
|
99
|
+
...structure !== void 0 && structure.tree.length > 0 ? { structure: [...structure.tree] } : {},
|
|
100
|
+
...source !== void 0 && Object.keys(source).length > 0 ? { source } : {},
|
|
101
|
+
...skippedSurfaces.length > 0 ? { skippedSurfaces } : {}
|
|
77
102
|
};
|
|
78
103
|
}
|
|
104
|
+
function noLayerName() {}
|
|
105
|
+
function noStructureOwner() {}
|
|
79
106
|
function readDeclaredPageBox(page, key) {
|
|
80
107
|
const arr = asArray(dictGet(page, key));
|
|
81
108
|
if (arr === void 0) return;
|
package/dist/structure.cjs
CHANGED
|
@@ -6,7 +6,16 @@ const NEVER_DICT = {
|
|
|
6
6
|
kind: "dict",
|
|
7
7
|
entries: /* @__PURE__ */ new Map()
|
|
8
8
|
};
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Reads the tagged-structure element tree and the (page, MCID) ownership map.
|
|
11
|
+
* @param catalog - the document catalog
|
|
12
|
+
* @param pages - every page of the page tree, in document order, so a position is a source page index
|
|
13
|
+
* @param includePage - whether the page at a source index is being read: ownership entries and parent-tree diagnostics are built for those pages only, while the element tree itself is document-level and always read whole
|
|
14
|
+
* @param resolver - the object resolver
|
|
15
|
+
* @param sink - where recoverable parse diagnostics go
|
|
16
|
+
* @returns the element tree and an owner lookup answering for included pages
|
|
17
|
+
*/
|
|
18
|
+
function readStructure(catalog, pages, includePage, resolver, sink) {
|
|
10
19
|
const root = resolver.resolveDict(require_objects.dictGet(catalog, "StructTreeRoot"));
|
|
11
20
|
if (root === void 0) return {
|
|
12
21
|
tree: [],
|
|
@@ -68,7 +77,7 @@ function readStructure(catalog, pages, resolver, sink) {
|
|
|
68
77
|
}
|
|
69
78
|
return {
|
|
70
79
|
tree,
|
|
71
|
-
ownerOf: parentTreeOwners(root, pages, resolver, idByDict, sink)
|
|
80
|
+
ownerOf: parentTreeOwners(root, pages, includePage, resolver, idByDict, sink)
|
|
72
81
|
};
|
|
73
82
|
}
|
|
74
83
|
const ATTRIBUTE_KEYS = [
|
|
@@ -146,7 +155,7 @@ function numberTreeEntries(node, resolver, visited) {
|
|
|
146
155
|
}
|
|
147
156
|
return entries;
|
|
148
157
|
}
|
|
149
|
-
function parentTreeOwners(root, pages, resolver, idByDict, sink) {
|
|
158
|
+
function parentTreeOwners(root, pages, includePage, resolver, idByDict, sink) {
|
|
150
159
|
const documentTree = resolver.resolveDict(require_objects.dictGet(root, "ParentTree"));
|
|
151
160
|
if (documentTree === void 0) {
|
|
152
161
|
if (require_objects.dictGet(root, "ParentTree") !== void 0) sink({
|
|
@@ -159,6 +168,7 @@ function parentTreeOwners(root, pages, resolver, idByDict, sink) {
|
|
|
159
168
|
const entries = new Map(numberTreeEntries(documentTree, resolver, /* @__PURE__ */ new Set()));
|
|
160
169
|
const owners = /* @__PURE__ */ new Map();
|
|
161
170
|
pages.forEach((page, pageIndex) => {
|
|
171
|
+
if (!includePage(pageIndex)) return;
|
|
162
172
|
const structParents = require_objects.asNumber(require_objects.dictGet(page, "StructParents"));
|
|
163
173
|
if (structParents === void 0) return;
|
|
164
174
|
const entry = entries.get(structParents);
|
package/dist/structure.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as PdfDiagnosticSink } from "./diagnostics-
|
|
1
|
+
import { i as PdfDiagnosticSink } from "./diagnostics-BkG_Ej2x.cjs";
|
|
2
2
|
import { n as PdfDict } from "./objects-e2RS3-yT.cjs";
|
|
3
3
|
import { PdfObjectResolver } from "./interpret-types.cjs";
|
|
4
4
|
import { LayoutStructureElement } from "./layout.cjs";
|
|
@@ -7,6 +7,15 @@ interface StructureContext {
|
|
|
7
7
|
readonly tree: readonly LayoutStructureElement[];
|
|
8
8
|
readonly ownerOf: (pageIndex: number, mcid: number) => string | undefined;
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Reads the tagged-structure element tree and the (page, MCID) ownership map.
|
|
12
|
+
* @param catalog - the document catalog
|
|
13
|
+
* @param pages - every page of the page tree, in document order, so a position is a source page index
|
|
14
|
+
* @param includePage - whether the page at a source index is being read: ownership entries and parent-tree diagnostics are built for those pages only, while the element tree itself is document-level and always read whole
|
|
15
|
+
* @param resolver - the object resolver
|
|
16
|
+
* @param sink - where recoverable parse diagnostics go
|
|
17
|
+
* @returns the element tree and an owner lookup answering for included pages
|
|
18
|
+
*/
|
|
19
|
+
declare function readStructure(catalog: PdfDict, pages: readonly PdfDict[], includePage: (pageIndex: number) => boolean, resolver: Readonly<PdfObjectResolver>, sink: PdfDiagnosticSink): StructureContext;
|
|
11
20
|
//#endregion
|
|
12
21
|
export { StructureContext, readStructure };
|
package/dist/structure.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as PdfDiagnosticSink } from "./diagnostics-
|
|
1
|
+
import { i as PdfDiagnosticSink } from "./diagnostics-BkG_Ej2x.js";
|
|
2
2
|
import { n as PdfDict } from "./objects-e2RS3-yT.js";
|
|
3
3
|
import { PdfObjectResolver } from "./interpret-types.js";
|
|
4
4
|
import { LayoutStructureElement } from "./layout.js";
|
|
@@ -7,6 +7,15 @@ interface StructureContext {
|
|
|
7
7
|
readonly tree: readonly LayoutStructureElement[];
|
|
8
8
|
readonly ownerOf: (pageIndex: number, mcid: number) => string | undefined;
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Reads the tagged-structure element tree and the (page, MCID) ownership map.
|
|
12
|
+
* @param catalog - the document catalog
|
|
13
|
+
* @param pages - every page of the page tree, in document order, so a position is a source page index
|
|
14
|
+
* @param includePage - whether the page at a source index is being read: ownership entries and parent-tree diagnostics are built for those pages only, while the element tree itself is document-level and always read whole
|
|
15
|
+
* @param resolver - the object resolver
|
|
16
|
+
* @param sink - where recoverable parse diagnostics go
|
|
17
|
+
* @returns the element tree and an owner lookup answering for included pages
|
|
18
|
+
*/
|
|
19
|
+
declare function readStructure(catalog: PdfDict, pages: readonly PdfDict[], includePage: (pageIndex: number) => boolean, resolver: Readonly<PdfObjectResolver>, sink: PdfDiagnosticSink): StructureContext;
|
|
11
20
|
//#endregion
|
|
12
21
|
export { StructureContext, readStructure };
|
package/dist/structure.js
CHANGED
|
@@ -5,7 +5,16 @@ const NEVER_DICT = {
|
|
|
5
5
|
kind: "dict",
|
|
6
6
|
entries: /* @__PURE__ */ new Map()
|
|
7
7
|
};
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Reads the tagged-structure element tree and the (page, MCID) ownership map.
|
|
10
|
+
* @param catalog - the document catalog
|
|
11
|
+
* @param pages - every page of the page tree, in document order, so a position is a source page index
|
|
12
|
+
* @param includePage - whether the page at a source index is being read: ownership entries and parent-tree diagnostics are built for those pages only, while the element tree itself is document-level and always read whole
|
|
13
|
+
* @param resolver - the object resolver
|
|
14
|
+
* @param sink - where recoverable parse diagnostics go
|
|
15
|
+
* @returns the element tree and an owner lookup answering for included pages
|
|
16
|
+
*/
|
|
17
|
+
function readStructure(catalog, pages, includePage, resolver, sink) {
|
|
9
18
|
const root = resolver.resolveDict(dictGet(catalog, "StructTreeRoot"));
|
|
10
19
|
if (root === void 0) return {
|
|
11
20
|
tree: [],
|
|
@@ -67,7 +76,7 @@ function readStructure(catalog, pages, resolver, sink) {
|
|
|
67
76
|
}
|
|
68
77
|
return {
|
|
69
78
|
tree,
|
|
70
|
-
ownerOf: parentTreeOwners(root, pages, resolver, idByDict, sink)
|
|
79
|
+
ownerOf: parentTreeOwners(root, pages, includePage, resolver, idByDict, sink)
|
|
71
80
|
};
|
|
72
81
|
}
|
|
73
82
|
const ATTRIBUTE_KEYS = [
|
|
@@ -145,7 +154,7 @@ function numberTreeEntries(node, resolver, visited) {
|
|
|
145
154
|
}
|
|
146
155
|
return entries;
|
|
147
156
|
}
|
|
148
|
-
function parentTreeOwners(root, pages, resolver, idByDict, sink) {
|
|
157
|
+
function parentTreeOwners(root, pages, includePage, resolver, idByDict, sink) {
|
|
149
158
|
const documentTree = resolver.resolveDict(dictGet(root, "ParentTree"));
|
|
150
159
|
if (documentTree === void 0) {
|
|
151
160
|
if (dictGet(root, "ParentTree") !== void 0) sink({
|
|
@@ -158,6 +167,7 @@ function parentTreeOwners(root, pages, resolver, idByDict, sink) {
|
|
|
158
167
|
const entries = new Map(numberTreeEntries(documentTree, resolver, /* @__PURE__ */ new Set()));
|
|
159
168
|
const owners = /* @__PURE__ */ new Map();
|
|
160
169
|
pages.forEach((page, pageIndex) => {
|
|
170
|
+
if (!includePage(pageIndex)) return;
|
|
161
171
|
const structParents = asNumber(dictGet(page, "StructParents"));
|
|
162
172
|
if (structParents === void 0) return;
|
|
163
173
|
const entry = entries.get(structParents);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/write-skipped-surfaces.ts
|
|
3
|
+
/**
|
|
4
|
+
* writePdf was handed a LayoutDocument from a read that left document-level surfaces out (one carrying `skippedSurfaces`, which only `ReadPdfOptions.surfaces` sets). Such a document is a reading with parts of the file deliberately unread, not a document to re-emit: each skipped surface's field is absent whatever the source holds, and its page items lack what depended on it (internal links without destinations, structure owners without the structure tree, layer names without layers), so writing it would silently drop the source's outline, form, layers, structure or residue. Thrown rather than writing a file that looks complete. Checked after `PdfPartialDocumentError`, so a page-selected read that also skipped surfaces reports the page selection.
|
|
5
|
+
*/
|
|
6
|
+
var PdfSkippedSurfacesError = class extends Error {
|
|
7
|
+
/** The surfaces the read skipped, as the document's `skippedSurfaces` lists them. */
|
|
8
|
+
skippedSurfaces;
|
|
9
|
+
constructor(skippedSurfaces) {
|
|
10
|
+
super(`this LayoutDocument was read with ${skippedSurfaces.join(", ")} left out (it carries skippedSurfaces, which only a read with ReadPdfOptions.surfaces sets); writing it would drop whatever the source holds there, so it cannot be written as a document of its own`);
|
|
11
|
+
this.name = "PdfSkippedSurfacesError";
|
|
12
|
+
this.skippedSurfaces = skippedSurfaces;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
exports.PdfSkippedSurfacesError = PdfSkippedSurfacesError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { LayoutDocumentSurface } from "./layout.cjs";
|
|
2
|
+
//#region src/write-skipped-surfaces.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* writePdf was handed a LayoutDocument from a read that left document-level surfaces out (one carrying `skippedSurfaces`, which only `ReadPdfOptions.surfaces` sets). Such a document is a reading with parts of the file deliberately unread, not a document to re-emit: each skipped surface's field is absent whatever the source holds, and its page items lack what depended on it (internal links without destinations, structure owners without the structure tree, layer names without layers), so writing it would silently drop the source's outline, form, layers, structure or residue. Thrown rather than writing a file that looks complete. Checked after `PdfPartialDocumentError`, so a page-selected read that also skipped surfaces reports the page selection.
|
|
5
|
+
*/
|
|
6
|
+
declare class PdfSkippedSurfacesError extends Error {
|
|
7
|
+
/** The surfaces the read skipped, as the document's `skippedSurfaces` lists them. */
|
|
8
|
+
readonly skippedSurfaces: readonly LayoutDocumentSurface[];
|
|
9
|
+
constructor(skippedSurfaces: readonly LayoutDocumentSurface[]);
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { PdfSkippedSurfacesError };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { LayoutDocumentSurface } from "./layout.js";
|
|
2
|
+
//#region src/write-skipped-surfaces.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* writePdf was handed a LayoutDocument from a read that left document-level surfaces out (one carrying `skippedSurfaces`, which only `ReadPdfOptions.surfaces` sets). Such a document is a reading with parts of the file deliberately unread, not a document to re-emit: each skipped surface's field is absent whatever the source holds, and its page items lack what depended on it (internal links without destinations, structure owners without the structure tree, layer names without layers), so writing it would silently drop the source's outline, form, layers, structure or residue. Thrown rather than writing a file that looks complete. Checked after `PdfPartialDocumentError`, so a page-selected read that also skipped surfaces reports the page selection.
|
|
5
|
+
*/
|
|
6
|
+
declare class PdfSkippedSurfacesError extends Error {
|
|
7
|
+
/** The surfaces the read skipped, as the document's `skippedSurfaces` lists them. */
|
|
8
|
+
readonly skippedSurfaces: readonly LayoutDocumentSurface[];
|
|
9
|
+
constructor(skippedSurfaces: readonly LayoutDocumentSurface[]);
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { PdfSkippedSurfacesError };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/write-skipped-surfaces.ts
|
|
2
|
+
/**
|
|
3
|
+
* writePdf was handed a LayoutDocument from a read that left document-level surfaces out (one carrying `skippedSurfaces`, which only `ReadPdfOptions.surfaces` sets). Such a document is a reading with parts of the file deliberately unread, not a document to re-emit: each skipped surface's field is absent whatever the source holds, and its page items lack what depended on it (internal links without destinations, structure owners without the structure tree, layer names without layers), so writing it would silently drop the source's outline, form, layers, structure or residue. Thrown rather than writing a file that looks complete. Checked after `PdfPartialDocumentError`, so a page-selected read that also skipped surfaces reports the page selection.
|
|
4
|
+
*/
|
|
5
|
+
var PdfSkippedSurfacesError = class extends Error {
|
|
6
|
+
/** The surfaces the read skipped, as the document's `skippedSurfaces` lists them. */
|
|
7
|
+
skippedSurfaces;
|
|
8
|
+
constructor(skippedSurfaces) {
|
|
9
|
+
super(`this LayoutDocument was read with ${skippedSurfaces.join(", ")} left out (it carries skippedSurfaces, which only a read with ReadPdfOptions.surfaces sets); writing it would drop whatever the source holds there, so it cannot be written as a document of its own`);
|
|
10
|
+
this.name = "PdfSkippedSurfacesError";
|
|
11
|
+
this.skippedSurfaces = skippedSurfaces;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { PdfSkippedSurfacesError };
|
package/dist/write.cjs
CHANGED
|
@@ -15,6 +15,7 @@ const require_font_registry = require("./font-registry.cjs");
|
|
|
15
15
|
const require_measure = require("./measure.cjs");
|
|
16
16
|
const require_write_page_emit = require("./write-page-emit.cjs");
|
|
17
17
|
const require_write_outline = require("./write-outline.cjs");
|
|
18
|
+
const require_write_skipped_surfaces = require("./write-skipped-surfaces.cjs");
|
|
18
19
|
let byte_codec = require("byte-codec");
|
|
19
20
|
//#region src/write.ts
|
|
20
21
|
const MATH_FONT_RESOURCE_NAME = "MF";
|
|
@@ -69,7 +70,22 @@ function buildFontObjects(standardName, descriptorRef) {
|
|
|
69
70
|
function codePointsOf(texts) {
|
|
70
71
|
return new Set(texts.flatMap((text) => [...text].map((character) => character.codePointAt(0))));
|
|
71
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* writePdf was handed a LayoutDocument from a page-selected read (one carrying `pageCount`). Such a document is a reading of part of a file, not a document to re-emit: its pages are a selection while its destinations, form widgets and outline still name source page indices, and the writer places pages by position, so writing it would point those references at the wrong pages or past the end. Thrown rather than writing a file whose links silently lie.
|
|
75
|
+
*/
|
|
76
|
+
var PdfPartialDocumentError = class extends Error {
|
|
77
|
+
pageCount;
|
|
78
|
+
selectedPageCount;
|
|
79
|
+
constructor(pageCount, selectedPageCount) {
|
|
80
|
+
super(`this LayoutDocument holds ${String(selectedPageCount)} pages selected from a ${String(pageCount)}-page file (it carries pageCount, which only a page-selected read sets); its page references name pages of the source file, so it cannot be written as a document of its own`);
|
|
81
|
+
this.name = "PdfPartialDocumentError";
|
|
82
|
+
this.pageCount = pageCount;
|
|
83
|
+
this.selectedPageCount = selectedPageCount;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
72
86
|
function writePdf(doc, options = {}) {
|
|
87
|
+
if (doc.pageCount !== void 0) throw new PdfPartialDocumentError(doc.pageCount, doc.pages.length);
|
|
88
|
+
if (doc.skippedSurfaces !== void 0) throw new require_write_skipped_surfaces.PdfSkippedSurfacesError(doc.skippedSurfaces);
|
|
73
89
|
const compress = options.compress ?? true;
|
|
74
90
|
const registry = options.fonts;
|
|
75
91
|
const measurer = require_measure.createFontMeasurer(registry);
|
|
@@ -478,4 +494,5 @@ function writePdf(doc, options = {}) {
|
|
|
478
494
|
return writer.toBytes();
|
|
479
495
|
}
|
|
480
496
|
//#endregion
|
|
497
|
+
exports.PdfPartialDocumentError = PdfPartialDocumentError;
|
|
481
498
|
exports.writePdf = writePdf;
|
package/dist/write.d.cts
CHANGED
|
@@ -18,6 +18,14 @@ interface WritePdfOptions {
|
|
|
18
18
|
readonly formulas?: readonly PositionedFormula[];
|
|
19
19
|
readonly encryption?: PdfEncryptionOptions;
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* writePdf was handed a LayoutDocument from a page-selected read (one carrying `pageCount`). Such a document is a reading of part of a file, not a document to re-emit: its pages are a selection while its destinations, form widgets and outline still name source page indices, and the writer places pages by position, so writing it would point those references at the wrong pages or past the end. Thrown rather than writing a file whose links silently lie.
|
|
23
|
+
*/
|
|
24
|
+
declare class PdfPartialDocumentError extends Error {
|
|
25
|
+
readonly pageCount: number;
|
|
26
|
+
readonly selectedPageCount: number;
|
|
27
|
+
constructor(pageCount: number, selectedPageCount: number);
|
|
28
|
+
}
|
|
21
29
|
declare function writePdf(doc: LayoutDocument, options?: WritePdfOptions): Uint8Array<ArrayBuffer>;
|
|
22
30
|
//#endregion
|
|
23
|
-
export { WritePdfOptions, writePdf };
|
|
31
|
+
export { PdfPartialDocumentError, WritePdfOptions, writePdf };
|
package/dist/write.d.ts
CHANGED
|
@@ -18,6 +18,14 @@ interface WritePdfOptions {
|
|
|
18
18
|
readonly formulas?: readonly PositionedFormula[];
|
|
19
19
|
readonly encryption?: PdfEncryptionOptions;
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* writePdf was handed a LayoutDocument from a page-selected read (one carrying `pageCount`). Such a document is a reading of part of a file, not a document to re-emit: its pages are a selection while its destinations, form widgets and outline still name source page indices, and the writer places pages by position, so writing it would point those references at the wrong pages or past the end. Thrown rather than writing a file whose links silently lie.
|
|
23
|
+
*/
|
|
24
|
+
declare class PdfPartialDocumentError extends Error {
|
|
25
|
+
readonly pageCount: number;
|
|
26
|
+
readonly selectedPageCount: number;
|
|
27
|
+
constructor(pageCount: number, selectedPageCount: number);
|
|
28
|
+
}
|
|
21
29
|
declare function writePdf(doc: LayoutDocument, options?: WritePdfOptions): Uint8Array<ArrayBuffer>;
|
|
22
30
|
//#endregion
|
|
23
|
-
export { WritePdfOptions, writePdf };
|
|
31
|
+
export { PdfPartialDocumentError, WritePdfOptions, writePdf };
|
package/dist/write.js
CHANGED
|
@@ -14,6 +14,7 @@ import { resolveFaceWithRegistry } from "./font-registry.js";
|
|
|
14
14
|
import { createFontMeasurer } from "./measure.js";
|
|
15
15
|
import { emitPageObjects } from "./write-page-emit.js";
|
|
16
16
|
import { emitOutlineObjects } from "./write-outline.js";
|
|
17
|
+
import { PdfSkippedSurfacesError } from "./write-skipped-surfaces.js";
|
|
17
18
|
import { base64ToBytes } from "byte-codec";
|
|
18
19
|
//#region src/write.ts
|
|
19
20
|
const MATH_FONT_RESOURCE_NAME = "MF";
|
|
@@ -68,7 +69,22 @@ function buildFontObjects(standardName, descriptorRef) {
|
|
|
68
69
|
function codePointsOf(texts) {
|
|
69
70
|
return new Set(texts.flatMap((text) => [...text].map((character) => character.codePointAt(0))));
|
|
70
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* writePdf was handed a LayoutDocument from a page-selected read (one carrying `pageCount`). Such a document is a reading of part of a file, not a document to re-emit: its pages are a selection while its destinations, form widgets and outline still name source page indices, and the writer places pages by position, so writing it would point those references at the wrong pages or past the end. Thrown rather than writing a file whose links silently lie.
|
|
74
|
+
*/
|
|
75
|
+
var PdfPartialDocumentError = class extends Error {
|
|
76
|
+
pageCount;
|
|
77
|
+
selectedPageCount;
|
|
78
|
+
constructor(pageCount, selectedPageCount) {
|
|
79
|
+
super(`this LayoutDocument holds ${String(selectedPageCount)} pages selected from a ${String(pageCount)}-page file (it carries pageCount, which only a page-selected read sets); its page references name pages of the source file, so it cannot be written as a document of its own`);
|
|
80
|
+
this.name = "PdfPartialDocumentError";
|
|
81
|
+
this.pageCount = pageCount;
|
|
82
|
+
this.selectedPageCount = selectedPageCount;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
71
85
|
function writePdf(doc, options = {}) {
|
|
86
|
+
if (doc.pageCount !== void 0) throw new PdfPartialDocumentError(doc.pageCount, doc.pages.length);
|
|
87
|
+
if (doc.skippedSurfaces !== void 0) throw new PdfSkippedSurfacesError(doc.skippedSurfaces);
|
|
72
88
|
const compress = options.compress ?? true;
|
|
73
89
|
const registry = options.fonts;
|
|
74
90
|
const measurer = createFontMeasurer(registry);
|
|
@@ -477,4 +493,4 @@ function writePdf(doc, options = {}) {
|
|
|
477
493
|
return writer.toBytes();
|
|
478
494
|
}
|
|
479
495
|
//#endregion
|
|
480
|
-
export { writePdf };
|
|
496
|
+
export { PdfPartialDocumentError, writePdf };
|
package/dist/xref.d.cts
CHANGED
package/dist/xref.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdf-codec",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.0",
|
|
4
4
|
"description": "Hand-written, dependency-minimal PDF codec: parses arbitrary real-world PDFs and generates new ones, built on its own codec-owned LayoutDocument item model and Zod 4 codecs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
//#region src/diagnostics.d.ts
|
|
2
|
-
type PdfDiagnosticSeverity = "info" | "warning";
|
|
3
|
-
interface PdfDiagnostic {
|
|
4
|
-
readonly code: string;
|
|
5
|
-
readonly severity: PdfDiagnosticSeverity;
|
|
6
|
-
readonly message: string;
|
|
7
|
-
readonly pageIndex?: number;
|
|
8
|
-
}
|
|
9
|
-
type PdfDiagnosticSink = (diagnostic: PdfDiagnostic) => void;
|
|
10
|
-
declare const NOOP_DIAGNOSTIC_SINK: PdfDiagnosticSink;
|
|
11
|
-
declare class PdfParseError extends Error {
|
|
12
|
-
readonly code: string;
|
|
13
|
-
constructor(code: string, message: string);
|
|
14
|
-
}
|
|
15
|
-
declare class PdfEncryptedError extends PdfParseError {
|
|
16
|
-
constructor(message?: string);
|
|
17
|
-
}
|
|
18
|
-
declare class PdfPasswordRequiredError extends PdfParseError {
|
|
19
|
-
constructor(message?: string);
|
|
20
|
-
}
|
|
21
|
-
//#endregion
|
|
22
|
-
export { PdfEncryptedError as a, PdfDiagnosticSink as i, PdfDiagnostic as n, PdfParseError as o, PdfDiagnosticSeverity as r, PdfPasswordRequiredError as s, NOOP_DIAGNOSTIC_SINK as t };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
//#region src/diagnostics.d.ts
|
|
2
|
-
type PdfDiagnosticSeverity = "info" | "warning";
|
|
3
|
-
interface PdfDiagnostic {
|
|
4
|
-
readonly code: string;
|
|
5
|
-
readonly severity: PdfDiagnosticSeverity;
|
|
6
|
-
readonly message: string;
|
|
7
|
-
readonly pageIndex?: number;
|
|
8
|
-
}
|
|
9
|
-
type PdfDiagnosticSink = (diagnostic: PdfDiagnostic) => void;
|
|
10
|
-
declare const NOOP_DIAGNOSTIC_SINK: PdfDiagnosticSink;
|
|
11
|
-
declare class PdfParseError extends Error {
|
|
12
|
-
readonly code: string;
|
|
13
|
-
constructor(code: string, message: string);
|
|
14
|
-
}
|
|
15
|
-
declare class PdfEncryptedError extends PdfParseError {
|
|
16
|
-
constructor(message?: string);
|
|
17
|
-
}
|
|
18
|
-
declare class PdfPasswordRequiredError extends PdfParseError {
|
|
19
|
-
constructor(message?: string);
|
|
20
|
-
}
|
|
21
|
-
//#endregion
|
|
22
|
-
export { PdfEncryptedError as a, PdfDiagnosticSink as i, PdfDiagnostic as n, PdfParseError as o, PdfDiagnosticSeverity as r, PdfPasswordRequiredError as s, NOOP_DIAGNOSTIC_SINK as t };
|