forge-terminal 0.1.0 → 0.2.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.
- package/README.md +333 -24
- package/dist/chunks/adm-zip-SQKLKBDP.js +3 -0
- package/dist/chunks/available-HEUREG6L.js +3 -0
- package/dist/chunks/boot-BUGERSEB.js +3 -0
- package/dist/chunks/catalog-NTACQH5V.js +3 -0
- package/dist/chunks/cfb-V6M2PYOD.js +13 -0
- package/dist/chunks/chunk-2WYHI73Y.js +3 -0
- package/dist/chunks/chunk-4YCO7S4K.js +3 -0
- package/dist/chunks/chunk-5B7WODWH.js +3 -0
- package/dist/chunks/chunk-C2WYXNRM.js +4 -0
- package/dist/chunks/chunk-C7OOWUMU.js +3 -0
- package/dist/chunks/chunk-EOYBXOCV.js +4 -0
- package/dist/chunks/chunk-G2D6BKOS.js +4 -0
- package/dist/chunks/chunk-HUAEVW2A.js +3 -0
- package/dist/chunks/chunk-J7FLFHQJ.js +3 -0
- package/dist/chunks/chunk-L76Q54MB.js +3 -0
- package/dist/chunks/chunk-LFE3WRU4.js +3 -0
- package/dist/chunks/chunk-PBIS2ONZ.js +3 -0
- package/dist/chunks/chunk-PD5DFBCU.js +3 -0
- package/dist/chunks/chunk-PN73WK2S.js +3 -0
- package/dist/chunks/chunk-QVACE4IE.js +3 -0
- package/dist/chunks/chunk-TEGB7JRM.js +3 -0
- package/dist/chunks/chunk-TMUK2DD3.js +3 -0
- package/dist/chunks/chunk-UNVHKJW4.js +41 -0
- package/dist/chunks/chunk-VH5RWPGZ.js +10 -0
- package/dist/chunks/chunk-XFEHJ4C7.js +3 -0
- package/dist/chunks/chunk-YXDEOLNA.js +3 -0
- package/dist/chunks/colour-DE6N6AZO.js +3 -0
- package/dist/chunks/config-command-SQXRTBRO.js +3 -0
- package/dist/chunks/dist-J2V6ITHG.js +29 -0
- package/dist/chunks/es-T2U7NBLD.js +3 -0
- package/dist/chunks/execute-M3C3TTWB.js +3 -0
- package/dist/chunks/extensions-command-HJDBUBYO.js +3 -0
- package/dist/chunks/image-GYUUMS6H.js +3 -0
- package/dist/chunks/launch-WBUFRVQ7.js +65 -0
- package/dist/chunks/lib-TPEJ5CJJ.js +46 -0
- package/dist/chunks/lock-XD4L3WFM.js +3 -0
- package/dist/chunks/manager-KBJTXZ27.js +4 -0
- package/dist/chunks/paths-JUM6GKQU.js +3 -0
- package/dist/chunks/pdf-compress-IMRFBLZF.js +3 -0
- package/dist/chunks/react-devtools-core-UM4FS6CU.js +3 -0
- package/dist/chunks/run-RXVQD4DZ.js +4 -0
- package/dist/chunks/server-DU7QWDAH.js +73 -0
- package/dist/chunks/theme-DESCJOCE.js +3 -0
- package/dist/chunks/word-OGQ2KZAJ.js +24 -0
- package/dist/index.js +67 -95
- package/dist/third-party-licenses.txt +2856 -0
- package/package.json +28 -21
- package/skills/forge/SKILL.md +149 -0
- package/dist/cli/args.js +0 -278
- package/dist/cli/args.js.map +0 -1
- package/dist/cli/config-command.js +0 -55
- package/dist/cli/config-command.js.map +0 -1
- package/dist/cli/execute.js +0 -377
- package/dist/cli/execute.js.map +0 -1
- package/dist/cli/report.js +0 -123
- package/dist/cli/report.js.map +0 -1
- package/dist/cli/spinner.js +0 -60
- package/dist/cli/spinner.js.map +0 -1
- package/dist/cli/stdin.js +0 -47
- package/dist/cli/stdin.js.map +0 -1
- package/dist/config/preferences.js +0 -143
- package/dist/config/preferences.js.map +0 -1
- package/dist/core/actions/compress.js +0 -157
- package/dist/core/actions/compress.js.map +0 -1
- package/dist/core/actions/convert.js +0 -270
- package/dist/core/actions/convert.js.map +0 -1
- package/dist/core/actions/extract.js +0 -94
- package/dist/core/actions/extract.js.map +0 -1
- package/dist/core/actions/index.js +0 -23
- package/dist/core/actions/index.js.map +0 -1
- package/dist/core/actions/merge.js +0 -16
- package/dist/core/actions/merge.js.map +0 -1
- package/dist/core/actions/rotate.js +0 -36
- package/dist/core/actions/rotate.js.map +0 -1
- package/dist/core/actions/split.js +0 -65
- package/dist/core/actions/split.js.map +0 -1
- package/dist/core/atomic.js +0 -61
- package/dist/core/atomic.js.map +0 -1
- package/dist/core/capabilities.js +0 -59
- package/dist/core/capabilities.js.map +0 -1
- package/dist/core/compress.js +0 -76
- package/dist/core/compress.js.map +0 -1
- package/dist/core/describe.js +0 -49
- package/dist/core/describe.js.map +0 -1
- package/dist/core/errors.js +0 -469
- package/dist/core/errors.js.map +0 -1
- package/dist/core/execute-jobs.js +0 -122
- package/dist/core/execute-jobs.js.map +0 -1
- package/dist/core/formats.js +0 -108
- package/dist/core/formats.js.map +0 -1
- package/dist/core/order.js +0 -44
- package/dist/core/order.js.map +0 -1
- package/dist/core/output-path.js +0 -176
- package/dist/core/output-path.js.map +0 -1
- package/dist/core/pages.js +0 -107
- package/dist/core/pages.js.map +0 -1
- package/dist/core/pdf-compress.js +0 -206
- package/dist/core/pdf-compress.js.map +0 -1
- package/dist/core/plan.js +0 -42
- package/dist/core/plan.js.map +0 -1
- package/dist/core/resolve.js +0 -115
- package/dist/core/resolve.js.map +0 -1
- package/dist/core/run.js +0 -66
- package/dist/core/run.js.map +0 -1
- package/dist/core/suggest.js +0 -50
- package/dist/core/suggest.js.map +0 -1
- package/dist/core/types.js +0 -2
- package/dist/core/types.js.map +0 -1
- package/dist/core/units.js +0 -94
- package/dist/core/units.js.map +0 -1
- package/dist/core/write-safety.js +0 -128
- package/dist/core/write-safety.js.map +0 -1
- package/dist/engines/heic.js +0 -166
- package/dist/engines/heic.js.map +0 -1
- package/dist/engines/image.js +0 -342
- package/dist/engines/image.js.map +0 -1
- package/dist/engines/pdf.js +0 -444
- package/dist/engines/pdf.js.map +0 -1
- package/dist/engines/pdfium.js +0 -161
- package/dist/engines/pdfium.js.map +0 -1
- package/dist/engines/registry.js +0 -65
- package/dist/engines/registry.js.map +0 -1
- package/dist/engines/types.js +0 -2
- package/dist/engines/types.js.map +0 -1
- package/dist/engines/word.js +0 -385
- package/dist/engines/word.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/shell/App.js +0 -1972
- package/dist/shell/App.js.map +0 -1
- package/dist/shell/ClickTargets.js +0 -183
- package/dist/shell/ClickTargets.js.map +0 -1
- package/dist/shell/ThemeContext.js +0 -21
- package/dist/shell/ThemeContext.js.map +0 -1
- package/dist/shell/blocks.js +0 -160
- package/dist/shell/blocks.js.map +0 -1
- package/dist/shell/clipboard.js +0 -64
- package/dist/shell/clipboard.js.map +0 -1
- package/dist/shell/commands.js +0 -51
- package/dist/shell/commands.js.map +0 -1
- package/dist/shell/components/Banner.js +0 -73
- package/dist/shell/components/Banner.js.map +0 -1
- package/dist/shell/components/CommandPalette.js +0 -37
- package/dist/shell/components/CommandPalette.js.map +0 -1
- package/dist/shell/components/FileCard.js +0 -59
- package/dist/shell/components/FileCard.js.map +0 -1
- package/dist/shell/components/HintBar.js +0 -18
- package/dist/shell/components/HintBar.js.map +0 -1
- package/dist/shell/components/Hints.js +0 -21
- package/dist/shell/components/Hints.js.map +0 -1
- package/dist/shell/components/MergeList.js +0 -268
- package/dist/shell/components/MergeList.js.map +0 -1
- package/dist/shell/components/ModeHeader.js +0 -21
- package/dist/shell/components/ModeHeader.js.map +0 -1
- package/dist/shell/components/PageGrid.js +0 -324
- package/dist/shell/components/PageGrid.js.map +0 -1
- package/dist/shell/components/PathInput.js +0 -147
- package/dist/shell/components/PathInput.js.map +0 -1
- package/dist/shell/components/Progress.js +0 -43
- package/dist/shell/components/Progress.js.map +0 -1
- package/dist/shell/components/Prompt.js +0 -667
- package/dist/shell/components/Prompt.js.map +0 -1
- package/dist/shell/components/ResultLinks.js +0 -26
- package/dist/shell/components/ResultLinks.js.map +0 -1
- package/dist/shell/components/Select.js +0 -152
- package/dist/shell/components/Select.js.map +0 -1
- package/dist/shell/components/Slider.js +0 -48
- package/dist/shell/components/Slider.js.map +0 -1
- package/dist/shell/components/StagedFiles.js +0 -92
- package/dist/shell/components/StagedFiles.js.map +0 -1
- package/dist/shell/components/ThemePicker.js +0 -18
- package/dist/shell/components/ThemePicker.js.map +0 -1
- package/dist/shell/components/UnitRow.js +0 -30
- package/dist/shell/components/UnitRow.js.map +0 -1
- package/dist/shell/flows/pdf.js +0 -424
- package/dist/shell/flows/pdf.js.map +0 -1
- package/dist/shell/frame-geometry.js +0 -56
- package/dist/shell/frame-geometry.js.map +0 -1
- package/dist/shell/hyperlink.js +0 -27
- package/dist/shell/hyperlink.js.map +0 -1
- package/dist/shell/intro.js +0 -101
- package/dist/shell/intro.js.map +0 -1
- package/dist/shell/launch.js +0 -75
- package/dist/shell/launch.js.map +0 -1
- package/dist/shell/mark.js +0 -134
- package/dist/shell/mark.js.map +0 -1
- package/dist/shell/mouse.js +0 -209
- package/dist/shell/mouse.js.map +0 -1
- package/dist/shell/redraw.js +0 -36
- package/dist/shell/redraw.js.map +0 -1
- package/dist/shell/reveal.js +0 -24
- package/dist/shell/reveal.js.map +0 -1
- package/dist/shell/stage.js +0 -34
- package/dist/shell/stage.js.map +0 -1
- package/dist/shell/terminal-write.js +0 -16
- package/dist/shell/terminal-write.js.map +0 -1
- package/dist/shell/theme.js +0 -178
- package/dist/shell/theme.js.map +0 -1
- package/dist/shell/useFrameOrigin.js +0 -175
- package/dist/shell/useFrameOrigin.js.map +0 -1
- package/dist/shell/useKeys.js +0 -28
- package/dist/shell/useKeys.js.map +0 -1
- package/dist/shell/useMouse.js +0 -150
- package/dist/shell/useMouse.js.map +0 -1
- package/dist/shell/useMouseRouting.js +0 -69
- package/dist/shell/useMouseRouting.js.map +0 -1
- package/dist/shell/width.js +0 -62
- package/dist/shell/width.js.map +0 -1
- package/dist/utils/display-path.js +0 -31
- package/dist/utils/display-path.js.map +0 -1
- package/dist/utils/unescape-path.js +0 -68
- package/dist/utils/unescape-path.js.map +0 -1
package/dist/engines/pdf.js
DELETED
|
@@ -1,444 +0,0 @@
|
|
|
1
|
-
import { readFile, rm, stat } from 'node:fs/promises';
|
|
2
|
-
import { basename } from 'node:path';
|
|
3
|
-
import { degrees, PDFDocument } from 'pdf-lib';
|
|
4
|
-
import sharp from 'sharp';
|
|
5
|
-
import { writeAtomic } from '../core/atomic.js';
|
|
6
|
-
import { emptySelection, encryptedSource } from '../core/errors.js';
|
|
7
|
-
import { FORMATS } from '../core/formats.js';
|
|
8
|
-
import { cutsToRanges, normalisePages } from '../core/pages.js';
|
|
9
|
-
import { compressPdf, surveyDocument } from '../core/pdf-compress.js';
|
|
10
|
-
const READS = new Set([
|
|
11
|
-
'pdf',
|
|
12
|
-
'jpeg',
|
|
13
|
-
'png',
|
|
14
|
-
'webp',
|
|
15
|
-
'avif',
|
|
16
|
-
'gif',
|
|
17
|
-
'tiff',
|
|
18
|
-
]);
|
|
19
|
-
const WRITES = new Set(['pdf']);
|
|
20
|
-
/**
|
|
21
|
-
* Load a document for inspection.
|
|
22
|
-
*
|
|
23
|
-
* `ignoreEncryption` is deliberate: an encrypted PDF must probe successfully
|
|
24
|
-
* and report `encrypted: true`, so the flow can refuse it with a message that
|
|
25
|
-
* names the fix. Failing here instead would surface as "not a format Forge
|
|
26
|
-
* reads", which is both wrong and unactionable.
|
|
27
|
-
*/
|
|
28
|
-
async function load(path) {
|
|
29
|
-
return PDFDocument.load(await readFile(path), { ignoreEncryption: true });
|
|
30
|
-
}
|
|
31
|
-
async function probe(path) {
|
|
32
|
-
const doc = await load(path);
|
|
33
|
-
const { size } = await stat(path);
|
|
34
|
-
return {
|
|
35
|
-
kind: 'document',
|
|
36
|
-
path,
|
|
37
|
-
format: 'pdf',
|
|
38
|
-
bytes: size,
|
|
39
|
-
pages: doc.getPageCount(),
|
|
40
|
-
encrypted: doc.isEncrypted,
|
|
41
|
-
// Surveyed from the document already parsed above rather than by reading
|
|
42
|
-
// the file a second time: probing runs on every drop, and parsing a large
|
|
43
|
-
// PDF twice to answer one question is lag the user feels.
|
|
44
|
-
images: surveyDocument(doc),
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Refuses a password-protected source before any page operation touches it.
|
|
49
|
-
* `probe` reports `encrypted` with `ignoreEncryption: true`, so this is the
|
|
50
|
-
* one place that turns "known to be locked" into a refusal that names the
|
|
51
|
-
* fix, rather than letting pdf-lib fail opaquely partway through a merge,
|
|
52
|
-
* split, extract, delete or rotate.
|
|
53
|
-
*/
|
|
54
|
-
function assertUnencrypted(sources) {
|
|
55
|
-
for (const s of sources) {
|
|
56
|
-
if (s.encrypted)
|
|
57
|
-
throw encryptedSource(s.path);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* pdf-lib embeds JPEG and PNG and nothing else. Anything else is decoded to
|
|
62
|
-
* PNG first, the same two-step `heic.ts` uses — one extra decode, no new
|
|
63
|
-
* dependency, and the alternative is refusing formats the capability graph
|
|
64
|
-
* has already offered.
|
|
65
|
-
*
|
|
66
|
-
* Invariant 4: `.rotate()` before anything else. pdf-lib does not read EXIF
|
|
67
|
-
* orientation itself, so embedding raw JPEG/PNG bytes unconditionally would
|
|
68
|
-
* ship every phone photo sideways — the exact bug `image.ts`'s Rule 1 exists
|
|
69
|
-
* to prevent, reintroduced here in a new file.
|
|
70
|
-
*
|
|
71
|
-
* The fix is deliberately not "always decode through Sharp": that would cost
|
|
72
|
-
* every JPEG a re-encode generation (and, if re-encoded as PNG, a size
|
|
73
|
-
* change) to correct a minority of files. Instead only a source that
|
|
74
|
-
* genuinely carries a non-trivial orientation tag pays for the decode.
|
|
75
|
-
*
|
|
76
|
-
* A rotated JPEG is re-encoded back to JPEG, at `FORMATS.jpeg.defaultQuality`
|
|
77
|
-
* /mozjpeg — the same quality this engine already
|
|
78
|
-
* accepts for every JPEG target it produces. Re-encoding as PNG instead was
|
|
79
|
-
* tried first and reverted: unlike `heic.ts`'s PNG intermediate, which is
|
|
80
|
-
* immediately re-compressed to whatever the user asked for at their chosen
|
|
81
|
-
* quality, PNG here would be the *terminal* artifact (`job.options.quality`
|
|
82
|
-
* is never consulted by this file), so "avoid a second lossy generation"
|
|
83
|
-
* bought a categorical size-class jump — a real photo measured ~7-9x larger
|
|
84
|
-
* as PNG than the JPEG re-encode below, both against the same source — not
|
|
85
|
-
* avoided one. Forge already accepts a second lossy JPEG generation for
|
|
86
|
-
* every other conversion this engine performs, so refusing it in only this
|
|
87
|
-
* one path was inconsistent with the tool's own established behaviour.
|
|
88
|
-
*
|
|
89
|
-
* What that argument does NOT establish: that a second q82 generation is
|
|
90
|
-
* perceptually harmless. It is reasoned from consistency, not measured — no
|
|
91
|
-
* SSIM or visual diff was run. The sizes below were measured; the quality
|
|
92
|
-
* claim was not. Recorded so a later reader does not mistake one for the other.
|
|
93
|
-
*
|
|
94
|
-
* A rotated PNG stays PNG: lossless-to-lossless has no quality trade to
|
|
95
|
-
* weigh. Every other source format (webp/avif/gif/tiff) was already being
|
|
96
|
-
* decoded through Sharp to become PNG regardless of orientation, so folding
|
|
97
|
-
* `.rotate()` into that same call is free — `.rotate()` with no arguments is
|
|
98
|
-
* sharp's EXIF auto-orient and a no-op when there is nothing to apply.
|
|
99
|
-
*/
|
|
100
|
-
async function embedBytes(doc, source, raw) {
|
|
101
|
-
const embeddable = source.format === 'jpeg' || source.format === 'png';
|
|
102
|
-
const orientation = embeddable ? (await sharp(raw).metadata()).orientation : undefined;
|
|
103
|
-
if (embeddable && (orientation === undefined || orientation === 1)) {
|
|
104
|
-
return source.format === 'jpeg' ? doc.embedJpg(raw) : doc.embedPng(raw);
|
|
105
|
-
}
|
|
106
|
-
if (source.format === 'jpeg') {
|
|
107
|
-
const rotated = await sharp(raw)
|
|
108
|
-
.rotate()
|
|
109
|
-
.jpeg({ quality: FORMATS.jpeg.defaultQuality, mozjpeg: true })
|
|
110
|
-
.toBuffer();
|
|
111
|
-
return doc.embedJpg(rotated);
|
|
112
|
-
}
|
|
113
|
-
return doc.embedPng(await sharp(raw).rotate().png().toBuffer());
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* One page, sized to the image, holding the whole image at its native
|
|
117
|
-
* resolution — the inbound half of phase 4a. `outputs`/`sources` are single
|
|
118
|
-
* because `convert` is defined that way (see `core/types.ts`); a multi-image
|
|
119
|
-
* PDF is a `merge` of several single-image PDFs, not a variant of this.
|
|
120
|
-
*
|
|
121
|
-
* `job.options` (background, keepMetadata) is accepted by the job shape but
|
|
122
|
-
* never consulted here: `embedPng`'s SMask already carries alpha, so there
|
|
123
|
-
* is nothing to flatten (unlike `image.ts`'s Rule 2), and pdf-lib strips
|
|
124
|
-
* source metadata by construction, so "keep metadata" has nothing to act on.
|
|
125
|
-
*/
|
|
126
|
-
async function imageToPdf(job, onPhase) {
|
|
127
|
-
const source = job.sources[0];
|
|
128
|
-
// Same reasoning as `image.ts`'s analogous guard: the registry routes a
|
|
129
|
-
// job to this engine by `reads`/`writes`, but `pdf` is offered as a
|
|
130
|
-
// same-format target for a PDF source the way any format is offered for
|
|
131
|
-
// itself (recompression) — and unlike an image, a PDF source has no
|
|
132
|
-
// pixels to embed. Without this, it reaches `embedBytes` and fails as an
|
|
133
|
-
// opaque Sharp decode error instead of a named one.
|
|
134
|
-
if (source.kind !== 'image') {
|
|
135
|
-
throw new Error(
|
|
136
|
-
// Unreachable while a convert job carries exactly one source: a
|
|
137
|
-
// document source is routed to `compressDocument` before this runs.
|
|
138
|
-
// Kept as a defensive throw so a future multi-source convert cannot
|
|
139
|
-
// reach `sharp()` with PDF bytes and die on a raw decode error.
|
|
140
|
-
'the pdf engine cannot embed a document source');
|
|
141
|
-
}
|
|
142
|
-
const warnings = [];
|
|
143
|
-
// Rule 4 (spec): never drop frames silently. A PDF page has no concept of
|
|
144
|
-
// animation, so any source with more than one frame loses everything past
|
|
145
|
-
// the first — the same warning `image.ts` raises for a lossless target
|
|
146
|
-
// that cannot animate either, reusing its code rather than inventing one.
|
|
147
|
-
if (source.frames > 1) {
|
|
148
|
-
warnings.push({
|
|
149
|
-
code: 'animation-flattened',
|
|
150
|
-
message: `${basename(source.path)} has ${source.frames} frames, and ` +
|
|
151
|
-
`${FORMATS.pdf.label} cannot animate. Only the first frame was converted.`,
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
onPhase({ phase: 'reading' });
|
|
155
|
-
const raw = await readFile(source.path);
|
|
156
|
-
onPhase({ phase: 'encoding' });
|
|
157
|
-
const doc = await PDFDocument.create();
|
|
158
|
-
const embedded = await embedBytes(doc, source, raw);
|
|
159
|
-
const page = doc.addPage([embedded.width, embedded.height]);
|
|
160
|
-
page.drawImage(embedded, { x: 0, y: 0, width: embedded.width, height: embedded.height });
|
|
161
|
-
onPhase({ phase: 'writing' });
|
|
162
|
-
const outputBytes = await writeAtomic(job.outputs[0], await doc.save());
|
|
163
|
-
return { job, outputBytes, warnings };
|
|
164
|
-
}
|
|
165
|
-
async function merge(job, onPhase) {
|
|
166
|
-
assertUnencrypted(job.sources.filter((s) => s.kind === 'document'));
|
|
167
|
-
const out = await PDFDocument.create();
|
|
168
|
-
for (const source of job.sources) {
|
|
169
|
-
onPhase({ phase: 'reading' });
|
|
170
|
-
const src = await load(source.path);
|
|
171
|
-
const pages = await out.copyPages(src, src.getPageIndices());
|
|
172
|
-
for (const page of pages)
|
|
173
|
-
out.addPage(page);
|
|
174
|
-
}
|
|
175
|
-
onPhase({ phase: 'writing' });
|
|
176
|
-
const bytes = await out.save();
|
|
177
|
-
const outputBytes = await writeAtomic(job.outputs[0], bytes);
|
|
178
|
-
return { job, outputBytes, warnings: [] };
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* `cutsToRanges` silently normalises its input (dedupes, sorts, drops
|
|
182
|
-
* out-of-range cuts) rather than validating it, so a `cuts` list that arrived
|
|
183
|
-
* unsorted or with duplicates still produces a correct partition here.
|
|
184
|
-
*
|
|
185
|
-
* Every output is written before any is kept. A split that fails half way
|
|
186
|
-
* through must not leave a folder of partial results (invariant 6).
|
|
187
|
-
*/
|
|
188
|
-
async function split(job, onPhase) {
|
|
189
|
-
const source = job.sources[0];
|
|
190
|
-
assertUnencrypted([source]);
|
|
191
|
-
onPhase({ phase: 'reading' });
|
|
192
|
-
const src = await load(source.path);
|
|
193
|
-
const ranges = cutsToRanges(job.cuts, src.getPageCount());
|
|
194
|
-
const written = [];
|
|
195
|
-
let outputBytes = 0;
|
|
196
|
-
try {
|
|
197
|
-
for (const [i, range] of ranges.entries()) {
|
|
198
|
-
const out = await PDFDocument.create();
|
|
199
|
-
const indices = Array.from({ length: range.to - range.from + 1 }, (_, n) => range.from + n);
|
|
200
|
-
const pages = await out.copyPages(src, indices);
|
|
201
|
-
for (const page of pages)
|
|
202
|
-
out.addPage(page);
|
|
203
|
-
const path = job.outputs[i];
|
|
204
|
-
if (path === undefined) {
|
|
205
|
-
throw new Error(`split produced ${ranges.length} parts but was given ${job.outputs.length} outputs`);
|
|
206
|
-
}
|
|
207
|
-
outputBytes += await writeAtomic(path, await out.save());
|
|
208
|
-
written.push(path);
|
|
209
|
-
onPhase({ phase: 'page', done: i + 1, total: ranges.length });
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
catch (e) {
|
|
213
|
-
await Promise.all(written.map((p) => rm(p, { force: true })));
|
|
214
|
-
throw e;
|
|
215
|
-
}
|
|
216
|
-
return { job, outputBytes, warnings: [] };
|
|
217
|
-
}
|
|
218
|
-
/** Copy an explicit page list into a new document, in the order given. */
|
|
219
|
-
async function pagesInto(src, indices) {
|
|
220
|
-
const out = await PDFDocument.create();
|
|
221
|
-
const pages = await out.copyPages(src, indices);
|
|
222
|
-
for (const page of pages)
|
|
223
|
-
out.addPage(page);
|
|
224
|
-
return out.save();
|
|
225
|
-
}
|
|
226
|
-
async function extract(job, onPhase) {
|
|
227
|
-
const source = job.sources[0];
|
|
228
|
-
assertUnencrypted([source]);
|
|
229
|
-
if (job.pages.length === 0) {
|
|
230
|
-
throw emptySelection('That extract selects no pages.');
|
|
231
|
-
}
|
|
232
|
-
onPhase({ phase: 'reading' });
|
|
233
|
-
const src = await load(source.path);
|
|
234
|
-
// The same function `extractAction.plan` names the outputs from, so
|
|
235
|
-
// `outputs[i]` is always the file named for `wanted[i]` (see
|
|
236
|
-
// `core/pages.ts`'s `normalisePages`).
|
|
237
|
-
const wanted = normalisePages(job.pages);
|
|
238
|
-
const written = [];
|
|
239
|
-
let outputBytes = 0;
|
|
240
|
-
try {
|
|
241
|
-
if (!job.separate) {
|
|
242
|
-
onPhase({ phase: 'writing' });
|
|
243
|
-
const path = job.outputs[0];
|
|
244
|
-
outputBytes = await writeAtomic(path, await pagesInto(src, wanted));
|
|
245
|
-
written.push(path);
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
for (const [i, page] of wanted.entries()) {
|
|
249
|
-
const path = job.outputs[i];
|
|
250
|
-
outputBytes += await writeAtomic(path, await pagesInto(src, [page]));
|
|
251
|
-
written.push(path);
|
|
252
|
-
onPhase({ phase: 'page', done: i + 1, total: wanted.length });
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
catch (e) {
|
|
257
|
-
await Promise.all(written.map((p) => rm(p, { force: true })));
|
|
258
|
-
throw e;
|
|
259
|
-
}
|
|
260
|
-
return { job, outputBytes, warnings: [] };
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Exact inverse of `extract`: the kept set is everything not in `job.pages`.
|
|
264
|
-
* Refuses to write an empty document, the same way `extract` refuses an
|
|
265
|
-
* empty selection — deleting every page is the delete-side of that case.
|
|
266
|
-
*/
|
|
267
|
-
async function deletePages(job, onPhase) {
|
|
268
|
-
const source = job.sources[0];
|
|
269
|
-
assertUnencrypted([source]);
|
|
270
|
-
onPhase({ phase: 'reading' });
|
|
271
|
-
const src = await load(source.path);
|
|
272
|
-
const drop = new Set(job.pages);
|
|
273
|
-
const keep = src.getPageIndices().filter((i) => !drop.has(i));
|
|
274
|
-
if (keep.length === 0) {
|
|
275
|
-
throw emptySelection('That would delete every page.');
|
|
276
|
-
}
|
|
277
|
-
onPhase({ phase: 'writing' });
|
|
278
|
-
const outputBytes = await writeAtomic(job.outputs[0], await pagesInto(src, keep));
|
|
279
|
-
return { job, outputBytes, warnings: [] };
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Rotation is *additive*.
|
|
283
|
-
*
|
|
284
|
-
* A page already at 90° rotated by another quarter turn must land at 180°.
|
|
285
|
-
* Setting the angle absolutely would silently discard a rotation the
|
|
286
|
-
* document already carried — the same class of bug as ignoring EXIF
|
|
287
|
-
* orientation, which this project treats as load-bearing (invariant 4).
|
|
288
|
-
*/
|
|
289
|
-
async function rotate(job, onPhase) {
|
|
290
|
-
const source = job.sources[0];
|
|
291
|
-
assertUnencrypted([source]);
|
|
292
|
-
onPhase({ phase: 'reading' });
|
|
293
|
-
const doc = await load(source.path);
|
|
294
|
-
for (const page of doc.getPages()) {
|
|
295
|
-
// Twice-modulo, because JS `%` keeps the sign: a page stored at -270
|
|
296
|
-
// rotated by a quarter turn would otherwise land at -180. Every value is
|
|
297
|
-
// right modulo 360 and viewers render either correctly, which is exactly
|
|
298
|
-
// why spec §6 pins the stored form to 0–270 rather than trusting it.
|
|
299
|
-
const turned = page.getRotation().angle + job.turns * 90;
|
|
300
|
-
const next = ((turned % 360) + 360) % 360;
|
|
301
|
-
page.setRotation(degrees(next));
|
|
302
|
-
}
|
|
303
|
-
onPhase({ phase: 'writing' });
|
|
304
|
-
const outputBytes = await writeAtomic(job.outputs[0], await doc.save());
|
|
305
|
-
return { job, outputBytes, warnings: [] };
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Shrinks a PDF by re-encoding the images inside it.
|
|
309
|
-
*
|
|
310
|
-
* Reached only when a `convert` job's source is itself a PDF — `/compress`
|
|
311
|
-
* plans exactly that shape, a job whose target equals its source format
|
|
312
|
-
* (`core/actions/compress.ts`). An images-to-PDF job has image sources and
|
|
313
|
-
* goes to `imageToPdf` instead.
|
|
314
|
-
*
|
|
315
|
-
* The warnings are the honest half. Re-encoding can find nothing to do — a
|
|
316
|
-
* text-only PDF has no images, and a screenshot PDF's images are Flate-encoded
|
|
317
|
-
* raw pixels this cannot rebuild — and in both cases the output is a near
|
|
318
|
-
* copy of the input. Writing that silently and reporting a size change of
|
|
319
|
-
* roughly zero would leave the user to guess why; the result says which of
|
|
320
|
-
* the two happened.
|
|
321
|
-
*/
|
|
322
|
-
async function compressDocument(job, onPhase) {
|
|
323
|
-
const source = job.sources[0];
|
|
324
|
-
assertUnencrypted([source]);
|
|
325
|
-
onPhase({ phase: 'reading' });
|
|
326
|
-
const original = await readFile(source.path);
|
|
327
|
-
onPhase({ phase: 'encoding' });
|
|
328
|
-
/**
|
|
329
|
-
* `/compress` always supplies a quality — from the slider, or as the answer
|
|
330
|
-
* the target-size search converged on. The fallback keeps a hand-built job
|
|
331
|
-
* from silently re-encoding at whatever Sharp defaults to.
|
|
332
|
-
*
|
|
333
|
-
* Resolution defaults to 150 dpi rather than leaving it alone. Reducing it
|
|
334
|
-
* is worth roughly ten times more than re-encoding: a 5.7 MB 300 dpi scan
|
|
335
|
-
* reaches 1065 KB on quality alone and 111 KB with both. 150 dpi is
|
|
336
|
-
* indistinguishable on screen and at upload sizes, which is what people
|
|
337
|
-
* compress a scan for. `--dpi` overrides it — pass the scan's own
|
|
338
|
-
* resolution to keep every pixel.
|
|
339
|
-
*/
|
|
340
|
-
const { bytes, recompressed, skipped, fromDpi, toDpi } = await compressPdf(original, {
|
|
341
|
-
quality: job.options.quality ?? 60,
|
|
342
|
-
dpi: job.options.dpi ?? 150,
|
|
343
|
-
});
|
|
344
|
-
onPhase({ phase: 'writing' });
|
|
345
|
-
const outputBytes = await writeAtomic(job.outputs[0], bytes);
|
|
346
|
-
const plural = (n) => (n === 1 ? '' : 's');
|
|
347
|
-
const warnings = [];
|
|
348
|
-
if (recompressed === 0 && skipped === 0) {
|
|
349
|
-
warnings.push({
|
|
350
|
-
code: 'pdf-no-images',
|
|
351
|
-
message: 'This PDF has no images to re-encode, so its size is almost unchanged.',
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
else if (recompressed === 0) {
|
|
355
|
-
warnings.push({
|
|
356
|
-
code: 'pdf-no-images',
|
|
357
|
-
message: `None of its ${skipped} image${plural(skipped)} could be re-encoded — they are not JPEG data.`,
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
else if (skipped > 0) {
|
|
361
|
-
warnings.push({
|
|
362
|
-
code: 'pdf-images-skipped',
|
|
363
|
-
message: `${recompressed} image${plural(recompressed)} recompressed, ${skipped} skipped (not JPEG).`,
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
// Resolution is the change a user is most likely to be surprised by, so it
|
|
367
|
-
// is stated whenever it happened — silently reducing what someone may need
|
|
368
|
-
// for printing is exactly the kind of quiet loss this project avoids.
|
|
369
|
-
if (fromDpi !== undefined && toDpi !== undefined) {
|
|
370
|
-
warnings.push({
|
|
371
|
-
code: 'pdf-downsampled',
|
|
372
|
-
message: `Images reduced from ${fromDpi} to ${toDpi} dpi. Pass --dpi ${fromDpi} to keep them.`,
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
return { job, outputBytes, warnings };
|
|
376
|
-
}
|
|
377
|
-
/**
|
|
378
|
-
* A PDF has two levers, so the search has two dimensions.
|
|
379
|
-
*
|
|
380
|
-
* Quality is tried first at the default resolution. If even quality 1
|
|
381
|
-
* overshoots, the resolution comes down a rung and the quality search runs
|
|
382
|
-
* again. The user named a size; reaching it is what they asked for, and
|
|
383
|
-
* refusing would send them to a website that would have done exactly this.
|
|
384
|
-
*
|
|
385
|
-
* Descending only when needed matters: most targets are met on the first rung,
|
|
386
|
-
* and each extra rung is another full bisection.
|
|
387
|
-
*
|
|
388
|
-
* 150 dpi leads because reducing resolution is worth roughly ten times more
|
|
389
|
-
* than re-encoding — a 5.7 MB 300 dpi scan reaches 1065 KB on quality alone
|
|
390
|
-
* and 111 KB with both — and it is indistinguishable on screen at the sizes
|
|
391
|
-
* people compress a scan for. An explicit `--dpi` leads instead, because that
|
|
392
|
-
* is the user asking to keep every pixel.
|
|
393
|
-
*
|
|
394
|
-
* The file is read once here rather than per attempt: the search calls
|
|
395
|
-
* `measure` up to eight times per rung, and re-reading a large scan each time
|
|
396
|
-
* is work the user waits through for nothing.
|
|
397
|
-
*/
|
|
398
|
-
async function measurer(job) {
|
|
399
|
-
if (job.op !== 'convert')
|
|
400
|
-
return undefined;
|
|
401
|
-
const source = job.sources[0];
|
|
402
|
-
// An image going to PDF is an embedding, not a compression: there is no
|
|
403
|
-
// resolution ladder to walk, so decline rather than invent one.
|
|
404
|
-
if (source.kind !== 'document')
|
|
405
|
-
return undefined;
|
|
406
|
-
const original = await readFile(source.path);
|
|
407
|
-
return {
|
|
408
|
-
ladder: [job.options.dpi ?? 150, 120, 96, 72].map((dpi) => ({ dpi })),
|
|
409
|
-
measure: async (options) => (await compressPdf(original, {
|
|
410
|
-
quality: options.quality ?? 60,
|
|
411
|
-
...(options.dpi === undefined ? {} : { dpi: options.dpi }),
|
|
412
|
-
})).bytes.byteLength,
|
|
413
|
-
};
|
|
414
|
-
}
|
|
415
|
-
export const pdfEngine = {
|
|
416
|
-
id: 'pdf',
|
|
417
|
-
measurer,
|
|
418
|
-
reads: READS,
|
|
419
|
-
writes: WRITES,
|
|
420
|
-
ops: new Set(['convert', 'merge', 'split', 'extract', 'delete', 'rotate']),
|
|
421
|
-
probe,
|
|
422
|
-
async run(job, onPhase) {
|
|
423
|
-
switch (job.op) {
|
|
424
|
-
case 'convert':
|
|
425
|
-
if (job.target !== 'pdf')
|
|
426
|
-
throw new Error('the pdf engine only converts to pdf');
|
|
427
|
-
// A PDF in and a PDF out is a compression, not an embedding.
|
|
428
|
-
return job.sources[0].kind === 'document'
|
|
429
|
-
? compressDocument(job, onPhase)
|
|
430
|
-
: imageToPdf(job, onPhase);
|
|
431
|
-
case 'merge':
|
|
432
|
-
return merge(job, onPhase);
|
|
433
|
-
case 'split':
|
|
434
|
-
return split(job, onPhase);
|
|
435
|
-
case 'extract':
|
|
436
|
-
return extract(job, onPhase);
|
|
437
|
-
case 'delete':
|
|
438
|
-
return deletePages(job, onPhase);
|
|
439
|
-
case 'rotate':
|
|
440
|
-
return rotate(job, onPhase);
|
|
441
|
-
}
|
|
442
|
-
},
|
|
443
|
-
};
|
|
444
|
-
//# sourceMappingURL=pdf.js.map
|
package/dist/engines/pdf.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pdf.js","sourceRoot":"","sources":["../../src/engines/pdf.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAC9C,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnE,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAC/D,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AAYrE,MAAM,KAAK,GAA0B,IAAI,GAAG,CAAW;IACrD,KAAK;IACL,MAAM;IACN,KAAK;IACL,MAAM;IACN,MAAM;IACN,KAAK;IACL,MAAM;CACP,CAAC,CAAA;AACF,MAAM,MAAM,GAA0B,IAAI,GAAG,CAAW,CAAC,KAAK,CAAC,CAAC,CAAA;AAEhE;;;;;;;GAOG;AACH,KAAK,UAAU,IAAI,CAAC,IAAY;IAC9B,OAAO,WAAW,CAAC,IAAI,CAAC,MAAM,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,gBAAgB,EAAE,IAAI,EAAE,CAAC,CAAA;AAC3E,CAAC;AAED,KAAK,UAAU,KAAK,CAAC,IAAY;IAC/B,MAAM,GAAG,GAAG,MAAM,IA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AG,CAAC,MAAM,KAAK,KAAK;oBAAE,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAA;gBAChF,6DAA6D;gBAC7D,OAAO,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU;oBACvC,CAAC,CAAC,gBAAgB,CAAC,GAAG,EAAE,OAAO,CAAC;oBAChC,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;YAC9B,KAAK,OAAO;gBACV,OAAO,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;YAC5B,KAAK,OAAO;gBACV,OAAO,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;YAC5B,KAAK,SAAS;gBACZ,OAAO,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;YAC9B,KAAK,QAAQ;gBACX,OAAO,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;YAClC,KAAK,QAAQ;gBACX,OAAO,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,CAAA;QAC/B,CAAC;IACH,CAAC;CACF,CAAA"}
|
package/dist/engines/pdfium.js
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import { readFile, rm } from 'node:fs/promises';
|
|
2
|
-
import { PDFiumLibrary } from '@hyzyla/pdfium';
|
|
3
|
-
import sharp from 'sharp';
|
|
4
|
-
import { writeAtomic } from '../core/atomic.js';
|
|
5
|
-
import { encryptedSource } from '../core/errors.js';
|
|
6
|
-
import { encode } from './image.js';
|
|
7
|
-
const READS = new Set(['pdf']);
|
|
8
|
-
const WRITES = new Set(['jpeg', 'png']);
|
|
9
|
-
const OPS = new Set(['convert']);
|
|
10
|
-
// PDFiumLibrary.init() compiles the wasm module. It costs real time and there
|
|
11
|
-
// is no reason to pay it per file, so it is memoised for the process. The
|
|
12
|
-
// promise itself is cached, not the resolved value, so two concurrent callers
|
|
13
|
-
// share one initialisation rather than racing two.
|
|
14
|
-
let library;
|
|
15
|
-
function getLibrary() {
|
|
16
|
-
library ??= PDFiumLibrary.init();
|
|
17
|
-
return library;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Open a PDF for reading.
|
|
21
|
-
*
|
|
22
|
-
* `password` is for ENCRYPTED SOURCES ONLY — PDFium can read a locked document
|
|
23
|
-
* but cannot write one, so there is no unlock feature here (ruling R7). The
|
|
24
|
-
* value must never be logged, returned, or attached to an error (invariant 8).
|
|
25
|
-
*/
|
|
26
|
-
export async function openPdf(bytes, password) {
|
|
27
|
-
const lib = await getLibrary();
|
|
28
|
-
return await lib.loadDocument(bytes, password);
|
|
29
|
-
}
|
|
30
|
-
export const pdfiumEngine = {
|
|
31
|
-
id: 'pdfium',
|
|
32
|
-
reads: READS,
|
|
33
|
-
writes: WRITES,
|
|
34
|
-
ops: OPS,
|
|
35
|
-
probe() {
|
|
36
|
-
// engines/pdf.ts already classifies PDFs by content and is registered
|
|
37
|
-
// first, so this is never reached. It throws rather than returning a
|
|
38
|
-
// wrong answer if the registration order is ever changed.
|
|
39
|
-
throw new Error('pdfium does not probe; engines/pdf.ts classifies PDFs');
|
|
40
|
-
},
|
|
41
|
-
/**
|
|
42
|
-
* Rasterises the selected pages of a PDF to JPEG or PNG, one output file
|
|
43
|
-
* per page.
|
|
44
|
-
*
|
|
45
|
-
* `job.options.pages` is 0-based, ascending and already deduped by
|
|
46
|
-
* whatever built the job (`core/pages.ts`'s `normalisePages`) — this loop
|
|
47
|
-
* writes `outputs[i]` from `pages[i]` verbatim and never sorts or dedupes.
|
|
48
|
-
* Phase 3's worst defect was exactly that shape: the engine reordering
|
|
49
|
-
* while the output naming did not, so a requested page silently became a
|
|
50
|
-
* different one on disk.
|
|
51
|
-
*
|
|
52
|
-
* A fresh `doc.getPage(index)` is taken for every render rather than
|
|
53
|
-
* reused: a `PDFiumPage` is single-use, and calling `.render()` twice on
|
|
54
|
-
* the same page object corrupts the wasm heap (`RuntimeError: table index
|
|
55
|
-
* is out of bounds`) instead of raising a catchable error.
|
|
56
|
-
*/
|
|
57
|
-
async run(job, onPhase) {
|
|
58
|
-
if (job.op !== 'convert')
|
|
59
|
-
throw new Error(`pdfium cannot ${job.op}`);
|
|
60
|
-
const source = job.sources[0];
|
|
61
|
-
if (source.kind !== 'document') {
|
|
62
|
-
throw new Error('the pdfium engine can only rasterise a document source');
|
|
63
|
-
}
|
|
64
|
-
// The refusal lives here, below every caller, rather than in the CLI's
|
|
65
|
-
// `buildRasterJob` alone: the shell reaches this engine through
|
|
66
|
-
// `convertAction.plan()` + `runJobs` with no password step of its own
|
|
67
|
-
// (spec §8 gives it no password field), so a guard in the CLI leaves the
|
|
68
|
-
// shell showing pdfium's opaque load failure flattened into a generic
|
|
69
|
-
// `conversion-failed`. Thrown as a `ForgeError`, which `runJobs` passes
|
|
70
|
-
// through untouched (`isForgeError` in `core/run.ts`), so both front ends
|
|
71
|
-
// get the wording that actually names `--password-stdin`.
|
|
72
|
-
if (source.encrypted && job.options.password === undefined) {
|
|
73
|
-
throw encryptedSource(source.path);
|
|
74
|
-
}
|
|
75
|
-
const dpi = job.options.dpi ?? 150;
|
|
76
|
-
// No `pages` means "convert this PDF" — read as every page. `outputs`
|
|
77
|
-
// promises at least one file ([string, ...string[]]); defaulting to an
|
|
78
|
-
// empty selection would report success while writing nothing.
|
|
79
|
-
const pages = job.options.pages ?? Array.from({ length: source.pages }, (_, i) => i);
|
|
80
|
-
// A programmer error, not a user error: whatever built this job promised
|
|
81
|
-
// one output per page and didn't keep that promise. Throwing here rather
|
|
82
|
-
// than truncating to the shorter length is what stops `job.outputs[i]`
|
|
83
|
-
// from silently reading past the end of a too-short array.
|
|
84
|
-
if (pages.length !== job.outputs.length) {
|
|
85
|
-
throw new Error(`pdfium was given ${pages.length} pages but ${job.outputs.length} outputs`);
|
|
86
|
-
}
|
|
87
|
-
// Also a caller bug: `doc.getPage(99)` on a 3-page document does not
|
|
88
|
-
// throw, it hands back an empty page whose render produces an empty
|
|
89
|
-
// bitmap — the user would see Sharp's opaque "Input Buffer is empty"
|
|
90
|
-
// instead of anything that names what actually went wrong. `source`
|
|
91
|
-
// already carries the real page count, so check before opening the doc.
|
|
92
|
-
for (const index of pages) {
|
|
93
|
-
if (index < 0 || index >= source.pages) {
|
|
94
|
-
throw new Error(`pdfium was asked for page ${index}, but ${source.path} has ${source.pages} pages`);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
// `job.options.keepMetadata` is accepted by the job shape but never
|
|
98
|
-
// consulted here, the same way `engines/pdf.ts`'s `imageToPdf` documents
|
|
99
|
-
// for its own unused options: a rendered bitmap is a fresh raster off
|
|
100
|
-
// the vector page, not a decode of a file carrying its own EXIF/ICC
|
|
101
|
-
// data, so there is nothing for "keep metadata" to act on.
|
|
102
|
-
const doc = await openPdf(await readFile(source.path), job.options.password);
|
|
103
|
-
try {
|
|
104
|
-
const written = [];
|
|
105
|
-
let outputBytes = 0;
|
|
106
|
-
try {
|
|
107
|
-
for (const [i, index] of pages.entries()) {
|
|
108
|
-
// render({ render: 'bitmap' }) returns RGBA, not BGRA — measured by
|
|
109
|
-
// spiking the library: a page painted R=51 G=102 B=229 produces
|
|
110
|
-
// first pixel [51,102,230,255]. bitmap.data is handed to Sharp's
|
|
111
|
-
// raw input unswapped; swapping the channels yields a visibly
|
|
112
|
-
// wrong image that still passes every width/height assertion.
|
|
113
|
-
//
|
|
114
|
-
// `transparent: true` is required, not cosmetic: pdfium's default
|
|
115
|
-
// is `transparent: false`, which pre-fills the bitmap with opaque
|
|
116
|
-
// white before painting anything, so `flatten()` below would be a
|
|
117
|
-
// no-op and `--background` would silently do nothing. With real
|
|
118
|
-
// transparency in the bitmap, flattening onto `job.options.background`
|
|
119
|
-
// applies to both JPEG (which cannot carry alpha at all) and PNG
|
|
120
|
-
// (which could, but a scanned page's unpainted area reads as paper,
|
|
121
|
-
// not a transparent cut-out — so both targets get the same paper
|
|
122
|
-
// colour rather than PNG alone defaulting to see-through).
|
|
123
|
-
const bitmap = await doc
|
|
124
|
-
.getPage(index)
|
|
125
|
-
.render({ scale: dpi / 72, render: 'bitmap', transparent: true });
|
|
126
|
-
// Sharp accepts a Uint8Array directly and the library already
|
|
127
|
-
// slices its own copy off the wasm heap, so wrapping it in
|
|
128
|
-
// `Buffer.from` here would be a second, needless full-page copy —
|
|
129
|
-
// ~140 MB at 600 dpi for nothing.
|
|
130
|
-
const image = sharp(bitmap.data, {
|
|
131
|
-
raw: { width: bitmap.width, height: bitmap.height, channels: 4 },
|
|
132
|
-
}).flatten({ background: job.options.background });
|
|
133
|
-
const bytes = await encode(image, job.target, job.options.quality).toBuffer();
|
|
134
|
-
// The arity guard above proves this index is in range, but
|
|
135
|
-
// TypeScript can't carry that fact through a runtime-computed
|
|
136
|
-
// index into a `[string, ...string[]]` tuple — under
|
|
137
|
-
// `noUncheckedIndexedAccess` a non-literal index still widens to
|
|
138
|
-
// `string | undefined`. The cast is what the guard buys back.
|
|
139
|
-
const out = job.outputs[i];
|
|
140
|
-
outputBytes += await writeAtomic(out, bytes);
|
|
141
|
-
written.push(out);
|
|
142
|
-
onPhase({ phase: 'page', done: i + 1, total: pages.length });
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
catch (e) {
|
|
146
|
-
// Invariant 6, extended for this phase: a multi-output job is
|
|
147
|
-
// all-or-nothing. A rasterisation that fails at page 200 of 248
|
|
148
|
-
// must not leave the first 199 behind — the same rule split() and
|
|
149
|
-
// extract()'s separate mode already apply in engines/pdf.ts.
|
|
150
|
-
await Promise.all(written.map((p) => rm(p, { force: true })));
|
|
151
|
-
throw e;
|
|
152
|
-
}
|
|
153
|
-
return { job, outputBytes, warnings: [] };
|
|
154
|
-
}
|
|
155
|
-
finally {
|
|
156
|
-
// Frees the wasm document. Skipping it leaks the page buffers.
|
|
157
|
-
doc.destroy();
|
|
158
|
-
}
|
|
159
|
-
},
|
|
160
|
-
};
|
|
161
|
-
//# sourceMappingURL=pdfium.js.map
|
|
@@ -1 +0,0 @@
|
|
|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