pdfium-native 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +74 -30
- package/binding.gyp +27 -1
- package/package.json +16 -3
- package/scripts/bundle-lib.mjs +24 -3
- package/scripts/download-pdfium.mjs +2 -1
- package/scripts/install.mjs +19 -1
- package/src/document.h +1 -1
- package/src/napi_helpers.h +2 -1
- package/src/page.h +11 -4
package/README.md
CHANGED
|
@@ -1,27 +1,15 @@
|
|
|
1
1
|
# pdfium-native
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/pdfium-native)
|
|
4
|
+
[](https://nodejs.org)
|
|
5
|
+
[](https://github.com/xonaman/nodejs-pdfium-native/blob/main/LICENSE)
|
|
6
|
+
[]()
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
Fast, native PDF rendering and text extraction for Node.js — powered by [PDFium](https://pdfium.googlesource.com/pdfium/), the same engine used in Chromium. Built as a C++ addon with N-API for ABI stability across Node.js versions.
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
> Designed for server-side workloads. Non-blocking, fast, and production-ready.
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
npm install pdfium-native
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
Prebuilt PDFium binaries are downloaded automatically during install. A C++ compiler is required (Xcode CLI tools on macOS, `build-essential` on Linux, Visual Studio on Windows).
|
|
14
|
-
|
|
15
|
-
## Supported Platforms
|
|
16
|
-
|
|
17
|
-
| OS | Architectures |
|
|
18
|
-
| --------------------- | ---------------------- |
|
|
19
|
-
| macOS | arm64, x64 |
|
|
20
|
-
| Linux (glibc) | x64, arm64, arm, ppc64 |
|
|
21
|
-
| Linux (musl / Alpine) | x64, arm64 |
|
|
22
|
-
| Windows | x64, arm64 |
|
|
23
|
-
|
|
24
|
-
## Quick Start
|
|
12
|
+
## 🚀 Quick Start
|
|
25
13
|
|
|
26
14
|
```typescript
|
|
27
15
|
import { loadDocument } from 'pdfium-native';
|
|
@@ -29,20 +17,76 @@ import { loadDocument } from 'pdfium-native';
|
|
|
29
17
|
const doc = await loadDocument('invoice.pdf');
|
|
30
18
|
const page = await doc.getPage(0);
|
|
31
19
|
|
|
32
|
-
// extract text
|
|
33
20
|
const text = await page.getText();
|
|
34
|
-
|
|
35
|
-
// render to buffer
|
|
36
|
-
const jpeg = await page.render({ scale: 3, format: 'jpeg', quality: 90 });
|
|
37
|
-
|
|
38
|
-
// render to file
|
|
39
|
-
await page.render({ scale: 4, format: 'png', output: 'page-0.png' });
|
|
21
|
+
const image = await page.render({ scale: 3, format: 'png' }); // high-resolution render
|
|
40
22
|
|
|
41
23
|
page.close();
|
|
42
24
|
doc.destroy();
|
|
43
25
|
```
|
|
44
26
|
|
|
45
|
-
##
|
|
27
|
+
## 💡 Why pdfium-native?
|
|
28
|
+
|
|
29
|
+
**⚡ Performance**
|
|
30
|
+
|
|
31
|
+
- Native C++ — no WASM overhead, no JS parsing
|
|
32
|
+
- Non-blocking — all operations run off the main thread via libuv workers
|
|
33
|
+
|
|
34
|
+
**🛠️ Developer experience**
|
|
35
|
+
|
|
36
|
+
- Built-in JPEG/PNG rendering — no extra dependencies like sharp
|
|
37
|
+
- Prebuilt binaries for 10 platform/arch combinations — no compile step
|
|
38
|
+
- Full TypeScript support with types included
|
|
39
|
+
|
|
40
|
+
**🔒 Reliability**
|
|
41
|
+
|
|
42
|
+
- Built on PDFium — the PDF engine used in Chromium
|
|
43
|
+
- ABI-stable via N-API — works across Node.js 18–24 without recompilation
|
|
44
|
+
- Password-protected PDFs supported out of the box
|
|
45
|
+
|
|
46
|
+
### 🎯 Use cases
|
|
47
|
+
|
|
48
|
+
- 🖼️ Generate thumbnails and previews for uploaded PDFs
|
|
49
|
+
- 📄 Extract searchable text from documents at scale
|
|
50
|
+
- ⚙️ Build server-side PDF processing pipelines
|
|
51
|
+
- 🔗 Read annotations, bookmarks, and links from existing PDFs
|
|
52
|
+
|
|
53
|
+
### 📊 How it compares
|
|
54
|
+
|
|
55
|
+
| | **pdfium-native** | @hyzyla/pdfium | pdfjs-dist | pdf-parse | pdf2json |
|
|
56
|
+
| ------------------- | ----------------------- | ------------------------- | ----------------- | -------------- | ------------------- |
|
|
57
|
+
| Engine | PDFium (C++ addon) | PDFium (WASM) | pdf.js (JS) | pdf.js (JS) | pdf.js fork (JS) |
|
|
58
|
+
| Rendering | ✅ JPEG/PNG built-in | ⚠️ Raw bitmap (BYO sharp) | ⚠️ Canvas/browser | ❌ | ❌ |
|
|
59
|
+
| Text extraction | ✅ | ❌ | ✅ | ✅ | ✅ (structured) |
|
|
60
|
+
| Search (with rects) | ✅ | ❌ | ⚠️ Manual | ❌ | ❌ |
|
|
61
|
+
| Annotations | ✅ | ❌ | ⚠️ Partial | ❌ | ❌ |
|
|
62
|
+
| Bookmarks | ✅ | ❌ | ✅ | ❌ | ❌ |
|
|
63
|
+
| Links | ✅ | ❌ | ✅ | ❌ | ❌ |
|
|
64
|
+
| Form fields | ❌ | ❌ | ✅ | ❌ | ✅ |
|
|
65
|
+
| Async I/O | ✅ libuv workers | ❌ Sync (WASM) | ❌ Main thread | ❌ Main thread | ⚠️ Events / streams |
|
|
66
|
+
| Environment | Node.js | Node.js + browser | Node.js + browser | Node.js | Node.js |
|
|
67
|
+
| Dependencies | None¹ | None (WASM bundled) | None | pdf.js | None (since v3) |
|
|
68
|
+
| Platforms | macOS / Linux / Windows | Any (WASM) | Any | Any | Any |
|
|
69
|
+
|
|
70
|
+
¹ Prebuilt binaries downloaded at install — no runtime dependencies. Falls back to source compilation if unavailable.
|
|
71
|
+
|
|
72
|
+
## 📦 Install
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
npm install pdfium-native
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Prebuilt binaries are available for all [supported platforms](#supported-platforms) — most installs require no compiler. If no prebuilt is available, the package falls back to compiling from source (requires a C++ toolchain: Xcode CLI tools on macOS, `build-essential` on Linux, Visual Studio on Windows).
|
|
79
|
+
|
|
80
|
+
## 🌍 Supported Platforms
|
|
81
|
+
|
|
82
|
+
| OS | Architectures |
|
|
83
|
+
| --------------------- | ---------------------- |
|
|
84
|
+
| macOS | arm64, x64 |
|
|
85
|
+
| Linux (glibc) | x64, arm64, arm, ppc64 |
|
|
86
|
+
| Linux (musl / Alpine) | x64, arm64 |
|
|
87
|
+
| Windows | x64, arm64 |
|
|
88
|
+
|
|
89
|
+
## 📚 API
|
|
46
90
|
|
|
47
91
|
### `loadDocument(input, password?)`
|
|
48
92
|
|
|
@@ -200,11 +244,11 @@ interface DocumentMetadata {
|
|
|
200
244
|
|
|
201
245
|
---
|
|
202
246
|
|
|
203
|
-
## Acknowledgements
|
|
247
|
+
## 🙏 Acknowledgements
|
|
204
248
|
|
|
205
249
|
This project uses prebuilt PDFium binaries from [bblanchon/pdfium-binaries](https://github.com/bblanchon/pdfium-binaries), which provides automated builds of the PDFium library for multiple platforms. Thanks to [@bblanchon](https://github.com/bblanchon) for maintaining this invaluable resource.
|
|
206
250
|
|
|
207
|
-
## Memory Management
|
|
251
|
+
## 🧹 Memory Management
|
|
208
252
|
|
|
209
253
|
Always call `page.close()` and `doc.destroy()` when done. While GC-triggered destructor hooks exist as a safety net, they should not be relied on — explicit cleanup ensures resources are freed promptly.
|
|
210
254
|
|
|
@@ -222,6 +266,6 @@ try {
|
|
|
222
266
|
}
|
|
223
267
|
```
|
|
224
268
|
|
|
225
|
-
## License
|
|
269
|
+
## 📄 License
|
|
226
270
|
|
|
227
271
|
MIT
|
package/binding.gyp
CHANGED
|
@@ -17,11 +17,18 @@
|
|
|
17
17
|
"cflags!": [
|
|
18
18
|
"-fno-exceptions"
|
|
19
19
|
],
|
|
20
|
+
"cflags": [
|
|
21
|
+
"-Os",
|
|
22
|
+
"-flto",
|
|
23
|
+
"-ffunction-sections",
|
|
24
|
+
"-fdata-sections"
|
|
25
|
+
],
|
|
20
26
|
"cflags_cc!": [
|
|
21
27
|
"-fno-exceptions"
|
|
22
28
|
],
|
|
23
29
|
"cflags_cc": [
|
|
24
|
-
"-std=c++17"
|
|
30
|
+
"-std=c++17",
|
|
31
|
+
"-fvisibility=hidden"
|
|
25
32
|
],
|
|
26
33
|
"conditions": [
|
|
27
34
|
[
|
|
@@ -30,10 +37,21 @@
|
|
|
30
37
|
"xcode_settings": {
|
|
31
38
|
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
|
|
32
39
|
"CLANG_CXX_LANGUAGE_STANDARD": "c++17",
|
|
40
|
+
"GCC_SYMBOLS_PRIVATE_EXTERN": "YES",
|
|
41
|
+
"DEAD_CODE_STRIPPING": "YES",
|
|
42
|
+
"LLVM_LTO": "YES",
|
|
43
|
+
"OTHER_CPLUSPLUSFLAGS": [
|
|
44
|
+
"-Os",
|
|
45
|
+
"-ffunction-sections",
|
|
46
|
+
"-fdata-sections"
|
|
47
|
+
],
|
|
33
48
|
"OTHER_LDFLAGS": [
|
|
34
49
|
"-L<(module_root_dir)/deps/pdfium/lib",
|
|
35
50
|
"-lpdfium",
|
|
36
51
|
"-Wl,-rpath,@loader_path",
|
|
52
|
+
"-Wl,-dead_strip",
|
|
53
|
+
"-Wl,-S",
|
|
54
|
+
"-flto",
|
|
37
55
|
"-framework CoreFoundation",
|
|
38
56
|
"-framework CoreGraphics"
|
|
39
57
|
]
|
|
@@ -47,6 +65,9 @@
|
|
|
47
65
|
"-L<(module_root_dir)/deps/pdfium/lib",
|
|
48
66
|
"-lpdfium",
|
|
49
67
|
"-Wl,-rpath,'$$ORIGIN'",
|
|
68
|
+
"-Wl,--gc-sections",
|
|
69
|
+
"-Wl,-S",
|
|
70
|
+
"-flto",
|
|
50
71
|
"-lpthread",
|
|
51
72
|
"-ldl"
|
|
52
73
|
]
|
|
@@ -61,9 +82,14 @@
|
|
|
61
82
|
"msvs_settings": {
|
|
62
83
|
"VCCLCompilerTool": {
|
|
63
84
|
"ExceptionHandling": 1,
|
|
85
|
+
"Optimization": 1,
|
|
86
|
+
"WholeProgramOptimization": "true",
|
|
64
87
|
"AdditionalOptions": [
|
|
65
88
|
"/std:c++17"
|
|
66
89
|
]
|
|
90
|
+
},
|
|
91
|
+
"VCLinkerTool": {
|
|
92
|
+
"LinkTimeCodeGeneration": 1
|
|
67
93
|
}
|
|
68
94
|
},
|
|
69
95
|
"libraries": [
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdfium-native",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "Native Node.js bindings for PDFium",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -13,9 +13,18 @@
|
|
|
13
13
|
"native",
|
|
14
14
|
"render",
|
|
15
15
|
"text-extraction",
|
|
16
|
-
"ocr",
|
|
17
16
|
"node-addon",
|
|
18
|
-
"napi"
|
|
17
|
+
"napi",
|
|
18
|
+
"pdf-parser",
|
|
19
|
+
"pdf-reader",
|
|
20
|
+
"pdf-to-text",
|
|
21
|
+
"pdf-to-image",
|
|
22
|
+
"pdf-render",
|
|
23
|
+
"extract-text",
|
|
24
|
+
"search",
|
|
25
|
+
"annotations",
|
|
26
|
+
"bookmarks",
|
|
27
|
+
"chromium"
|
|
19
28
|
],
|
|
20
29
|
"main": "dist/index.js",
|
|
21
30
|
"types": "dist/index.d.ts",
|
|
@@ -36,6 +45,10 @@
|
|
|
36
45
|
"type": "git",
|
|
37
46
|
"url": "git+https://github.com/xonaman/nodejs-pdfium-native.git"
|
|
38
47
|
},
|
|
48
|
+
"homepage": "https://github.com/xonaman/nodejs-pdfium-native#readme",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/xonaman/nodejs-pdfium-native/issues"
|
|
51
|
+
},
|
|
39
52
|
"publishConfig": {
|
|
40
53
|
"access": "public"
|
|
41
54
|
},
|
package/scripts/bundle-lib.mjs
CHANGED
|
@@ -11,15 +11,28 @@ if (!existsSync(nodeFile)) {
|
|
|
11
11
|
process.exit(1);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
/** Strip debug symbols from a binary. */
|
|
15
|
+
function strip(file) {
|
|
16
|
+
// macOS: -S strips debug symbols only (preserves load commands in dylibs)
|
|
17
|
+
// Linux: -s strips all symbols
|
|
18
|
+
const args = process.platform === 'darwin' ? ['-S'] : ['-s'];
|
|
19
|
+
try {
|
|
20
|
+
execFileSync('strip', [...args, file], { stdio: 'inherit' });
|
|
21
|
+
console.log(`Stripped ${file.split('/').pop()}`);
|
|
22
|
+
} catch {
|
|
23
|
+
console.warn(`strip failed for ${file.split('/').pop()} — continuing`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
14
27
|
// on Windows, binding.gyp copies the DLL via "copies" — nothing else to do
|
|
15
28
|
if (process.platform === 'win32') {
|
|
16
29
|
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 {
|
|
30
|
+
if (!existsSync(dll)) {
|
|
20
31
|
console.error('pdfium.dll not found in build/Release/ — check binding.gyp copies.');
|
|
21
32
|
process.exit(1);
|
|
22
33
|
}
|
|
34
|
+
console.log('pdfium.dll already in build/Release/ (copied by node-gyp).');
|
|
35
|
+
strip(nodeFile);
|
|
23
36
|
console.log('Bundle complete.');
|
|
24
37
|
process.exit(0);
|
|
25
38
|
}
|
|
@@ -63,4 +76,12 @@ if (process.platform === 'darwin') {
|
|
|
63
76
|
}
|
|
64
77
|
}
|
|
65
78
|
|
|
79
|
+
// strip debug symbols from shared libraries on Linux to reduce binary size
|
|
80
|
+
// pdfium.node is stripped at link time via -Wl,-S in binding.gyp
|
|
81
|
+
if (process.platform === 'linux') {
|
|
82
|
+
for (const lib of libs) {
|
|
83
|
+
strip(join(outDir, lib));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
66
87
|
console.log('Bundle complete.');
|
|
@@ -40,7 +40,8 @@ const TARGETS = {
|
|
|
40
40
|
};
|
|
41
41
|
|
|
42
42
|
const platform = process.platform;
|
|
43
|
-
|
|
43
|
+
// support cross-compilation via npm_config_arch (e.g. win32 x64 → arm64)
|
|
44
|
+
const arch = process.env.npm_config_arch || process.arch;
|
|
44
45
|
const musl = isMusl();
|
|
45
46
|
const key = musl ? `${platform}-musl-${arch}` : `${platform}-${arch}`;
|
|
46
47
|
const target = TARGETS[key];
|
package/scripts/install.mjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Falls back to compiling from source if no prebuilt is available.
|
|
4
4
|
*
|
|
5
5
|
* Prebuilt tarball naming: pdfium-v{version}-{platform}-{arch}.tar.gz
|
|
6
|
+
* Musl (Alpine) naming: pdfium-v{version}-linux-musl-{arch}.tar.gz
|
|
6
7
|
* Contents: build/Release/pdfium.node + build/Release/libpdfium.{dylib,so,dll}
|
|
7
8
|
*/
|
|
8
9
|
import { existsSync, mkdirSync, createWriteStream, readFileSync, unlinkSync } from 'node:fs';
|
|
@@ -20,9 +21,26 @@ if (!/^\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?$/.test(version)) {
|
|
|
20
21
|
process.exit(1);
|
|
21
22
|
}
|
|
22
23
|
|
|
24
|
+
// detect musl libc (Alpine, Void, etc.)
|
|
25
|
+
function isMusl() {
|
|
26
|
+
if (process.platform !== 'linux') return false;
|
|
27
|
+
try {
|
|
28
|
+
const ldd = execSync('ldd --version 2>&1 || true', { encoding: 'utf8' });
|
|
29
|
+
return ldd.toLowerCase().includes('musl');
|
|
30
|
+
} catch {
|
|
31
|
+
return (
|
|
32
|
+
existsSync('/lib/ld-musl-x86_64.so.1') ||
|
|
33
|
+
existsSync('/lib/ld-musl-aarch64.so.1') ||
|
|
34
|
+
existsSync('/lib/ld-musl-armhf.so.1')
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
23
39
|
const platform = process.platform;
|
|
24
40
|
const arch = process.arch;
|
|
25
|
-
const
|
|
41
|
+
const musl = isMusl();
|
|
42
|
+
const platformKey = musl ? `${platform}-musl` : platform;
|
|
43
|
+
const tarName = `pdfium-v${version}-${platformKey}-${arch}.tar.gz`;
|
|
26
44
|
|
|
27
45
|
// hardcoded origin ensures the URL can never point to an attacker-controlled host
|
|
28
46
|
const releaseOrigin = 'https://github.com';
|
package/src/document.h
CHANGED
|
@@ -149,7 +149,7 @@ protected:
|
|
|
149
149
|
Napi::Env env = Env();
|
|
150
150
|
Napi::Object pageObj = PDFiumDocument::pageConstructor.New({});
|
|
151
151
|
PDFiumPage *pageWrapper = PDFiumPage::Unwrap(pageObj);
|
|
152
|
-
pageWrapper->SetPage(page_, doc_, pageIndex_);
|
|
152
|
+
pageWrapper->SetPage(page_, doc_, pageIndex_, width_, height_);
|
|
153
153
|
pageWrapper->SetDocAlive(docAlive_);
|
|
154
154
|
|
|
155
155
|
// register the page's alive flag with the document for invalidation
|
package/src/napi_helpers.h
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
|
+
#include <atomic>
|
|
3
4
|
#include <mutex>
|
|
4
5
|
#include <napi.h>
|
|
5
6
|
|
|
6
7
|
#include "fpdfview.h"
|
|
7
8
|
|
|
8
|
-
inline bool g_initialized
|
|
9
|
+
inline std::atomic<bool> g_initialized{false};
|
|
9
10
|
|
|
10
11
|
// PDFium is not thread-safe — serialize all calls through a global mutex.
|
|
11
12
|
// This unblocks the Node.js event loop while waiting for the lock.
|
package/src/page.h
CHANGED
|
@@ -37,10 +37,13 @@ public:
|
|
|
37
37
|
: Napi::ObjectWrap<PDFiumPage>(info),
|
|
38
38
|
alive_(std::make_shared<std::atomic<bool>>(true)) {}
|
|
39
39
|
|
|
40
|
-
void SetPage(FPDF_PAGE page, FPDF_DOCUMENT doc, int index
|
|
40
|
+
void SetPage(FPDF_PAGE page, FPDF_DOCUMENT doc, int index, float width,
|
|
41
|
+
float height) {
|
|
41
42
|
page_ = page;
|
|
42
43
|
doc_ = doc;
|
|
43
44
|
index_ = index;
|
|
45
|
+
width_ = width;
|
|
46
|
+
height_ = height;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
// called by document to give this page a reference to the document's alive
|
|
@@ -56,6 +59,8 @@ private:
|
|
|
56
59
|
FPDF_PAGE page_ = nullptr;
|
|
57
60
|
FPDF_DOCUMENT doc_ = nullptr;
|
|
58
61
|
int index_ = -1;
|
|
62
|
+
float width_ = 0;
|
|
63
|
+
float height_ = 0;
|
|
59
64
|
std::shared_ptr<std::atomic<bool>> alive_;
|
|
60
65
|
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
61
66
|
|
|
@@ -102,8 +107,10 @@ private:
|
|
|
102
107
|
if (env.IsExceptionPending())
|
|
103
108
|
return env.Null();
|
|
104
109
|
|
|
105
|
-
|
|
106
|
-
|
|
110
|
+
// use cached dimensions (set under mutex in GetPageWorker) to avoid
|
|
111
|
+
// PDFium API calls on the main thread that could race with workers
|
|
112
|
+
float origWidth = width_;
|
|
113
|
+
float origHeight = height_;
|
|
107
114
|
double scale = 1.0;
|
|
108
115
|
int renderWidth = 0;
|
|
109
116
|
int renderHeight = 0;
|
|
@@ -269,8 +276,8 @@ private:
|
|
|
269
276
|
|
|
270
277
|
void CleanUp() {
|
|
271
278
|
if (page_) {
|
|
272
|
-
alive_->store(false);
|
|
273
279
|
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
280
|
+
alive_->store(false);
|
|
274
281
|
FPDF_ClosePage(page_);
|
|
275
282
|
page_ = nullptr;
|
|
276
283
|
doc_ = nullptr;
|