pdfium-native 0.7.0 → 0.9.0
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 +42 -1
- package/README.md +324 -4
- package/dist/document.d.ts +43 -2
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +49 -6
- package/dist/document.js.map +1 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +9 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +60 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -1
- package/dist/page.d.ts +35 -1
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +51 -0
- package/dist/page.js.map +1 -1
- package/dist/types.d.ts +255 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +14 -0
- package/dist/validate.d.ts.map +1 -0
- package/dist/validate.js +16 -0
- package/dist/validate.js.map +1 -0
- package/lib/document.ts +80 -7
- package/lib/errors.ts +10 -0
- package/lib/index.ts +159 -0
- package/lib/page.ts +80 -0
- package/lib/types.ts +280 -0
- package/lib/validate.ts +15 -0
- package/package.json +1 -1
- package/scripts/generate-fixtures.mjs +310 -0
- package/scripts/prebuilds.json +10 -10
- package/src/annotations_worker.h +111 -0
- package/src/assemble_worker.h +235 -0
- package/src/attachments_worker.h +44 -45
- package/src/attachments_write_worker.h +158 -0
- package/src/characters_worker.h +197 -0
- package/src/destinations_worker.h +160 -0
- package/src/document.h +86 -0
- package/src/document_io.h +78 -0
- package/src/flatten_worker.h +133 -0
- package/src/javascript_worker.h +105 -0
- package/src/napi_helpers.h +55 -0
- package/src/page.h +82 -0
- package/src/pdfium_addon.cc +310 -0
- package/src/signatures_worker.h +248 -0
- package/src/split_merge_worker.h +2 -93
- package/src/structtree_worker.h +164 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "napi_helpers.h"
|
|
4
|
+
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <memory>
|
|
7
|
+
#include <string>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
#include "fpdf_javascript.h"
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// GetJavaScriptActionsWorker — async document-level JavaScript listing
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// These are the scripts in the document's /Names /JavaScript tree, which a
|
|
16
|
+
// viewer runs when the document opens. Surfacing them matters mostly for
|
|
17
|
+
// inspection and triage: a PDF arriving from outside that carries
|
|
18
|
+
// document-open JavaScript is worth a second look before it is rendered.
|
|
19
|
+
//
|
|
20
|
+
// pdfium-native never executes any of it — the build has V8 disabled, so the
|
|
21
|
+
// script comes back as inert text.
|
|
22
|
+
|
|
23
|
+
struct JavaScriptActionInfo {
|
|
24
|
+
int index = 0;
|
|
25
|
+
std::u16string name;
|
|
26
|
+
std::u16string script;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
class GetJavaScriptActionsWorker : public SafeAsyncWorker {
|
|
30
|
+
public:
|
|
31
|
+
GetJavaScriptActionsWorker(Napi::Env env, FPDF_DOCUMENT doc,
|
|
32
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
33
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
34
|
+
doc_(doc), docAlive_(std::move(docAlive)) {}
|
|
35
|
+
|
|
36
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
37
|
+
|
|
38
|
+
protected:
|
|
39
|
+
void Execute() override {
|
|
40
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
41
|
+
if (!docAlive_ || !docAlive_->load()) {
|
|
42
|
+
SetError("Document was destroyed");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
int count = FPDFDoc_GetJavaScriptActionCount(doc_);
|
|
47
|
+
if (count <= 0)
|
|
48
|
+
return;
|
|
49
|
+
|
|
50
|
+
actions_.reserve(count);
|
|
51
|
+
for (int i = 0; i < count; i++) {
|
|
52
|
+
FPDF_JAVASCRIPT_ACTION action = FPDFDoc_GetJavaScriptAction(doc_, i);
|
|
53
|
+
if (!action)
|
|
54
|
+
continue;
|
|
55
|
+
|
|
56
|
+
JavaScriptActionInfo info;
|
|
57
|
+
info.index = i;
|
|
58
|
+
|
|
59
|
+
info.name = ReadU16(
|
|
60
|
+
[&](auto *, unsigned long) {
|
|
61
|
+
return FPDFJavaScriptAction_GetName(action, nullptr, 0);
|
|
62
|
+
},
|
|
63
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
64
|
+
return FPDFJavaScriptAction_GetName(action, buf, len);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
info.script = ReadU16(
|
|
68
|
+
[&](auto *, unsigned long) {
|
|
69
|
+
return FPDFJavaScriptAction_GetScript(action, nullptr, 0);
|
|
70
|
+
},
|
|
71
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
72
|
+
return FPDFJavaScriptAction_GetScript(action, buf, len);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// unlike attachments and signatures, the caller owns this handle
|
|
76
|
+
FPDFDoc_CloseJavaScriptAction(action);
|
|
77
|
+
|
|
78
|
+
actions_.push_back(std::move(info));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
void OnOK() override {
|
|
83
|
+
Napi::Env env = Env();
|
|
84
|
+
Napi::Array arr = Napi::Array::New(env, actions_.size());
|
|
85
|
+
for (uint32_t i = 0; i < actions_.size(); i++) {
|
|
86
|
+
const auto &info = actions_[i];
|
|
87
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
88
|
+
obj.Set("index", Napi::Number::New(env, info.index));
|
|
89
|
+
SetU16(obj, "name", env, info.name);
|
|
90
|
+
SetU16(obj, "script", env, info.script);
|
|
91
|
+
arr.Set(i, obj);
|
|
92
|
+
}
|
|
93
|
+
deferred_.Resolve(arr);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
void OnError(const Napi::Error &err) override {
|
|
97
|
+
deferred_.Reject(err.Value());
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private:
|
|
101
|
+
Napi::Promise::Deferred deferred_;
|
|
102
|
+
FPDF_DOCUMENT doc_;
|
|
103
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
104
|
+
std::vector<JavaScriptActionInfo> actions_;
|
|
105
|
+
};
|
package/src/napi_helpers.h
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
3
|
#include <atomic>
|
|
4
|
+
#include <cstdio>
|
|
4
5
|
#include <mutex>
|
|
5
6
|
#include <napi.h>
|
|
6
7
|
#include <string>
|
|
7
8
|
#include <vector>
|
|
8
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
|
+
|
|
9
18
|
#include "fpdf_doc.h"
|
|
10
19
|
#include "fpdfview.h"
|
|
11
20
|
|
|
@@ -172,6 +181,19 @@ inline std::u16string ReadU16(GetLen getLen, GetData getData) {
|
|
|
172
181
|
len / sizeof(unsigned short) - 1);
|
|
173
182
|
}
|
|
174
183
|
|
|
184
|
+
// read a PDFium null-terminated byte buffer (7-bit ASCII or UTF-8) into a
|
|
185
|
+
// std::string. Both callbacks return the byte length including the null
|
|
186
|
+
// terminator; the terminator is dropped from the result.
|
|
187
|
+
template <typename GetLen, typename GetData>
|
|
188
|
+
inline std::string ReadUtf8(GetLen getLen, GetData getData) {
|
|
189
|
+
unsigned long len = getLen(static_cast<char *>(nullptr), 0);
|
|
190
|
+
if (len < 2)
|
|
191
|
+
return {};
|
|
192
|
+
std::vector<char> buf(len);
|
|
193
|
+
getData(buf.data(), len);
|
|
194
|
+
return std::string(buf.data(), len - 1);
|
|
195
|
+
}
|
|
196
|
+
|
|
175
197
|
// convert a std::u16string to a Napi::String (empty string if input is empty)
|
|
176
198
|
inline Napi::String ToNapiString(Napi::Env env, const std::u16string &s) {
|
|
177
199
|
if (s.empty())
|
|
@@ -193,6 +215,39 @@ inline void SetU16IfPresent(Napi::Object &obj, const char *key, Napi::Env env,
|
|
|
193
215
|
obj.Set(key, ToNapiString(env, s));
|
|
194
216
|
}
|
|
195
217
|
|
|
218
|
+
// ---------------------------------------------------------------------------
|
|
219
|
+
// File output helper
|
|
220
|
+
// ---------------------------------------------------------------------------
|
|
221
|
+
|
|
222
|
+
// Writes bytes to `path`, verifying the parent directory exists first (mirrors
|
|
223
|
+
// RenderWorker). Returns false and sets `err` on failure.
|
|
224
|
+
inline bool WriteBytesToFile(const std::string &path,
|
|
225
|
+
const std::vector<uint8_t> &data,
|
|
226
|
+
std::string &err) {
|
|
227
|
+
auto slash = path.rfind('/');
|
|
228
|
+
if (slash != std::string::npos && slash > 0) {
|
|
229
|
+
std::string parentDir = path.substr(0, slash);
|
|
230
|
+
if (access(parentDir.c_str(), F_OK) != 0) {
|
|
231
|
+
err = "Parent directory does not exist: " + parentDir;
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
FILE *f = fopen(path.c_str(), "wb");
|
|
237
|
+
if (!f) {
|
|
238
|
+
err = "Failed to open output file: " + path;
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
size_t total = data.size();
|
|
242
|
+
size_t wrote = total == 0 ? 0 : fwrite(data.data(), 1, total, f);
|
|
243
|
+
fclose(f);
|
|
244
|
+
if (wrote != total) {
|
|
245
|
+
err = "Failed to write output file: " + path;
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
|
|
196
251
|
// ---------------------------------------------------------------------------
|
|
197
252
|
// action type string from FPDF action type constant
|
|
198
253
|
// ---------------------------------------------------------------------------
|
package/src/page.h
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
3
|
#include "annotations_worker.h"
|
|
4
|
+
#include "characters_worker.h"
|
|
4
5
|
#include "form_fields_worker.h"
|
|
5
6
|
#include "links_worker.h"
|
|
6
7
|
#include "objects_worker.h"
|
|
7
8
|
#include "render_image_worker.h"
|
|
8
9
|
#include "render_worker.h"
|
|
9
10
|
#include "search_worker.h"
|
|
11
|
+
#include "structtree_worker.h"
|
|
10
12
|
#include "text_worker.h"
|
|
11
13
|
|
|
12
14
|
#include <atomic>
|
|
@@ -29,13 +31,17 @@ public:
|
|
|
29
31
|
{
|
|
30
32
|
InstanceAccessor<&PDFiumPage::GetNumber>("number"),
|
|
31
33
|
InstanceMethod<&PDFiumPage::GetText>("getText"),
|
|
34
|
+
InstanceMethod<&PDFiumPage::GetCharacters>("getCharacters"),
|
|
32
35
|
InstanceMethod<&PDFiumPage::Render>("render"),
|
|
33
36
|
InstanceMethod<&PDFiumPage::GetObject>("getObject"),
|
|
34
37
|
InstanceMethod<&PDFiumPage::RenderImage>("renderImage"),
|
|
35
38
|
InstanceMethod<&PDFiumPage::GetLinks>("getLinks"),
|
|
36
39
|
InstanceMethod<&PDFiumPage::Search>("search"),
|
|
37
40
|
InstanceMethod<&PDFiumPage::GetAnnotations>("getAnnotations"),
|
|
41
|
+
InstanceMethod<&PDFiumPage::GetAnnotationAttachment>(
|
|
42
|
+
"getAnnotationAttachment"),
|
|
38
43
|
InstanceMethod<&PDFiumPage::GetFormFields>("getFormFields"),
|
|
44
|
+
InstanceMethod<&PDFiumPage::GetStructTree>("getStructTree"),
|
|
39
45
|
InstanceMethod<&PDFiumPage::Close>("close"),
|
|
40
46
|
});
|
|
41
47
|
}
|
|
@@ -105,6 +111,52 @@ private:
|
|
|
105
111
|
return promise;
|
|
106
112
|
}
|
|
107
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Extracts every character with its position, font and rotation (async).
|
|
116
|
+
* Optional (start, count) arguments restrict the range; a negative count
|
|
117
|
+
* means "to the end of the page".
|
|
118
|
+
*/
|
|
119
|
+
Napi::Value GetCharacters(const Napi::CallbackInfo &info) {
|
|
120
|
+
Napi::Env env = info.Env();
|
|
121
|
+
EnsureOpen(env);
|
|
122
|
+
if (env.IsExceptionPending())
|
|
123
|
+
return env.Null();
|
|
124
|
+
|
|
125
|
+
int start = 0;
|
|
126
|
+
int count = -1;
|
|
127
|
+
if (info.Length() > 0 && info[0].IsNumber())
|
|
128
|
+
start = info[0].As<Napi::Number>().Int32Value();
|
|
129
|
+
if (info.Length() > 1 && info[1].IsNumber())
|
|
130
|
+
count = info[1].As<Napi::Number>().Int32Value();
|
|
131
|
+
|
|
132
|
+
if (start < 0) {
|
|
133
|
+
Napi::RangeError::New(env, "Character start index must not be negative")
|
|
134
|
+
.ThrowAsJavaScriptException();
|
|
135
|
+
return env.Null();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
auto *worker =
|
|
139
|
+
new GetCharactersWorker(env, page_, start, count, alive_, docAlive_);
|
|
140
|
+
auto promise = worker->Promise();
|
|
141
|
+
worker->Queue();
|
|
142
|
+
return promise;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Returns the tagged-PDF structure tree for this page (async).
|
|
147
|
+
*/
|
|
148
|
+
Napi::Value GetStructTree(const Napi::CallbackInfo &info) {
|
|
149
|
+
Napi::Env env = info.Env();
|
|
150
|
+
EnsureOpen(env);
|
|
151
|
+
if (env.IsExceptionPending())
|
|
152
|
+
return env.Null();
|
|
153
|
+
|
|
154
|
+
auto *worker = new GetStructTreeWorker(env, page_, alive_, docAlive_);
|
|
155
|
+
auto promise = worker->Promise();
|
|
156
|
+
worker->Queue();
|
|
157
|
+
return promise;
|
|
158
|
+
}
|
|
159
|
+
|
|
108
160
|
/**
|
|
109
161
|
* Renders the page to a JPEG or PNG buffer (async).
|
|
110
162
|
*/
|
|
@@ -345,6 +397,36 @@ private:
|
|
|
345
397
|
return promise;
|
|
346
398
|
}
|
|
347
399
|
|
|
400
|
+
/**
|
|
401
|
+
* Reads the embedded file of the FileAttachment annotation at `index`
|
|
402
|
+
* (async). Resolves to a Buffer, or writes to an optional output path and
|
|
403
|
+
* resolves to undefined.
|
|
404
|
+
*/
|
|
405
|
+
Napi::Value GetAnnotationAttachment(const Napi::CallbackInfo &info) {
|
|
406
|
+
Napi::Env env = info.Env();
|
|
407
|
+
EnsureOpen(env);
|
|
408
|
+
if (env.IsExceptionPending())
|
|
409
|
+
return env.Null();
|
|
410
|
+
|
|
411
|
+
if (info.Length() < 1 || !info[0].IsNumber()) {
|
|
412
|
+
Napi::TypeError::New(env, "Expected annotation index")
|
|
413
|
+
.ThrowAsJavaScriptException();
|
|
414
|
+
return env.Null();
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
int index = info[0].As<Napi::Number>().Int32Value();
|
|
418
|
+
|
|
419
|
+
std::string outputPath;
|
|
420
|
+
if (info.Length() > 1 && info[1].IsString())
|
|
421
|
+
outputPath = info[1].As<Napi::String>().Utf8Value();
|
|
422
|
+
|
|
423
|
+
auto *worker = new GetAnnotationAttachmentWorker(
|
|
424
|
+
env, page_, index, std::move(outputPath), alive_, docAlive_);
|
|
425
|
+
auto promise = worker->Promise();
|
|
426
|
+
worker->Queue();
|
|
427
|
+
return promise;
|
|
428
|
+
}
|
|
429
|
+
|
|
348
430
|
/**
|
|
349
431
|
* Returns all form fields on the page (async). Returns a
|
|
350
432
|
* Promise<FormField[]>.
|
package/src/pdfium_addon.cc
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
#include <climits>
|
|
4
4
|
#include <cstring>
|
|
5
5
|
|
|
6
|
+
#include "assemble_worker.h"
|
|
7
|
+
#include "attachments_write_worker.h"
|
|
8
|
+
#include "flatten_worker.h"
|
|
6
9
|
#include "fpdf_attachment.h"
|
|
7
10
|
#include "fpdf_catalog.h"
|
|
8
11
|
#include "fpdf_signature.h"
|
|
@@ -376,6 +379,309 @@ Napi::Value MergeDocuments(const Napi::CallbackInfo &info) {
|
|
|
376
379
|
return promise;
|
|
377
380
|
}
|
|
378
381
|
|
|
382
|
+
// ---------------------------------------------------------------------------
|
|
383
|
+
// assemblePages — build a PDF from selected source pages, in order (async)
|
|
384
|
+
// ---------------------------------------------------------------------------
|
|
385
|
+
|
|
386
|
+
Napi::Value AssemblePages(const Napi::CallbackInfo &info) {
|
|
387
|
+
Napi::Env env = info.Env();
|
|
388
|
+
|
|
389
|
+
if (info.Length() < 2 || !info[1].IsArray()) {
|
|
390
|
+
Napi::TypeError::New(env, "Expected (input, pages[], options?) arguments")
|
|
391
|
+
.ThrowAsJavaScriptException();
|
|
392
|
+
return env.Null();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
Napi::Array jsPages = info[1].As<Napi::Array>();
|
|
396
|
+
if (jsPages.Length() == 0) {
|
|
397
|
+
Napi::TypeError::New(env, "At least one page index is required")
|
|
398
|
+
.ThrowAsJavaScriptException();
|
|
399
|
+
return env.Null();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
std::vector<int> pages;
|
|
403
|
+
pages.reserve(jsPages.Length());
|
|
404
|
+
for (uint32_t i = 0; i < jsPages.Length(); i++) {
|
|
405
|
+
pages.push_back(jsPages.Get(i).As<Napi::Number>().Int32Value());
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
std::string outputPath;
|
|
409
|
+
std::string password;
|
|
410
|
+
if (info.Length() > 2 && info[2].IsObject()) {
|
|
411
|
+
Napi::Object opts = info[2].As<Napi::Object>();
|
|
412
|
+
if (opts.Has("output") && opts.Get("output").IsString()) {
|
|
413
|
+
outputPath = opts.Get("output").As<Napi::String>().Utf8Value();
|
|
414
|
+
}
|
|
415
|
+
if (opts.Has("password") && opts.Get("password").IsString()) {
|
|
416
|
+
password = opts.Get("password").As<Napi::String>().Utf8Value();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
Napi::Value arg = info[0];
|
|
421
|
+
AssemblePagesWorker *worker = nullptr;
|
|
422
|
+
|
|
423
|
+
if (arg.IsBuffer()) {
|
|
424
|
+
auto buffer = arg.As<Napi::Buffer<uint8_t>>();
|
|
425
|
+
std::vector<uint8_t> data(buffer.Data(), buffer.Data() + buffer.Length());
|
|
426
|
+
worker = new AssemblePagesWorker(env, std::move(data), std::move(pages),
|
|
427
|
+
std::move(outputPath), std::move(password));
|
|
428
|
+
} else if (arg.IsString()) {
|
|
429
|
+
std::string path = arg.As<Napi::String>().Utf8Value();
|
|
430
|
+
worker = new AssemblePagesWorker(env, std::move(path), std::move(pages),
|
|
431
|
+
std::move(outputPath), std::move(password));
|
|
432
|
+
} else {
|
|
433
|
+
Napi::TypeError::New(env, "Expected a Buffer or string path argument")
|
|
434
|
+
.ThrowAsJavaScriptException();
|
|
435
|
+
return env.Null();
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
auto promise = worker->Promise();
|
|
439
|
+
worker->Queue();
|
|
440
|
+
return promise;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// ---------------------------------------------------------------------------
|
|
444
|
+
// nUpPages — impose several source pages onto each output page (async)
|
|
445
|
+
// ---------------------------------------------------------------------------
|
|
446
|
+
|
|
447
|
+
Napi::Value NUpPages(const Napi::CallbackInfo &info) {
|
|
448
|
+
Napi::Env env = info.Env();
|
|
449
|
+
|
|
450
|
+
if (info.Length() < 2 || !info[1].IsObject()) {
|
|
451
|
+
Napi::TypeError::New(env, "Expected (input, options) arguments")
|
|
452
|
+
.ThrowAsJavaScriptException();
|
|
453
|
+
return env.Null();
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
Napi::Object opts = info[1].As<Napi::Object>();
|
|
457
|
+
if (!opts.Has("columns") || !opts.Has("rows")) {
|
|
458
|
+
Napi::TypeError::New(env, "Expected columns and rows options")
|
|
459
|
+
.ThrowAsJavaScriptException();
|
|
460
|
+
return env.Null();
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
int columns = opts.Get("columns").As<Napi::Number>().Int32Value();
|
|
464
|
+
int rows = opts.Get("rows").As<Napi::Number>().Int32Value();
|
|
465
|
+
if (columns <= 0 || rows <= 0) {
|
|
466
|
+
Napi::RangeError::New(env, "columns and rows must be positive")
|
|
467
|
+
.ThrowAsJavaScriptException();
|
|
468
|
+
return env.Null();
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// 0 means "take it from the first source page", resolved inside the worker
|
|
472
|
+
float width = 0;
|
|
473
|
+
float height = 0;
|
|
474
|
+
if (opts.Has("width") && opts.Get("width").IsNumber()) {
|
|
475
|
+
width = opts.Get("width").As<Napi::Number>().FloatValue();
|
|
476
|
+
}
|
|
477
|
+
if (opts.Has("height") && opts.Get("height").IsNumber()) {
|
|
478
|
+
height = opts.Get("height").As<Napi::Number>().FloatValue();
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
std::string outputPath;
|
|
482
|
+
std::string password;
|
|
483
|
+
if (opts.Has("output") && opts.Get("output").IsString()) {
|
|
484
|
+
outputPath = opts.Get("output").As<Napi::String>().Utf8Value();
|
|
485
|
+
}
|
|
486
|
+
if (opts.Has("password") && opts.Get("password").IsString()) {
|
|
487
|
+
password = opts.Get("password").As<Napi::String>().Utf8Value();
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
Napi::Value arg = info[0];
|
|
491
|
+
NUpPagesWorker *worker = nullptr;
|
|
492
|
+
|
|
493
|
+
if (arg.IsBuffer()) {
|
|
494
|
+
auto buffer = arg.As<Napi::Buffer<uint8_t>>();
|
|
495
|
+
std::vector<uint8_t> data(buffer.Data(), buffer.Data() + buffer.Length());
|
|
496
|
+
worker = new NUpPagesWorker(env, std::move(data), columns, rows, width,
|
|
497
|
+
height, std::move(outputPath),
|
|
498
|
+
std::move(password));
|
|
499
|
+
} else if (arg.IsString()) {
|
|
500
|
+
std::string path = arg.As<Napi::String>().Utf8Value();
|
|
501
|
+
worker = new NUpPagesWorker(env, std::move(path), columns, rows, width,
|
|
502
|
+
height, std::move(outputPath),
|
|
503
|
+
std::move(password));
|
|
504
|
+
} else {
|
|
505
|
+
Napi::TypeError::New(env, "Expected a Buffer or string path argument")
|
|
506
|
+
.ThrowAsJavaScriptException();
|
|
507
|
+
return env.Null();
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
auto promise = worker->Promise();
|
|
511
|
+
worker->Queue();
|
|
512
|
+
return promise;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// ---------------------------------------------------------------------------
|
|
516
|
+
// addAttachments — embed files into a copy of a document (async)
|
|
517
|
+
// ---------------------------------------------------------------------------
|
|
518
|
+
|
|
519
|
+
Napi::Value AddAttachments(const Napi::CallbackInfo &info) {
|
|
520
|
+
Napi::Env env = info.Env();
|
|
521
|
+
|
|
522
|
+
if (info.Length() < 2 || !info[1].IsArray()) {
|
|
523
|
+
Napi::TypeError::New(env,
|
|
524
|
+
"Expected (input, attachments[], options?) arguments")
|
|
525
|
+
.ThrowAsJavaScriptException();
|
|
526
|
+
return env.Null();
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
Napi::Array jsAttachments = info[1].As<Napi::Array>();
|
|
530
|
+
if (jsAttachments.Length() == 0) {
|
|
531
|
+
Napi::TypeError::New(env, "At least one attachment is required")
|
|
532
|
+
.ThrowAsJavaScriptException();
|
|
533
|
+
return env.Null();
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
std::vector<AttachmentToAdd> attachments;
|
|
537
|
+
attachments.reserve(jsAttachments.Length());
|
|
538
|
+
|
|
539
|
+
for (uint32_t i = 0; i < jsAttachments.Length(); i++) {
|
|
540
|
+
Napi::Value item = jsAttachments.Get(i);
|
|
541
|
+
if (!item.IsObject()) {
|
|
542
|
+
Napi::TypeError::New(env, "Each attachment must be an object")
|
|
543
|
+
.ThrowAsJavaScriptException();
|
|
544
|
+
return env.Null();
|
|
545
|
+
}
|
|
546
|
+
Napi::Object obj = item.As<Napi::Object>();
|
|
547
|
+
|
|
548
|
+
if (!obj.Has("name") || !obj.Get("name").IsString()) {
|
|
549
|
+
Napi::TypeError::New(env, "Each attachment needs a 'name' string")
|
|
550
|
+
.ThrowAsJavaScriptException();
|
|
551
|
+
return env.Null();
|
|
552
|
+
}
|
|
553
|
+
if (!obj.Has("data") || !obj.Get("data").IsBuffer()) {
|
|
554
|
+
Napi::TypeError::New(env, "Each attachment needs a 'data' Buffer")
|
|
555
|
+
.ThrowAsJavaScriptException();
|
|
556
|
+
return env.Null();
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
AttachmentToAdd add;
|
|
560
|
+
add.name = obj.Get("name").As<Napi::String>().Utf16Value();
|
|
561
|
+
|
|
562
|
+
auto buffer = obj.Get("data").As<Napi::Buffer<uint8_t>>();
|
|
563
|
+
add.data.assign(buffer.Data(), buffer.Data() + buffer.Length());
|
|
564
|
+
|
|
565
|
+
if (obj.Has("creationDate") && obj.Get("creationDate").IsString()) {
|
|
566
|
+
add.creationDate =
|
|
567
|
+
obj.Get("creationDate").As<Napi::String>().Utf16Value();
|
|
568
|
+
}
|
|
569
|
+
if (obj.Has("modDate") && obj.Get("modDate").IsString()) {
|
|
570
|
+
add.modDate = obj.Get("modDate").As<Napi::String>().Utf16Value();
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
attachments.push_back(std::move(add));
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
std::string outputPath;
|
|
577
|
+
std::string password;
|
|
578
|
+
if (info.Length() > 2 && info[2].IsObject()) {
|
|
579
|
+
Napi::Object opts = info[2].As<Napi::Object>();
|
|
580
|
+
if (opts.Has("output") && opts.Get("output").IsString()) {
|
|
581
|
+
outputPath = opts.Get("output").As<Napi::String>().Utf8Value();
|
|
582
|
+
}
|
|
583
|
+
if (opts.Has("password") && opts.Get("password").IsString()) {
|
|
584
|
+
password = opts.Get("password").As<Napi::String>().Utf8Value();
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
Napi::Value arg = info[0];
|
|
589
|
+
AddAttachmentsWorker *worker = nullptr;
|
|
590
|
+
|
|
591
|
+
if (arg.IsBuffer()) {
|
|
592
|
+
auto buffer = arg.As<Napi::Buffer<uint8_t>>();
|
|
593
|
+
std::vector<uint8_t> data(buffer.Data(), buffer.Data() + buffer.Length());
|
|
594
|
+
worker = new AddAttachmentsWorker(env, std::move(data),
|
|
595
|
+
std::move(attachments),
|
|
596
|
+
std::move(outputPath),
|
|
597
|
+
std::move(password));
|
|
598
|
+
} else if (arg.IsString()) {
|
|
599
|
+
std::string path = arg.As<Napi::String>().Utf8Value();
|
|
600
|
+
worker = new AddAttachmentsWorker(env, std::move(path),
|
|
601
|
+
std::move(attachments),
|
|
602
|
+
std::move(outputPath),
|
|
603
|
+
std::move(password));
|
|
604
|
+
} else {
|
|
605
|
+
Napi::TypeError::New(env, "Expected a Buffer or string path argument")
|
|
606
|
+
.ThrowAsJavaScriptException();
|
|
607
|
+
return env.Null();
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
auto promise = worker->Promise();
|
|
611
|
+
worker->Queue();
|
|
612
|
+
return promise;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// ---------------------------------------------------------------------------
|
|
616
|
+
// flattenDocument — bake annotations into page content (async)
|
|
617
|
+
// ---------------------------------------------------------------------------
|
|
618
|
+
|
|
619
|
+
Napi::Value FlattenDocument(const Napi::CallbackInfo &info) {
|
|
620
|
+
Napi::Env env = info.Env();
|
|
621
|
+
|
|
622
|
+
if (info.Length() < 1) {
|
|
623
|
+
Napi::TypeError::New(env, "Expected (input, options?) arguments")
|
|
624
|
+
.ThrowAsJavaScriptException();
|
|
625
|
+
return env.Null();
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
int flag = FLAT_NORMALDISPLAY;
|
|
629
|
+
std::vector<int> pages;
|
|
630
|
+
std::string outputPath;
|
|
631
|
+
std::string password;
|
|
632
|
+
|
|
633
|
+
if (info.Length() > 1 && info[1].IsObject()) {
|
|
634
|
+
Napi::Object opts = info[1].As<Napi::Object>();
|
|
635
|
+
if (opts.Has("usage") && opts.Get("usage").IsString()) {
|
|
636
|
+
std::string usage = opts.Get("usage").As<Napi::String>().Utf8Value();
|
|
637
|
+
if (usage == "print") {
|
|
638
|
+
flag = FLAT_PRINT;
|
|
639
|
+
} else if (usage != "display") {
|
|
640
|
+
Napi::TypeError::New(env, "usage must be 'display' or 'print'")
|
|
641
|
+
.ThrowAsJavaScriptException();
|
|
642
|
+
return env.Null();
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
if (opts.Has("pages") && opts.Get("pages").IsArray()) {
|
|
646
|
+
Napi::Array jsPages = opts.Get("pages").As<Napi::Array>();
|
|
647
|
+
pages.reserve(jsPages.Length());
|
|
648
|
+
for (uint32_t i = 0; i < jsPages.Length(); i++) {
|
|
649
|
+
pages.push_back(jsPages.Get(i).As<Napi::Number>().Int32Value());
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
if (opts.Has("output") && opts.Get("output").IsString()) {
|
|
653
|
+
outputPath = opts.Get("output").As<Napi::String>().Utf8Value();
|
|
654
|
+
}
|
|
655
|
+
if (opts.Has("password") && opts.Get("password").IsString()) {
|
|
656
|
+
password = opts.Get("password").As<Napi::String>().Utf8Value();
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
Napi::Value arg = info[0];
|
|
661
|
+
FlattenDocumentWorker *worker = nullptr;
|
|
662
|
+
|
|
663
|
+
if (arg.IsBuffer()) {
|
|
664
|
+
auto buffer = arg.As<Napi::Buffer<uint8_t>>();
|
|
665
|
+
std::vector<uint8_t> data(buffer.Data(), buffer.Data() + buffer.Length());
|
|
666
|
+
worker = new FlattenDocumentWorker(env, std::move(data), flag,
|
|
667
|
+
std::move(pages), std::move(outputPath),
|
|
668
|
+
std::move(password));
|
|
669
|
+
} else if (arg.IsString()) {
|
|
670
|
+
std::string path = arg.As<Napi::String>().Utf8Value();
|
|
671
|
+
worker = new FlattenDocumentWorker(env, std::move(path), flag,
|
|
672
|
+
std::move(pages), std::move(outputPath),
|
|
673
|
+
std::move(password));
|
|
674
|
+
} else {
|
|
675
|
+
Napi::TypeError::New(env, "Expected a Buffer or string path argument")
|
|
676
|
+
.ThrowAsJavaScriptException();
|
|
677
|
+
return env.Null();
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
auto promise = worker->Promise();
|
|
681
|
+
worker->Queue();
|
|
682
|
+
return promise;
|
|
683
|
+
}
|
|
684
|
+
|
|
379
685
|
// ---------------------------------------------------------------------------
|
|
380
686
|
// Module init
|
|
381
687
|
// ---------------------------------------------------------------------------
|
|
@@ -430,6 +736,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
|
430
736
|
exports.Set("loadDocument", Napi::Function::New(env, LoadDocument));
|
|
431
737
|
exports.Set("splitDocument", Napi::Function::New(env, SplitDocument));
|
|
432
738
|
exports.Set("mergeDocuments", Napi::Function::New(env, MergeDocuments));
|
|
739
|
+
exports.Set("assemblePages", Napi::Function::New(env, AssemblePages));
|
|
740
|
+
exports.Set("nUpPages", Napi::Function::New(env, NUpPages));
|
|
741
|
+
exports.Set("addAttachments", Napi::Function::New(env, AddAttachments));
|
|
742
|
+
exports.Set("flattenDocument", Napi::Function::New(env, FlattenDocument));
|
|
433
743
|
exports.Set("prepareShutdown", Napi::Function::New(env, PrepareShutdown));
|
|
434
744
|
|
|
435
745
|
return exports;
|