pdf-codec 1.1.2 → 1.1.3
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 +4 -4
- package/dist/index.cjs +5 -5
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/ExaDev/pdf-codec) [](https://www.npmjs.com/package/pdf-codec) [](https://github.com/ExaDev/pdf-codec/releases/latest) [](https://github.com/ExaDev/pdf-codec/actions)
|
|
4
4
|
|
|
5
|
-
> A hand-written, dependency-minimal PDF codec: parses arbitrary real-world PDFs into a structured, positioned-content document and generates new PDFs from one, built on [`document-
|
|
5
|
+
> A hand-written, dependency-minimal PDF codec: parses arbitrary real-world PDFs into a structured, positioned-content document and generates new PDFs from one, built on [`document-schema.js`](https://github.com/ExaDev/document-schema.js)'s `LayoutDocument` pivot and [Zod 4](https://zod.dev) codecs.
|
|
6
6
|
|
|
7
7
|
`pdf-codec` is the PDF-reading-and-writing half of [`documents.js`](https://github.com/ExaDev/documents.js), extracted into its own package: every layer of the PDF format — the object model, the cross-reference table, the content-stream operators, standard-font metrics, the parser's cross-reference/object-stream resolution and content-stream interpreter — is hand-written against the ISO 32000-1 specification, with no external PDF library (`pdf-lib`, `pdfjs-dist`, `mupdf`, or any other) as a dependency. The one exception is [`fflate`](https://github.com/101arrowz/fflate) for raw DEFLATE/zlib compression underneath PDF's `FlateDecode` filter and PNG's `IDAT` chunks. The OpenType/CFF font parsing this package's own writer uses to embed a real math font (`sfnt.ts`/`math-*.ts`) is hand-written too, for the same "no supply-chain surface beyond what's already declared" reason — the one bundled binary asset is the vendored STIX Two Math font itself (OFL-1.1, see [Fidelity](#fidelity) and `assets/fonts/NOTICE.md`), not a library.
|
|
8
8
|
|
|
9
|
-
That is a genuinely large undertaking — this codec is comparable in size to a small application in its own right — and it comes with an honest trade-off spelled out in [Fidelity](#fidelity) below: this is not, and does not attempt to be, as robust against adversarial or badly malformed real-world PDFs as a library with 15+ years of hardening. What it buys instead is a dependency-free, fully auditable PDF implementation with no supply-chain surface beyond `document-
|
|
9
|
+
That is a genuinely large undertaking — this codec is comparable in size to a small application in its own right — and it comes with an honest trade-off spelled out in [Fidelity](#fidelity) below: this is not, and does not attempt to be, as robust against adversarial or badly malformed real-world PDFs as a library with 15+ years of hardening. What it buys instead is a dependency-free, fully auditable PDF implementation with no supply-chain surface beyond `document-schema.js`, `fflate`, and `zod`.
|
|
10
10
|
|
|
11
11
|
`documents.js` uses this package to convert docx/pptx/odt/odp/ods/odg to and from PDF, and to render MathML formulas (typeset by its own `src/mathml/` engine) through the embedded math font this package parses and writes. That MathML *layout* engine deliberately stays in `documents.js` — see [Architecture](#architecture) below for exactly where the boundary between the two packages sits and why a real `MathBox` value crosses it with zero cast or wrapper.
|
|
12
12
|
|
|
@@ -87,7 +87,7 @@ Dependency direction is strictly downward and checkable: `math-types`/`formula`/
|
|
|
87
87
|
|
|
88
88
|
## Conventions
|
|
89
89
|
|
|
90
|
-
- **Zod-first schema/type/guard**: `PdfBytesSchema`/`LayoutDocumentSchema` (the latter imported from `document-
|
|
90
|
+
- **Zod-first schema/type/guard**: `PdfBytesSchema`/`LayoutDocumentSchema` (the latter imported from `document-schema.js`) are the only two schemas this package validates against; every other model type (`PdfObject`, `MathBox` and friends) is plain TypeScript, never Zod-validated, for reasons specific to each — see the next two bullets.
|
|
91
91
|
- **`z.codec()` for the one schema-to-schema round trip this package owns**: `pdfCodec` (PDF bytes ⇄ `LayoutDocument`), wrapping the already-independently-tested `readPdf`/`writePdf` pair and adding automatic two-way schema validation. Deliberately the no-options form — `readPdf`/`writePdf` remain the primary entry points wherever a caller needs an `AbortSignal`, a `PdfDiagnosticSink`, or an `onSubstitution` callback, since `z.codec()`'s fixed `decode(input)`/`encode(output)` signature has no room for side-channel options.
|
|
92
92
|
- **`PdfObject` has no Zod schema at all**, deliberately: it never crosses a public boundary or round-trips through JSON, and is constructed exclusively by this package's own parser — validating it would just be validating our own output. It narrows natively on its own `kind` discriminant instead.
|
|
93
93
|
- **The `MathBox`/`MathFontMetrics` family is structurally typed on purpose, not validated by Zod either.** This is the mechanism that lets a caller (`documents.js`) hand this package a real value produced by a completely independent module, with zero cast, zero wrapper, and no shared class or branded type — see [Architecture](#architecture).
|
|
@@ -129,7 +129,7 @@ Commits follow Conventional Commits (`feat:`, `fix:`, `test:`, `chore:`, …), e
|
|
|
129
129
|
## References
|
|
130
130
|
|
|
131
131
|
- [documents.js](https://github.com/ExaDev/documents.js) — the package this codec was extracted from, and its principal downstream consumer: docx/pptx/odt/odp/ods/odg ⇄ PDF conversion, and MathML formula rendering (its own `src/mathml/` typesetting engine feeds a real `MathBox` into this package's `writePdf({ formulas })` with zero cast — see [Architecture](#architecture)).
|
|
132
|
-
- [document-
|
|
132
|
+
- [document-schema.js](https://github.com/ExaDev/document-schema.js) — the sibling package that owns `LayoutDocument` itself (the PDF-side pivot this codec reads into and writes from), and the canonical `ContentDocument` pivot the wider `documents.js`/`odf.js`/`ooxml.js` family shares.
|
|
133
133
|
- [STIX Two Math](https://github.com/stipub/stixfonts) — the embedded math font `math-font.ts` parses and `writePdf({ formulas })` renders through, vendored at `assets/fonts/STIXTwoMath-Regular.otf` and embedded into `dist/` as a base64 string (`src/assets/stix-two-math-font.ts`, generated by `scripts/generate-math-font-asset.mjs`) rather than read from disk at runtime. Copyright 2001-2021 The STIX Fonts Project Authors, licensed [OFL-1.1](assets/fonts/OFL.txt) — see `assets/fonts/NOTICE.md` for the exact source commit and version this was vendored from.
|
|
134
134
|
|
|
135
135
|
## License
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
let
|
|
2
|
+
let document_schema_js = require("document-schema.js");
|
|
3
3
|
let fflate = require("fflate");
|
|
4
4
|
let zod = require("zod");
|
|
5
5
|
//#region src/util/base64.ts
|
|
@@ -3432,8 +3432,8 @@ function interpretContentStream(bytes, resources, context) {
|
|
|
3432
3432
|
const items = [];
|
|
3433
3433
|
runContentStream(bytes, resources, {
|
|
3434
3434
|
ctm: IDENTITY_MATRIX,
|
|
3435
|
-
fillColor:
|
|
3436
|
-
strokeColor:
|
|
3435
|
+
fillColor: document_schema_js.COLOR_BLACK,
|
|
3436
|
+
strokeColor: document_schema_js.COLOR_BLACK,
|
|
3437
3437
|
lineWidth: DEFAULT_LINE_WIDTH_PT
|
|
3438
3438
|
}, context, items, 0);
|
|
3439
3439
|
return items;
|
|
@@ -5276,7 +5276,7 @@ function readPdf(bytes, options) {
|
|
|
5276
5276
|
return readPage(pageDict, resolver, fontResolver, images, imageIdCache, sink);
|
|
5277
5277
|
});
|
|
5278
5278
|
return {
|
|
5279
|
-
formatVersion:
|
|
5279
|
+
formatVersion: document_schema_js.LAYOUT_FORMAT_VERSION,
|
|
5280
5280
|
metadata: readMetadata(doc.trailer, resolver),
|
|
5281
5281
|
pages,
|
|
5282
5282
|
images
|
|
@@ -5666,7 +5666,7 @@ function containsBytesWithin(bytes, signature, window) {
|
|
|
5666
5666
|
return false;
|
|
5667
5667
|
}
|
|
5668
5668
|
const PdfBytesSchema = zod.z.instanceof(Uint8Array).refine((bytes) => containsBytesWithin(bytes, PDF_HEADER, PDF_HEADER_SEARCH_WINDOW), { message: "not a valid PDF file: missing the %PDF- header" });
|
|
5669
|
-
const pdfCodec = zod.z.codec(PdfBytesSchema,
|
|
5669
|
+
const pdfCodec = zod.z.codec(PdfBytesSchema, document_schema_js.LayoutDocumentSchema, {
|
|
5670
5670
|
decode: (bytes) => readPdf(bytes),
|
|
5671
5671
|
encode: (doc) => writePdf(doc)
|
|
5672
5672
|
});
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { COLOR_BLACK, LAYOUT_FORMAT_VERSION, LayoutDocumentSchema } from "document-
|
|
1
|
+
import { COLOR_BLACK, LAYOUT_FORMAT_VERSION, LayoutDocumentSchema } from "document-schema.js";
|
|
2
2
|
import { Unzlib, inflateSync, unzlibSync, zlibSync } from "fflate";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
//#region src/util/base64.ts
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdf-codec",
|
|
3
|
-
"version": "1.1.
|
|
4
|
-
"description": "Hand-written, dependency-minimal PDF codec: parses arbitrary real-world PDFs and generates new ones, built on document-
|
|
3
|
+
"version": "1.1.3",
|
|
4
|
+
"description": "Hand-written, dependency-minimal PDF codec: parses arbitrary real-world PDFs and generates new ones, built on document-schema.js's LayoutDocument pivot and Zod 4 codecs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"license": "MIT",
|
|
63
63
|
"packageManager": "pnpm@11.6.0",
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"document-
|
|
65
|
+
"document-schema.js": "^1.5.3",
|
|
66
66
|
"fflate": "^0.8.3",
|
|
67
67
|
"zod": "^4.4.3"
|
|
68
68
|
},
|