-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.ts
More file actions
60 lines (54 loc) · 1.63 KB
/
Copy pathindex.ts
File metadata and controls
60 lines (54 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { ConvertOptions } from './types.js';
// Native addon — resolved at runtime via optional dep lookup
function loadNative(): {
convert: (inputPath: string, options?: Record<string, unknown>) => string;
version: () => string;
} {
const platforms: Record<string, string> = {
'linux-x64': 'edgeparse-linux-x64-gnu',
'linux-arm64': 'edgeparse-linux-arm64-gnu',
'darwin-x64': 'edgeparse-darwin-x64',
'darwin-arm64': 'edgeparse-darwin-arm64',
'win32-x64': 'edgeparse-win32-x64-msvc',
};
const key = `${process.platform}-${process.arch}`;
const pkg = platforms[key];
if (!pkg) throw new Error(`edgeparse: unsupported platform: ${key}`);
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(pkg);
}
let native: ReturnType<typeof loadNative> | undefined;
function getNative() {
if (!native) {
native = loadNative();
}
return native;
}
/**
* Convert a PDF file and return the extracted content as a string.
*
* @param inputPath - Path to the PDF file.
* @param options - Conversion options.
* @returns The extracted content as a string.
*/
export function convert(
inputPath: string,
options?: ConvertOptions,
): string {
const n = getNative();
return n.convert(inputPath, options ? {
format: options.format,
pages: options.pages,
password: options.password,
reading_order: options.readingOrder,
table_method: options.tableMethod,
image_output: options.imageOutput,
} : undefined);
}
/**
* Return the edgeparse version string.
*/
export function version(): string {
return getNative().version();
}
export type { ConvertOptions };