pdfium-native 0.1.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/LICENSE +21 -0
- package/README.md +227 -0
- package/binding.gyp +85 -0
- package/dist/document.d.ts +23 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +36 -0
- package/dist/document.js.map +1 -0
- package/dist/errors.d.ts +23 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +58 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/page.d.ts +40 -0
- package/dist/page.d.ts.map +1 -0
- package/dist/page.js +59 -0
- package/dist/page.js.map +1 -0
- package/dist/types.d.ts +120 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/lib/__tests__/document.test.ts +99 -0
- package/lib/__tests__/features.test.ts +72 -0
- package/lib/__tests__/fixtures.ts +71 -0
- package/lib/__tests__/load.test.ts +68 -0
- package/lib/__tests__/page.test.ts +123 -0
- package/lib/__tests__/render.test.ts +103 -0
- package/lib/__tests__/search.test.ts +44 -0
- package/lib/document.ts +40 -0
- package/lib/errors.ts +63 -0
- package/lib/index.ts +63 -0
- package/lib/page.ts +80 -0
- package/lib/types.ts +154 -0
- package/package.json +80 -0
- package/scripts/bundle-lib.mjs +66 -0
- package/scripts/download-pdfium.mjs +84 -0
- package/scripts/generate-fixtures.mjs +194 -0
- package/scripts/install.mjs +80 -0
- package/src/document.h +254 -0
- package/src/napi_helpers.h +50 -0
- package/src/page.h +576 -0
- package/src/pdfium_addon.cc +218 -0
- package/src/render_worker.h +141 -0
- package/src/stb_image_write.c +2 -0
- package/src/stb_image_write.h +1724 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { loadDocument } from '../index.js';
|
|
3
|
+
import { textPdfBuffer } from './fixtures.js';
|
|
4
|
+
|
|
5
|
+
describe('PDFiumPage.search', () => {
|
|
6
|
+
it('finds text occurrences with positions', async () => {
|
|
7
|
+
const doc = await loadDocument(textPdfBuffer);
|
|
8
|
+
const page = await doc.getPage(0);
|
|
9
|
+
const results = page.search('Hello');
|
|
10
|
+
expect(results.length).toBe(1);
|
|
11
|
+
expect(results[0]!.charIndex).toBeGreaterThanOrEqual(0);
|
|
12
|
+
expect(results[0]!.length).toBe(5);
|
|
13
|
+
expect(Array.isArray(results[0]!.rects)).toBe(true);
|
|
14
|
+
expect(results[0]!.rects.length).toBeGreaterThan(0);
|
|
15
|
+
expect(results[0]!.rects[0]).toHaveProperty('left');
|
|
16
|
+
expect(results[0]!.rects[0]).toHaveProperty('top');
|
|
17
|
+
expect(results[0]!.rects[0]).toHaveProperty('right');
|
|
18
|
+
expect(results[0]!.rects[0]).toHaveProperty('bottom');
|
|
19
|
+
page.close();
|
|
20
|
+
doc.destroy();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('returns empty for non-matching text', async () => {
|
|
24
|
+
const doc = await loadDocument(textPdfBuffer);
|
|
25
|
+
const page = await doc.getPage(0);
|
|
26
|
+
const results = page.search('xyz-no-match');
|
|
27
|
+
expect(results.length).toBe(0);
|
|
28
|
+
page.close();
|
|
29
|
+
doc.destroy();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('respects caseSensitive option', async () => {
|
|
33
|
+
const doc = await loadDocument(textPdfBuffer);
|
|
34
|
+
const page = await doc.getPage(0);
|
|
35
|
+
// case-insensitive (default) should find it
|
|
36
|
+
const insensitive = page.search('hello');
|
|
37
|
+
expect(insensitive.length).toBe(1);
|
|
38
|
+
// case-sensitive should not find lowercase when original is capitalized
|
|
39
|
+
const sensitive = page.search('hello', { caseSensitive: true });
|
|
40
|
+
expect(sensitive.length).toBe(0);
|
|
41
|
+
page.close();
|
|
42
|
+
doc.destroy();
|
|
43
|
+
});
|
|
44
|
+
});
|
package/lib/document.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { PDFiumPage } from './page.js';
|
|
2
|
+
import type { Bookmark, DocumentMetadata, NativeDocument } from './types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A loaded PDF document.
|
|
6
|
+
*/
|
|
7
|
+
export class PDFiumDocument {
|
|
8
|
+
/** Total number of pages. */
|
|
9
|
+
readonly pageCount: number;
|
|
10
|
+
/** Document metadata (title, author, dates, etc.). */
|
|
11
|
+
readonly metadata: DocumentMetadata;
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
constructor(private native: NativeDocument) {
|
|
15
|
+
this.pageCount = native.pageCount;
|
|
16
|
+
this.metadata = native.metadata;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Gets a page by 0-based index. */
|
|
20
|
+
async getPage(index: number): Promise<PDFiumPage> {
|
|
21
|
+
return new PDFiumPage(await this.native.getPage(index));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Iterates over all pages. Caller is responsible for closing each page. */
|
|
25
|
+
async *pages(): AsyncGenerator<PDFiumPage> {
|
|
26
|
+
for (let i = 0; i < this.pageCount; i++) {
|
|
27
|
+
yield this.getPage(i);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Closes the document and frees all resources. */
|
|
32
|
+
destroy(): void {
|
|
33
|
+
this.native.destroy();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Returns the bookmark/outline tree. */
|
|
37
|
+
getBookmarks(): Bookmark[] {
|
|
38
|
+
return this.native.getBookmarks();
|
|
39
|
+
}
|
|
40
|
+
}
|
package/lib/errors.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** Base error for all PDFium operations. */
|
|
2
|
+
export class PDFiumError extends Error {
|
|
3
|
+
readonly code: string;
|
|
4
|
+
constructor(code: string, message: string) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'PDFiumError';
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Thrown when the file cannot be found or opened. */
|
|
12
|
+
export class PDFiumFileError extends PDFiumError {
|
|
13
|
+
constructor(message: string) {
|
|
14
|
+
super('FILE', message);
|
|
15
|
+
this.name = 'PDFiumFileError';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Thrown when the input is not a valid PDF or is corrupted. */
|
|
20
|
+
export class PDFiumFormatError extends PDFiumError {
|
|
21
|
+
constructor(message: string) {
|
|
22
|
+
super('FORMAT', message);
|
|
23
|
+
this.name = 'PDFiumFormatError';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Thrown when a password is required or incorrect. */
|
|
28
|
+
export class PDFiumPasswordError extends PDFiumError {
|
|
29
|
+
constructor(message: string) {
|
|
30
|
+
super('PASSWORD', message);
|
|
31
|
+
this.name = 'PDFiumPasswordError';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Thrown when the PDF uses an unsupported security scheme. */
|
|
36
|
+
export class PDFiumSecurityError extends PDFiumError {
|
|
37
|
+
constructor(message: string) {
|
|
38
|
+
super('SECURITY', message);
|
|
39
|
+
this.name = 'PDFiumSecurityError';
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function parseNativeError(err: unknown): PDFiumError {
|
|
44
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
45
|
+
const colonIdx = msg.indexOf(':');
|
|
46
|
+
if (colonIdx === -1) return new PDFiumError('UNKNOWN', msg);
|
|
47
|
+
|
|
48
|
+
const code = msg.slice(0, colonIdx);
|
|
49
|
+
const text = msg.slice(colonIdx + 1);
|
|
50
|
+
|
|
51
|
+
switch (code) {
|
|
52
|
+
case 'FILE':
|
|
53
|
+
return new PDFiumFileError(text);
|
|
54
|
+
case 'FORMAT':
|
|
55
|
+
return new PDFiumFormatError(text);
|
|
56
|
+
case 'PASSWORD':
|
|
57
|
+
return new PDFiumPasswordError(text);
|
|
58
|
+
case 'SECURITY':
|
|
59
|
+
return new PDFiumSecurityError(text);
|
|
60
|
+
default:
|
|
61
|
+
return new PDFiumError(code, text);
|
|
62
|
+
}
|
|
63
|
+
}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { dirname, resolve } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { PDFiumDocument } from './document.js';
|
|
5
|
+
import { parseNativeError } from './errors.js';
|
|
6
|
+
import type { NativeAddon } from './types.js';
|
|
7
|
+
|
|
8
|
+
const require = createRequire(import.meta.url);
|
|
9
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
|
|
11
|
+
type PdfInput = Buffer | string;
|
|
12
|
+
|
|
13
|
+
// ensure the dynamic linker can find libpdfium next to pdfium.node.
|
|
14
|
+
// note: LD_LIBRARY_PATH is read at process startup, so this only helps
|
|
15
|
+
// child processes or dlopen calls that haven't been resolved yet.
|
|
16
|
+
const addonDir = resolve(__dirname, '..', 'build', 'Release');
|
|
17
|
+
if (process.platform === 'win32') {
|
|
18
|
+
process.env.PATH = `${addonDir};${process.env.PATH ?? ''}`;
|
|
19
|
+
} else {
|
|
20
|
+
process.env.LD_LIBRARY_PATH = `${addonDir}:${process.env.LD_LIBRARY_PATH ?? ''}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const addon: NativeAddon = require('../build/Release/pdfium.node');
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Opens a PDF document from a Buffer or file path.
|
|
27
|
+
*/
|
|
28
|
+
export async function loadDocument(input: PdfInput, password?: string): Promise<PDFiumDocument> {
|
|
29
|
+
try {
|
|
30
|
+
return new PDFiumDocument(await addon.loadDocument(input, password));
|
|
31
|
+
} catch (err) {
|
|
32
|
+
throw parseNativeError(err);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { PDFiumDocument } from './document.js';
|
|
37
|
+
export {
|
|
38
|
+
PDFiumError,
|
|
39
|
+
PDFiumFileError,
|
|
40
|
+
PDFiumFormatError,
|
|
41
|
+
PDFiumPasswordError,
|
|
42
|
+
PDFiumSecurityError,
|
|
43
|
+
} from './errors.js';
|
|
44
|
+
export { PDFiumPage } from './page.js';
|
|
45
|
+
export type {
|
|
46
|
+
Annotation,
|
|
47
|
+
AnnotationType,
|
|
48
|
+
Bookmark,
|
|
49
|
+
DocumentMetadata,
|
|
50
|
+
ImagePageObject,
|
|
51
|
+
Link,
|
|
52
|
+
OtherPageObject,
|
|
53
|
+
PageObject,
|
|
54
|
+
PageObjectBounds,
|
|
55
|
+
PageObjectType,
|
|
56
|
+
PageRenderOptions,
|
|
57
|
+
PageSize,
|
|
58
|
+
RGBA,
|
|
59
|
+
SearchMatch,
|
|
60
|
+
SearchOptions,
|
|
61
|
+
SearchRect,
|
|
62
|
+
TextPageObject,
|
|
63
|
+
} from './types.js';
|
package/lib/page.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Annotation,
|
|
3
|
+
Link,
|
|
4
|
+
NativePage,
|
|
5
|
+
PageObject,
|
|
6
|
+
PageRenderOptions,
|
|
7
|
+
PageSize,
|
|
8
|
+
SearchMatch,
|
|
9
|
+
SearchOptions,
|
|
10
|
+
} from './types.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A page in a PDF document.
|
|
14
|
+
*/
|
|
15
|
+
export class PDFiumPage {
|
|
16
|
+
/** Page width in points (1 pt = 1/72 inch). */
|
|
17
|
+
readonly width: number;
|
|
18
|
+
/** Page height in points (1 pt = 1/72 inch). */
|
|
19
|
+
readonly height: number;
|
|
20
|
+
/** Page dimensions in points. */
|
|
21
|
+
readonly size: PageSize;
|
|
22
|
+
/** 0-based page index. */
|
|
23
|
+
readonly number: number;
|
|
24
|
+
/** Number of page objects (text spans, paths, images, etc.). */
|
|
25
|
+
readonly objectCount: number;
|
|
26
|
+
|
|
27
|
+
/** @internal */
|
|
28
|
+
constructor(private native: NativePage) {
|
|
29
|
+
this.width = native.width;
|
|
30
|
+
this.height = native.height;
|
|
31
|
+
this.size = native.size;
|
|
32
|
+
this.number = native.number;
|
|
33
|
+
this.objectCount = native.objectCount;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Extracts all text from the page. */
|
|
37
|
+
getText(): string {
|
|
38
|
+
return this.native.getText();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Renders the page to a file. */
|
|
42
|
+
render(options: PageRenderOptions & { output: string }): Promise<void>;
|
|
43
|
+
/** Renders the page to an encoded image buffer (JPEG or PNG). */
|
|
44
|
+
render(options?: PageRenderOptions): Promise<Buffer>;
|
|
45
|
+
render(options?: PageRenderOptions): Promise<Buffer | void> {
|
|
46
|
+
return this.native.render(options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Returns the page object at the given index with type and bounds. */
|
|
50
|
+
getObject(index: number): PageObject {
|
|
51
|
+
return this.native.getObject(index);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Iterates over all page objects (text spans, paths, images, etc.). */
|
|
55
|
+
*objects(): Generator<PageObject> {
|
|
56
|
+
for (let i = 0; i < this.objectCount; i++) {
|
|
57
|
+
yield this.getObject(i);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Closes the page and frees resources. */
|
|
62
|
+
close(): void {
|
|
63
|
+
this.native.close();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Returns all links on the page. */
|
|
67
|
+
getLinks(): Link[] {
|
|
68
|
+
return this.native.getLinks();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Searches for text on the page. Returns matches with positions and bounding rects. */
|
|
72
|
+
search(text: string, options?: SearchOptions): SearchMatch[] {
|
|
73
|
+
return this.native.search(text, options);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Returns all annotations on the page. */
|
|
77
|
+
getAnnotations(): Annotation[] {
|
|
78
|
+
return this.native.getAnnotations();
|
|
79
|
+
}
|
|
80
|
+
}
|
package/lib/types.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
export interface PageSize {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface PageRenderOptions {
|
|
7
|
+
/** Scale factor (default: 1, meaning 72 DPI). Use 3–4 for print quality. */
|
|
8
|
+
scale?: number;
|
|
9
|
+
/** Override render width in pixels. */
|
|
10
|
+
width?: number;
|
|
11
|
+
/** Override render height in pixels. */
|
|
12
|
+
height?: number;
|
|
13
|
+
/** Output format (default: 'jpeg'). */
|
|
14
|
+
format?: 'jpeg' | 'png';
|
|
15
|
+
/** JPEG quality 1–100 (default: 100). Ignored for PNG. */
|
|
16
|
+
quality?: number;
|
|
17
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
18
|
+
output?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PageObjectBounds {
|
|
22
|
+
left: number;
|
|
23
|
+
bottom: number;
|
|
24
|
+
right: number;
|
|
25
|
+
top: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface RGBA {
|
|
29
|
+
r: number;
|
|
30
|
+
g: number;
|
|
31
|
+
b: number;
|
|
32
|
+
a: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface BasePageObject {
|
|
36
|
+
bounds: PageObjectBounds;
|
|
37
|
+
fillColor: RGBA | null;
|
|
38
|
+
strokeColor: RGBA | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface TextPageObject extends BasePageObject {
|
|
42
|
+
type: 'text';
|
|
43
|
+
text: string;
|
|
44
|
+
fontSize: number;
|
|
45
|
+
fontName: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ImagePageObject extends BasePageObject {
|
|
49
|
+
type: 'image';
|
|
50
|
+
imageWidth: number;
|
|
51
|
+
imageHeight: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface OtherPageObject extends BasePageObject {
|
|
55
|
+
type: 'path' | 'shading' | 'form' | 'unknown';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type PageObject = TextPageObject | ImagePageObject | OtherPageObject;
|
|
59
|
+
export type PageObjectType = PageObject['type'];
|
|
60
|
+
|
|
61
|
+
export interface SearchOptions {
|
|
62
|
+
caseSensitive?: boolean;
|
|
63
|
+
wholeWord?: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface SearchMatch {
|
|
67
|
+
charIndex: number;
|
|
68
|
+
length: number;
|
|
69
|
+
rects: SearchRect[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface SearchRect {
|
|
73
|
+
left: number;
|
|
74
|
+
top: number;
|
|
75
|
+
right: number;
|
|
76
|
+
bottom: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type AnnotationType =
|
|
80
|
+
| 'text'
|
|
81
|
+
| 'link'
|
|
82
|
+
| 'freetext'
|
|
83
|
+
| 'line'
|
|
84
|
+
| 'square'
|
|
85
|
+
| 'circle'
|
|
86
|
+
| 'highlight'
|
|
87
|
+
| 'underline'
|
|
88
|
+
| 'squiggly'
|
|
89
|
+
| 'strikeout'
|
|
90
|
+
| 'stamp'
|
|
91
|
+
| 'ink'
|
|
92
|
+
| 'popup'
|
|
93
|
+
| 'widget'
|
|
94
|
+
| 'redact'
|
|
95
|
+
| 'unknown';
|
|
96
|
+
|
|
97
|
+
export interface Annotation {
|
|
98
|
+
type: AnnotationType;
|
|
99
|
+
bounds?: PageObjectBounds;
|
|
100
|
+
contents: string;
|
|
101
|
+
color: RGBA | null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface Link {
|
|
105
|
+
bounds?: PageObjectBounds;
|
|
106
|
+
url?: string;
|
|
107
|
+
pageIndex?: number;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface Bookmark {
|
|
111
|
+
title: string;
|
|
112
|
+
pageIndex?: number;
|
|
113
|
+
children?: Bookmark[];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface DocumentMetadata {
|
|
117
|
+
title: string;
|
|
118
|
+
author: string;
|
|
119
|
+
subject: string;
|
|
120
|
+
keywords: string;
|
|
121
|
+
creator: string;
|
|
122
|
+
producer: string;
|
|
123
|
+
creationDate: string;
|
|
124
|
+
modDate: string;
|
|
125
|
+
pdfVersion: number;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// native addon bindings (internal)
|
|
129
|
+
export interface NativePage {
|
|
130
|
+
readonly number: number;
|
|
131
|
+
readonly width: number;
|
|
132
|
+
readonly height: number;
|
|
133
|
+
readonly size: PageSize;
|
|
134
|
+
readonly objectCount: number;
|
|
135
|
+
getText(): string;
|
|
136
|
+
render(options?: PageRenderOptions): Promise<Buffer | void>;
|
|
137
|
+
getObject(index: number): PageObject;
|
|
138
|
+
getLinks(): Link[];
|
|
139
|
+
search(text: string, options?: SearchOptions): SearchMatch[];
|
|
140
|
+
getAnnotations(): Annotation[];
|
|
141
|
+
close(): void;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface NativeDocument {
|
|
145
|
+
readonly pageCount: number;
|
|
146
|
+
readonly metadata: DocumentMetadata;
|
|
147
|
+
getPage(index: number): Promise<NativePage>;
|
|
148
|
+
getBookmarks(): Bookmark[];
|
|
149
|
+
destroy(): void;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface NativeAddon {
|
|
153
|
+
loadDocument(input: Buffer | string, password?: string): Promise<NativeDocument>;
|
|
154
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pdfium-native",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Native Node.js bindings for PDFium",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=18.0.0"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"pdf",
|
|
12
|
+
"pdfium",
|
|
13
|
+
"native",
|
|
14
|
+
"render",
|
|
15
|
+
"text-extraction",
|
|
16
|
+
"ocr",
|
|
17
|
+
"node-addon",
|
|
18
|
+
"napi"
|
|
19
|
+
],
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"src",
|
|
31
|
+
"lib",
|
|
32
|
+
"scripts",
|
|
33
|
+
"binding.gyp"
|
|
34
|
+
],
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/xonaman/nodejs-pdfium-native.git"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"os": [
|
|
43
|
+
"darwin",
|
|
44
|
+
"linux",
|
|
45
|
+
"win32"
|
|
46
|
+
],
|
|
47
|
+
"cpu": [
|
|
48
|
+
"x64",
|
|
49
|
+
"arm64",
|
|
50
|
+
"arm",
|
|
51
|
+
"ppc64"
|
|
52
|
+
],
|
|
53
|
+
"scripts": {
|
|
54
|
+
"install": "node scripts/install.mjs",
|
|
55
|
+
"build": "node-gyp rebuild && node scripts/bundle-lib.mjs",
|
|
56
|
+
"build:ts": "tsc",
|
|
57
|
+
"prepublishOnly": "tsc",
|
|
58
|
+
"download": "node scripts/download-pdfium.mjs",
|
|
59
|
+
"test": "vitest run",
|
|
60
|
+
"lint": "eslint --fix .",
|
|
61
|
+
"lint:check": "eslint .",
|
|
62
|
+
"format": "prettier --write .",
|
|
63
|
+
"format:check": "prettier --check ."
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {
|
|
66
|
+
"node-addon-api": "^8.0.0"
|
|
67
|
+
},
|
|
68
|
+
"devDependencies": {
|
|
69
|
+
"@eslint/js": "^10.0.1",
|
|
70
|
+
"@types/node": "^25.5.0",
|
|
71
|
+
"eslint": "^10.1.0",
|
|
72
|
+
"eslint-config-prettier": "^10.1.8",
|
|
73
|
+
"eslint-plugin-prettier": "^5.5.5",
|
|
74
|
+
"node-gyp": "^11.0.0",
|
|
75
|
+
"prettier": "^3.8.1",
|
|
76
|
+
"typescript": "^5.9.3",
|
|
77
|
+
"typescript-eslint": "^8.57.2",
|
|
78
|
+
"vitest": "^4.1.2"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { cpSync, existsSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
|
|
5
|
+
const root = join(import.meta.dirname, '..');
|
|
6
|
+
const outDir = join(root, 'build', 'Release');
|
|
7
|
+
const nodeFile = join(outDir, 'pdfium.node');
|
|
8
|
+
|
|
9
|
+
if (!existsSync(nodeFile)) {
|
|
10
|
+
console.error('pdfium.node not found — run node-gyp rebuild first');
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// on Windows, binding.gyp copies the DLL via "copies" — nothing else to do
|
|
15
|
+
if (process.platform === 'win32') {
|
|
16
|
+
const dll = join(outDir, 'pdfium.dll');
|
|
17
|
+
if (existsSync(dll)) {
|
|
18
|
+
console.log('pdfium.dll already in build/Release/ (copied by node-gyp).');
|
|
19
|
+
} else {
|
|
20
|
+
console.error('pdfium.dll not found in build/Release/ — check binding.gyp copies.');
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
console.log('Bundle complete.');
|
|
24
|
+
process.exit(0);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// macOS / Linux: copy shared library next to pdfium.node
|
|
28
|
+
const libDir = join(root, 'deps', 'pdfium', 'lib');
|
|
29
|
+
const libs = readdirSync(libDir).filter((f) => f.endsWith('.dylib') || f.endsWith('.so'));
|
|
30
|
+
if (libs.length === 0) {
|
|
31
|
+
console.error('No shared library found in deps/pdfium/lib/');
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
for (const lib of libs) {
|
|
36
|
+
const src = join(libDir, lib);
|
|
37
|
+
const dst = join(outDir, lib);
|
|
38
|
+
cpSync(src, dst);
|
|
39
|
+
console.log(`Copied ${lib} → build/Release/`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// on macOS, fix the install name so pdfium.node looks for the dylib next to itself
|
|
43
|
+
if (process.platform === 'darwin') {
|
|
44
|
+
const dylib = libs.find((f) => f.endsWith('.dylib'));
|
|
45
|
+
if (dylib) {
|
|
46
|
+
// change the dylib's own install name
|
|
47
|
+
execFileSync('install_name_tool', ['-id', `@loader_path/${dylib}`, join(outDir, dylib)], {
|
|
48
|
+
stdio: 'inherit',
|
|
49
|
+
});
|
|
50
|
+
// change the reference inside pdfium.node
|
|
51
|
+
const oldName = execFileSync('otool', ['-L', nodeFile])
|
|
52
|
+
.toString()
|
|
53
|
+
.split('\n')
|
|
54
|
+
.find((line) => line.includes(dylib))
|
|
55
|
+
?.trim()
|
|
56
|
+
.split(' ')[0];
|
|
57
|
+
if (oldName) {
|
|
58
|
+
execFileSync('install_name_tool', ['-change', oldName, `@loader_path/${dylib}`, nodeFile], {
|
|
59
|
+
stdio: 'inherit',
|
|
60
|
+
});
|
|
61
|
+
console.log(`Fixed install name: ${oldName} → @loader_path/${dylib}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
console.log('Bundle complete.');
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { execSync, execFileSync } from 'node:child_process';
|
|
2
|
+
import { createWriteStream, mkdirSync, existsSync, rmSync } from 'node:fs';
|
|
3
|
+
import { pipeline } from 'node:stream/promises';
|
|
4
|
+
import { Readable } from 'node:stream';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
|
|
7
|
+
const PDFIUM_VERSION = 'chromium/7749';
|
|
8
|
+
const BASE_URL = 'https://github.com/bblanchon/pdfium-binaries/releases/download';
|
|
9
|
+
|
|
10
|
+
// detect musl libc (Alpine, Void, etc.)
|
|
11
|
+
function isMusl() {
|
|
12
|
+
if (process.platform !== 'linux') return false;
|
|
13
|
+
try {
|
|
14
|
+
const ldd = execSync('ldd --version 2>&1 || true', { encoding: 'utf8' });
|
|
15
|
+
return ldd.toLowerCase().includes('musl');
|
|
16
|
+
} catch {
|
|
17
|
+
return (
|
|
18
|
+
existsSync('/lib/ld-musl-x86_64.so.1') ||
|
|
19
|
+
existsSync('/lib/ld-musl-aarch64.so.1') ||
|
|
20
|
+
existsSync('/lib/ld-musl-armhf.so.1')
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const TARGETS = {
|
|
26
|
+
// macOS
|
|
27
|
+
'darwin-arm64': 'pdfium-mac-arm64',
|
|
28
|
+
'darwin-x64': 'pdfium-mac-x64',
|
|
29
|
+
// Linux (glibc)
|
|
30
|
+
'linux-x64': 'pdfium-linux-x64',
|
|
31
|
+
'linux-arm64': 'pdfium-linux-arm64',
|
|
32
|
+
'linux-arm': 'pdfium-linux-arm',
|
|
33
|
+
'linux-ppc64': 'pdfium-linux-ppc64',
|
|
34
|
+
// Linux (musl / Alpine)
|
|
35
|
+
'linux-musl-x64': 'pdfium-linux-musl-x64',
|
|
36
|
+
'linux-musl-arm64': 'pdfium-linux-musl-arm64',
|
|
37
|
+
// Windows
|
|
38
|
+
'win32-x64': 'pdfium-win-x64',
|
|
39
|
+
'win32-arm64': 'pdfium-win-arm64',
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const platform = process.platform;
|
|
43
|
+
const arch = process.arch;
|
|
44
|
+
const musl = isMusl();
|
|
45
|
+
const key = musl ? `${platform}-musl-${arch}` : `${platform}-${arch}`;
|
|
46
|
+
const target = TARGETS[key];
|
|
47
|
+
|
|
48
|
+
if (!target) {
|
|
49
|
+
console.error(`Unsupported platform: ${key}`);
|
|
50
|
+
console.error(`Supported: ${Object.keys(TARGETS).join(', ')}`);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const depsDir = join(import.meta.dirname, '..', 'deps', 'pdfium');
|
|
55
|
+
|
|
56
|
+
if (existsSync(join(depsDir, 'include', 'fpdfview.h'))) {
|
|
57
|
+
console.log('PDFium already downloaded, skipping.');
|
|
58
|
+
process.exit(0);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const encodedVersion = encodeURIComponent(PDFIUM_VERSION);
|
|
62
|
+
const url = `${BASE_URL}/${encodedVersion}/${target}.tgz`;
|
|
63
|
+
const tarball = join(import.meta.dirname, '..', `${target}.tgz`);
|
|
64
|
+
|
|
65
|
+
console.log(`Downloading PDFium ${PDFIUM_VERSION} for ${key}...`);
|
|
66
|
+
console.log(`URL: ${url}`);
|
|
67
|
+
|
|
68
|
+
const response = await fetch(url, { redirect: 'follow' });
|
|
69
|
+
if (!response.ok) {
|
|
70
|
+
console.error(`Download failed: ${response.status} ${response.statusText}`);
|
|
71
|
+
process.exit(1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
mkdirSync(join(import.meta.dirname, '..', 'deps'), { recursive: true });
|
|
75
|
+
await pipeline(Readable.fromWeb(response.body), createWriteStream(tarball));
|
|
76
|
+
|
|
77
|
+
console.log('Extracting...');
|
|
78
|
+
mkdirSync(depsDir, { recursive: true });
|
|
79
|
+
execFileSync('tar', ['-xzf', tarball, '-C', depsDir], { stdio: 'inherit' });
|
|
80
|
+
|
|
81
|
+
// clean up tarball
|
|
82
|
+
rmSync(tarball);
|
|
83
|
+
|
|
84
|
+
console.log(`PDFium installed to ${depsDir}`);
|