pdfium-native 0.8.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +46 -1
- package/README.md +302 -3
- package/dist/document.d.ts +43 -2
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +41 -0
- 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 +58 -0
- package/dist/index.js.map +1 -1
- package/dist/page.d.ts +22 -1
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +41 -0
- package/dist/page.js.map +1 -1
- package/dist/types.d.ts +271 -0
- package/dist/types.d.ts.map +1 -1
- package/lib/document.ts +70 -1
- package/lib/errors.ts +10 -0
- package/lib/index.ts +162 -0
- package/lib/page.ts +52 -0
- package/lib/types.ts +296 -0
- package/package.json +1 -1
- package/scripts/generate-fixtures.mjs +353 -0
- package/scripts/prebuilds.json +10 -10
- package/src/annotations_worker.h +1 -1
- package/src/assemble_worker.h +235 -0
- package/src/attachments_worker.h +1 -39
- package/src/attachments_write_worker.h +168 -0
- package/src/characters_worker.h +240 -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 +138 -0
- package/src/javascript_worker.h +105 -0
- package/src/napi_helpers.h +55 -0
- package/src/page.h +50 -0
- package/src/pdfium_addon.cc +314 -0
- package/src/signatures_worker.h +248 -0
- package/src/split_merge_worker.h +2 -93
- package/src/structtree_worker.h +189 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "document_io.h"
|
|
4
|
+
|
|
5
|
+
#include <climits>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
#include "fpdf_attachment.h"
|
|
10
|
+
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
// AddAttachmentsWorker — embed files into a copy of a document
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// This is the one write path in the library, and PDFium's embedding API is
|
|
15
|
+
// much narrower than the read side. Against the pinned chromium/7920 build it
|
|
16
|
+
// reaches exactly three things:
|
|
17
|
+
//
|
|
18
|
+
// /UF + /F the file name (FPDFDoc_AddAttachment)
|
|
19
|
+
// the stream the file bytes (FPDFAttachment_SetFile)
|
|
20
|
+
// /Params Size, CreationDate, ModDate, CheckSum
|
|
21
|
+
// (FPDFAttachment_SetStringValue)
|
|
22
|
+
//
|
|
23
|
+
// It cannot set /Subtype (the MIME type) or /AFRelationship: both sit on the
|
|
24
|
+
// filespec dict or the stream dict, not in the /Params sub-dictionary that
|
|
25
|
+
// SetStringValue writes to, and PDFium exposes no setter for either.
|
|
26
|
+
// /Desc is out of reach too — FPDFAttachment_SetDescription exists upstream but
|
|
27
|
+
// not in chromium/7920.
|
|
28
|
+
//
|
|
29
|
+
// A file embedded this way is therefore a valid PDF attachment but NOT a
|
|
30
|
+
// conformant PDF/A-3 / ZUGFeRD / Factur-X e-invoice. See the note on
|
|
31
|
+
// addAttachments() in the docs.
|
|
32
|
+
|
|
33
|
+
struct AttachmentToAdd {
|
|
34
|
+
std::u16string name;
|
|
35
|
+
std::vector<uint8_t> data;
|
|
36
|
+
std::u16string creationDate;
|
|
37
|
+
std::u16string modDate;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
class AddAttachmentsWorker : public SafeAsyncWorker {
|
|
41
|
+
public:
|
|
42
|
+
// buffer variant
|
|
43
|
+
AddAttachmentsWorker(Napi::Env env, std::vector<uint8_t> data,
|
|
44
|
+
std::vector<AttachmentToAdd> attachments,
|
|
45
|
+
std::string outputPath, std::string password)
|
|
46
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
47
|
+
bufferData_(std::move(data)), attachments_(std::move(attachments)),
|
|
48
|
+
outputPath_(std::move(outputPath)), password_(std::move(password)),
|
|
49
|
+
useFile_(false) {}
|
|
50
|
+
|
|
51
|
+
// file path variant
|
|
52
|
+
AddAttachmentsWorker(Napi::Env env, std::string filePath,
|
|
53
|
+
std::vector<AttachmentToAdd> attachments,
|
|
54
|
+
std::string outputPath, std::string password)
|
|
55
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
56
|
+
filePath_(std::move(filePath)), attachments_(std::move(attachments)),
|
|
57
|
+
outputPath_(std::move(outputPath)), password_(std::move(password)),
|
|
58
|
+
useFile_(true) {}
|
|
59
|
+
|
|
60
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
61
|
+
|
|
62
|
+
protected:
|
|
63
|
+
void Execute() override {
|
|
64
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
65
|
+
|
|
66
|
+
std::string loadError;
|
|
67
|
+
FPDF_DOCUMENT doc =
|
|
68
|
+
LoadDoc(bufferData_, filePath_, useFile_, password_, loadError);
|
|
69
|
+
if (!doc) {
|
|
70
|
+
SetError(loadError);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const auto &item : attachments_) {
|
|
75
|
+
FPDF_ATTACHMENT attachment =
|
|
76
|
+
FPDFDoc_AddAttachment(doc, ToWideString(item.name));
|
|
77
|
+
if (!attachment) {
|
|
78
|
+
FPDF_CloseDocument(doc);
|
|
79
|
+
// AddAttachment also rejects a name that already exists in the
|
|
80
|
+
// embedded-files name tree, which is the likely cause here.
|
|
81
|
+
SetError("Failed to add attachment (empty or duplicate name): " +
|
|
82
|
+
Utf8Of(item.name));
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// unsigned long is 32-bit on Windows, so an oversized payload would be
|
|
87
|
+
// truncated mod 2^32 and silently written short. PDFium rejects anything
|
|
88
|
+
// above INT_MAX anyway; refuse it here with a message that says why.
|
|
89
|
+
if (item.data.size() > static_cast<size_t>(INT_MAX)) {
|
|
90
|
+
FPDF_CloseDocument(doc);
|
|
91
|
+
SetError("Attachment is too large (max 2 GiB): " + Utf8Of(item.name));
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// SetFile must come first: it rebuilds the embedded-file stream and its
|
|
96
|
+
// /Params, discarding anything already written there.
|
|
97
|
+
if (!FPDFAttachment_SetFile(attachment, doc, item.data.data(),
|
|
98
|
+
static_cast<unsigned long>(
|
|
99
|
+
item.data.size()))) {
|
|
100
|
+
FPDF_CloseDocument(doc);
|
|
101
|
+
SetError("Failed to write attachment data: " + Utf8Of(item.name));
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// SetFile stamps CreationDate with the current time; an explicit value
|
|
106
|
+
// overwrites it, which is also what makes output reproducible.
|
|
107
|
+
if (!item.creationDate.empty()) {
|
|
108
|
+
FPDFAttachment_SetStringValue(attachment, "CreationDate",
|
|
109
|
+
ToWideString(item.creationDate));
|
|
110
|
+
}
|
|
111
|
+
if (!item.modDate.empty()) {
|
|
112
|
+
FPDFAttachment_SetStringValue(attachment, "ModDate",
|
|
113
|
+
ToWideString(item.modDate));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
std::string saveError;
|
|
118
|
+
if (!SaveDocument(doc, outputPath_, resultData_, saveError)) {
|
|
119
|
+
FPDF_CloseDocument(doc);
|
|
120
|
+
SetError(saveError);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
FPDF_CloseDocument(doc);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
void OnOK() override {
|
|
128
|
+
Napi::Env env = Env();
|
|
129
|
+
if (!outputPath_.empty()) {
|
|
130
|
+
deferred_.Resolve(env.Undefined());
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
auto *vec = new std::vector<uint8_t>(std::move(resultData_));
|
|
134
|
+
deferred_.Resolve(Napi::Buffer<uint8_t>::New(
|
|
135
|
+
env, vec->data(), vec->size(),
|
|
136
|
+
[](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; }, vec));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
void OnError(const Napi::Error &err) override {
|
|
140
|
+
deferred_.Reject(err.Value());
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private:
|
|
144
|
+
// FPDF_WIDESTRING is a NUL-terminated UTF-16LE string; std::u16string
|
|
145
|
+
// already stores exactly that and guarantees the terminator.
|
|
146
|
+
static FPDF_WIDESTRING ToWideString(const std::u16string &s) {
|
|
147
|
+
return reinterpret_cast<FPDF_WIDESTRING>(s.c_str());
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// best-effort ASCII rendering of a name, for error messages only
|
|
151
|
+
static std::string Utf8Of(const std::u16string &s) {
|
|
152
|
+
std::string out;
|
|
153
|
+
out.reserve(s.size());
|
|
154
|
+
for (char16_t c : s) {
|
|
155
|
+
out.push_back(c < 0x80 ? static_cast<char>(c) : '?');
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
Napi::Promise::Deferred deferred_;
|
|
161
|
+
std::vector<uint8_t> bufferData_;
|
|
162
|
+
std::string filePath_;
|
|
163
|
+
std::vector<AttachmentToAdd> attachments_;
|
|
164
|
+
std::string outputPath_;
|
|
165
|
+
std::string password_;
|
|
166
|
+
bool useFile_ = false;
|
|
167
|
+
std::vector<uint8_t> resultData_;
|
|
168
|
+
};
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "napi_helpers.h"
|
|
4
|
+
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <climits>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <string>
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
#include "fpdf_text.h"
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// GetCharactersWorker — async positioned text extraction
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// page.getText() flattens a page to a single string and throws the geometry
|
|
17
|
+
// away. This worker keeps it: every character comes back with its bounding box,
|
|
18
|
+
// baseline origin, font and rotation, which is what layout reconstruction,
|
|
19
|
+
// coordinate mapping and redaction targeting all need.
|
|
20
|
+
//
|
|
21
|
+
// A dense page can hold tens of thousands of characters, so the range options
|
|
22
|
+
// exist to let callers page through instead of materialising everything.
|
|
23
|
+
|
|
24
|
+
inline bool IsHighSurrogate(unsigned int u) {
|
|
25
|
+
return u >= 0xD800 && u <= 0xDBFF;
|
|
26
|
+
}
|
|
27
|
+
inline bool IsLowSurrogate(unsigned int u) {
|
|
28
|
+
return u >= 0xDC00 && u <= 0xDFFF;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Encode one text-page entry as the JS string for its `char` field.
|
|
32
|
+
//
|
|
33
|
+
// FPDFText_GetUnicode returns a single UTF-16 *code unit* per character index —
|
|
34
|
+
// on POSIX as well as Windows — so a non-BMP character occupies two consecutive
|
|
35
|
+
// indices holding its high and low surrogate. A lone surrogate cannot survive
|
|
36
|
+
// the trip into a JS string (V8 substitutes U+FFFD), so emitting each unit
|
|
37
|
+
// separately would corrupt every astral character.
|
|
38
|
+
//
|
|
39
|
+
// Instead the pair is joined onto the FIRST index and the second yields an
|
|
40
|
+
// empty string. That keeps both invariants callers rely on: indices still line
|
|
41
|
+
// up one-to-one with getText() and with search() results, and concatenating
|
|
42
|
+
// every `char` still reproduces getText() exactly.
|
|
43
|
+
inline std::u16string EncodeCharAt(const std::vector<unsigned int> &units,
|
|
44
|
+
size_t i) {
|
|
45
|
+
unsigned int u = units[i];
|
|
46
|
+
if (u == 0 || u > 0x10FFFF)
|
|
47
|
+
return {};
|
|
48
|
+
|
|
49
|
+
if (IsHighSurrogate(u)) {
|
|
50
|
+
if (i + 1 < units.size() && IsLowSurrogate(units[i + 1])) {
|
|
51
|
+
std::u16string out;
|
|
52
|
+
out.push_back(static_cast<char16_t>(u));
|
|
53
|
+
out.push_back(static_cast<char16_t>(units[i + 1]));
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
return {}; // unpaired high surrogate — not representable
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// the low half of a pair already emitted on the previous index
|
|
60
|
+
if (IsLowSurrogate(u))
|
|
61
|
+
return {};
|
|
62
|
+
|
|
63
|
+
if (u <= 0xFFFF)
|
|
64
|
+
return std::u16string(1, static_cast<char16_t>(u));
|
|
65
|
+
|
|
66
|
+
// a whole code point above the BMP, should PDFium ever report one directly
|
|
67
|
+
unsigned int v = u - 0x10000;
|
|
68
|
+
std::u16string out;
|
|
69
|
+
out.push_back(static_cast<char16_t>(0xD800 + (v >> 10)));
|
|
70
|
+
out.push_back(static_cast<char16_t>(0xDC00 + (v & 0x3FF)));
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
struct CharacterInfo {
|
|
75
|
+
int index = 0;
|
|
76
|
+
unsigned int unicode = 0;
|
|
77
|
+
double left = 0, bottom = 0, right = 0, top = 0;
|
|
78
|
+
bool hasBounds = false;
|
|
79
|
+
double originX = 0, originY = 0;
|
|
80
|
+
bool hasOrigin = false;
|
|
81
|
+
double fontSize = 0;
|
|
82
|
+
std::string fontName;
|
|
83
|
+
int fontFlags = 0;
|
|
84
|
+
int fontWeight = -1;
|
|
85
|
+
float angle = 0;
|
|
86
|
+
bool isGenerated = false;
|
|
87
|
+
bool isHyphen = false;
|
|
88
|
+
bool hasUnicodeMapError = false;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
class GetCharactersWorker : public SafeAsyncWorker {
|
|
92
|
+
public:
|
|
93
|
+
GetCharactersWorker(Napi::Env env, FPDF_PAGE page, int start, int count,
|
|
94
|
+
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
95
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
96
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
97
|
+
page_(page), start_(start), count_(count),
|
|
98
|
+
pageAlive_(std::move(pageAlive)), docAlive_(std::move(docAlive)) {}
|
|
99
|
+
|
|
100
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
101
|
+
|
|
102
|
+
protected:
|
|
103
|
+
void Execute() override {
|
|
104
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
105
|
+
CHECK_ALIVE();
|
|
106
|
+
|
|
107
|
+
FPDF_TEXTPAGE textPage = FPDFText_LoadPage(page_);
|
|
108
|
+
if (!textPage) {
|
|
109
|
+
SetError("Failed to load text page");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
int charCount = FPDFText_CountChars(textPage);
|
|
114
|
+
if (charCount <= 0) {
|
|
115
|
+
FPDFText_ClosePage(textPage);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (start_ >= charCount) {
|
|
120
|
+
FPDFText_ClosePage(textPage);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// count_ < 0 means "to the end of the page"
|
|
125
|
+
int end = charCount;
|
|
126
|
+
if (count_ >= 0) {
|
|
127
|
+
// start_ + count_ cannot overflow: both are non-negative and bounded by
|
|
128
|
+
// the caller-side 32-bit index guard
|
|
129
|
+
long long requestedEnd =
|
|
130
|
+
static_cast<long long>(start_) + static_cast<long long>(count_);
|
|
131
|
+
if (requestedEnd < end)
|
|
132
|
+
end = static_cast<int>(requestedEnd);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
characters_.reserve(static_cast<size_t>(end - start_));
|
|
136
|
+
for (int i = start_; i < end; i++) {
|
|
137
|
+
CharacterInfo info;
|
|
138
|
+
info.index = i;
|
|
139
|
+
info.unicode = FPDFText_GetUnicode(textPage, i);
|
|
140
|
+
|
|
141
|
+
double left, right, bottom, top;
|
|
142
|
+
if (FPDFText_GetCharBox(textPage, i, &left, &right, &bottom, &top)) {
|
|
143
|
+
info.hasBounds = true;
|
|
144
|
+
info.left = left;
|
|
145
|
+
info.right = right;
|
|
146
|
+
info.bottom = bottom;
|
|
147
|
+
info.top = top;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
double x, y;
|
|
151
|
+
if (FPDFText_GetCharOrigin(textPage, i, &x, &y)) {
|
|
152
|
+
info.hasOrigin = true;
|
|
153
|
+
info.originX = x;
|
|
154
|
+
info.originY = y;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
info.fontSize = FPDFText_GetFontSize(textPage, i);
|
|
158
|
+
info.fontWeight = FPDFText_GetFontWeight(textPage, i);
|
|
159
|
+
|
|
160
|
+
int flags = 0;
|
|
161
|
+
info.fontName = ReadUtf8(
|
|
162
|
+
[&](char *, unsigned long) {
|
|
163
|
+
return FPDFText_GetFontInfo(textPage, i, nullptr, 0, &flags);
|
|
164
|
+
},
|
|
165
|
+
[&](char *buf, unsigned long len) {
|
|
166
|
+
return FPDFText_GetFontInfo(textPage, i, buf, len, &flags);
|
|
167
|
+
});
|
|
168
|
+
info.fontFlags = flags;
|
|
169
|
+
|
|
170
|
+
// PDFium already normalizes the angle to [0, 2pi), so a negative value
|
|
171
|
+
// can only be its -1 error return; report 0 instead of a bogus angle
|
|
172
|
+
float angle = FPDFText_GetCharAngle(textPage, i);
|
|
173
|
+
info.angle = angle < 0 ? 0.0f : angle;
|
|
174
|
+
|
|
175
|
+
// each of these returns -1 on error, which is not "true"
|
|
176
|
+
info.isGenerated = FPDFText_IsGenerated(textPage, i) == 1;
|
|
177
|
+
info.isHyphen = FPDFText_IsHyphen(textPage, i) == 1;
|
|
178
|
+
info.hasUnicodeMapError = FPDFText_HasUnicodeMapError(textPage, i) == 1;
|
|
179
|
+
|
|
180
|
+
characters_.push_back(std::move(info));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
FPDFText_ClosePage(textPage);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
void OnOK() override {
|
|
187
|
+
Napi::Env env = Env();
|
|
188
|
+
|
|
189
|
+
// EncodeCharAt needs the next entry to pair a surrogate with its partner
|
|
190
|
+
std::vector<unsigned int> units;
|
|
191
|
+
units.reserve(characters_.size());
|
|
192
|
+
for (const auto &info : characters_)
|
|
193
|
+
units.push_back(info.unicode);
|
|
194
|
+
|
|
195
|
+
Napi::Array arr = Napi::Array::New(env, characters_.size());
|
|
196
|
+
for (uint32_t i = 0; i < characters_.size(); i++) {
|
|
197
|
+
const auto &info = characters_[i];
|
|
198
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
199
|
+
obj.Set("index", Napi::Number::New(env, info.index));
|
|
200
|
+
SetU16(obj, "char", env, EncodeCharAt(units, i));
|
|
201
|
+
obj.Set("unicode", Napi::Number::New(env, info.unicode));
|
|
202
|
+
if (info.hasBounds) {
|
|
203
|
+
obj.Set("bounds", CreateBoundsObject(env, info.left, info.bottom,
|
|
204
|
+
info.right, info.top));
|
|
205
|
+
}
|
|
206
|
+
if (info.hasOrigin) {
|
|
207
|
+
obj.Set("x", Napi::Number::New(env, info.originX));
|
|
208
|
+
obj.Set("y", Napi::Number::New(env, info.originY));
|
|
209
|
+
}
|
|
210
|
+
obj.Set("fontSize", Napi::Number::New(env, info.fontSize));
|
|
211
|
+
obj.Set("fontName", Napi::String::New(env, info.fontName));
|
|
212
|
+
obj.Set("fontFlags", Napi::Number::New(env, info.fontFlags));
|
|
213
|
+
// -1 is PDFium's error return and 0 means the font declares no weight;
|
|
214
|
+
// neither is a usable value, so omit the key rather than report a
|
|
215
|
+
// meaningless 0 for every non-embedded font.
|
|
216
|
+
if (info.fontWeight > 0)
|
|
217
|
+
obj.Set("fontWeight", Napi::Number::New(env, info.fontWeight));
|
|
218
|
+
obj.Set("angle", Napi::Number::New(env, info.angle));
|
|
219
|
+
obj.Set("isGenerated", Napi::Boolean::New(env, info.isGenerated));
|
|
220
|
+
obj.Set("isHyphen", Napi::Boolean::New(env, info.isHyphen));
|
|
221
|
+
obj.Set("hasUnicodeMapError",
|
|
222
|
+
Napi::Boolean::New(env, info.hasUnicodeMapError));
|
|
223
|
+
arr.Set(i, obj);
|
|
224
|
+
}
|
|
225
|
+
deferred_.Resolve(arr);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
void OnError(const Napi::Error &err) override {
|
|
229
|
+
deferred_.Reject(err.Value());
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private:
|
|
233
|
+
Napi::Promise::Deferred deferred_;
|
|
234
|
+
FPDF_PAGE page_;
|
|
235
|
+
int start_;
|
|
236
|
+
int count_;
|
|
237
|
+
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
238
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
239
|
+
std::vector<CharacterInfo> characters_;
|
|
240
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "napi_helpers.h"
|
|
4
|
+
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <memory>
|
|
7
|
+
#include <string>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// GetNamedDestinationsWorker — async named-destination listing
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Named destinations are the document's table of anchors: /Dests entries that
|
|
14
|
+
// GoTo actions and external links target by name rather than by page. Listing
|
|
15
|
+
// them turns "#Chapter2" in an incoming link into a page index, and gives a
|
|
16
|
+
// document's internal structure without walking every link on every page.
|
|
17
|
+
|
|
18
|
+
inline const char *DestinationViewString(unsigned long view) {
|
|
19
|
+
switch (view) {
|
|
20
|
+
case PDFDEST_VIEW_XYZ:
|
|
21
|
+
return "xyz";
|
|
22
|
+
case PDFDEST_VIEW_FIT:
|
|
23
|
+
return "fit";
|
|
24
|
+
case PDFDEST_VIEW_FITH:
|
|
25
|
+
return "fitH";
|
|
26
|
+
case PDFDEST_VIEW_FITV:
|
|
27
|
+
return "fitV";
|
|
28
|
+
case PDFDEST_VIEW_FITR:
|
|
29
|
+
return "fitR";
|
|
30
|
+
case PDFDEST_VIEW_FITB:
|
|
31
|
+
return "fitB";
|
|
32
|
+
case PDFDEST_VIEW_FITBH:
|
|
33
|
+
return "fitBH";
|
|
34
|
+
case PDFDEST_VIEW_FITBV:
|
|
35
|
+
return "fitBV";
|
|
36
|
+
default:
|
|
37
|
+
return "unknown";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
struct NamedDestinationInfo {
|
|
42
|
+
std::u16string name;
|
|
43
|
+
int pageIndex = -1;
|
|
44
|
+
const char *view = "unknown";
|
|
45
|
+
std::vector<float> viewParams;
|
|
46
|
+
bool hasX = false, hasY = false, hasZoom = false;
|
|
47
|
+
float x = 0, y = 0, zoom = 0;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
class GetNamedDestinationsWorker : public SafeAsyncWorker {
|
|
51
|
+
public:
|
|
52
|
+
GetNamedDestinationsWorker(Napi::Env env, FPDF_DOCUMENT doc,
|
|
53
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
54
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
55
|
+
doc_(doc), docAlive_(std::move(docAlive)) {}
|
|
56
|
+
|
|
57
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
58
|
+
|
|
59
|
+
protected:
|
|
60
|
+
void Execute() override {
|
|
61
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
62
|
+
if (!docAlive_ || !docAlive_->load()) {
|
|
63
|
+
SetError("Document was destroyed");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
unsigned long count = FPDF_CountNamedDests(doc_);
|
|
68
|
+
if (count == 0)
|
|
69
|
+
return;
|
|
70
|
+
|
|
71
|
+
destinations_.reserve(count);
|
|
72
|
+
for (unsigned long i = 0; i < count; i++) {
|
|
73
|
+
NamedDestinationInfo info;
|
|
74
|
+
|
|
75
|
+
// FPDF_GetNamedDest uses an in/out byte length rather than the usual
|
|
76
|
+
// two-pass protocol, and despite the header calling the buffer a
|
|
77
|
+
// wchar_t* it is written as UTF-16LE on every platform.
|
|
78
|
+
long nameLen = 0;
|
|
79
|
+
FPDF_DEST dest =
|
|
80
|
+
FPDF_GetNamedDest(doc_, static_cast<int>(i), nullptr, &nameLen);
|
|
81
|
+
if (!dest)
|
|
82
|
+
continue;
|
|
83
|
+
|
|
84
|
+
if (nameLen >= 2 && nameLen % 2 == 0) {
|
|
85
|
+
std::vector<unsigned short> buf(static_cast<size_t>(nameLen) /
|
|
86
|
+
sizeof(unsigned short));
|
|
87
|
+
long outLen = nameLen;
|
|
88
|
+
FPDF_GetNamedDest(doc_, static_cast<int>(i), buf.data(), &outLen);
|
|
89
|
+
if (outLen == nameLen) {
|
|
90
|
+
info.name = std::u16string(reinterpret_cast<const char16_t *>(
|
|
91
|
+
buf.data()),
|
|
92
|
+
buf.size() - 1);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
info.pageIndex = FPDFDest_GetDestPageIndex(doc_, dest);
|
|
97
|
+
|
|
98
|
+
unsigned long numParams = 0;
|
|
99
|
+
FS_FLOAT params[4] = {0, 0, 0, 0};
|
|
100
|
+
unsigned long view = FPDFDest_GetView(dest, &numParams, params);
|
|
101
|
+
info.view = DestinationViewString(view);
|
|
102
|
+
if (numParams > 4)
|
|
103
|
+
numParams = 4;
|
|
104
|
+
info.viewParams.assign(params, params + numParams);
|
|
105
|
+
|
|
106
|
+
FPDF_BOOL hasX = 0, hasY = 0, hasZoom = 0;
|
|
107
|
+
FS_FLOAT x = 0, y = 0, zoom = 0;
|
|
108
|
+
if (FPDFDest_GetLocationInPage(dest, &hasX, &hasY, &hasZoom, &x, &y,
|
|
109
|
+
&zoom)) {
|
|
110
|
+
info.hasX = hasX != 0;
|
|
111
|
+
info.hasY = hasY != 0;
|
|
112
|
+
info.hasZoom = hasZoom != 0;
|
|
113
|
+
info.x = x;
|
|
114
|
+
info.y = y;
|
|
115
|
+
info.zoom = zoom;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
destinations_.push_back(std::move(info));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
void OnOK() override {
|
|
123
|
+
Napi::Env env = Env();
|
|
124
|
+
Napi::Array arr = Napi::Array::New(env, destinations_.size());
|
|
125
|
+
for (uint32_t i = 0; i < destinations_.size(); i++) {
|
|
126
|
+
const auto &info = destinations_[i];
|
|
127
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
128
|
+
SetU16(obj, "name", env, info.name);
|
|
129
|
+
if (info.pageIndex >= 0)
|
|
130
|
+
obj.Set("pageIndex", Napi::Number::New(env, info.pageIndex));
|
|
131
|
+
obj.Set("view", Napi::String::New(env, info.view));
|
|
132
|
+
|
|
133
|
+
Napi::Array params = Napi::Array::New(env, info.viewParams.size());
|
|
134
|
+
for (uint32_t j = 0; j < info.viewParams.size(); j++) {
|
|
135
|
+
params.Set(j, Napi::Number::New(env, info.viewParams[j]));
|
|
136
|
+
}
|
|
137
|
+
obj.Set("viewParams", params);
|
|
138
|
+
|
|
139
|
+
if (info.hasX)
|
|
140
|
+
obj.Set("destX", Napi::Number::New(env, info.x));
|
|
141
|
+
if (info.hasY)
|
|
142
|
+
obj.Set("destY", Napi::Number::New(env, info.y));
|
|
143
|
+
if (info.hasZoom)
|
|
144
|
+
obj.Set("destZoom", Napi::Number::New(env, info.zoom));
|
|
145
|
+
|
|
146
|
+
arr.Set(i, obj);
|
|
147
|
+
}
|
|
148
|
+
deferred_.Resolve(arr);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
void OnError(const Napi::Error &err) override {
|
|
152
|
+
deferred_.Reject(err.Value());
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private:
|
|
156
|
+
Napi::Promise::Deferred deferred_;
|
|
157
|
+
FPDF_DOCUMENT doc_;
|
|
158
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
159
|
+
std::vector<NamedDestinationInfo> destinations_;
|
|
160
|
+
};
|
package/src/document.h
CHANGED
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
#include "attachments_worker.h"
|
|
4
4
|
#include "bookmarks_worker.h"
|
|
5
|
+
#include "destinations_worker.h"
|
|
6
|
+
#include "javascript_worker.h"
|
|
5
7
|
#include "page.h"
|
|
8
|
+
#include "signatures_worker.h"
|
|
6
9
|
|
|
7
10
|
#include <algorithm>
|
|
8
11
|
#include <atomic>
|
|
@@ -24,6 +27,13 @@ public:
|
|
|
24
27
|
InstanceMethod<&PDFiumDocument::GetBookmarks>("getBookmarks"),
|
|
25
28
|
InstanceMethod<&PDFiumDocument::GetAttachments>("getAttachments"),
|
|
26
29
|
InstanceMethod<&PDFiumDocument::GetAttachment>("getAttachment"),
|
|
30
|
+
InstanceMethod<&PDFiumDocument::GetSignatures>("getSignatures"),
|
|
31
|
+
InstanceMethod<&PDFiumDocument::GetSignatureContents>(
|
|
32
|
+
"getSignatureContents"),
|
|
33
|
+
InstanceMethod<&PDFiumDocument::GetJavaScriptActions>(
|
|
34
|
+
"getJavaScriptActions"),
|
|
35
|
+
InstanceMethod<&PDFiumDocument::GetNamedDestinations>(
|
|
36
|
+
"getNamedDestinations"),
|
|
27
37
|
InstanceMethod<&PDFiumDocument::Destroy>("destroy"),
|
|
28
38
|
});
|
|
29
39
|
}
|
|
@@ -130,6 +140,82 @@ private:
|
|
|
130
140
|
return promise;
|
|
131
141
|
}
|
|
132
142
|
|
|
143
|
+
/**
|
|
144
|
+
* Returns metadata for every digital signature in the document (async).
|
|
145
|
+
*/
|
|
146
|
+
Napi::Value GetSignatures(const Napi::CallbackInfo &info) {
|
|
147
|
+
Napi::Env env = info.Env();
|
|
148
|
+
EnsureOpen(env);
|
|
149
|
+
if (env.IsExceptionPending())
|
|
150
|
+
return env.Null();
|
|
151
|
+
|
|
152
|
+
auto *worker = new GetSignaturesWorker(env, doc_, docAlive_);
|
|
153
|
+
auto promise = worker->Promise();
|
|
154
|
+
worker->Queue();
|
|
155
|
+
return promise;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Reads the raw /Contents bytes (PKCS#1 / PKCS#7 DER) of the signature at
|
|
160
|
+
* `index` (async). Resolves to a Buffer, or writes to an optional output
|
|
161
|
+
* path and resolves to undefined.
|
|
162
|
+
*/
|
|
163
|
+
Napi::Value GetSignatureContents(const Napi::CallbackInfo &info) {
|
|
164
|
+
Napi::Env env = info.Env();
|
|
165
|
+
EnsureOpen(env);
|
|
166
|
+
if (env.IsExceptionPending())
|
|
167
|
+
return env.Null();
|
|
168
|
+
|
|
169
|
+
if (info.Length() < 1 || !info[0].IsNumber()) {
|
|
170
|
+
Napi::TypeError::New(env, "Expected signature index")
|
|
171
|
+
.ThrowAsJavaScriptException();
|
|
172
|
+
return env.Null();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
int index = info[0].As<Napi::Number>().Int32Value();
|
|
176
|
+
|
|
177
|
+
std::string outputPath;
|
|
178
|
+
if (info.Length() > 1 && info[1].IsString()) {
|
|
179
|
+
outputPath = info[1].As<Napi::String>().Utf8Value();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
auto *worker = new GetSignatureContentsWorker(
|
|
183
|
+
env, doc_, index, std::move(outputPath), docAlive_);
|
|
184
|
+
auto promise = worker->Promise();
|
|
185
|
+
worker->Queue();
|
|
186
|
+
return promise;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Returns every document-level JavaScript action (async).
|
|
191
|
+
*/
|
|
192
|
+
Napi::Value GetJavaScriptActions(const Napi::CallbackInfo &info) {
|
|
193
|
+
Napi::Env env = info.Env();
|
|
194
|
+
EnsureOpen(env);
|
|
195
|
+
if (env.IsExceptionPending())
|
|
196
|
+
return env.Null();
|
|
197
|
+
|
|
198
|
+
auto *worker = new GetJavaScriptActionsWorker(env, doc_, docAlive_);
|
|
199
|
+
auto promise = worker->Promise();
|
|
200
|
+
worker->Queue();
|
|
201
|
+
return promise;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Returns every named destination in the document (async).
|
|
206
|
+
*/
|
|
207
|
+
Napi::Value GetNamedDestinations(const Napi::CallbackInfo &info) {
|
|
208
|
+
Napi::Env env = info.Env();
|
|
209
|
+
EnsureOpen(env);
|
|
210
|
+
if (env.IsExceptionPending())
|
|
211
|
+
return env.Null();
|
|
212
|
+
|
|
213
|
+
auto *worker = new GetNamedDestinationsWorker(env, doc_, docAlive_);
|
|
214
|
+
auto promise = worker->Promise();
|
|
215
|
+
worker->Queue();
|
|
216
|
+
return promise;
|
|
217
|
+
}
|
|
218
|
+
|
|
133
219
|
/**
|
|
134
220
|
* Closes the document and releases all resources.
|
|
135
221
|
*/
|