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
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
export interface PageSize {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
}
|
|
5
|
+
export interface PageRenderOptions {
|
|
6
|
+
/** Scale factor (default: 1, meaning 72 DPI). Use 3–4 for print quality. */
|
|
7
|
+
scale?: number;
|
|
8
|
+
/** Override render width in pixels. */
|
|
9
|
+
width?: number;
|
|
10
|
+
/** Override render height in pixels. */
|
|
11
|
+
height?: number;
|
|
12
|
+
/** Output format (default: 'jpeg'). */
|
|
13
|
+
format?: 'jpeg' | 'png';
|
|
14
|
+
/** JPEG quality 1–100 (default: 100). Ignored for PNG. */
|
|
15
|
+
quality?: number;
|
|
16
|
+
/** Write to this file path instead of returning a Buffer. */
|
|
17
|
+
output?: string;
|
|
18
|
+
}
|
|
19
|
+
export interface PageObjectBounds {
|
|
20
|
+
left: number;
|
|
21
|
+
bottom: number;
|
|
22
|
+
right: number;
|
|
23
|
+
top: number;
|
|
24
|
+
}
|
|
25
|
+
export interface RGBA {
|
|
26
|
+
r: number;
|
|
27
|
+
g: number;
|
|
28
|
+
b: number;
|
|
29
|
+
a: number;
|
|
30
|
+
}
|
|
31
|
+
interface BasePageObject {
|
|
32
|
+
bounds: PageObjectBounds;
|
|
33
|
+
fillColor: RGBA | null;
|
|
34
|
+
strokeColor: RGBA | null;
|
|
35
|
+
}
|
|
36
|
+
export interface TextPageObject extends BasePageObject {
|
|
37
|
+
type: 'text';
|
|
38
|
+
text: string;
|
|
39
|
+
fontSize: number;
|
|
40
|
+
fontName: string;
|
|
41
|
+
}
|
|
42
|
+
export interface ImagePageObject extends BasePageObject {
|
|
43
|
+
type: 'image';
|
|
44
|
+
imageWidth: number;
|
|
45
|
+
imageHeight: number;
|
|
46
|
+
}
|
|
47
|
+
export interface OtherPageObject extends BasePageObject {
|
|
48
|
+
type: 'path' | 'shading' | 'form' | 'unknown';
|
|
49
|
+
}
|
|
50
|
+
export type PageObject = TextPageObject | ImagePageObject | OtherPageObject;
|
|
51
|
+
export type PageObjectType = PageObject['type'];
|
|
52
|
+
export interface SearchOptions {
|
|
53
|
+
caseSensitive?: boolean;
|
|
54
|
+
wholeWord?: boolean;
|
|
55
|
+
}
|
|
56
|
+
export interface SearchMatch {
|
|
57
|
+
charIndex: number;
|
|
58
|
+
length: number;
|
|
59
|
+
rects: SearchRect[];
|
|
60
|
+
}
|
|
61
|
+
export interface SearchRect {
|
|
62
|
+
left: number;
|
|
63
|
+
top: number;
|
|
64
|
+
right: number;
|
|
65
|
+
bottom: number;
|
|
66
|
+
}
|
|
67
|
+
export type AnnotationType = 'text' | 'link' | 'freetext' | 'line' | 'square' | 'circle' | 'highlight' | 'underline' | 'squiggly' | 'strikeout' | 'stamp' | 'ink' | 'popup' | 'widget' | 'redact' | 'unknown';
|
|
68
|
+
export interface Annotation {
|
|
69
|
+
type: AnnotationType;
|
|
70
|
+
bounds?: PageObjectBounds;
|
|
71
|
+
contents: string;
|
|
72
|
+
color: RGBA | null;
|
|
73
|
+
}
|
|
74
|
+
export interface Link {
|
|
75
|
+
bounds?: PageObjectBounds;
|
|
76
|
+
url?: string;
|
|
77
|
+
pageIndex?: number;
|
|
78
|
+
}
|
|
79
|
+
export interface Bookmark {
|
|
80
|
+
title: string;
|
|
81
|
+
pageIndex?: number;
|
|
82
|
+
children?: Bookmark[];
|
|
83
|
+
}
|
|
84
|
+
export interface DocumentMetadata {
|
|
85
|
+
title: string;
|
|
86
|
+
author: string;
|
|
87
|
+
subject: string;
|
|
88
|
+
keywords: string;
|
|
89
|
+
creator: string;
|
|
90
|
+
producer: string;
|
|
91
|
+
creationDate: string;
|
|
92
|
+
modDate: string;
|
|
93
|
+
pdfVersion: number;
|
|
94
|
+
}
|
|
95
|
+
export interface NativePage {
|
|
96
|
+
readonly number: number;
|
|
97
|
+
readonly width: number;
|
|
98
|
+
readonly height: number;
|
|
99
|
+
readonly size: PageSize;
|
|
100
|
+
readonly objectCount: number;
|
|
101
|
+
getText(): string;
|
|
102
|
+
render(options?: PageRenderOptions): Promise<Buffer | void>;
|
|
103
|
+
getObject(index: number): PageObject;
|
|
104
|
+
getLinks(): Link[];
|
|
105
|
+
search(text: string, options?: SearchOptions): SearchMatch[];
|
|
106
|
+
getAnnotations(): Annotation[];
|
|
107
|
+
close(): void;
|
|
108
|
+
}
|
|
109
|
+
export interface NativeDocument {
|
|
110
|
+
readonly pageCount: number;
|
|
111
|
+
readonly metadata: DocumentMetadata;
|
|
112
|
+
getPage(index: number): Promise<NativePage>;
|
|
113
|
+
getBookmarks(): Bookmark[];
|
|
114
|
+
destroy(): void;
|
|
115
|
+
}
|
|
116
|
+
export interface NativeAddon {
|
|
117
|
+
loadDocument(input: Buffer | string, password?: string): Promise<NativeDocument>;
|
|
118
|
+
}
|
|
119
|
+
export {};
|
|
120
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../lib/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,QAAQ;IACvB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,iBAAiB;IAChC,4EAA4E;IAC5E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uCAAuC;IACvC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uCAAuC;IACvC,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IACxB,0DAA0D;IAC1D,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6DAA6D;IAC7D,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,IAAI;IACnB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAA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GAAG,UAAU,CAAC;IACrC,QAAQ,IAAI,IAAI,EAAE,CAAC;IACnB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,WAAW,EAAE,CAAC;IAC7D,cAAc,IAAI,UAAU,EAAE,CAAC;IAC/B,KAAK,IAAI,IAAI,CAAC;CACf;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,gBAAgB,CAAC;IACpC,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC5C,YAAY,IAAI,QAAQ,EAAE,CAAC;IAC3B,OAAO,IAAI,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,WAAW;IAC1B,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;CAClF"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../lib/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { loadDocument, PDFiumPage } from '../index.js';
|
|
3
|
+
import { fixturePath, pdfBuffer, twoPagesBuffer } from './fixtures.js';
|
|
4
|
+
|
|
5
|
+
describe('PDFiumDocument', () => {
|
|
6
|
+
it('returns correct page count', async () => {
|
|
7
|
+
const doc = await loadDocument(twoPagesBuffer);
|
|
8
|
+
expect(doc.pageCount).toBe(2);
|
|
9
|
+
doc.destroy();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('getPage returns a PDFiumPage', async () => {
|
|
13
|
+
const doc = await loadDocument(pdfBuffer);
|
|
14
|
+
const page = await doc.getPage(0);
|
|
15
|
+
expect(page).toBeInstanceOf(PDFiumPage);
|
|
16
|
+
page.close();
|
|
17
|
+
doc.destroy();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('getPage rejects for out-of-range index', async () => {
|
|
21
|
+
const doc = await loadDocument(pdfBuffer);
|
|
22
|
+
await expect(doc.getPage(5)).rejects.toThrow();
|
|
23
|
+
await expect(doc.getPage(-1)).rejects.toThrow();
|
|
24
|
+
doc.destroy();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('destroy prevents further use', async () => {
|
|
28
|
+
const doc = await loadDocument(pdfBuffer);
|
|
29
|
+
doc.destroy();
|
|
30
|
+
await expect(doc.getPage(0)).rejects.toThrow('Document is destroyed');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('pages() iterates over all pages', async () => {
|
|
34
|
+
const doc = await loadDocument(twoPagesBuffer);
|
|
35
|
+
const pages: PDFiumPage[] = [];
|
|
36
|
+
for await (const page of doc.pages()) {
|
|
37
|
+
pages.push(page);
|
|
38
|
+
}
|
|
39
|
+
expect(pages).toHaveLength(2);
|
|
40
|
+
expect(pages[0]!.number).toBe(0);
|
|
41
|
+
expect(pages[1]!.number).toBe(1);
|
|
42
|
+
for (const p of pages) p.close();
|
|
43
|
+
doc.destroy();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('PDFiumDocument.getMetadata', () => {
|
|
48
|
+
it('returns metadata object with expected keys', async () => {
|
|
49
|
+
const doc = await loadDocument(pdfBuffer);
|
|
50
|
+
const meta = doc.metadata;
|
|
51
|
+
expect(meta).toHaveProperty('title');
|
|
52
|
+
expect(meta).toHaveProperty('author');
|
|
53
|
+
expect(meta).toHaveProperty('subject');
|
|
54
|
+
expect(meta).toHaveProperty('keywords');
|
|
55
|
+
expect(meta).toHaveProperty('creator');
|
|
56
|
+
expect(meta).toHaveProperty('producer');
|
|
57
|
+
expect(meta).toHaveProperty('creationDate');
|
|
58
|
+
expect(meta).toHaveProperty('modDate');
|
|
59
|
+
expect(meta).toHaveProperty('pdfVersion');
|
|
60
|
+
expect(typeof meta.title).toBe('string');
|
|
61
|
+
expect(typeof meta.pdfVersion).toBe('number');
|
|
62
|
+
doc.destroy();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('returns correct metadata values from a rich PDF', async () => {
|
|
66
|
+
const doc = await loadDocument(fixturePath('metadata.pdf'));
|
|
67
|
+
const meta = doc.metadata;
|
|
68
|
+
expect(meta.title).toBe('Test Document Title');
|
|
69
|
+
expect(meta.author).toBe('Test Author');
|
|
70
|
+
expect(meta.subject).toBe('Test Subject');
|
|
71
|
+
expect(meta.creator).toBe('pdfium-native test');
|
|
72
|
+
expect(meta.producer).toContain('pdf-lib');
|
|
73
|
+
doc.destroy();
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('PDFiumDocument.getBookmarks', () => {
|
|
78
|
+
it('returns an empty array for a PDF without bookmarks', async () => {
|
|
79
|
+
const doc = await loadDocument(pdfBuffer);
|
|
80
|
+
const bookmarks = doc.getBookmarks();
|
|
81
|
+
expect(Array.isArray(bookmarks)).toBe(true);
|
|
82
|
+
expect(bookmarks.length).toBe(0);
|
|
83
|
+
doc.destroy();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('returns bookmarks with titles and page indices', async () => {
|
|
87
|
+
const doc = await loadDocument(fixturePath('bookmarks.pdf'));
|
|
88
|
+
expect(doc.pageCount).toBe(3);
|
|
89
|
+
const bookmarks = doc.getBookmarks();
|
|
90
|
+
expect(bookmarks.length).toBe(3);
|
|
91
|
+
expect(bookmarks[0]!.title).toBe('Chapter 1');
|
|
92
|
+
expect(bookmarks[0]!.pageIndex).toBe(0);
|
|
93
|
+
expect(bookmarks[1]!.title).toBe('Chapter 2');
|
|
94
|
+
expect(bookmarks[1]!.pageIndex).toBe(1);
|
|
95
|
+
expect(bookmarks[2]!.title).toBe('Chapter 3');
|
|
96
|
+
expect(bookmarks[2]!.pageIndex).toBe(2);
|
|
97
|
+
doc.destroy();
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { loadDocument } from '../index.js';
|
|
3
|
+
import { fixturePath, pdfBuffer } from './fixtures.js';
|
|
4
|
+
|
|
5
|
+
describe('PDFiumPage.getLinks', () => {
|
|
6
|
+
it('returns an empty array for a page with no links', async () => {
|
|
7
|
+
const doc = await loadDocument(pdfBuffer);
|
|
8
|
+
const page = await doc.getPage(0);
|
|
9
|
+
const links = page.getLinks();
|
|
10
|
+
expect(Array.isArray(links)).toBe(true);
|
|
11
|
+
expect(links.length).toBe(0);
|
|
12
|
+
page.close();
|
|
13
|
+
doc.destroy();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('returns links with URLs and page indices', async () => {
|
|
17
|
+
const doc = await loadDocument(fixturePath('links.pdf'));
|
|
18
|
+
const page = await doc.getPage(0);
|
|
19
|
+
const links = page.getLinks();
|
|
20
|
+
expect(links.length).toBe(2);
|
|
21
|
+
|
|
22
|
+
// internal link (to page 2)
|
|
23
|
+
const internalLink = links.find((l) => l.pageIndex !== undefined);
|
|
24
|
+
expect(internalLink).toBeDefined();
|
|
25
|
+
expect(internalLink!.pageIndex).toBe(1);
|
|
26
|
+
expect(internalLink!.bounds).toBeDefined();
|
|
27
|
+
|
|
28
|
+
// external link (URL)
|
|
29
|
+
const externalLink = links.find((l) => l.url !== undefined);
|
|
30
|
+
expect(externalLink).toBeDefined();
|
|
31
|
+
expect(externalLink!.url).toBe('https://example.com');
|
|
32
|
+
expect(externalLink!.bounds).toBeDefined();
|
|
33
|
+
|
|
34
|
+
page.close();
|
|
35
|
+
doc.destroy();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('PDFiumPage.getAnnotations', () => {
|
|
40
|
+
it('returns an empty array for a page with no annotations', async () => {
|
|
41
|
+
const doc = await loadDocument(pdfBuffer);
|
|
42
|
+
const page = await doc.getPage(0);
|
|
43
|
+
const annotations = page.getAnnotations();
|
|
44
|
+
expect(Array.isArray(annotations)).toBe(true);
|
|
45
|
+
expect(annotations.length).toBe(0);
|
|
46
|
+
page.close();
|
|
47
|
+
doc.destroy();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('returns annotations with types, bounds, and contents', async () => {
|
|
51
|
+
const doc = await loadDocument(fixturePath('annotations.pdf'));
|
|
52
|
+
const page = await doc.getPage(0);
|
|
53
|
+
const annotations = page.getAnnotations();
|
|
54
|
+
expect(annotations.length).toBe(2);
|
|
55
|
+
|
|
56
|
+
// text annotation (sticky note)
|
|
57
|
+
const textAnnot = annotations.find((a) => a.type === 'text');
|
|
58
|
+
expect(textAnnot).toBeDefined();
|
|
59
|
+
expect(textAnnot!.contents).toBe('This is a sticky note');
|
|
60
|
+
expect(textAnnot!.bounds).toBeDefined();
|
|
61
|
+
expect(textAnnot!.color).toBeDefined();
|
|
62
|
+
|
|
63
|
+
// highlight annotation
|
|
64
|
+
const highlightAnnot = annotations.find((a) => a.type === 'highlight');
|
|
65
|
+
expect(highlightAnnot).toBeDefined();
|
|
66
|
+
expect(highlightAnnot!.contents).toBe('Highlighted text');
|
|
67
|
+
expect(highlightAnnot!.bounds).toBeDefined();
|
|
68
|
+
|
|
69
|
+
page.close();
|
|
70
|
+
doc.destroy();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { dirname, resolve } from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
|
|
4
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
5
|
+
|
|
6
|
+
// minimal valid PDF: single page (612×792 pts), no content
|
|
7
|
+
export const MINIMAL_PDF = `%PDF-1.0
|
|
8
|
+
1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj
|
|
9
|
+
2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj
|
|
10
|
+
3 0 obj<</Type/Page/MediaBox[0 0 612 792]/Parent 2 0 R/Resources<<>>>>endobj
|
|
11
|
+
xref
|
|
12
|
+
0 4
|
|
13
|
+
0000000000 65535 f
|
|
14
|
+
0000000009 00000 n
|
|
15
|
+
0000000058 00000 n
|
|
16
|
+
0000000115 00000 n
|
|
17
|
+
trailer<</Size 4/Root 1 0 R>>
|
|
18
|
+
startxref
|
|
19
|
+
206
|
|
20
|
+
%%EOF`;
|
|
21
|
+
|
|
22
|
+
// 2-page PDF
|
|
23
|
+
export const TWO_PAGE_PDF = `%PDF-1.0
|
|
24
|
+
1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj
|
|
25
|
+
2 0 obj<</Type/Pages/Kids[3 0 R 4 0 R]/Count 2>>endobj
|
|
26
|
+
3 0 obj<</Type/Page/MediaBox[0 0 612 792]/Parent 2 0 R/Resources<<>>>>endobj
|
|
27
|
+
4 0 obj<</Type/Page/MediaBox[0 0 400 600]/Parent 2 0 R/Resources<<>>>>endobj
|
|
28
|
+
xref
|
|
29
|
+
0 5
|
|
30
|
+
0000000000 65535 f
|
|
31
|
+
0000000009 00000 n
|
|
32
|
+
0000000058 00000 n
|
|
33
|
+
0000000115 00000 n
|
|
34
|
+
0000000206 00000 n
|
|
35
|
+
trailer<</Size 5/Root 1 0 R>>
|
|
36
|
+
startxref
|
|
37
|
+
297
|
|
38
|
+
%%EOF`;
|
|
39
|
+
|
|
40
|
+
// PDF with text content (Helvetica "Hello" at 12pt)
|
|
41
|
+
export const TEXT_PDF = `%PDF-1.4
|
|
42
|
+
1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj
|
|
43
|
+
2 0 obj<</Type/Pages/Kids[3 0 R]/Count 1>>endobj
|
|
44
|
+
3 0 obj<</Type/Page/MediaBox[0 0 612 792]/Parent 2 0 R/Resources<</Font<</F1 4 0 R>>>>>/Contents 5 0 R>>endobj
|
|
45
|
+
4 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>endobj
|
|
46
|
+
5 0 obj<</Length 44>>stream
|
|
47
|
+
BT /F1 12 Tf 100 700 Td (Hello) Tj ET
|
|
48
|
+
endstream
|
|
49
|
+
endobj
|
|
50
|
+
xref
|
|
51
|
+
0 6
|
|
52
|
+
0000000000 65535 f \r
|
|
53
|
+
0000000009 00000 n \r
|
|
54
|
+
0000000058 00000 n \r
|
|
55
|
+
0000000115 00000 n \r
|
|
56
|
+
0000000266 00000 n \r
|
|
57
|
+
0000000340 00000 n \r
|
|
58
|
+
trailer<</Size 6/Root 1 0 R>>
|
|
59
|
+
startxref
|
|
60
|
+
434
|
|
61
|
+
%%EOF`;
|
|
62
|
+
|
|
63
|
+
export const pdfBuffer = Buffer.from(MINIMAL_PDF);
|
|
64
|
+
export const twoPagesBuffer = Buffer.from(TWO_PAGE_PDF);
|
|
65
|
+
export const textPdfBuffer = Buffer.from(TEXT_PDF);
|
|
66
|
+
|
|
67
|
+
export const fixturesDir = resolve(__dirname, '..', '..', 'test', 'fixtures');
|
|
68
|
+
|
|
69
|
+
export function fixturePath(name: string): string {
|
|
70
|
+
return resolve(fixturesDir, name);
|
|
71
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { unlinkSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
4
|
+
import {
|
|
5
|
+
loadDocument,
|
|
6
|
+
PDFiumDocument,
|
|
7
|
+
PDFiumError,
|
|
8
|
+
PDFiumFileError,
|
|
9
|
+
PDFiumFormatError,
|
|
10
|
+
} from '../index.js';
|
|
11
|
+
import { pdfBuffer } from './fixtures.js';
|
|
12
|
+
|
|
13
|
+
const tmpFilePath = resolve(import.meta.dirname!, 'test-fixture.pdf');
|
|
14
|
+
|
|
15
|
+
beforeAll(() => {
|
|
16
|
+
writeFileSync(tmpFilePath, pdfBuffer);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterAll(() => {
|
|
20
|
+
try {
|
|
21
|
+
unlinkSync(tmpFilePath);
|
|
22
|
+
} catch {
|
|
23
|
+
// ignore
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('loadDocument', () => {
|
|
28
|
+
it('loads a PDF from a Buffer', async () => {
|
|
29
|
+
const doc = await loadDocument(pdfBuffer);
|
|
30
|
+
expect(doc).toBeInstanceOf(PDFiumDocument);
|
|
31
|
+
expect(doc.pageCount).toBe(1);
|
|
32
|
+
doc.destroy();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('loads a PDF from a file path', async () => {
|
|
36
|
+
const doc = await loadDocument(tmpFilePath);
|
|
37
|
+
expect(doc).toBeInstanceOf(PDFiumDocument);
|
|
38
|
+
expect(doc.pageCount).toBe(1);
|
|
39
|
+
doc.destroy();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('rejects with PDFiumFormatError for invalid data', async () => {
|
|
43
|
+
await expect(loadDocument(Buffer.from('not a pdf'))).rejects.toThrow(PDFiumFormatError);
|
|
44
|
+
try {
|
|
45
|
+
await loadDocument(Buffer.from('not a pdf'));
|
|
46
|
+
} catch (err) {
|
|
47
|
+
expect(err).toBeInstanceOf(PDFiumError);
|
|
48
|
+
expect((err as PDFiumError).code).toBe('FORMAT');
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('rejects with PDFiumFileError for non-existent file', async () => {
|
|
53
|
+
await expect(loadDocument('/tmp/does-not-exist.pdf')).rejects.toThrow(PDFiumFileError);
|
|
54
|
+
try {
|
|
55
|
+
await loadDocument('/tmp/does-not-exist.pdf');
|
|
56
|
+
} catch (err) {
|
|
57
|
+
expect(err).toBeInstanceOf(PDFiumError);
|
|
58
|
+
expect((err as PDFiumError).code).toBe('FILE');
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('rejects with an error for wrong password', async () => {
|
|
63
|
+
// since our test PDF is not encrypted, this just opens normally
|
|
64
|
+
const doc = await loadDocument(pdfBuffer, 'unused-password');
|
|
65
|
+
expect(doc.pageCount).toBe(1);
|
|
66
|
+
doc.destroy();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
2
|
+
import { loadDocument, PDFiumDocument, PDFiumPage } from '../index.js';
|
|
3
|
+
import { pdfBuffer, textPdfBuffer, twoPagesBuffer } from './fixtures.js';
|
|
4
|
+
|
|
5
|
+
describe('PDFiumPage', () => {
|
|
6
|
+
let doc: PDFiumDocument;
|
|
7
|
+
let page: PDFiumPage;
|
|
8
|
+
|
|
9
|
+
beforeAll(async () => {
|
|
10
|
+
doc = await loadDocument(pdfBuffer);
|
|
11
|
+
page = await doc.getPage(0);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterAll(() => {
|
|
15
|
+
page.close();
|
|
16
|
+
doc.destroy();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('has correct page number', () => {
|
|
20
|
+
expect(page.number).toBe(0);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('returns width in points', () => {
|
|
24
|
+
expect(page.width).toBeCloseTo(612, 0);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('returns height in points', () => {
|
|
28
|
+
expect(page.height).toBeCloseTo(792, 0);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('size returns { width, height }', () => {
|
|
32
|
+
expect(page.size).toHaveProperty('width');
|
|
33
|
+
expect(page.size).toHaveProperty('height');
|
|
34
|
+
expect(page.size.width).toBeCloseTo(612, 0);
|
|
35
|
+
expect(page.size.height).toBeCloseTo(792, 0);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('getText returns a string', () => {
|
|
39
|
+
const text = page.getText();
|
|
40
|
+
expect(typeof text).toBe('string');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('objectCount returns a number', () => {
|
|
44
|
+
expect(typeof page.objectCount).toBe('number');
|
|
45
|
+
expect(page.objectCount).toBeGreaterThanOrEqual(0);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('getObject returns type and bounds', async () => {
|
|
49
|
+
const doc2 = await loadDocument(textPdfBuffer);
|
|
50
|
+
const p = await doc2.getPage(0);
|
|
51
|
+
const count = p.objectCount;
|
|
52
|
+
expect(count).toBeGreaterThan(0);
|
|
53
|
+
const obj = p.getObject(0);
|
|
54
|
+
expect(obj).toHaveProperty('type');
|
|
55
|
+
expect(obj).toHaveProperty('bounds');
|
|
56
|
+
expect(['text', 'path', 'image', 'shading', 'form', 'unknown']).toContain(obj.type);
|
|
57
|
+
expect(typeof obj.bounds.left).toBe('number');
|
|
58
|
+
expect(typeof obj.bounds.bottom).toBe('number');
|
|
59
|
+
expect(typeof obj.bounds.right).toBe('number');
|
|
60
|
+
expect(typeof obj.bounds.top).toBe('number');
|
|
61
|
+
|
|
62
|
+
// colors are present for all objects
|
|
63
|
+
expect(obj).toHaveProperty('fillColor');
|
|
64
|
+
expect(obj).toHaveProperty('strokeColor');
|
|
65
|
+
|
|
66
|
+
// find the text object and verify enriched properties
|
|
67
|
+
let foundText = false;
|
|
68
|
+
for (let i = 0; i < count; i++) {
|
|
69
|
+
const o = p.getObject(i);
|
|
70
|
+
if (o.type === 'text') {
|
|
71
|
+
foundText = true;
|
|
72
|
+
expect(o.text).toBe('Hello');
|
|
73
|
+
expect(o.fontSize).toBeCloseTo(12, 0);
|
|
74
|
+
expect(o.fontName).toBe('Helvetica');
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
expect(foundText).toBe(true);
|
|
79
|
+
|
|
80
|
+
p.close();
|
|
81
|
+
doc2.destroy();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('multi-page dimensions', () => {
|
|
86
|
+
it('each page has its own dimensions', async () => {
|
|
87
|
+
const doc = await loadDocument(twoPagesBuffer);
|
|
88
|
+
|
|
89
|
+
const page0 = await doc.getPage(0);
|
|
90
|
+
expect(page0.width).toBeCloseTo(612, 0);
|
|
91
|
+
expect(page0.height).toBeCloseTo(792, 0);
|
|
92
|
+
page0.close();
|
|
93
|
+
|
|
94
|
+
const page1 = await doc.getPage(1);
|
|
95
|
+
expect(page1.width).toBeCloseTo(400, 0);
|
|
96
|
+
expect(page1.height).toBeCloseTo(600, 0);
|
|
97
|
+
page1.close();
|
|
98
|
+
|
|
99
|
+
doc.destroy();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe('page close', () => {
|
|
104
|
+
it('close prevents further method calls', async () => {
|
|
105
|
+
const doc = await loadDocument(pdfBuffer);
|
|
106
|
+
const page = await doc.getPage(0);
|
|
107
|
+
page.close();
|
|
108
|
+
// width/height are cached — still accessible after close
|
|
109
|
+
expect(page.width).toBeCloseTo(612, 0);
|
|
110
|
+
expect(page.height).toBeCloseTo(792, 0);
|
|
111
|
+
// native methods throw after close
|
|
112
|
+
expect(() => page.getText()).toThrow('Page is closed');
|
|
113
|
+
doc.destroy();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('double close does not throw', async () => {
|
|
117
|
+
const doc = await loadDocument(pdfBuffer);
|
|
118
|
+
const page = await doc.getPage(0);
|
|
119
|
+
page.close();
|
|
120
|
+
expect(() => page.close()).not.toThrow();
|
|
121
|
+
doc.destroy();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { existsSync, readFileSync, unlinkSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
4
|
+
import { loadDocument, PDFiumDocument } from '../index.js';
|
|
5
|
+
import { pdfBuffer } from './fixtures.js';
|
|
6
|
+
|
|
7
|
+
describe('PDFiumPage.render', () => {
|
|
8
|
+
let doc: PDFiumDocument;
|
|
9
|
+
|
|
10
|
+
beforeAll(async () => {
|
|
11
|
+
doc = await loadDocument(pdfBuffer);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
afterAll(() => {
|
|
15
|
+
doc.destroy();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('renders as JPEG by default', async () => {
|
|
19
|
+
const page = await doc.getPage(0);
|
|
20
|
+
const buf = await page.render();
|
|
21
|
+
expect(buf).toBeInstanceOf(Buffer);
|
|
22
|
+
expect(buf.length).toBeGreaterThan(0);
|
|
23
|
+
// JPEG magic bytes: FF D8 FF
|
|
24
|
+
expect(buf[0]).toBe(0xff);
|
|
25
|
+
expect(buf[1]).toBe(0xd8);
|
|
26
|
+
expect(buf[2]).toBe(0xff);
|
|
27
|
+
page.close();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('renders as PNG when format is png', async () => {
|
|
31
|
+
const page = await doc.getPage(0);
|
|
32
|
+
const buf = await page.render({ format: 'png' });
|
|
33
|
+
expect(buf).toBeInstanceOf(Buffer);
|
|
34
|
+
// PNG magic bytes: 89 50 4E 47
|
|
35
|
+
expect(buf[0]).toBe(0x89);
|
|
36
|
+
expect(buf[1]).toBe(0x50);
|
|
37
|
+
expect(buf[2]).toBe(0x4e);
|
|
38
|
+
expect(buf[3]).toBe(0x47);
|
|
39
|
+
page.close();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('respects quality option for JPEG', async () => {
|
|
43
|
+
const page = await doc.getPage(0);
|
|
44
|
+
const high = await page.render({ quality: 100 });
|
|
45
|
+
const low = await page.render({ quality: 10 });
|
|
46
|
+
expect(low.length).toBeLessThan(high.length);
|
|
47
|
+
page.close();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('renders with scale=2', async () => {
|
|
51
|
+
const page = await doc.getPage(0);
|
|
52
|
+
const buf = await page.render({ scale: 2 });
|
|
53
|
+
expect(buf).toBeInstanceOf(Buffer);
|
|
54
|
+
expect(buf.length).toBeGreaterThan(0);
|
|
55
|
+
page.close();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('renders with explicit width/height', async () => {
|
|
59
|
+
const page = await doc.getPage(0);
|
|
60
|
+
const buf = await page.render({ width: 100, height: 200 });
|
|
61
|
+
expect(buf).toBeInstanceOf(Buffer);
|
|
62
|
+
expect(buf.length).toBeGreaterThan(0);
|
|
63
|
+
page.close();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('writes JPEG to file when output is specified', async () => {
|
|
67
|
+
const page = await doc.getPage(0);
|
|
68
|
+
const outPath = resolve(import.meta.dirname!, '__test_output.jpg');
|
|
69
|
+
try {
|
|
70
|
+
const result = await page.render({ output: outPath });
|
|
71
|
+
expect(result).toBeUndefined();
|
|
72
|
+
expect(existsSync(outPath)).toBe(true);
|
|
73
|
+
const bytes = readFileSync(outPath);
|
|
74
|
+
expect(bytes[0]).toBe(0xff);
|
|
75
|
+
expect(bytes[1]).toBe(0xd8);
|
|
76
|
+
expect(bytes[2]).toBe(0xff);
|
|
77
|
+
} finally {
|
|
78
|
+
page.close();
|
|
79
|
+
try {
|
|
80
|
+
unlinkSync(outPath);
|
|
81
|
+
} catch {}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('writes PNG to file when output and format=png', async () => {
|
|
86
|
+
const page = await doc.getPage(0);
|
|
87
|
+
const outPath = resolve(import.meta.dirname!, '__test_output.png');
|
|
88
|
+
try {
|
|
89
|
+
const result = await page.render({ output: outPath, format: 'png' });
|
|
90
|
+
expect(result).toBeUndefined();
|
|
91
|
+
const bytes = readFileSync(outPath);
|
|
92
|
+
expect(bytes[0]).toBe(0x89);
|
|
93
|
+
expect(bytes[1]).toBe(0x50);
|
|
94
|
+
expect(bytes[2]).toBe(0x4e);
|
|
95
|
+
expect(bytes[3]).toBe(0x47);
|
|
96
|
+
} finally {
|
|
97
|
+
page.close();
|
|
98
|
+
try {
|
|
99
|
+
unlinkSync(outPath);
|
|
100
|
+
} catch {}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|