@effortlessmotion/pdf-to-md 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +157 -0
- package/bin/cli.js +9 -0
- package/dist/cjs/analyze.d.cts +35 -0
- package/dist/cjs/analyze.d.ts +35 -0
- package/dist/cjs/analyze.js +620 -0
- package/dist/cjs/analyze.js.map +1 -0
- package/dist/cjs/audit.d.cts +38 -0
- package/dist/cjs/audit.d.ts +38 -0
- package/dist/cjs/audit.js +252 -0
- package/dist/cjs/audit.js.map +1 -0
- package/dist/cjs/cli/audit.d.cts +3 -0
- package/dist/cjs/cli/audit.d.ts +3 -0
- package/dist/cjs/cli/audit.js +269 -0
- package/dist/cjs/cli/audit.js.map +1 -0
- package/dist/cjs/cli/extract.d.cts +3 -0
- package/dist/cjs/cli/extract.d.ts +3 -0
- package/dist/cjs/cli/extract.js +704 -0
- package/dist/cjs/cli/extract.js.map +1 -0
- package/dist/cjs/cli/index.d.cts +5 -0
- package/dist/cjs/cli/index.d.ts +5 -0
- package/dist/cjs/cli/index.js +58 -0
- package/dist/cjs/cli/index.js.map +1 -0
- package/dist/cjs/cli/render.d.cts +3 -0
- package/dist/cjs/cli/render.d.ts +3 -0
- package/dist/cjs/cli/render.js +361 -0
- package/dist/cjs/cli/render.js.map +1 -0
- package/dist/cjs/cli/types.d.cts +10 -0
- package/dist/cjs/cli/types.d.ts +10 -0
- package/dist/cjs/cli/types.js +7 -0
- package/dist/cjs/cli/types.js.map +1 -0
- package/dist/cjs/cli.d.cts +2 -0
- package/dist/cjs/cli.d.ts +2 -0
- package/dist/cjs/cli.js +345 -0
- package/dist/cjs/cli.js.map +1 -0
- package/dist/cjs/collect.d.cts +6 -0
- package/dist/cjs/collect.d.ts +6 -0
- package/dist/cjs/collect.js +370 -0
- package/dist/cjs/collect.js.map +1 -0
- package/dist/cjs/dict.d.cts +8 -0
- package/dist/cjs/dict.d.ts +8 -0
- package/dist/cjs/dict.js +55 -0
- package/dist/cjs/dict.js.map +1 -0
- package/dist/cjs/emit.d.cts +27 -0
- package/dist/cjs/emit.d.ts +27 -0
- package/dist/cjs/emit.js +187 -0
- package/dist/cjs/emit.js.map +1 -0
- package/dist/cjs/engines/tesseract.d.cts +30 -0
- package/dist/cjs/engines/tesseract.d.ts +30 -0
- package/dist/cjs/engines/tesseract.js +650 -0
- package/dist/cjs/engines/tesseract.js.map +1 -0
- package/dist/cjs/errors.d.cts +21 -0
- package/dist/cjs/errors.d.ts +21 -0
- package/dist/cjs/errors.js +124 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/extract.d.cts +88 -0
- package/dist/cjs/extract.d.ts +88 -0
- package/dist/cjs/extract.js +436 -0
- package/dist/cjs/extract.js.map +1 -0
- package/dist/cjs/geometry.d.cts +63 -0
- package/dist/cjs/geometry.d.ts +63 -0
- package/dist/cjs/geometry.js +521 -0
- package/dist/cjs/geometry.js.map +1 -0
- package/dist/cjs/index.d.cts +15 -0
- package/dist/cjs/index.d.ts +15 -0
- package/dist/cjs/index.js +56 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/lines.d.cts +3 -0
- package/dist/cjs/lines.d.ts +3 -0
- package/dist/cjs/lines.js +181 -0
- package/dist/cjs/lines.js.map +1 -0
- package/dist/cjs/markdown.d.cts +2 -0
- package/dist/cjs/markdown.d.ts +2 -0
- package/dist/cjs/markdown.js +222 -0
- package/dist/cjs/markdown.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/page-marker.d.cts +16 -0
- package/dist/cjs/page-marker.d.ts +16 -0
- package/dist/cjs/page-marker.js +85 -0
- package/dist/cjs/page-marker.js.map +1 -0
- package/dist/cjs/pdf-open.d.cts +12 -0
- package/dist/cjs/pdf-open.d.ts +12 -0
- package/dist/cjs/pdf-open.js +257 -0
- package/dist/cjs/pdf-open.js.map +1 -0
- package/dist/cjs/raster.d.cts +26 -0
- package/dist/cjs/raster.d.ts +26 -0
- package/dist/cjs/raster.js +262 -0
- package/dist/cjs/raster.js.map +1 -0
- package/dist/cjs/render-analysis.d.cts +25 -0
- package/dist/cjs/render-analysis.d.ts +25 -0
- package/dist/cjs/render-analysis.js +319 -0
- package/dist/cjs/render-analysis.js.map +1 -0
- package/dist/cjs/report.d.cts +12 -0
- package/dist/cjs/report.d.ts +12 -0
- package/dist/cjs/report.js +392 -0
- package/dist/cjs/report.js.map +1 -0
- package/dist/cjs/resolve.d.cts +1 -0
- package/dist/cjs/resolve.d.ts +1 -0
- package/dist/cjs/resolve.js +18 -0
- package/dist/cjs/resolve.js.map +1 -0
- package/dist/cjs/types.d.cts +171 -0
- package/dist/cjs/types.d.ts +171 -0
- package/dist/cjs/types.js +9 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/validate.d.cts +14 -0
- package/dist/cjs/validate.d.ts +14 -0
- package/dist/cjs/validate.js +96 -0
- package/dist/cjs/validate.js.map +1 -0
- package/dist/esm/analyze.d.ts +35 -0
- package/dist/esm/analyze.js +270 -0
- package/dist/esm/analyze.js.map +1 -0
- package/dist/esm/audit.d.ts +38 -0
- package/dist/esm/audit.js +113 -0
- package/dist/esm/audit.js.map +1 -0
- package/dist/esm/cli/audit.d.ts +3 -0
- package/dist/esm/cli/audit.js +20 -0
- package/dist/esm/cli/audit.js.map +1 -0
- package/dist/esm/cli/extract.d.ts +3 -0
- package/dist/esm/cli/extract.js +313 -0
- package/dist/esm/cli/extract.js.map +1 -0
- package/dist/esm/cli/index.d.ts +5 -0
- package/dist/esm/cli/index.js +7 -0
- package/dist/esm/cli/index.js.map +1 -0
- package/dist/esm/cli/render.d.ts +3 -0
- package/dist/esm/cli/render.js +77 -0
- package/dist/esm/cli/render.js.map +1 -0
- package/dist/esm/cli/types.d.ts +10 -0
- package/dist/esm/cli/types.js +3 -0
- package/dist/esm/cli/types.js.map +1 -0
- package/dist/esm/cli.d.ts +2 -0
- package/dist/esm/cli.js +90 -0
- package/dist/esm/cli.js.map +1 -0
- package/dist/esm/collect.d.ts +6 -0
- package/dist/esm/collect.js +80 -0
- package/dist/esm/collect.js.map +1 -0
- package/dist/esm/dict.d.ts +8 -0
- package/dist/esm/dict.js +28 -0
- package/dist/esm/dict.js.map +1 -0
- package/dist/esm/emit.d.ts +27 -0
- package/dist/esm/emit.js +131 -0
- package/dist/esm/emit.js.map +1 -0
- package/dist/esm/engines/tesseract.d.ts +30 -0
- package/dist/esm/engines/tesseract.js +190 -0
- package/dist/esm/engines/tesseract.js.map +1 -0
- package/dist/esm/errors.d.ts +21 -0
- package/dist/esm/errors.js +9 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/extract.d.ts +88 -0
- package/dist/esm/extract.js +147 -0
- package/dist/esm/extract.js.map +1 -0
- package/dist/esm/geometry.d.ts +63 -0
- package/dist/esm/geometry.js +279 -0
- package/dist/esm/geometry.js.map +1 -0
- package/dist/esm/index.d.ts +15 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lines.d.ts +3 -0
- package/dist/esm/lines.js +67 -0
- package/dist/esm/lines.js.map +1 -0
- package/dist/esm/markdown.d.ts +2 -0
- package/dist/esm/markdown.js +153 -0
- package/dist/esm/markdown.js.map +1 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/page-marker.d.ts +16 -0
- package/dist/esm/page-marker.js +40 -0
- package/dist/esm/page-marker.js.map +1 -0
- package/dist/esm/pdf-open.d.ts +12 -0
- package/dist/esm/pdf-open.js +61 -0
- package/dist/esm/pdf-open.js.map +1 -0
- package/dist/esm/raster.d.ts +26 -0
- package/dist/esm/raster.js +60 -0
- package/dist/esm/raster.js.map +1 -0
- package/dist/esm/render-analysis.d.ts +25 -0
- package/dist/esm/render-analysis.js +115 -0
- package/dist/esm/render-analysis.js.map +1 -0
- package/dist/esm/report.d.ts +12 -0
- package/dist/esm/report.js +182 -0
- package/dist/esm/report.js.map +1 -0
- package/dist/esm/resolve.d.ts +1 -0
- package/dist/esm/resolve.js +7 -0
- package/dist/esm/resolve.js.map +1 -0
- package/dist/esm/types.d.ts +171 -0
- package/dist/esm/types.js +6 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/validate.d.ts +14 -0
- package/dist/esm/validate.js +56 -0
- package/dist/esm/validate.js.map +1 -0
- package/package.json +67 -0
- package/skills/pdf-to-md/SKILL.md +102 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
// pdf-open.ts — the one seam that opens a PDF via pdfjs-dist, in the two
|
|
2
|
+
// shapes callers need: openDocument (data-based, already-open doc) and openPdfForRender (url-based, task you must await + destroy()).
|
|
3
|
+
// Both wrap a failure the same way: PdfToMdError/PDF_OPEN, original message preserved.
|
|
4
|
+
"use strict";
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
function _export(target, all) {
|
|
9
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
_export(exports, {
|
|
15
|
+
get awaitPdfOpen () {
|
|
16
|
+
return awaitPdfOpen;
|
|
17
|
+
},
|
|
18
|
+
get openDocument () {
|
|
19
|
+
return openDocument;
|
|
20
|
+
},
|
|
21
|
+
get openPdfForRender () {
|
|
22
|
+
return openPdfForRender;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
var _nodefs = require("node:fs");
|
|
26
|
+
var _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path"));
|
|
27
|
+
var _pdf = require("pdfjs-dist/legacy/build/pdf.mjs");
|
|
28
|
+
var _errorsts = require("./errors.js");
|
|
29
|
+
var _resolvets = require("./resolve.js");
|
|
30
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
31
|
+
try {
|
|
32
|
+
var info = gen[key](arg);
|
|
33
|
+
var value = info.value;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
reject(error);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (info.done) resolve(value);
|
|
39
|
+
else Promise.resolve(value).then(_next, _throw);
|
|
40
|
+
}
|
|
41
|
+
function _async_to_generator(fn) {
|
|
42
|
+
return function() {
|
|
43
|
+
var self = this, args = arguments;
|
|
44
|
+
return new Promise(function(resolve, reject) {
|
|
45
|
+
var gen = fn.apply(self, args);
|
|
46
|
+
function _next(value) {
|
|
47
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
48
|
+
}
|
|
49
|
+
function _throw(err) {
|
|
50
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
51
|
+
}
|
|
52
|
+
_next(undefined);
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function _interop_require_default(obj) {
|
|
57
|
+
return obj && obj.__esModule ? obj : {
|
|
58
|
+
default: obj
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function _ts_generator(thisArg, body) {
|
|
62
|
+
var f, y, t, _ = {
|
|
63
|
+
label: 0,
|
|
64
|
+
sent: function() {
|
|
65
|
+
if (t[0] & 1) throw t[1];
|
|
66
|
+
return t[1];
|
|
67
|
+
},
|
|
68
|
+
trys: [],
|
|
69
|
+
ops: []
|
|
70
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
|
|
71
|
+
return d(g, "next", {
|
|
72
|
+
value: verb(0)
|
|
73
|
+
}), d(g, "throw", {
|
|
74
|
+
value: verb(1)
|
|
75
|
+
}), d(g, "return", {
|
|
76
|
+
value: verb(2)
|
|
77
|
+
}), typeof Symbol === "function" && d(g, Symbol.iterator, {
|
|
78
|
+
value: function() {
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
}), g;
|
|
82
|
+
function verb(n) {
|
|
83
|
+
return function(v) {
|
|
84
|
+
return step([
|
|
85
|
+
n,
|
|
86
|
+
v
|
|
87
|
+
]);
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function step(op) {
|
|
91
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
92
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
93
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
94
|
+
if (y = 0, t) op = [
|
|
95
|
+
op[0] & 2,
|
|
96
|
+
t.value
|
|
97
|
+
];
|
|
98
|
+
switch(op[0]){
|
|
99
|
+
case 0:
|
|
100
|
+
case 1:
|
|
101
|
+
t = op;
|
|
102
|
+
break;
|
|
103
|
+
case 4:
|
|
104
|
+
_.label++;
|
|
105
|
+
return {
|
|
106
|
+
value: op[1],
|
|
107
|
+
done: false
|
|
108
|
+
};
|
|
109
|
+
case 5:
|
|
110
|
+
_.label++;
|
|
111
|
+
y = op[1];
|
|
112
|
+
op = [
|
|
113
|
+
0
|
|
114
|
+
];
|
|
115
|
+
continue;
|
|
116
|
+
case 7:
|
|
117
|
+
op = _.ops.pop();
|
|
118
|
+
_.trys.pop();
|
|
119
|
+
continue;
|
|
120
|
+
default:
|
|
121
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
122
|
+
_ = 0;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
126
|
+
_.label = op[1];
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
130
|
+
_.label = t[1];
|
|
131
|
+
t = op;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
if (t && _.label < t[2]) {
|
|
135
|
+
_.label = t[2];
|
|
136
|
+
_.ops.push(op);
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
if (t[2]) _.ops.pop();
|
|
140
|
+
_.trys.pop();
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
op = body.call(thisArg, _);
|
|
144
|
+
} catch (e) {
|
|
145
|
+
op = [
|
|
146
|
+
6,
|
|
147
|
+
e
|
|
148
|
+
];
|
|
149
|
+
y = 0;
|
|
150
|
+
} finally{
|
|
151
|
+
f = t = 0;
|
|
152
|
+
}
|
|
153
|
+
if (op[0] & 5) throw op[1];
|
|
154
|
+
return {
|
|
155
|
+
value: op[0] ? op[1] : void 0,
|
|
156
|
+
done: true
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function openDocument(src) {
|
|
161
|
+
return _async_to_generator(function() {
|
|
162
|
+
var data, err;
|
|
163
|
+
return _ts_generator(this, function(_state) {
|
|
164
|
+
switch(_state.label){
|
|
165
|
+
case 0:
|
|
166
|
+
try {
|
|
167
|
+
data = new Uint8Array((0, _nodefs.readFileSync)(src));
|
|
168
|
+
} catch (err) {
|
|
169
|
+
throw new _errorsts.PdfToMdError('PDF_OPEN', "cannot read ".concat(src, ": ").concat(err.message));
|
|
170
|
+
}
|
|
171
|
+
_state.label = 1;
|
|
172
|
+
case 1:
|
|
173
|
+
_state.trys.push([
|
|
174
|
+
1,
|
|
175
|
+
3,
|
|
176
|
+
,
|
|
177
|
+
4
|
|
178
|
+
]);
|
|
179
|
+
return [
|
|
180
|
+
4,
|
|
181
|
+
(0, _pdf.getDocument)({
|
|
182
|
+
data: data,
|
|
183
|
+
useSystemFonts: true,
|
|
184
|
+
verbosity: _pdf.VerbosityLevel.ERRORS
|
|
185
|
+
}).promise
|
|
186
|
+
];
|
|
187
|
+
case 2:
|
|
188
|
+
return [
|
|
189
|
+
2,
|
|
190
|
+
_state.sent()
|
|
191
|
+
];
|
|
192
|
+
case 3:
|
|
193
|
+
err = _state.sent();
|
|
194
|
+
throw new _errorsts.PdfToMdError('PDF_OPEN', "cannot open ".concat(src, " as a PDF: ").concat(err.message));
|
|
195
|
+
case 4:
|
|
196
|
+
return [
|
|
197
|
+
2
|
|
198
|
+
];
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
})();
|
|
202
|
+
}
|
|
203
|
+
// Wires up pdfjs's bundled cmaps/standard_fonts so CJK and non-embedded
|
|
204
|
+
// standard fonts resolve instead of falling back to substitute glyphs.
|
|
205
|
+
//
|
|
206
|
+
// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`
|
|
207
|
+
// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.
|
|
208
|
+
var PDFJS_DIST_ROOT = _nodepath.default.dirname((0, _resolvets.resolvePackagePath)('pdfjs-dist/package.json'));
|
|
209
|
+
var CMAP_URL = _nodepath.default.join(PDFJS_DIST_ROOT, 'cmaps') + _nodepath.default.sep;
|
|
210
|
+
var STANDARD_FONT_DATA_URL = _nodepath.default.join(PDFJS_DIST_ROOT, 'standard_fonts') + _nodepath.default.sep;
|
|
211
|
+
// JBIG2/JPEG2000 decoding lives in pdfjs's WASM modules: without this, bitonal
|
|
212
|
+
// scans (the usual encoding for B&W page images) decode to a BLANK page and OCR sees nothing.
|
|
213
|
+
var WASM_URL = _nodepath.default.join(PDFJS_DIST_ROOT, 'wasm') + _nodepath.default.sep;
|
|
214
|
+
function openPdfForRender(pdfPath) {
|
|
215
|
+
// No `worker` option: this pdfjs-dist Node build needs no real Worker thread wired up to run.
|
|
216
|
+
return (0, _pdf.getDocument)({
|
|
217
|
+
url: pdfPath,
|
|
218
|
+
cMapUrl: CMAP_URL,
|
|
219
|
+
cMapPacked: true,
|
|
220
|
+
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
|
221
|
+
wasmUrl: WASM_URL,
|
|
222
|
+
verbosity: _pdf.VerbosityLevel.ERRORS
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
function awaitPdfOpen(loadingTask, pdfPath) {
|
|
226
|
+
return _async_to_generator(function() {
|
|
227
|
+
var err;
|
|
228
|
+
return _ts_generator(this, function(_state) {
|
|
229
|
+
switch(_state.label){
|
|
230
|
+
case 0:
|
|
231
|
+
_state.trys.push([
|
|
232
|
+
0,
|
|
233
|
+
2,
|
|
234
|
+
,
|
|
235
|
+
3
|
|
236
|
+
]);
|
|
237
|
+
return [
|
|
238
|
+
4,
|
|
239
|
+
loadingTask.promise
|
|
240
|
+
];
|
|
241
|
+
case 1:
|
|
242
|
+
return [
|
|
243
|
+
2,
|
|
244
|
+
_state.sent()
|
|
245
|
+
];
|
|
246
|
+
case 2:
|
|
247
|
+
err = _state.sent();
|
|
248
|
+
throw new _errorsts.PdfToMdError('PDF_OPEN', "could not open PDF: ".concat(pdfPath, " (").concat(err.message, ")"));
|
|
249
|
+
case 3:
|
|
250
|
+
return [
|
|
251
|
+
2
|
|
252
|
+
];
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
})();
|
|
256
|
+
}
|
|
257
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/pdf-open.ts"],"sourcesContent":["// pdf-open.ts — the one seam that opens a PDF via pdfjs-dist, in the two\n// shapes callers need: openDocument (data-based, already-open doc) and openPdfForRender (url-based, task you must await + destroy()).\n// Both wrap a failure the same way: PdfToMdError/PDF_OPEN, original message preserved.\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\nimport type { PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { getDocument, VerbosityLevel } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { PdfToMdError } from './errors.ts';\nimport { resolvePackagePath } from './resolve.ts';\n\n// `verbosity: VerbosityLevel.ERRORS` silences pdfjs's own console.warn calls\n// — the library's documented promise is it never writes to stdout/stderr on its own.\n\n/** Data-based open (collect.ts's seam): no cMaps/fonts — text-layer extraction\n * only, verified to decode CJK correctly. Wraps missing/unreadable and non-PDF files as PdfToMdError/PDF_OPEN. */\nexport async function openDocument(src: string): Promise<PDFDocumentProxy> {\n let data: Uint8Array;\n try {\n data = new Uint8Array(readFileSync(src));\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot read ${src}: ${(err as Error).message}`);\n }\n try {\n return await getDocument({\n data,\n useSystemFonts: true,\n verbosity: VerbosityLevel.ERRORS,\n }).promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `cannot open ${src} as a PDF: ${(err as Error).message}`);\n }\n}\n\n// Wires up pdfjs's bundled cmaps/standard_fonts so CJK and non-embedded\n// standard fonts resolve instead of falling back to substitute glyphs.\n//\n// GOTCHA: despite the `...Url` name, pdfjs's Node loader does `fs.readFile`\n// on the string directly — a `file://` URL silently fails every lookup (only a stderr warning); plain filesystem paths are required.\nconst PDFJS_DIST_ROOT = path.dirname(resolvePackagePath('pdfjs-dist/package.json'));\nconst CMAP_URL = path.join(PDFJS_DIST_ROOT, 'cmaps') + path.sep;\nconst STANDARD_FONT_DATA_URL = path.join(PDFJS_DIST_ROOT, 'standard_fonts') + path.sep;\n// JBIG2/JPEG2000 decoding lives in pdfjs's WASM modules: without this, bitonal\n// scans (the usual encoding for B&W page images) decode to a BLANK page and OCR sees nothing.\nconst WASM_URL = path.join(PDFJS_DIST_ROOT, 'wasm') + path.sep;\n\n/**\n * Open a PDF for rendering (raster.ts/tesseract.ts's seam): cMaps + standard\n * fonts wired up, url-based. Callers must `await` the returned task's `.promise` (via `awaitPdfOpen`) and call `.destroy()` when done.\n */\nexport function openPdfForRender(pdfPath: string): PDFDocumentLoadingTask {\n // No `worker` option: this pdfjs-dist Node build needs no real Worker thread wired up to run.\n return getDocument({\n url: pdfPath,\n cMapUrl: CMAP_URL,\n cMapPacked: true,\n standardFontDataUrl: STANDARD_FONT_DATA_URL,\n wasmUrl: WASM_URL,\n verbosity: VerbosityLevel.ERRORS,\n });\n}\n\n/** Shared error wrapping for `openPdfForRender`'s task: a raw pdfjs open\n * failure becomes PdfToMdError/PDF_OPEN, original message preserved. */\nexport async function awaitPdfOpen(loadingTask: PDFDocumentLoadingTask, pdfPath: string): Promise<PDFDocumentProxy> {\n try {\n return await loadingTask.promise;\n } catch (err) {\n throw new PdfToMdError('PDF_OPEN', `could not open PDF: ${pdfPath} (${(err as Error).message})`);\n }\n}\n"],"names":["awaitPdfOpen","openDocument","openPdfForRender","src","data","err","Uint8Array","readFileSync","PdfToMdError","message","getDocument","useSystemFonts","verbosity","VerbosityLevel","ERRORS","promise","PDFJS_DIST_ROOT","path","dirname","resolvePackagePath","CMAP_URL","join","sep","STANDARD_FONT_DATA_URL","WASM_URL","pdfPath","url","cMapUrl","cMapPacked","standardFontDataUrl","wasmUrl","loadingTask"],"mappings":"AAAA,yEAAyE;AACzE,sIAAsI;AACtI,uFAAuF;;;;;;;;;;;;QA6DjEA;eAAAA;;QAhDAC;eAAAA;;QAkCNC;eAAAA;;;sBA9Ca;+DACZ;mBAE2B;wBACf;yBACM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAO5B,SAAeD,aAAaE,GAAW;;YACxCC,MAYKC;;;;oBAXT,IAAI;wBACFD,OAAO,IAAIE,WAAWC,IAAAA,oBAAY,EAACJ;oBACrC,EAAE,OAAOE,KAAK;wBACZ,MAAM,IAAIG,sBAAY,CAAC,YAAY,AAAC,eAAsB,OAARL,KAAI,MAA2B,OAAvB,AAACE,IAAcI,OAAO;oBAClF;;;;;;;;;oBAES;;wBAAMC,IAAAA,gBAAW,EAAC;4BACvBN,MAAAA;4BACAO,gBAAgB;4BAChBC,WAAWC,mBAAc,CAACC,MAAM;wBAClC,GAAGC,OAAO;;;oBAJV;;wBAAO;;;oBAKAV;oBACP,MAAM,IAAIG,sBAAY,CAAC,YAAY,AAAC,eAA+B,OAAjBL,KAAI,eAAoC,OAAvB,AAACE,IAAcI,OAAO;;;;;;;IAE7F;;AAEA,wEAAwE;AACxE,uEAAuE;AACvE,EAAE;AACF,4EAA4E;AAC5E,qIAAqI;AACrI,IAAMO,kBAAkBC,iBAAI,CAACC,OAAO,CAACC,IAAAA,6BAAkB,EAAC;AACxD,IAAMC,WAAWH,iBAAI,CAACI,IAAI,CAACL,iBAAiB,WAAWC,iBAAI,CAACK,GAAG;AAC/D,IAAMC,yBAAyBN,iBAAI,CAACI,IAAI,CAACL,iBAAiB,oBAAoBC,iBAAI,CAACK,GAAG;AACtF,+EAA+E;AAC/E,8FAA8F;AAC9F,IAAME,WAAWP,iBAAI,CAACI,IAAI,CAACL,iBAAiB,UAAUC,iBAAI,CAACK,GAAG;AAMvD,SAASpB,iBAAiBuB,OAAe;IAC9C,8FAA8F;IAC9F,OAAOf,IAAAA,gBAAW,EAAC;QACjBgB,KAAKD;QACLE,SAASP;QACTQ,YAAY;QACZC,qBAAqBN;QACrBO,SAASN;QACTZ,WAAWC,mBAAc,CAACC,MAAM;IAClC;AACF;AAIO,SAAed,aAAa+B,WAAmC,EAAEN,OAAe;;YAG5EpB;;;;;;;;;;oBADA;;wBAAM0B,YAAYhB,OAAO;;;oBAAhC;;wBAAO;;;oBACAV;oBACP,MAAM,IAAIG,sBAAY,CAAC,YAAY,AAAC,uBAAkC,OAAZiB,SAAQ,MAA2B,OAAvB,AAACpB,IAAcI,OAAO,EAAC;;;;;;;IAEjG"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';
|
|
2
|
+
export { openPdfForRender } from './pdf-open.js';
|
|
3
|
+
/** One rendered page, from renderPageToPNG (or the `pdf-to-md render` CLI
|
|
4
|
+
* command it backs). */
|
|
5
|
+
export interface RenderPageResult {
|
|
6
|
+
/** the rendered page, PNG-encoded */
|
|
7
|
+
png: Buffer;
|
|
8
|
+
/** pixel width at the requested dpi */
|
|
9
|
+
width: number;
|
|
10
|
+
/** pixel height at the requested dpi */
|
|
11
|
+
height: number;
|
|
12
|
+
/** the page's own /Rotate entry, baked into `png`/`width`/`height` already
|
|
13
|
+
* — informational, not something a caller needs to apply again */
|
|
14
|
+
rotation: number;
|
|
15
|
+
}
|
|
16
|
+
/** Render one already-open document's page to a PNG buffer at the given DPI. */
|
|
17
|
+
export declare function renderLoadedPage(doc: PDFDocumentProxy, pageNum: number, dpi: number): Promise<RenderPageResult>;
|
|
18
|
+
/**
|
|
19
|
+
* Render one page of a PDF to a PNG buffer at the given DPI. Opens and
|
|
20
|
+
* closes the document itself — for more than one page from the same PDF, use `openPdfForRender` + `renderLoadedPage` instead.
|
|
21
|
+
*
|
|
22
|
+
* @param pdfPath path to the PDF file
|
|
23
|
+
* @param pageNum 1-based page number
|
|
24
|
+
* @param dpi PDF user space is 72 units/inch, so scale = dpi / 72
|
|
25
|
+
*/
|
|
26
|
+
export declare function renderPageToPNG(pdfPath: string, pageNum: number, dpi: number): Promise<RenderPageResult>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { PDFDocumentProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';
|
|
2
|
+
export { openPdfForRender } from './pdf-open.js';
|
|
3
|
+
/** One rendered page, from renderPageToPNG (or the `pdf-to-md render` CLI
|
|
4
|
+
* command it backs). */
|
|
5
|
+
export interface RenderPageResult {
|
|
6
|
+
/** the rendered page, PNG-encoded */
|
|
7
|
+
png: Buffer;
|
|
8
|
+
/** pixel width at the requested dpi */
|
|
9
|
+
width: number;
|
|
10
|
+
/** pixel height at the requested dpi */
|
|
11
|
+
height: number;
|
|
12
|
+
/** the page's own /Rotate entry, baked into `png`/`width`/`height` already
|
|
13
|
+
* — informational, not something a caller needs to apply again */
|
|
14
|
+
rotation: number;
|
|
15
|
+
}
|
|
16
|
+
/** Render one already-open document's page to a PNG buffer at the given DPI. */
|
|
17
|
+
export declare function renderLoadedPage(doc: PDFDocumentProxy, pageNum: number, dpi: number): Promise<RenderPageResult>;
|
|
18
|
+
/**
|
|
19
|
+
* Render one page of a PDF to a PNG buffer at the given DPI. Opens and
|
|
20
|
+
* closes the document itself — for more than one page from the same PDF, use `openPdfForRender` + `renderLoadedPage` instead.
|
|
21
|
+
*
|
|
22
|
+
* @param pdfPath path to the PDF file
|
|
23
|
+
* @param pageNum 1-based page number
|
|
24
|
+
* @param dpi PDF user space is 72 units/inch, so scale = dpi / 72
|
|
25
|
+
*/
|
|
26
|
+
export declare function renderPageToPNG(pdfPath: string, pageNum: number, dpi: number): Promise<RenderPageResult>;
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// Renders PDF pages to PNG with pdfjs-dist + @napi-rs/canvas. Shared by the
|
|
2
|
+
// `render` CLI command (human/vision-model page verification) and src/engines/tesseract.ts (OCR page images).
|
|
3
|
+
"use strict";
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
function _export(target, all) {
|
|
8
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
_export(exports, {
|
|
14
|
+
get openPdfForRender () {
|
|
15
|
+
return _pdfopents.openPdfForRender;
|
|
16
|
+
},
|
|
17
|
+
get renderLoadedPage () {
|
|
18
|
+
return renderLoadedPage;
|
|
19
|
+
},
|
|
20
|
+
get renderPageToPNG () {
|
|
21
|
+
return renderPageToPNG;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
var _canvas = require("@napi-rs/canvas");
|
|
25
|
+
var _errorsts = require("./errors.js");
|
|
26
|
+
var _pdfopents = require("./pdf-open.js");
|
|
27
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
28
|
+
try {
|
|
29
|
+
var info = gen[key](arg);
|
|
30
|
+
var value = info.value;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
reject(error);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (info.done) resolve(value);
|
|
36
|
+
else Promise.resolve(value).then(_next, _throw);
|
|
37
|
+
}
|
|
38
|
+
function _async_to_generator(fn) {
|
|
39
|
+
return function() {
|
|
40
|
+
var self = this, args = arguments;
|
|
41
|
+
return new Promise(function(resolve, reject) {
|
|
42
|
+
var gen = fn.apply(self, args);
|
|
43
|
+
function _next(value) {
|
|
44
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
45
|
+
}
|
|
46
|
+
function _throw(err) {
|
|
47
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
48
|
+
}
|
|
49
|
+
_next(undefined);
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function _ts_generator(thisArg, body) {
|
|
54
|
+
var f, y, t, _ = {
|
|
55
|
+
label: 0,
|
|
56
|
+
sent: function() {
|
|
57
|
+
if (t[0] & 1) throw t[1];
|
|
58
|
+
return t[1];
|
|
59
|
+
},
|
|
60
|
+
trys: [],
|
|
61
|
+
ops: []
|
|
62
|
+
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
|
|
63
|
+
return d(g, "next", {
|
|
64
|
+
value: verb(0)
|
|
65
|
+
}), d(g, "throw", {
|
|
66
|
+
value: verb(1)
|
|
67
|
+
}), d(g, "return", {
|
|
68
|
+
value: verb(2)
|
|
69
|
+
}), typeof Symbol === "function" && d(g, Symbol.iterator, {
|
|
70
|
+
value: function() {
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
}), g;
|
|
74
|
+
function verb(n) {
|
|
75
|
+
return function(v) {
|
|
76
|
+
return step([
|
|
77
|
+
n,
|
|
78
|
+
v
|
|
79
|
+
]);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function step(op) {
|
|
83
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
84
|
+
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
85
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
86
|
+
if (y = 0, t) op = [
|
|
87
|
+
op[0] & 2,
|
|
88
|
+
t.value
|
|
89
|
+
];
|
|
90
|
+
switch(op[0]){
|
|
91
|
+
case 0:
|
|
92
|
+
case 1:
|
|
93
|
+
t = op;
|
|
94
|
+
break;
|
|
95
|
+
case 4:
|
|
96
|
+
_.label++;
|
|
97
|
+
return {
|
|
98
|
+
value: op[1],
|
|
99
|
+
done: false
|
|
100
|
+
};
|
|
101
|
+
case 5:
|
|
102
|
+
_.label++;
|
|
103
|
+
y = op[1];
|
|
104
|
+
op = [
|
|
105
|
+
0
|
|
106
|
+
];
|
|
107
|
+
continue;
|
|
108
|
+
case 7:
|
|
109
|
+
op = _.ops.pop();
|
|
110
|
+
_.trys.pop();
|
|
111
|
+
continue;
|
|
112
|
+
default:
|
|
113
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
114
|
+
_ = 0;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
118
|
+
_.label = op[1];
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
122
|
+
_.label = t[1];
|
|
123
|
+
t = op;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
if (t && _.label < t[2]) {
|
|
127
|
+
_.label = t[2];
|
|
128
|
+
_.ops.push(op);
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
if (t[2]) _.ops.pop();
|
|
132
|
+
_.trys.pop();
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
op = body.call(thisArg, _);
|
|
136
|
+
} catch (e) {
|
|
137
|
+
op = [
|
|
138
|
+
6,
|
|
139
|
+
e
|
|
140
|
+
];
|
|
141
|
+
y = 0;
|
|
142
|
+
} finally{
|
|
143
|
+
f = t = 0;
|
|
144
|
+
}
|
|
145
|
+
if (op[0] & 5) throw op[1];
|
|
146
|
+
return {
|
|
147
|
+
value: op[0] ? op[1] : void 0,
|
|
148
|
+
done: true
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function renderLoadedPage(doc, pageNum, dpi) {
|
|
153
|
+
return _async_to_generator(function() {
|
|
154
|
+
var page, scale, viewport, width, height, canvas, context, renderTask, png;
|
|
155
|
+
return _ts_generator(this, function(_state) {
|
|
156
|
+
switch(_state.label){
|
|
157
|
+
case 0:
|
|
158
|
+
if (!(pageNum >= 1) || pageNum > doc.numPages) {
|
|
159
|
+
throw new _errorsts.PdfToMdError('PAGE_RANGE', "page ".concat(pageNum, " is outside this document (1-").concat(doc.numPages, ")"));
|
|
160
|
+
}
|
|
161
|
+
return [
|
|
162
|
+
4,
|
|
163
|
+
doc.getPage(pageNum)
|
|
164
|
+
];
|
|
165
|
+
case 1:
|
|
166
|
+
page = _state.sent();
|
|
167
|
+
// getViewport({scale}) bakes in the page's own /Rotate entry AND its
|
|
168
|
+
// mediaBox origin automatically — no manual origin translate needed on top of it.
|
|
169
|
+
scale = dpi / 72;
|
|
170
|
+
viewport = page.getViewport({
|
|
171
|
+
scale: scale
|
|
172
|
+
});
|
|
173
|
+
width = Math.round(viewport.width);
|
|
174
|
+
height = Math.round(viewport.height);
|
|
175
|
+
if (width <= 0 || height <= 0) throw new _errorsts.PdfToMdError('PAGE_RENDER', "page ".concat(pageNum, " has non-positive dimensions ").concat(width, "x").concat(height));
|
|
176
|
+
canvas = (0, _canvas.createCanvas)(width, height);
|
|
177
|
+
context = canvas.getContext('2d');
|
|
178
|
+
// Paint white first: an unpainted @napi-rs/canvas context is transparent,
|
|
179
|
+
// which would encode as black/checkered rather than a white page.
|
|
180
|
+
context.fillStyle = 'white';
|
|
181
|
+
context.fillRect(0, 0, width, height);
|
|
182
|
+
// pdfjs's RenderParameters types canvas/context against DOM types;
|
|
183
|
+
// @napi-rs/canvas's are API-compatible at runtime but not that DOM type — the interop boundary the "no any" rule allows a cast for.
|
|
184
|
+
renderTask = page.render({
|
|
185
|
+
canvas: canvas,
|
|
186
|
+
canvasContext: context,
|
|
187
|
+
viewport: viewport
|
|
188
|
+
});
|
|
189
|
+
return [
|
|
190
|
+
4,
|
|
191
|
+
renderTask.promise
|
|
192
|
+
];
|
|
193
|
+
case 2:
|
|
194
|
+
_state.sent();
|
|
195
|
+
return [
|
|
196
|
+
4,
|
|
197
|
+
canvas.encode('png')
|
|
198
|
+
];
|
|
199
|
+
case 3:
|
|
200
|
+
png = _state.sent();
|
|
201
|
+
return [
|
|
202
|
+
2,
|
|
203
|
+
{
|
|
204
|
+
png: png,
|
|
205
|
+
width: width,
|
|
206
|
+
height: height,
|
|
207
|
+
rotation: viewport.rotation
|
|
208
|
+
}
|
|
209
|
+
];
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
})();
|
|
213
|
+
}
|
|
214
|
+
function renderPageToPNG(pdfPath, pageNum, dpi) {
|
|
215
|
+
return _async_to_generator(function() {
|
|
216
|
+
var loadingTask, doc;
|
|
217
|
+
return _ts_generator(this, function(_state) {
|
|
218
|
+
switch(_state.label){
|
|
219
|
+
case 0:
|
|
220
|
+
loadingTask = (0, _pdfopents.openPdfForRender)(pdfPath);
|
|
221
|
+
_state.label = 1;
|
|
222
|
+
case 1:
|
|
223
|
+
_state.trys.push([
|
|
224
|
+
1,
|
|
225
|
+
,
|
|
226
|
+
4,
|
|
227
|
+
6
|
|
228
|
+
]);
|
|
229
|
+
return [
|
|
230
|
+
4,
|
|
231
|
+
(0, _pdfopents.awaitPdfOpen)(loadingTask, pdfPath)
|
|
232
|
+
];
|
|
233
|
+
case 2:
|
|
234
|
+
doc = _state.sent();
|
|
235
|
+
return [
|
|
236
|
+
4,
|
|
237
|
+
renderLoadedPage(doc, pageNum, dpi)
|
|
238
|
+
];
|
|
239
|
+
case 3:
|
|
240
|
+
return [
|
|
241
|
+
2,
|
|
242
|
+
_state.sent()
|
|
243
|
+
];
|
|
244
|
+
case 4:
|
|
245
|
+
return [
|
|
246
|
+
4,
|
|
247
|
+
loadingTask.destroy()
|
|
248
|
+
];
|
|
249
|
+
case 5:
|
|
250
|
+
_state.sent();
|
|
251
|
+
return [
|
|
252
|
+
7
|
|
253
|
+
];
|
|
254
|
+
case 6:
|
|
255
|
+
return [
|
|
256
|
+
2
|
|
257
|
+
];
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
})();
|
|
261
|
+
}
|
|
262
|
+
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Projects/pdf-to-md/src/raster.ts"],"sourcesContent":["// Renders PDF pages to PNG with pdfjs-dist + @napi-rs/canvas. Shared by the\n// `render` CLI command (human/vision-model page verification) and src/engines/tesseract.ts (OCR page images).\n\nimport { createCanvas } from '@napi-rs/canvas';\nimport type { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist/legacy/build/pdf.mjs';\nimport { PdfToMdError } from './errors.ts';\nimport { awaitPdfOpen, openPdfForRender } from './pdf-open.ts';\n\n// pdfjs-dist doesn't export RenderParameters from the package root; derive\n// it from PDFPageProxy['render'] rather than hand-declaring a shape that could drift.\ntype RenderParams = Parameters<PDFPageProxy['render']>[0];\n\n// No CanvasFactory shim needed: pdfjs-dist detects `@napi-rs/canvas` itself\n// and uses it internally (patterns, SMasks, Type3 glyphs); we only hand it a `canvasContext` for the page itself, created directly below.\n\nexport { openPdfForRender } from './pdf-open.ts';\n\n/** One rendered page, from renderPageToPNG (or the `pdf-to-md render` CLI\n * command it backs). */\nexport interface RenderPageResult {\n /** the rendered page, PNG-encoded */\n png: Buffer;\n /** pixel width at the requested dpi */\n width: number;\n /** pixel height at the requested dpi */\n height: number;\n /** the page's own /Rotate entry, baked into `png`/`width`/`height` already\n * — informational, not something a caller needs to apply again */\n rotation: number;\n}\n\n/** Render one already-open document's page to a PNG buffer at the given DPI. */\nexport async function renderLoadedPage(doc: PDFDocumentProxy, pageNum: number, dpi: number): Promise<RenderPageResult> {\n if (!(pageNum >= 1) || pageNum > doc.numPages) {\n throw new PdfToMdError('PAGE_RANGE', `page ${pageNum} is outside this document (1-${doc.numPages})`);\n }\n const page = await doc.getPage(pageNum);\n\n // getViewport({scale}) bakes in the page's own /Rotate entry AND its\n // mediaBox origin automatically — no manual origin translate needed on top of it.\n const scale = dpi / 72;\n const viewport = page.getViewport({ scale });\n const width = Math.round(viewport.width);\n const height = Math.round(viewport.height);\n if (width <= 0 || height <= 0) throw new PdfToMdError('PAGE_RENDER', `page ${pageNum} has non-positive dimensions ${width}x${height}`);\n\n const canvas = createCanvas(width, height);\n const context = canvas.getContext('2d');\n\n // Paint white first: an unpainted @napi-rs/canvas context is transparent,\n // which would encode as black/checkered rather than a white page.\n context.fillStyle = 'white';\n context.fillRect(0, 0, width, height);\n\n // pdfjs's RenderParameters types canvas/context against DOM types;\n // @napi-rs/canvas's are API-compatible at runtime but not that DOM type — the interop boundary the \"no any\" rule allows a cast for.\n const renderTask = page.render({\n canvas: canvas as unknown as RenderParams['canvas'],\n canvasContext: context as unknown as RenderParams['canvasContext'],\n viewport,\n });\n await renderTask.promise;\n\n const png = await canvas.encode('png');\n return { png, width, height, rotation: viewport.rotation };\n}\n\n/**\n * Render one page of a PDF to a PNG buffer at the given DPI. Opens and\n * closes the document itself — for more than one page from the same PDF, use `openPdfForRender` + `renderLoadedPage` instead.\n *\n * @param pdfPath path to the PDF file\n * @param pageNum 1-based page number\n * @param dpi PDF user space is 72 units/inch, so scale = dpi / 72\n */\nexport async function renderPageToPNG(pdfPath: string, pageNum: number, dpi: number): Promise<RenderPageResult> {\n const loadingTask = openPdfForRender(pdfPath);\n try {\n const doc = await awaitPdfOpen(loadingTask, pdfPath);\n return await renderLoadedPage(doc, pageNum, dpi);\n } finally {\n await loadingTask.destroy();\n }\n}\n"],"names":["openPdfForRender","renderLoadedPage","renderPageToPNG","doc","pageNum","dpi","page","scale","viewport","width","height","canvas","context","renderTask","png","numPages","PdfToMdError","getPage","getViewport","Math","round","createCanvas","getContext","fillStyle","fillRect","render","canvasContext","promise","encode","rotation","pdfPath","loadingTask","awaitPdfOpen","destroy"],"mappings":"AAAA,4EAA4E;AAC5E,8GAA8G;;;;;;;;;;;;QAcrGA;eAAAA,2BAAgB;;QAiBHC;eAAAA;;QA2CAC;eAAAA;;;sBAxEO;wBAEA;yBACkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BxC,SAAeD,iBAAiBE,GAAqB,EAAEC,OAAe,EAAEC,GAAW;;YAIlFC,MAIAC,OACAC,UACAC,OACAC,QAGAC,QACAC,SASAC,YAOAC;;;;oBA9BN,IAAI,CAAEV,CAAAA,WAAW,CAAA,KAAMA,UAAUD,IAAIY,QAAQ,EAAE;wBAC7C,MAAM,IAAIC,sBAAY,CAAC,cAAc,AAAC,QAA8Cb,OAAvCC,SAAQ,iCAA4C,OAAbD,IAAIY,QAAQ,EAAC;oBACnG;oBACa;;wBAAMZ,IAAIc,OAAO,CAACb;;;oBAAzBE,OAAO;oBAEb,qEAAqE;oBACrE,kFAAkF;oBAC5EC,QAAQF,MAAM;oBACdG,WAAWF,KAAKY,WAAW,CAAC;wBAAEX,OAAAA;oBAAM;oBACpCE,QAAQU,KAAKC,KAAK,CAACZ,SAASC,KAAK;oBACjCC,SAASS,KAAKC,KAAK,CAACZ,SAASE,MAAM;oBACzC,IAAID,SAAS,KAAKC,UAAU,GAAG,MAAM,IAAIM,sBAAY,CAAC,eAAe,AAAC,QAA8CP,OAAvCL,SAAQ,iCAAwCM,OAATD,OAAM,KAAU,OAAPC;oBAEvHC,SAASU,IAAAA,oBAAY,EAACZ,OAAOC;oBAC7BE,UAAUD,OAAOW,UAAU,CAAC;oBAElC,0EAA0E;oBAC1E,kEAAkE;oBAClEV,QAAQW,SAAS,GAAG;oBACpBX,QAAQY,QAAQ,CAAC,GAAG,GAAGf,OAAOC;oBAE9B,mEAAmE;oBACnE,oIAAoI;oBAC9HG,aAAaP,KAAKmB,MAAM,CAAC;wBAC7Bd,QAAQA;wBACRe,eAAed;wBACfJ,UAAAA;oBACF;oBACA;;wBAAMK,WAAWc,OAAO;;;oBAAxB;oBAEY;;wBAAMhB,OAAOiB,MAAM,CAAC;;;oBAA1Bd,MAAM;oBACZ;;wBAAO;4BAAEA,KAAAA;4BAAKL,OAAAA;4BAAOC,QAAAA;4BAAQmB,UAAUrB,SAASqB,QAAQ;wBAAC;;;;IAC3D;;AAUO,SAAe3B,gBAAgB4B,OAAe,EAAE1B,OAAe,EAAEC,GAAW;;YAC3E0B,aAEE5B;;;;oBAFF4B,cAAc/B,IAAAA,2BAAgB,EAAC8B;;;;;;;;;oBAEvB;;wBAAME,IAAAA,uBAAY,EAACD,aAAaD;;;oBAAtC3B,MAAM;oBACL;;wBAAMF,iBAAiBE,KAAKC,SAASC;;;oBAA5C;;wBAAO;;;oBAEP;;wBAAM0B,YAAYE,OAAO;;;oBAAzB;;;;;;;;;;IAEJ"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Analysis, AnalysisPage } from './types.js';
|
|
2
|
+
/** Unvalidated core, reused by `src/analyze.ts`. `forcePageMarkers` is the
|
|
3
|
+
* OCR path's always-on "### pN"; `pageMarkers` is the text path's opt-in flag, ignored when `forcePageMarkers` is set. */
|
|
4
|
+
export declare function renderPagesToMarkdown(pages: AnalysisPage[], opts: {
|
|
5
|
+
forcePageMarkers: boolean;
|
|
6
|
+
pageMarkers: boolean;
|
|
7
|
+
}): string;
|
|
8
|
+
export declare function renderPagesToText(pages: AnalysisPage[]): string;
|
|
9
|
+
/**
|
|
10
|
+
* Render an Analysis to markdown. PURE and fail-fast over any well-formed Analysis (including hand-built/spliced ones) — throws `PdfToMdError`/`'ANALYSIS_INPUT'` before any output on a malformed one.
|
|
11
|
+
* OCR-origin output is byte-identical to extractOcr's own markdown (shared core); text-origin is NOT byte-identical to extractText's (see `AnalysisPage.words`, `src/types.ts`).
|
|
12
|
+
*
|
|
13
|
+
* @param analysis the Analysis to render (from analyze(), or hand-built/spliced)
|
|
14
|
+
* @param opts.pageMarkers text-origin only; an OCR-origin Analysis always carries markers
|
|
15
|
+
*/
|
|
16
|
+
export declare function toMarkdown(analysis: Analysis, opts?: {
|
|
17
|
+
pageMarkers?: boolean;
|
|
18
|
+
}): string;
|
|
19
|
+
/**
|
|
20
|
+
* Render an Analysis to reading-order plain text: headings drop their `#`;
|
|
21
|
+
* float/review content is INCLUDED with only its markdown decoration stripped, so no content is lost silently. Same fail-fast validation as toMarkdown.
|
|
22
|
+
*
|
|
23
|
+
* @param analysis the Analysis to render (from analyze(), or hand-built/spliced)
|
|
24
|
+
*/
|
|
25
|
+
export declare function toText(analysis: Analysis): string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Analysis, AnalysisPage } from './types.js';
|
|
2
|
+
/** Unvalidated core, reused by `src/analyze.ts`. `forcePageMarkers` is the
|
|
3
|
+
* OCR path's always-on "### pN"; `pageMarkers` is the text path's opt-in flag, ignored when `forcePageMarkers` is set. */
|
|
4
|
+
export declare function renderPagesToMarkdown(pages: AnalysisPage[], opts: {
|
|
5
|
+
forcePageMarkers: boolean;
|
|
6
|
+
pageMarkers: boolean;
|
|
7
|
+
}): string;
|
|
8
|
+
export declare function renderPagesToText(pages: AnalysisPage[]): string;
|
|
9
|
+
/**
|
|
10
|
+
* Render an Analysis to markdown. PURE and fail-fast over any well-formed Analysis (including hand-built/spliced ones) — throws `PdfToMdError`/`'ANALYSIS_INPUT'` before any output on a malformed one.
|
|
11
|
+
* OCR-origin output is byte-identical to extractOcr's own markdown (shared core); text-origin is NOT byte-identical to extractText's (see `AnalysisPage.words`, `src/types.ts`).
|
|
12
|
+
*
|
|
13
|
+
* @param analysis the Analysis to render (from analyze(), or hand-built/spliced)
|
|
14
|
+
* @param opts.pageMarkers text-origin only; an OCR-origin Analysis always carries markers
|
|
15
|
+
*/
|
|
16
|
+
export declare function toMarkdown(analysis: Analysis, opts?: {
|
|
17
|
+
pageMarkers?: boolean;
|
|
18
|
+
}): string;
|
|
19
|
+
/**
|
|
20
|
+
* Render an Analysis to reading-order plain text: headings drop their `#`;
|
|
21
|
+
* float/review content is INCLUDED with only its markdown decoration stripped, so no content is lost silently. Same fail-fast validation as toMarkdown.
|
|
22
|
+
*
|
|
23
|
+
* @param analysis the Analysis to render (from analyze(), or hand-built/spliced)
|
|
24
|
+
*/
|
|
25
|
+
export declare function toText(analysis: Analysis): string;
|