pdfium-native 0.5.2 → 0.5.4
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/CHANGELOG.md +24 -0
- package/binding.gyp +3 -3
- package/package.json +1 -1
- package/src/annotations_worker.h +4 -7
- package/src/bookmarks_worker.h +3 -7
- package/src/document.h +12 -11
- package/src/form_fields_worker.h +4 -7
- package/src/links_worker.h +4 -7
- package/src/napi_helpers.h +110 -11
- package/src/objects_worker.h +4 -7
- package/src/pdfium_addon.cc +42 -67
- package/src/pixel_convert.h +29 -0
- package/src/render_image_worker.h +35 -31
- package/src/render_worker.h +30 -30
- package/src/search_worker.h +4 -7
- package/src/split_merge_worker.h +40 -48
- package/src/text_worker.h +4 -7
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,30 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project adheres to [Semantic Versioning](https://semver.org/).
|
|
6
6
|
|
|
7
|
+
## [0.5.4] - 2026-04-11
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
|
|
11
|
+
- Remove `%PDF-` header validation — PDFium already returns `FORMAT` error for invalid input; the pre-check was blocking PDF repair for files with damaged or missing headers
|
|
12
|
+
- Ref-counted PDFium initialization (`atomic<int>` refcount) for multi-worker-thread safety — init on first env, destroy on last
|
|
13
|
+
- Zero-copy `Buffer::New` with heap-allocated vector and release callback for render, image extraction, split, and merge operations (replaces `Buffer::Copy`)
|
|
14
|
+
- Fix `ReadU16` minimum length check from 4 to 2 bytes
|
|
15
|
+
- Fix `AddonData` memory leak in cleanup hook
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- Extract shared BGRA→RGB/RGBA pixel conversion into `pixel_convert.h` inline helper
|
|
20
|
+
- Consolidate `GetPdfiumErrorMessage()` into `napi_helpers.h`, removing duplicates
|
|
21
|
+
- Switch compiler optimization from `-Os` to `-O2`; replace `<filesystem>` with POSIX `access()`
|
|
22
|
+
- Compact `pageAliveFlags` and optimize pixel conversion loops
|
|
23
|
+
|
|
24
|
+
## [0.5.3] - 2026-04-08
|
|
25
|
+
|
|
26
|
+
### Fixed
|
|
27
|
+
|
|
28
|
+
- Store document and page constructor references per-env instead of as static globals — fixes cross-isolate V8 crash when multiple worker threads load the addon concurrently
|
|
29
|
+
- Add `SafeAsyncWorker` base class with `OnWorkComplete` guard (env-alive check, `napi_open_handle_scope` probe, try-catch) — all 13 async worker classes now inherit from it
|
|
30
|
+
|
|
7
31
|
## [0.5.2] - 2026-04-08
|
|
8
32
|
|
|
9
33
|
### Added
|
package/binding.gyp
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"-fno-exceptions"
|
|
19
19
|
],
|
|
20
20
|
"cflags": [
|
|
21
|
-
"-
|
|
21
|
+
"-O2",
|
|
22
22
|
"-flto",
|
|
23
23
|
"-ffunction-sections",
|
|
24
24
|
"-fdata-sections"
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"DEAD_CODE_STRIPPING": "YES",
|
|
42
42
|
"LLVM_LTO": "YES",
|
|
43
43
|
"OTHER_CPLUSPLUSFLAGS": [
|
|
44
|
-
"-
|
|
44
|
+
"-O2",
|
|
45
45
|
"-ffunction-sections",
|
|
46
46
|
"-fdata-sections"
|
|
47
47
|
],
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"msvs_settings": {
|
|
83
83
|
"VCCLCompilerTool": {
|
|
84
84
|
"ExceptionHandling": 1,
|
|
85
|
-
"Optimization":
|
|
85
|
+
"Optimization": 2,
|
|
86
86
|
"WholeProgramOptimization": "true",
|
|
87
87
|
"AdditionalOptions": [
|
|
88
88
|
"/std:c++17"
|
package/package.json
CHANGED
package/src/annotations_worker.h
CHANGED
|
@@ -35,14 +35,14 @@ struct AnnotationData {
|
|
|
35
35
|
std::vector<QuadPoints> quadPoints;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
class GetAnnotationsWorker : public
|
|
38
|
+
class GetAnnotationsWorker : public SafeAsyncWorker {
|
|
39
39
|
public:
|
|
40
40
|
GetAnnotationsWorker(Napi::Env env, FPDF_PAGE page,
|
|
41
41
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
42
42
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
43
|
-
:
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
44
|
+
page_(page), pageAlive_(std::move(pageAlive)),
|
|
45
|
+
docAlive_(std::move(docAlive)) {}
|
|
46
46
|
|
|
47
47
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
48
48
|
|
|
@@ -208,7 +208,6 @@ protected:
|
|
|
208
208
|
}
|
|
209
209
|
|
|
210
210
|
void OnOK() override {
|
|
211
|
-
CHECK_ENV();
|
|
212
211
|
Napi::Env env = Env();
|
|
213
212
|
Napi::Array arr = Napi::Array::New(env, annotations_.size());
|
|
214
213
|
for (uint32_t i = 0; i < annotations_.size(); i++) {
|
|
@@ -263,13 +262,11 @@ protected:
|
|
|
263
262
|
}
|
|
264
263
|
|
|
265
264
|
void OnError(const Napi::Error &err) override {
|
|
266
|
-
CHECK_ENV();
|
|
267
265
|
deferred_.Reject(err.Value());
|
|
268
266
|
}
|
|
269
267
|
|
|
270
268
|
private:
|
|
271
269
|
Napi::Promise::Deferred deferred_;
|
|
272
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
273
270
|
FPDF_PAGE page_;
|
|
274
271
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
275
272
|
std::shared_ptr<std::atomic<bool>> docAlive_;
|
package/src/bookmarks_worker.h
CHANGED
|
@@ -25,13 +25,12 @@ struct BookmarkData {
|
|
|
25
25
|
std::vector<BookmarkData> children;
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
class GetBookmarksWorker : public
|
|
28
|
+
class GetBookmarksWorker : public SafeAsyncWorker {
|
|
29
29
|
public:
|
|
30
30
|
GetBookmarksWorker(Napi::Env env, FPDF_DOCUMENT doc,
|
|
31
31
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
32
|
-
:
|
|
33
|
-
|
|
34
|
-
}
|
|
32
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
33
|
+
doc_(doc), docAlive_(std::move(docAlive)) {}
|
|
35
34
|
|
|
36
35
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
37
36
|
|
|
@@ -46,18 +45,15 @@ protected:
|
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
void OnOK() override {
|
|
49
|
-
CHECK_ENV();
|
|
50
48
|
deferred_.Resolve(BuildArray(Env(), bookmarks_));
|
|
51
49
|
}
|
|
52
50
|
|
|
53
51
|
void OnError(const Napi::Error &err) override {
|
|
54
|
-
CHECK_ENV();
|
|
55
52
|
deferred_.Reject(err.Value());
|
|
56
53
|
}
|
|
57
54
|
|
|
58
55
|
private:
|
|
59
56
|
Napi::Promise::Deferred deferred_;
|
|
60
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
61
57
|
FPDF_DOCUMENT doc_;
|
|
62
58
|
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
63
59
|
std::vector<BookmarkData> bookmarks_;
|
package/src/document.h
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
#include "bookmarks_worker.h"
|
|
4
4
|
#include "page.h"
|
|
5
5
|
|
|
6
|
+
#include <algorithm>
|
|
6
7
|
#include <atomic>
|
|
7
8
|
#include <memory>
|
|
8
9
|
|
|
@@ -37,15 +38,17 @@ public:
|
|
|
37
38
|
|
|
38
39
|
// register a page's alive flag so we can invalidate it on destroy
|
|
39
40
|
void RegisterPageAlive(std::shared_ptr<std::atomic<bool>> flag) {
|
|
41
|
+
// compact: remove entries for pages already closed/GC'd
|
|
42
|
+
pageAliveFlags_.erase(
|
|
43
|
+
std::remove_if(pageAliveFlags_.begin(), pageAliveFlags_.end(),
|
|
44
|
+
[](const auto &f) { return !f->load(); }),
|
|
45
|
+
pageAliveFlags_.end());
|
|
40
46
|
pageAliveFlags_.push_back(std::move(flag));
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
// returns the document alive flag (shared with pages)
|
|
44
50
|
std::shared_ptr<std::atomic<bool>> GetDocAlive() const { return docAlive_; }
|
|
45
51
|
|
|
46
|
-
// store the page constructor so we can create instances
|
|
47
|
-
static Napi::FunctionReference pageConstructor;
|
|
48
|
-
|
|
49
52
|
private:
|
|
50
53
|
FPDF_DOCUMENT doc_ = nullptr;
|
|
51
54
|
std::vector<uint8_t> ownedBuffer_;
|
|
@@ -110,14 +113,14 @@ private:
|
|
|
110
113
|
// GetPageWorker — async page loading
|
|
111
114
|
// ---------------------------------------------------------------------------
|
|
112
115
|
|
|
113
|
-
class GetPageWorker : public
|
|
116
|
+
class GetPageWorker : public SafeAsyncWorker {
|
|
114
117
|
public:
|
|
115
118
|
GetPageWorker(Napi::Env env, FPDF_DOCUMENT doc, int pageIndex,
|
|
116
119
|
PDFiumDocument *docWrapper,
|
|
117
120
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
118
|
-
:
|
|
119
|
-
|
|
120
|
-
|
|
121
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
122
|
+
doc_(doc), pageIndex_(pageIndex), docWrapper_(docWrapper),
|
|
123
|
+
docAlive_(std::move(docAlive)) {}
|
|
121
124
|
|
|
122
125
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
123
126
|
|
|
@@ -177,9 +180,9 @@ protected:
|
|
|
177
180
|
}
|
|
178
181
|
|
|
179
182
|
void OnOK() override {
|
|
180
|
-
CHECK_ENV();
|
|
181
183
|
Napi::Env env = Env();
|
|
182
|
-
|
|
184
|
+
auto *addon = env.GetInstanceData<AddonData>();
|
|
185
|
+
Napi::Object pageObj = addon->pageConstructor.New({});
|
|
183
186
|
PDFiumPage *pageWrapper = PDFiumPage::Unwrap(pageObj);
|
|
184
187
|
pageWrapper->SetPage(page_, doc_, pageIndex_, width_, height_);
|
|
185
188
|
pageWrapper->SetDocAlive(docAlive_);
|
|
@@ -205,13 +208,11 @@ protected:
|
|
|
205
208
|
}
|
|
206
209
|
|
|
207
210
|
void OnError(const Napi::Error &err) override {
|
|
208
|
-
CHECK_ENV();
|
|
209
211
|
deferred_.Reject(err.Value());
|
|
210
212
|
}
|
|
211
213
|
|
|
212
214
|
private:
|
|
213
215
|
Napi::Promise::Deferred deferred_;
|
|
214
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
215
216
|
FPDF_DOCUMENT doc_;
|
|
216
217
|
int pageIndex_;
|
|
217
218
|
PDFiumDocument *docWrapper_;
|
package/src/form_fields_worker.h
CHANGED
|
@@ -32,14 +32,14 @@ struct FormFieldData {
|
|
|
32
32
|
std::vector<FormFieldOptionData> options;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
-
class GetFormFieldsWorker : public
|
|
35
|
+
class GetFormFieldsWorker : public SafeAsyncWorker {
|
|
36
36
|
public:
|
|
37
37
|
GetFormFieldsWorker(Napi::Env env, FPDF_PAGE page, FPDF_DOCUMENT doc,
|
|
38
38
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
39
39
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
40
|
-
:
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
41
|
+
page_(page), doc_(doc), pageAlive_(std::move(pageAlive)),
|
|
42
|
+
docAlive_(std::move(docAlive)) {}
|
|
43
43
|
|
|
44
44
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
45
45
|
|
|
@@ -201,7 +201,6 @@ protected:
|
|
|
201
201
|
}
|
|
202
202
|
|
|
203
203
|
void OnOK() override {
|
|
204
|
-
CHECK_ENV();
|
|
205
204
|
Napi::Env env = Env();
|
|
206
205
|
Napi::Array arr = Napi::Array::New(env, fields_.size());
|
|
207
206
|
for (uint32_t i = 0; i < fields_.size(); i++) {
|
|
@@ -238,13 +237,11 @@ protected:
|
|
|
238
237
|
}
|
|
239
238
|
|
|
240
239
|
void OnError(const Napi::Error &err) override {
|
|
241
|
-
CHECK_ENV();
|
|
242
240
|
deferred_.Reject(err.Value());
|
|
243
241
|
}
|
|
244
242
|
|
|
245
243
|
private:
|
|
246
244
|
Napi::Promise::Deferred deferred_;
|
|
247
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
248
245
|
FPDF_PAGE page_;
|
|
249
246
|
FPDF_DOCUMENT doc_;
|
|
250
247
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
package/src/links_worker.h
CHANGED
|
@@ -26,14 +26,14 @@ struct LinkData {
|
|
|
26
26
|
std::string filePath;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
-
class GetLinksWorker : public
|
|
29
|
+
class GetLinksWorker : public SafeAsyncWorker {
|
|
30
30
|
public:
|
|
31
31
|
GetLinksWorker(Napi::Env env, FPDF_PAGE page, FPDF_DOCUMENT doc,
|
|
32
32
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
33
33
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
34
|
-
:
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
35
|
+
page_(page), doc_(doc), pageAlive_(std::move(pageAlive)),
|
|
36
|
+
docAlive_(std::move(docAlive)) {}
|
|
37
37
|
|
|
38
38
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
39
39
|
|
|
@@ -117,7 +117,6 @@ protected:
|
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
void OnOK() override {
|
|
120
|
-
CHECK_ENV();
|
|
121
120
|
Napi::Env env = Env();
|
|
122
121
|
Napi::Array arr = Napi::Array::New(env, links_.size());
|
|
123
122
|
for (uint32_t i = 0; i < links_.size(); i++) {
|
|
@@ -146,13 +145,11 @@ protected:
|
|
|
146
145
|
}
|
|
147
146
|
|
|
148
147
|
void OnError(const Napi::Error &err) override {
|
|
149
|
-
CHECK_ENV();
|
|
150
148
|
deferred_.Reject(err.Value());
|
|
151
149
|
}
|
|
152
150
|
|
|
153
151
|
private:
|
|
154
152
|
Napi::Promise::Deferred deferred_;
|
|
155
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
156
153
|
FPDF_PAGE page_;
|
|
157
154
|
FPDF_DOCUMENT doc_;
|
|
158
155
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
package/src/napi_helpers.h
CHANGED
|
@@ -9,7 +9,41 @@
|
|
|
9
9
|
#include "fpdf_doc.h"
|
|
10
10
|
#include "fpdfview.h"
|
|
11
11
|
|
|
12
|
-
inline std::atomic<
|
|
12
|
+
inline std::atomic<int> g_pdfium_refcount{0};
|
|
13
|
+
|
|
14
|
+
// format the last PDFium error as "CODE:message"
|
|
15
|
+
inline std::string GetPdfiumErrorMessage() {
|
|
16
|
+
unsigned long err = FPDF_GetLastError();
|
|
17
|
+
const char *code;
|
|
18
|
+
const char *msg;
|
|
19
|
+
switch (err) {
|
|
20
|
+
case FPDF_ERR_FILE:
|
|
21
|
+
code = "FILE";
|
|
22
|
+
msg = "File not found or could not be opened";
|
|
23
|
+
break;
|
|
24
|
+
case FPDF_ERR_FORMAT:
|
|
25
|
+
code = "FORMAT";
|
|
26
|
+
msg = "Not a valid PDF or corrupted";
|
|
27
|
+
break;
|
|
28
|
+
case FPDF_ERR_PASSWORD:
|
|
29
|
+
code = "PASSWORD";
|
|
30
|
+
msg = "Password required or incorrect";
|
|
31
|
+
break;
|
|
32
|
+
case FPDF_ERR_SECURITY:
|
|
33
|
+
code = "SECURITY";
|
|
34
|
+
msg = "Unsupported security scheme";
|
|
35
|
+
break;
|
|
36
|
+
case FPDF_ERR_PAGE:
|
|
37
|
+
code = "PAGE";
|
|
38
|
+
msg = "Page error";
|
|
39
|
+
break;
|
|
40
|
+
default:
|
|
41
|
+
code = "UNKNOWN";
|
|
42
|
+
msg = "Unknown error";
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
return std::string(code) + ":" + msg;
|
|
46
|
+
}
|
|
13
47
|
|
|
14
48
|
// PDFium is not thread-safe — serialize all calls through a global mutex.
|
|
15
49
|
// This unblocks the Node.js event loop while waiting for the lock.
|
|
@@ -34,15 +68,38 @@ constexpr int MAX_BOOKMARK_DEPTH = 64;
|
|
|
34
68
|
// ---------------------------------------------------------------------------
|
|
35
69
|
// Per-environment alive flag for worker thread safety
|
|
36
70
|
// ---------------------------------------------------------------------------
|
|
37
|
-
// When a Node.js worker thread is terminated (
|
|
38
|
-
//
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
//
|
|
71
|
+
// When a Node.js worker thread is terminated (worker.terminate()), it calls
|
|
72
|
+
// TerminateExecution() + set_can_call_into_js(false) and then drains the libuv
|
|
73
|
+
// event loop. Async work completions fire during this drain. The base
|
|
74
|
+
// AsyncWorker::OnWorkComplete creates a HandleScope which crashes if V8 is
|
|
75
|
+
// shutting down ("Cannot create a handle without a HandleScope" or "Entering
|
|
76
|
+
// the V8 API without proper locking").
|
|
77
|
+
//
|
|
78
|
+
// We use three layers of defense:
|
|
79
|
+
//
|
|
80
|
+
// Layer 1 — C-level env probe: napi_throw(env, nullptr) uses NAPI_PREAMBLE
|
|
81
|
+
// which checks can_call_into_js(). worker.terminate() sets this to false
|
|
82
|
+
// BEFORE draining the loop, so completions see it immediately. Returns
|
|
83
|
+
// napi_invalid_arg when alive (null value rejected), napi_cannot_run_js
|
|
84
|
+
// when shutting down. No V8 calls in either path.
|
|
85
|
+
//
|
|
86
|
+
// Layer 2 — envAlive_ atomic + cleanup hook: the cleanup hook fires during
|
|
87
|
+
// RunCleanup() after the uv drain, catching any completions that arrive
|
|
88
|
+
// during or after the cleanup phase.
|
|
89
|
+
//
|
|
90
|
+
// Layer 3 — prepareShutdown() JS export: called from the worker thread JS
|
|
91
|
+
// code before it signals readiness for termination. Sets envAlive_ to
|
|
92
|
+
// false while V8 is fully alive (belt-and-suspenders).
|
|
42
93
|
|
|
43
94
|
struct AddonData {
|
|
44
95
|
std::shared_ptr<std::atomic<bool>> envAlive =
|
|
45
96
|
std::make_shared<std::atomic<bool>>(true);
|
|
97
|
+
|
|
98
|
+
// per-env constructor references — must NOT be static globals because each
|
|
99
|
+
// worker thread has its own V8 isolate and env. A static global would be
|
|
100
|
+
// overwritten by the last thread to call Init(), causing cross-isolate use.
|
|
101
|
+
Napi::FunctionReference docConstructor;
|
|
102
|
+
Napi::FunctionReference pageConstructor;
|
|
46
103
|
};
|
|
47
104
|
|
|
48
105
|
inline std::shared_ptr<std::atomic<bool>> GetEnvAlive(Napi::Env env) {
|
|
@@ -50,10 +107,52 @@ inline std::shared_ptr<std::atomic<bool>> GetEnvAlive(Napi::Env env) {
|
|
|
50
107
|
return data ? data->envAlive : nullptr;
|
|
51
108
|
}
|
|
52
109
|
|
|
53
|
-
//
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
// SafeAsyncWorker — guards OnWorkComplete against env teardown
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
// All async workers MUST inherit from this class instead of Napi::AsyncWorker
|
|
114
|
+
// to be safe for use in worker threads that may be terminated at any time.
|
|
115
|
+
//
|
|
116
|
+
// OnWorkComplete is overridden to skip ALL V8 API calls when the environment
|
|
117
|
+
// is no longer alive. The JS promise will never settle, but the worker thread
|
|
118
|
+
// is being destroyed anyway — there is no listener for the result.
|
|
119
|
+
|
|
120
|
+
class SafeAsyncWorker : public Napi::AsyncWorker {
|
|
121
|
+
public:
|
|
122
|
+
void OnWorkComplete(Napi::Env env, napi_status status) override {
|
|
123
|
+
// Layer 2: cleanup hook already fired — env is being torn down
|
|
124
|
+
if (envAlive_ && !envAlive_->load())
|
|
125
|
+
return;
|
|
126
|
+
|
|
127
|
+
// Layer 1: probe whether V8 is still accessible by attempting the exact
|
|
128
|
+
// operation that crashes in the base class: opening a handle scope.
|
|
129
|
+
// napi_open_handle_scope is a raw C call — no C++ exception on failure.
|
|
130
|
+
// worker.terminate() races with this: the parent sets can_call_into_js
|
|
131
|
+
// to false and calls TerminateExecution, but with relaxed atomics the
|
|
132
|
+
// worker thread may not see the flag yet. If the scope open fails here,
|
|
133
|
+
// we know the env is dead and bail out immediately.
|
|
134
|
+
napi_handle_scope scope = nullptr;
|
|
135
|
+
if (napi_open_handle_scope(env, &scope) != napi_ok)
|
|
136
|
+
return;
|
|
137
|
+
napi_close_handle_scope(env, scope);
|
|
138
|
+
|
|
139
|
+
// safety net: catch Napi::Error in case the env becomes unusable between
|
|
140
|
+
// our probe and the base class's HandleScope / OnOK / OnError calls.
|
|
141
|
+
// The base OnWorkComplete creates a HandleScope OUTSIDE WrapCallback's
|
|
142
|
+
// try-catch, so a failure there throws an uncaught Napi::Error.
|
|
143
|
+
try {
|
|
144
|
+
Napi::AsyncWorker::OnWorkComplete(env, status);
|
|
145
|
+
} catch (const Napi::Error &) {
|
|
146
|
+
// env tore down between our probe and the base class call — swallow
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
protected:
|
|
151
|
+
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
152
|
+
|
|
153
|
+
explicit SafeAsyncWorker(Napi::Env env)
|
|
154
|
+
: Napi::AsyncWorker(env), envAlive_(GetEnvAlive(env)) {}
|
|
155
|
+
};
|
|
57
156
|
|
|
58
157
|
// ---------------------------------------------------------------------------
|
|
59
158
|
// PDFium UTF-16 string helpers
|
|
@@ -65,7 +164,7 @@ inline std::shared_ptr<std::atomic<bool>> GetEnvAlive(Napi::Env env) {
|
|
|
65
164
|
template <typename GetLen, typename GetData>
|
|
66
165
|
inline std::u16string ReadU16(GetLen getLen, GetData getData) {
|
|
67
166
|
unsigned long len = getLen(static_cast<FPDF_WCHAR *>(nullptr), 0);
|
|
68
|
-
if (len <
|
|
167
|
+
if (len < 2 || len % 2 != 0)
|
|
69
168
|
return {};
|
|
70
169
|
std::vector<unsigned short> buf(len / sizeof(unsigned short));
|
|
71
170
|
getData(reinterpret_cast<FPDF_WCHAR *>(buf.data()), len);
|
package/src/objects_worker.h
CHANGED
|
@@ -41,15 +41,15 @@ struct PageObjectData {
|
|
|
41
41
|
bool hasImageMetadata = false;
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
class GetObjectWorker : public
|
|
44
|
+
class GetObjectWorker : public SafeAsyncWorker {
|
|
45
45
|
public:
|
|
46
46
|
GetObjectWorker(Napi::Env env, FPDF_PAGE page, int index,
|
|
47
47
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
48
48
|
std::shared_ptr<std::atomic<bool>> docAlive,
|
|
49
49
|
Napi::Object pageObj)
|
|
50
|
-
:
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
51
|
+
page_(page), index_(index), pageAlive_(std::move(pageAlive)),
|
|
52
|
+
docAlive_(std::move(docAlive)),
|
|
53
53
|
pageRef_(Napi::Persistent(pageObj)) {}
|
|
54
54
|
|
|
55
55
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
@@ -248,7 +248,6 @@ protected:
|
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
void OnOK() override {
|
|
251
|
-
CHECK_ENV();
|
|
252
251
|
Napi::Env env = Env();
|
|
253
252
|
Napi::Object result = Napi::Object::New(env);
|
|
254
253
|
result.Set("type", Napi::String::New(env, data_.type));
|
|
@@ -323,13 +322,11 @@ protected:
|
|
|
323
322
|
}
|
|
324
323
|
|
|
325
324
|
void OnError(const Napi::Error &err) override {
|
|
326
|
-
CHECK_ENV();
|
|
327
325
|
deferred_.Reject(err.Value());
|
|
328
326
|
}
|
|
329
327
|
|
|
330
328
|
private:
|
|
331
329
|
Napi::Promise::Deferred deferred_;
|
|
332
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
333
330
|
FPDF_PAGE page_;
|
|
334
331
|
int index_;
|
|
335
332
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
package/src/pdfium_addon.cc
CHANGED
|
@@ -8,65 +8,28 @@
|
|
|
8
8
|
#include "fpdf_signature.h"
|
|
9
9
|
#include "split_merge_worker.h"
|
|
10
10
|
|
|
11
|
-
Napi::FunctionReference PDFiumDocument::pageConstructor;
|
|
12
|
-
|
|
13
11
|
// ---------------------------------------------------------------------------
|
|
14
12
|
// Module-level: loadDocument (async)
|
|
15
13
|
// ---------------------------------------------------------------------------
|
|
16
14
|
|
|
17
|
-
static Napi::FunctionReference g_docConstructor;
|
|
18
|
-
|
|
19
|
-
static std::string GetPdfiumErrorMessage() {
|
|
20
|
-
unsigned long err = FPDF_GetLastError();
|
|
21
|
-
const char *code;
|
|
22
|
-
const char *msg;
|
|
23
|
-
switch (err) {
|
|
24
|
-
case FPDF_ERR_FILE:
|
|
25
|
-
code = "FILE";
|
|
26
|
-
msg = "File not found or could not be opened";
|
|
27
|
-
break;
|
|
28
|
-
case FPDF_ERR_FORMAT:
|
|
29
|
-
code = "FORMAT";
|
|
30
|
-
msg = "Not a valid PDF or corrupted";
|
|
31
|
-
break;
|
|
32
|
-
case FPDF_ERR_PASSWORD:
|
|
33
|
-
code = "PASSWORD";
|
|
34
|
-
msg = "Password required or incorrect";
|
|
35
|
-
break;
|
|
36
|
-
case FPDF_ERR_SECURITY:
|
|
37
|
-
code = "SECURITY";
|
|
38
|
-
msg = "Unsupported security scheme";
|
|
39
|
-
break;
|
|
40
|
-
case FPDF_ERR_PAGE:
|
|
41
|
-
code = "PAGE";
|
|
42
|
-
msg = "Page error";
|
|
43
|
-
break;
|
|
44
|
-
default:
|
|
45
|
-
code = "UNKNOWN";
|
|
46
|
-
msg = "Unknown error";
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
return std::string(code) + ":" + msg;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
15
|
// ---------------------------------------------------------------------------
|
|
53
16
|
// LoadDocumentWorker — async document loading
|
|
54
17
|
// ---------------------------------------------------------------------------
|
|
55
18
|
|
|
56
|
-
class LoadDocumentWorker : public
|
|
19
|
+
class LoadDocumentWorker : public SafeAsyncWorker {
|
|
57
20
|
public:
|
|
58
21
|
// buffer variant — copies data since buffer may be GC'd
|
|
59
22
|
LoadDocumentWorker(Napi::Env env, std::vector<uint8_t> data,
|
|
60
23
|
std::string password)
|
|
61
|
-
:
|
|
62
|
-
|
|
63
|
-
|
|
24
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
25
|
+
bufferData_(std::move(data)), password_(std::move(password)),
|
|
26
|
+
useFile_(false) {}
|
|
64
27
|
|
|
65
28
|
// file path variant
|
|
66
29
|
LoadDocumentWorker(Napi::Env env, std::string path, std::string password)
|
|
67
|
-
:
|
|
68
|
-
|
|
69
|
-
|
|
30
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
31
|
+
filePath_(std::move(path)), password_(std::move(password)),
|
|
32
|
+
useFile_(true) {}
|
|
70
33
|
|
|
71
34
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
72
35
|
|
|
@@ -79,11 +42,6 @@ protected:
|
|
|
79
42
|
if (useFile_) {
|
|
80
43
|
doc_ = FPDF_LoadDocument(filePath_.c_str(), pw);
|
|
81
44
|
} else {
|
|
82
|
-
if (bufferData_.size() < 5 ||
|
|
83
|
-
memcmp(bufferData_.data(), "%PDF-", 5) != 0) {
|
|
84
|
-
SetError("FORMAT:Input is not a valid PDF (missing %PDF- header)");
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
45
|
if (bufferData_.size() > static_cast<size_t>(INT_MAX)) {
|
|
88
46
|
SetError("FORMAT:Buffer too large");
|
|
89
47
|
return;
|
|
@@ -153,9 +111,9 @@ protected:
|
|
|
153
111
|
}
|
|
154
112
|
|
|
155
113
|
void OnOK() override {
|
|
156
|
-
CHECK_ENV();
|
|
157
114
|
Napi::Env env = Env();
|
|
158
|
-
|
|
115
|
+
auto *addon = Env().GetInstanceData<AddonData>();
|
|
116
|
+
Napi::Object docObj = addon->docConstructor.New({});
|
|
159
117
|
PDFiumDocument *docWrapper = PDFiumDocument::Unwrap(docObj);
|
|
160
118
|
docWrapper->SetDocument(doc_);
|
|
161
119
|
|
|
@@ -211,13 +169,11 @@ protected:
|
|
|
211
169
|
}
|
|
212
170
|
|
|
213
171
|
void OnError(const Napi::Error &err) override {
|
|
214
|
-
CHECK_ENV();
|
|
215
172
|
deferred_.Reject(err.Value());
|
|
216
173
|
}
|
|
217
174
|
|
|
218
175
|
private:
|
|
219
176
|
Napi::Promise::Deferred deferred_;
|
|
220
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
221
177
|
std::vector<uint8_t> bufferData_;
|
|
222
178
|
std::string filePath_;
|
|
223
179
|
std::string password_;
|
|
@@ -424,38 +380,57 @@ Napi::Value MergeDocuments(const Napi::CallbackInfo &info) {
|
|
|
424
380
|
// Module init
|
|
425
381
|
// ---------------------------------------------------------------------------
|
|
426
382
|
|
|
383
|
+
/**
|
|
384
|
+
* Marks the environment as shutting down so in-flight async workers bail
|
|
385
|
+
* out of OnWorkComplete instead of creating a HandleScope on a dead isolate.
|
|
386
|
+
* Called from worker thread JS before signaling readiness for termination.
|
|
387
|
+
*/
|
|
388
|
+
Napi::Value PrepareShutdown(const Napi::CallbackInfo &info) {
|
|
389
|
+
auto envAlive = GetEnvAlive(info.Env());
|
|
390
|
+
if (envAlive)
|
|
391
|
+
envAlive->store(false);
|
|
392
|
+
return info.Env().Undefined();
|
|
393
|
+
}
|
|
394
|
+
|
|
427
395
|
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
428
|
-
//
|
|
396
|
+
// per-environment alive flag for async worker safety (Layer 2+3)
|
|
429
397
|
auto *addonData = new AddonData();
|
|
430
398
|
env.SetInstanceData(addonData);
|
|
431
399
|
auto envAlive = addonData->envAlive;
|
|
432
|
-
env.AddCleanupHook([envAlive]() { envAlive->store(false); });
|
|
433
400
|
|
|
434
|
-
|
|
401
|
+
// Layer 2: cleanup hook — fires during RunCleanup() after uv drain
|
|
402
|
+
env.AddCleanupHook([addonData]() {
|
|
403
|
+
addonData->envAlive->store(false);
|
|
404
|
+
delete addonData;
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
if (g_pdfium_refcount.fetch_add(1) == 0) {
|
|
435
408
|
FPDF_InitLibrary();
|
|
436
|
-
|
|
409
|
+
}
|
|
437
410
|
|
|
438
|
-
|
|
439
|
-
|
|
411
|
+
// decrement ref count on environment teardown; destroy when last env exits
|
|
412
|
+
env.AddCleanupHook([]() {
|
|
413
|
+
if (g_pdfium_refcount.fetch_sub(1) == 1) {
|
|
440
414
|
FPDF_DestroyLibrary();
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
}
|
|
415
|
+
}
|
|
416
|
+
});
|
|
444
417
|
|
|
445
|
-
// register classes
|
|
418
|
+
// register classes — constructor refs are per-env (not static globals)
|
|
419
|
+
// because each worker thread has its own V8 isolate
|
|
446
420
|
Napi::Function pageCtor = PDFiumPage::Init(env);
|
|
447
|
-
|
|
448
|
-
|
|
421
|
+
addonData->pageConstructor = Napi::Persistent(pageCtor);
|
|
422
|
+
addonData->pageConstructor.SuppressDestruct();
|
|
449
423
|
|
|
450
424
|
Napi::Function docCtor = PDFiumDocument::Init(env);
|
|
451
|
-
|
|
452
|
-
|
|
425
|
+
addonData->docConstructor = Napi::Persistent(docCtor);
|
|
426
|
+
addonData->docConstructor.SuppressDestruct();
|
|
453
427
|
|
|
454
428
|
exports.Set("PDFiumDocument", docCtor);
|
|
455
429
|
exports.Set("PDFiumPage", pageCtor);
|
|
456
430
|
exports.Set("loadDocument", Napi::Function::New(env, LoadDocument));
|
|
457
431
|
exports.Set("splitDocument", Napi::Function::New(env, SplitDocument));
|
|
458
432
|
exports.Set("mergeDocuments", Napi::Function::New(env, MergeDocuments));
|
|
433
|
+
exports.Set("prepareShutdown", Napi::Function::New(env, PrepareShutdown));
|
|
459
434
|
|
|
460
435
|
return exports;
|
|
461
436
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// shared BGRA → RGB/RGBA pixel conversion for render workers
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
|
|
6
|
+
// convert one row of BGRA source pixels to RGB (dstChannels=3) or
|
|
7
|
+
// RGBA (dstChannels=4). srcBpp is the source bytes per pixel (3 or 4).
|
|
8
|
+
// hoisted conditional keeps inner loop branch-free for auto-vectorization.
|
|
9
|
+
inline void convertBgraRow(const uint8_t *src, uint8_t *dst, int width,
|
|
10
|
+
int srcBpp, int dstChannels) {
|
|
11
|
+
if (dstChannels == 4) {
|
|
12
|
+
for (int x = 0; x < width; x++) {
|
|
13
|
+
dst[0] = src[2]; // R ← B
|
|
14
|
+
dst[1] = src[1]; // G
|
|
15
|
+
dst[2] = src[0]; // B ← R
|
|
16
|
+
dst[3] = src[3]; // A
|
|
17
|
+
src += srcBpp;
|
|
18
|
+
dst += 4;
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
for (int x = 0; x < width; x++) {
|
|
22
|
+
dst[0] = src[2]; // R ← B
|
|
23
|
+
dst[1] = src[1]; // G
|
|
24
|
+
dst[2] = src[0]; // B ← R
|
|
25
|
+
src += srcBpp;
|
|
26
|
+
dst += 3;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -5,10 +5,17 @@
|
|
|
5
5
|
|
|
6
6
|
#include <atomic>
|
|
7
7
|
#include <cstdio>
|
|
8
|
-
#include <filesystem>
|
|
9
8
|
#include <memory>
|
|
10
9
|
#include <vector>
|
|
11
10
|
|
|
11
|
+
#ifdef _WIN32
|
|
12
|
+
#include <io.h>
|
|
13
|
+
#define F_OK 0
|
|
14
|
+
#define access _access
|
|
15
|
+
#else
|
|
16
|
+
#include <unistd.h>
|
|
17
|
+
#endif
|
|
18
|
+
|
|
12
19
|
#include "fpdf_edit.h"
|
|
13
20
|
|
|
14
21
|
// max total pixels for image bitmap allocation (~256 MP, ~1 GB at 4 channels)
|
|
@@ -26,17 +33,16 @@ enum RenderImageMode {
|
|
|
26
33
|
// RenderImageWorker — async image rendering from a page object
|
|
27
34
|
// ---------------------------------------------------------------------------
|
|
28
35
|
|
|
29
|
-
class RenderImageWorker : public
|
|
36
|
+
class RenderImageWorker : public SafeAsyncWorker {
|
|
30
37
|
public:
|
|
31
38
|
RenderImageWorker(Napi::Env env, FPDF_PAGE page, FPDF_DOCUMENT doc,
|
|
32
39
|
int objectIndex, int format, int quality,
|
|
33
40
|
std::string outputPath, int mode,
|
|
34
41
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
35
42
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
36
|
-
:
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
outputPath_(std::move(outputPath)), mode_(mode),
|
|
43
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
44
|
+
page_(page), doc_(doc), objectIndex_(objectIndex), format_(format),
|
|
45
|
+
quality_(quality), outputPath_(std::move(outputPath)), mode_(mode),
|
|
40
46
|
pageAlive_(std::move(pageAlive)), docAlive_(std::move(docAlive)) {}
|
|
41
47
|
|
|
42
48
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
@@ -83,11 +89,12 @@ protected:
|
|
|
83
89
|
}
|
|
84
90
|
|
|
85
91
|
void OnOK() override {
|
|
86
|
-
CHECK_ENV();
|
|
87
92
|
Napi::Env env = Env();
|
|
88
93
|
if (outputPath_.empty()) {
|
|
89
|
-
auto
|
|
90
|
-
|
|
94
|
+
auto *vec = new std::vector<uint8_t>(std::move(encodedData_));
|
|
95
|
+
auto data = Napi::Buffer<uint8_t>::New(
|
|
96
|
+
env, vec->data(), vec->size(),
|
|
97
|
+
[](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; }, vec);
|
|
91
98
|
deferred_.Resolve(data);
|
|
92
99
|
} else {
|
|
93
100
|
deferred_.Resolve(env.Undefined());
|
|
@@ -95,13 +102,11 @@ protected:
|
|
|
95
102
|
}
|
|
96
103
|
|
|
97
104
|
void OnError(const Napi::Error &err) override {
|
|
98
|
-
CHECK_ENV();
|
|
99
105
|
deferred_.Reject(err.Value());
|
|
100
106
|
}
|
|
101
107
|
|
|
102
108
|
private:
|
|
103
109
|
Napi::Promise::Deferred deferred_;
|
|
104
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
105
110
|
FPDF_PAGE page_;
|
|
106
111
|
FPDF_DOCUMENT doc_;
|
|
107
112
|
int objectIndex_;
|
|
@@ -199,22 +204,18 @@ private:
|
|
|
199
204
|
std::vector<uint8_t> pixels(static_cast<size_t>(width) *
|
|
200
205
|
static_cast<size_t>(height) * outChannels);
|
|
201
206
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
pixels[dstIdx] = row[x];
|
|
209
|
-
} else {
|
|
210
|
-
int srcIdx = x * srcBytesPerPixel;
|
|
211
|
-
pixels[dstIdx + 0] = row[srcIdx + 2]; // R ← B
|
|
212
|
-
pixels[dstIdx + 1] = row[srcIdx + 1]; // G
|
|
213
|
-
pixels[dstIdx + 2] = row[srcIdx + 0]; // B ← R
|
|
214
|
-
if (keepAlpha)
|
|
215
|
-
pixels[dstIdx + 3] = row[srcIdx + 3]; // A
|
|
216
|
-
}
|
|
207
|
+
if (bitmapFormat == FPDFBitmap_Gray) {
|
|
208
|
+
for (int y = 0; y < height; y++) {
|
|
209
|
+
auto *src = static_cast<uint8_t *>(bufferData) + y * stride;
|
|
210
|
+
auto *dst = pixels.data() + y * width;
|
|
211
|
+
for (int x = 0; x < width; x++)
|
|
212
|
+
dst[x] = src[x];
|
|
217
213
|
}
|
|
214
|
+
} else {
|
|
215
|
+
for (int y = 0; y < height; y++)
|
|
216
|
+
convertBgraRow(static_cast<uint8_t *>(bufferData) + y * stride,
|
|
217
|
+
pixels.data() + y * width * outChannels, width,
|
|
218
|
+
srcBytesPerPixel, outChannels);
|
|
218
219
|
}
|
|
219
220
|
|
|
220
221
|
FPDFBitmap_Destroy(bitmap);
|
|
@@ -238,11 +239,14 @@ private:
|
|
|
238
239
|
}
|
|
239
240
|
|
|
240
241
|
bool WriteToFile() {
|
|
241
|
-
|
|
242
|
-
auto
|
|
243
|
-
if (
|
|
244
|
-
|
|
245
|
-
|
|
242
|
+
// check parent directory exists
|
|
243
|
+
auto slash = outputPath_.rfind('/');
|
|
244
|
+
if (slash != std::string::npos && slash > 0) {
|
|
245
|
+
std::string parentDir = outputPath_.substr(0, slash);
|
|
246
|
+
if (access(parentDir.c_str(), F_OK) != 0) {
|
|
247
|
+
SetError("Parent directory does not exist: " + parentDir);
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
246
250
|
}
|
|
247
251
|
|
|
248
252
|
FILE *f = fopen(outputPath_.c_str(), "wb");
|
package/src/render_worker.h
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
3
|
#include "napi_helpers.h"
|
|
4
|
+
#include "pixel_convert.h"
|
|
4
5
|
|
|
5
6
|
#include <atomic>
|
|
6
7
|
#include <cstdio>
|
|
7
|
-
#include <filesystem>
|
|
8
8
|
#include <memory>
|
|
9
9
|
|
|
10
|
+
#ifdef _WIN32
|
|
11
|
+
#include <io.h>
|
|
12
|
+
#define F_OK 0
|
|
13
|
+
#define access _access
|
|
14
|
+
#else
|
|
15
|
+
#include <unistd.h>
|
|
16
|
+
#endif
|
|
17
|
+
|
|
10
18
|
#include "stb_image_write.h"
|
|
11
19
|
|
|
12
20
|
// stb write callback — appends bytes to a std::vector
|
|
@@ -22,18 +30,17 @@ inline void stb_write_callback(void *context, void *data, int size) {
|
|
|
22
30
|
// RenderWorker — async page rendering
|
|
23
31
|
// ---------------------------------------------------------------------------
|
|
24
32
|
|
|
25
|
-
class RenderWorker : public
|
|
33
|
+
class RenderWorker : public SafeAsyncWorker {
|
|
26
34
|
public:
|
|
27
35
|
RenderWorker(Napi::Env env, FPDF_PAGE page, int renderWidth, int renderHeight,
|
|
28
36
|
int format, int quality, std::string outputPath, int rotation,
|
|
29
37
|
bool transparent, int renderFlags,
|
|
30
38
|
std::shared_ptr<std::atomic<bool>> pageAlive)
|
|
31
|
-
:
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
pageAlive_(std::move(pageAlive)) {}
|
|
39
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
40
|
+
page_(page), renderWidth_(renderWidth), renderHeight_(renderHeight),
|
|
41
|
+
format_(format), quality_(quality), outputPath_(std::move(outputPath)),
|
|
42
|
+
rotation_(rotation), transparent_(transparent),
|
|
43
|
+
renderFlags_(renderFlags), pageAlive_(std::move(pageAlive)) {}
|
|
37
44
|
|
|
38
45
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
39
46
|
|
|
@@ -74,18 +81,10 @@ protected:
|
|
|
74
81
|
std::vector<uint8_t> pixels(static_cast<size_t>(renderWidth_) *
|
|
75
82
|
static_cast<size_t>(renderHeight_) * channels);
|
|
76
83
|
|
|
77
|
-
for (int y = 0; y < renderHeight_; y++)
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
int dstIdx = (y * renderWidth_ + x) * channels;
|
|
82
|
-
pixels[dstIdx + 0] = row[srcIdx + 2]; // R ← BGRA[2]
|
|
83
|
-
pixels[dstIdx + 1] = row[srcIdx + 1]; // G ← BGRA[1]
|
|
84
|
-
pixels[dstIdx + 2] = row[srcIdx + 0]; // B ← BGRA[0]
|
|
85
|
-
if (transparent_)
|
|
86
|
-
pixels[dstIdx + 3] = row[srcIdx + 3]; // A ← BGRA[3]
|
|
87
|
-
}
|
|
88
|
-
}
|
|
84
|
+
for (int y = 0; y < renderHeight_; y++)
|
|
85
|
+
convertBgraRow(static_cast<uint8_t *>(bufferData) + y * stride,
|
|
86
|
+
pixels.data() + y * renderWidth_ * channels, renderWidth_,
|
|
87
|
+
4, channels);
|
|
89
88
|
|
|
90
89
|
FPDFBitmap_Destroy(bitmap);
|
|
91
90
|
|
|
@@ -109,11 +108,13 @@ protected:
|
|
|
109
108
|
// write to file if output path was specified
|
|
110
109
|
if (!outputPath_.empty()) {
|
|
111
110
|
// verify parent directory exists
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
111
|
+
auto slash = outputPath_.rfind('/');
|
|
112
|
+
if (slash != std::string::npos && slash > 0) {
|
|
113
|
+
std::string parentDir = outputPath_.substr(0, slash);
|
|
114
|
+
if (access(parentDir.c_str(), F_OK) != 0) {
|
|
115
|
+
SetError("Parent directory does not exist: " + parentDir);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
117
118
|
}
|
|
118
119
|
|
|
119
120
|
FILE *f = fopen(outputPath_.c_str(), "wb");
|
|
@@ -132,11 +133,12 @@ protected:
|
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
void OnOK() override {
|
|
135
|
-
CHECK_ENV();
|
|
136
136
|
Napi::Env env = Env();
|
|
137
137
|
if (outputPath_.empty()) {
|
|
138
|
-
auto
|
|
139
|
-
|
|
138
|
+
auto *vec = new std::vector<uint8_t>(std::move(encodedData_));
|
|
139
|
+
auto data = Napi::Buffer<uint8_t>::New(
|
|
140
|
+
env, vec->data(), vec->size(),
|
|
141
|
+
[](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; }, vec);
|
|
140
142
|
deferred_.Resolve(data);
|
|
141
143
|
} else {
|
|
142
144
|
deferred_.Resolve(env.Undefined());
|
|
@@ -144,13 +146,11 @@ protected:
|
|
|
144
146
|
}
|
|
145
147
|
|
|
146
148
|
void OnError(const Napi::Error &err) override {
|
|
147
|
-
CHECK_ENV();
|
|
148
149
|
deferred_.Reject(err.Value());
|
|
149
150
|
}
|
|
150
151
|
|
|
151
152
|
private:
|
|
152
153
|
Napi::Promise::Deferred deferred_;
|
|
153
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
154
154
|
FPDF_PAGE page_;
|
|
155
155
|
int renderWidth_;
|
|
156
156
|
int renderHeight_;
|
package/src/search_worker.h
CHANGED
|
@@ -24,15 +24,15 @@ struct SearchMatchData {
|
|
|
24
24
|
std::vector<Rect> rects;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
class SearchWorker : public
|
|
27
|
+
class SearchWorker : public SafeAsyncWorker {
|
|
28
28
|
public:
|
|
29
29
|
SearchWorker(Napi::Env env, FPDF_PAGE page, std::u16string needle,
|
|
30
30
|
unsigned long flags,
|
|
31
31
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
32
32
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
33
|
-
:
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
34
|
+
page_(page), needle_(std::move(needle)), flags_(flags),
|
|
35
|
+
pageAlive_(std::move(pageAlive)),
|
|
36
36
|
docAlive_(std::move(docAlive)) {}
|
|
37
37
|
|
|
38
38
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
@@ -87,7 +87,6 @@ protected:
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
void OnOK() override {
|
|
90
|
-
CHECK_ENV();
|
|
91
90
|
Napi::Env env = Env();
|
|
92
91
|
Napi::Array results = Napi::Array::New(env, matches_.size());
|
|
93
92
|
for (uint32_t i = 0; i < matches_.size(); i++) {
|
|
@@ -109,13 +108,11 @@ protected:
|
|
|
109
108
|
}
|
|
110
109
|
|
|
111
110
|
void OnError(const Napi::Error &err) override {
|
|
112
|
-
CHECK_ENV();
|
|
113
111
|
deferred_.Reject(err.Value());
|
|
114
112
|
}
|
|
115
113
|
|
|
116
114
|
private:
|
|
117
115
|
Napi::Promise::Deferred deferred_;
|
|
118
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
119
116
|
FPDF_PAGE page_;
|
|
120
117
|
std::u16string needle_;
|
|
121
118
|
unsigned long flags_;
|
package/src/split_merge_worker.h
CHANGED
|
@@ -4,9 +4,16 @@
|
|
|
4
4
|
|
|
5
5
|
#include <climits>
|
|
6
6
|
#include <cstdio>
|
|
7
|
-
#include <filesystem>
|
|
8
7
|
#include <vector>
|
|
9
8
|
|
|
9
|
+
#ifdef _WIN32
|
|
10
|
+
#include <io.h>
|
|
11
|
+
#define F_OK 0
|
|
12
|
+
#define access _access
|
|
13
|
+
#else
|
|
14
|
+
#include <unistd.h>
|
|
15
|
+
#endif
|
|
16
|
+
|
|
10
17
|
#include "fpdf_edit.h"
|
|
11
18
|
#include "fpdf_ppo.h"
|
|
12
19
|
#include "fpdf_save.h"
|
|
@@ -42,11 +49,13 @@ static inline bool SaveDocument(FPDF_DOCUMENT doc,
|
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
if (!outputPath.empty()) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
auto slash = outputPath.rfind('/');
|
|
53
|
+
if (slash != std::string::npos && slash > 0) {
|
|
54
|
+
std::string parentDir = outputPath.substr(0, slash);
|
|
55
|
+
if (access(parentDir.c_str(), F_OK) != 0) {
|
|
56
|
+
outError = "Parent directory does not exist: " + parentDir;
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
50
59
|
}
|
|
51
60
|
FILE *f = fopen(outputPath.c_str(), "wb");
|
|
52
61
|
if (!f) {
|
|
@@ -85,24 +94,7 @@ static inline FPDF_DOCUMENT LoadDoc(const std::vector<uint8_t> &bufferData,
|
|
|
85
94
|
}
|
|
86
95
|
|
|
87
96
|
if (!doc) {
|
|
88
|
-
|
|
89
|
-
switch (err) {
|
|
90
|
-
case FPDF_ERR_FILE:
|
|
91
|
-
outError = "FILE:File not found or could not be opened";
|
|
92
|
-
break;
|
|
93
|
-
case FPDF_ERR_FORMAT:
|
|
94
|
-
outError = "FORMAT:Not a valid PDF or corrupted";
|
|
95
|
-
break;
|
|
96
|
-
case FPDF_ERR_PASSWORD:
|
|
97
|
-
outError = "PASSWORD:Password required or incorrect";
|
|
98
|
-
break;
|
|
99
|
-
case FPDF_ERR_SECURITY:
|
|
100
|
-
outError = "SECURITY:Unsupported security scheme";
|
|
101
|
-
break;
|
|
102
|
-
default:
|
|
103
|
-
outError = "UNKNOWN:Unknown error";
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
97
|
+
outError = GetPdfiumErrorMessage();
|
|
106
98
|
}
|
|
107
99
|
return doc;
|
|
108
100
|
}
|
|
@@ -111,27 +103,27 @@ static inline FPDF_DOCUMENT LoadDoc(const std::vector<uint8_t> &bufferData,
|
|
|
111
103
|
// SplitDocumentWorker — split a PDF into multiple documents at given indices
|
|
112
104
|
// ---------------------------------------------------------------------------
|
|
113
105
|
|
|
114
|
-
class SplitDocumentWorker : public
|
|
106
|
+
class SplitDocumentWorker : public SafeAsyncWorker {
|
|
115
107
|
public:
|
|
116
108
|
// buffer variant
|
|
117
109
|
SplitDocumentWorker(Napi::Env env, std::vector<uint8_t> data,
|
|
118
110
|
std::vector<int> splitAt,
|
|
119
111
|
std::vector<std::string> outputPaths,
|
|
120
112
|
std::string password)
|
|
121
|
-
:
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
113
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
114
|
+
bufferData_(std::move(data)), splitAt_(std::move(splitAt)),
|
|
115
|
+
outputPaths_(std::move(outputPaths)), password_(std::move(password)),
|
|
116
|
+
useFile_(false) {}
|
|
125
117
|
|
|
126
118
|
// file path variant
|
|
127
119
|
SplitDocumentWorker(Napi::Env env, std::string filePath,
|
|
128
120
|
std::vector<int> splitAt,
|
|
129
121
|
std::vector<std::string> outputPaths,
|
|
130
122
|
std::string password)
|
|
131
|
-
:
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
123
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
124
|
+
filePath_(std::move(filePath)), splitAt_(std::move(splitAt)),
|
|
125
|
+
outputPaths_(std::move(outputPaths)), password_(std::move(password)),
|
|
126
|
+
useFile_(true) {}
|
|
135
127
|
|
|
136
128
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
137
129
|
|
|
@@ -213,29 +205,30 @@ protected:
|
|
|
213
205
|
}
|
|
214
206
|
|
|
215
207
|
void OnOK() override {
|
|
216
|
-
CHECK_ENV();
|
|
217
208
|
Napi::Env env = Env();
|
|
218
209
|
if (!outputPaths_.empty()) {
|
|
219
210
|
deferred_.Resolve(env.Undefined());
|
|
220
211
|
} else {
|
|
221
212
|
Napi::Array arr = Napi::Array::New(env, resultParts_.size());
|
|
222
213
|
for (size_t i = 0; i < resultParts_.size(); i++) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
214
|
+
auto *vec = new std::vector<uint8_t>(std::move(resultParts_[i]));
|
|
215
|
+
arr.Set(
|
|
216
|
+
static_cast<uint32_t>(i),
|
|
217
|
+
Napi::Buffer<uint8_t>::New(
|
|
218
|
+
env, vec->data(), vec->size(),
|
|
219
|
+
[](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; },
|
|
220
|
+
vec));
|
|
226
221
|
}
|
|
227
222
|
deferred_.Resolve(arr);
|
|
228
223
|
}
|
|
229
224
|
}
|
|
230
225
|
|
|
231
226
|
void OnError(const Napi::Error &err) override {
|
|
232
|
-
CHECK_ENV();
|
|
233
227
|
deferred_.Reject(err.Value());
|
|
234
228
|
}
|
|
235
229
|
|
|
236
230
|
private:
|
|
237
231
|
Napi::Promise::Deferred deferred_;
|
|
238
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
239
232
|
std::vector<uint8_t> bufferData_;
|
|
240
233
|
std::string filePath_;
|
|
241
234
|
std::vector<int> splitAt_;
|
|
@@ -256,13 +249,12 @@ struct DocInput {
|
|
|
256
249
|
bool useFile;
|
|
257
250
|
};
|
|
258
251
|
|
|
259
|
-
class MergeDocumentsWorker : public
|
|
252
|
+
class MergeDocumentsWorker : public SafeAsyncWorker {
|
|
260
253
|
public:
|
|
261
254
|
MergeDocumentsWorker(Napi::Env env, std::vector<DocInput> inputs,
|
|
262
255
|
std::string outputPath)
|
|
263
|
-
:
|
|
264
|
-
|
|
265
|
-
outputPath_(std::move(outputPath)) {}
|
|
256
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
257
|
+
inputs_(std::move(inputs)), outputPath_(std::move(outputPath)) {}
|
|
266
258
|
|
|
267
259
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
268
260
|
|
|
@@ -326,24 +318,24 @@ protected:
|
|
|
326
318
|
}
|
|
327
319
|
|
|
328
320
|
void OnOK() override {
|
|
329
|
-
CHECK_ENV();
|
|
330
321
|
Napi::Env env = Env();
|
|
331
322
|
if (outputPath_.empty()) {
|
|
332
|
-
|
|
333
|
-
|
|
323
|
+
auto *vec = new std::vector<uint8_t>(std::move(resultData_));
|
|
324
|
+
deferred_.Resolve(Napi::Buffer<uint8_t>::New(
|
|
325
|
+
env, vec->data(), vec->size(),
|
|
326
|
+
[](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; },
|
|
327
|
+
vec));
|
|
334
328
|
} else {
|
|
335
329
|
deferred_.Resolve(env.Undefined());
|
|
336
330
|
}
|
|
337
331
|
}
|
|
338
332
|
|
|
339
333
|
void OnError(const Napi::Error &err) override {
|
|
340
|
-
CHECK_ENV();
|
|
341
334
|
deferred_.Reject(err.Value());
|
|
342
335
|
}
|
|
343
336
|
|
|
344
337
|
private:
|
|
345
338
|
Napi::Promise::Deferred deferred_;
|
|
346
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
347
339
|
std::vector<DocInput> inputs_;
|
|
348
340
|
std::string outputPath_;
|
|
349
341
|
std::vector<uint8_t> resultData_;
|
package/src/text_worker.h
CHANGED
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
// GetTextWorker — async text extraction
|
|
14
14
|
// ---------------------------------------------------------------------------
|
|
15
15
|
|
|
16
|
-
class GetTextWorker : public
|
|
16
|
+
class GetTextWorker : public SafeAsyncWorker {
|
|
17
17
|
public:
|
|
18
18
|
GetTextWorker(Napi::Env env, FPDF_PAGE page,
|
|
19
19
|
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
20
20
|
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
21
|
-
:
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
22
|
+
page_(page), pageAlive_(std::move(pageAlive)),
|
|
23
|
+
docAlive_(std::move(docAlive)) {}
|
|
24
24
|
|
|
25
25
|
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
26
26
|
|
|
@@ -46,7 +46,6 @@ protected:
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
void OnOK() override {
|
|
49
|
-
CHECK_ENV();
|
|
50
49
|
Napi::Env env = Env();
|
|
51
50
|
if (textLen_ == 0) {
|
|
52
51
|
deferred_.Resolve(Napi::String::New(env, ""));
|
|
@@ -57,13 +56,11 @@ protected:
|
|
|
57
56
|
}
|
|
58
57
|
|
|
59
58
|
void OnError(const Napi::Error &err) override {
|
|
60
|
-
CHECK_ENV();
|
|
61
59
|
deferred_.Reject(err.Value());
|
|
62
60
|
}
|
|
63
61
|
|
|
64
62
|
private:
|
|
65
63
|
Napi::Promise::Deferred deferred_;
|
|
66
|
-
std::shared_ptr<std::atomic<bool>> envAlive_;
|
|
67
64
|
FPDF_PAGE page_;
|
|
68
65
|
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
69
66
|
std::shared_ptr<std::atomic<bool>> docAlive_;
|