pdfium-native 0.2.2 → 0.2.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/README.md +114 -27
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/page.d.ts +13 -3
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +22 -3
- package/dist/page.js.map +1 -1
- package/dist/types.d.ts +213 -7
- package/dist/types.d.ts.map +1 -1
- package/lib/index.ts +9 -2
- package/lib/page.ts +22 -4
- package/lib/types.ts +256 -7
- package/package.json +2 -2
- package/scripts/generate-fixtures.mjs +308 -1
- package/scripts/install.mjs +4 -3
- package/src/annotations_worker.h +274 -0
- package/src/bookmarks_worker.h +162 -0
- package/src/document.h +47 -4
- package/src/form_fields_worker.h +250 -0
- package/src/links_worker.h +158 -0
- package/src/napi_helpers.h +74 -0
- package/src/objects_worker.h +322 -0
- package/src/page.h +51 -5
- package/src/pdfium_addon.cc +93 -17
- package/src/render_worker.h +37 -17
- package/src/search_worker.h +121 -0
- package/src/text_worker.h +69 -0
- package/src/page_workers.h +0 -724
package/src/napi_helpers.h
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
#include <atomic>
|
|
4
4
|
#include <mutex>
|
|
5
5
|
#include <napi.h>
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <vector>
|
|
6
8
|
|
|
9
|
+
#include "fpdf_doc.h"
|
|
7
10
|
#include "fpdfview.h"
|
|
8
11
|
|
|
9
12
|
inline std::atomic<bool> g_initialized{false};
|
|
@@ -21,6 +24,77 @@ constexpr int MAX_RENDER_DIMENSION = 16384;
|
|
|
21
24
|
// maximum bookmark tree recursion depth
|
|
22
25
|
constexpr int MAX_BOOKMARK_DEPTH = 64;
|
|
23
26
|
|
|
27
|
+
// common alive-check macro for all page workers
|
|
28
|
+
#define CHECK_ALIVE() \
|
|
29
|
+
if (!pageAlive_->load() || (docAlive_ && !docAlive_->load())) { \
|
|
30
|
+
SetError("Page or document was closed"); \
|
|
31
|
+
return; \
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// PDFium UTF-16 string helpers
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
// read a PDFium UTF-16LE buffer into a std::u16string.
|
|
39
|
+
// `getLen` returns the byte length (including null terminator).
|
|
40
|
+
// `getData` writes into the provided buffer and returns the byte length.
|
|
41
|
+
template <typename GetLen, typename GetData>
|
|
42
|
+
inline std::u16string ReadU16(GetLen getLen, GetData getData) {
|
|
43
|
+
unsigned long len = getLen(static_cast<FPDF_WCHAR *>(nullptr), 0);
|
|
44
|
+
if (len < 4 || len % 2 != 0)
|
|
45
|
+
return {};
|
|
46
|
+
std::vector<unsigned short> buf(len / sizeof(unsigned short));
|
|
47
|
+
getData(reinterpret_cast<FPDF_WCHAR *>(buf.data()), len);
|
|
48
|
+
return std::u16string(reinterpret_cast<const char16_t *>(buf.data()),
|
|
49
|
+
len / sizeof(unsigned short) - 1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// convert a std::u16string to a Napi::String (empty string if input is empty)
|
|
53
|
+
inline Napi::String ToNapiString(Napi::Env env, const std::u16string &s) {
|
|
54
|
+
if (s.empty())
|
|
55
|
+
return Napi::String::New(env, "");
|
|
56
|
+
return Napi::String::New(env, reinterpret_cast<const char16_t *>(s.data()),
|
|
57
|
+
s.size());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// set a Napi object property from a u16string (always sets, uses "" if empty)
|
|
61
|
+
inline void SetU16(Napi::Object &obj, const char *key, Napi::Env env,
|
|
62
|
+
const std::u16string &s) {
|
|
63
|
+
obj.Set(key, ToNapiString(env, s));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// set a Napi object property from a u16string only if non-empty
|
|
67
|
+
inline void SetU16IfPresent(Napi::Object &obj, const char *key, Napi::Env env,
|
|
68
|
+
const std::u16string &s) {
|
|
69
|
+
if (!s.empty())
|
|
70
|
+
obj.Set(key, ToNapiString(env, s));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
// action type string from FPDF action type constant
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
inline const char *ActionTypeString(unsigned long actionType) {
|
|
78
|
+
switch (actionType) {
|
|
79
|
+
case PDFACTION_GOTO:
|
|
80
|
+
return "goto";
|
|
81
|
+
case PDFACTION_REMOTEGOTO:
|
|
82
|
+
return "remoteGoto";
|
|
83
|
+
case PDFACTION_URI:
|
|
84
|
+
return "uri";
|
|
85
|
+
case PDFACTION_LAUNCH:
|
|
86
|
+
return "launch";
|
|
87
|
+
case PDFACTION_EMBEDDEDGOTO:
|
|
88
|
+
return "embeddedGoto";
|
|
89
|
+
default:
|
|
90
|
+
return "unknown";
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
// Napi object helpers
|
|
96
|
+
// ---------------------------------------------------------------------------
|
|
97
|
+
|
|
24
98
|
// helper: create a { left, bottom, right, top } bounds object
|
|
25
99
|
inline Napi::Object CreateBoundsObject(Napi::Env env, double left,
|
|
26
100
|
double bottom, double right,
|
|
@@ -0,0 +1,322 @@
|
|
|
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_edit.h"
|
|
11
|
+
#include "fpdf_text.h"
|
|
12
|
+
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// GetObjectWorker — async page object extraction
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
struct PageObjectData {
|
|
18
|
+
std::string type;
|
|
19
|
+
float left = 0, bottom = 0, right = 0, top = 0;
|
|
20
|
+
unsigned int fillR = 0, fillG = 0, fillB = 0, fillA = 0;
|
|
21
|
+
bool hasFillColor = false;
|
|
22
|
+
unsigned int strokeR = 0, strokeG = 0, strokeB = 0, strokeA = 0;
|
|
23
|
+
bool hasStrokeColor = false;
|
|
24
|
+
// text-specific
|
|
25
|
+
std::u16string text;
|
|
26
|
+
float fontSize = 0;
|
|
27
|
+
std::string fontName;
|
|
28
|
+
int fontWeight = -1;
|
|
29
|
+
int italicAngle = 0;
|
|
30
|
+
bool hasItalicAngle = false;
|
|
31
|
+
std::string renderMode;
|
|
32
|
+
std::string fontFamily;
|
|
33
|
+
int isEmbedded = -1; // -1 = unknown, 0 = no, 1 = yes
|
|
34
|
+
int fontFlags = -1;
|
|
35
|
+
// image-specific
|
|
36
|
+
unsigned int imageWidth = 0, imageHeight = 0;
|
|
37
|
+
float horizontalDpi = 0, verticalDpi = 0;
|
|
38
|
+
unsigned int bitsPerPixel = 0;
|
|
39
|
+
std::string colorspace;
|
|
40
|
+
std::vector<std::string> filters;
|
|
41
|
+
bool hasImageMetadata = false;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
class GetObjectWorker : public Napi::AsyncWorker {
|
|
45
|
+
public:
|
|
46
|
+
GetObjectWorker(Napi::Env env, FPDF_PAGE page, int index,
|
|
47
|
+
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
48
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
49
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
50
|
+
page_(page), index_(index), pageAlive_(std::move(pageAlive)),
|
|
51
|
+
docAlive_(std::move(docAlive)) {}
|
|
52
|
+
|
|
53
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
54
|
+
|
|
55
|
+
protected:
|
|
56
|
+
void Execute() override {
|
|
57
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
58
|
+
CHECK_ALIVE();
|
|
59
|
+
|
|
60
|
+
FPDF_PAGEOBJECT obj = FPDFPage_GetObject(page_, index_);
|
|
61
|
+
if (!obj) {
|
|
62
|
+
SetError("Object not found");
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
int type = FPDFPageObj_GetType(obj);
|
|
67
|
+
switch (type) {
|
|
68
|
+
case FPDF_PAGEOBJ_TEXT:
|
|
69
|
+
data_.type = "text";
|
|
70
|
+
break;
|
|
71
|
+
case FPDF_PAGEOBJ_PATH:
|
|
72
|
+
data_.type = "path";
|
|
73
|
+
break;
|
|
74
|
+
case FPDF_PAGEOBJ_IMAGE:
|
|
75
|
+
data_.type = "image";
|
|
76
|
+
break;
|
|
77
|
+
case FPDF_PAGEOBJ_SHADING:
|
|
78
|
+
data_.type = "shading";
|
|
79
|
+
break;
|
|
80
|
+
case FPDF_PAGEOBJ_FORM:
|
|
81
|
+
data_.type = "form";
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
data_.type = "unknown";
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
FPDFPageObj_GetBounds(obj, &data_.left, &data_.bottom, &data_.right,
|
|
89
|
+
&data_.top);
|
|
90
|
+
|
|
91
|
+
unsigned int r, g, b, a;
|
|
92
|
+
if (FPDFPageObj_GetFillColor(obj, &r, &g, &b, &a)) {
|
|
93
|
+
data_.hasFillColor = true;
|
|
94
|
+
data_.fillR = r;
|
|
95
|
+
data_.fillG = g;
|
|
96
|
+
data_.fillB = b;
|
|
97
|
+
data_.fillA = a;
|
|
98
|
+
}
|
|
99
|
+
if (FPDFPageObj_GetStrokeColor(obj, &r, &g, &b, &a)) {
|
|
100
|
+
data_.hasStrokeColor = true;
|
|
101
|
+
data_.strokeR = r;
|
|
102
|
+
data_.strokeG = g;
|
|
103
|
+
data_.strokeB = b;
|
|
104
|
+
data_.strokeA = a;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (type == FPDF_PAGEOBJ_TEXT) {
|
|
108
|
+
FPDF_TEXTPAGE textPage = FPDFText_LoadPage(page_);
|
|
109
|
+
if (textPage) {
|
|
110
|
+
data_.text = ReadU16(
|
|
111
|
+
[&](auto *, unsigned long) {
|
|
112
|
+
return FPDFTextObj_GetText(obj, textPage, nullptr, 0);
|
|
113
|
+
},
|
|
114
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
115
|
+
return FPDFTextObj_GetText(obj, textPage, buf, len);
|
|
116
|
+
});
|
|
117
|
+
FPDFText_ClosePage(textPage);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
float fs = 0;
|
|
121
|
+
if (FPDFTextObj_GetFontSize(obj, &fs))
|
|
122
|
+
data_.fontSize = fs;
|
|
123
|
+
|
|
124
|
+
FPDF_FONT font = FPDFTextObj_GetFont(obj);
|
|
125
|
+
if (font) {
|
|
126
|
+
size_t nameLen = FPDFFont_GetBaseFontName(font, nullptr, 0);
|
|
127
|
+
if (nameLen > 1) {
|
|
128
|
+
std::vector<char> nameBuf(nameLen);
|
|
129
|
+
FPDFFont_GetBaseFontName(font, nameBuf.data(), nameLen);
|
|
130
|
+
data_.fontName = std::string(nameBuf.data(), nameLen - 1);
|
|
131
|
+
}
|
|
132
|
+
data_.fontWeight = FPDFFont_GetWeight(font);
|
|
133
|
+
int angle = 0;
|
|
134
|
+
if (FPDFFont_GetItalicAngle(font, &angle)) {
|
|
135
|
+
data_.hasItalicAngle = true;
|
|
136
|
+
data_.italicAngle = angle;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// font family name
|
|
140
|
+
size_t famLen = FPDFFont_GetFamilyName(font, nullptr, 0);
|
|
141
|
+
if (famLen > 1) {
|
|
142
|
+
std::vector<char> famBuf(famLen);
|
|
143
|
+
FPDFFont_GetFamilyName(font, famBuf.data(), famLen);
|
|
144
|
+
data_.fontFamily = std::string(famBuf.data(), famLen - 1);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
data_.isEmbedded = FPDFFont_GetIsEmbedded(font);
|
|
148
|
+
data_.fontFlags = FPDFFont_GetFlags(font);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// text render mode
|
|
152
|
+
FPDF_TEXT_RENDERMODE rm = FPDFTextObj_GetTextRenderMode(obj);
|
|
153
|
+
switch (rm) {
|
|
154
|
+
case FPDF_TEXTRENDERMODE_FILL:
|
|
155
|
+
data_.renderMode = "fill";
|
|
156
|
+
break;
|
|
157
|
+
case FPDF_TEXTRENDERMODE_STROKE:
|
|
158
|
+
data_.renderMode = "stroke";
|
|
159
|
+
break;
|
|
160
|
+
case FPDF_TEXTRENDERMODE_FILL_STROKE:
|
|
161
|
+
data_.renderMode = "fillStroke";
|
|
162
|
+
break;
|
|
163
|
+
case FPDF_TEXTRENDERMODE_INVISIBLE:
|
|
164
|
+
data_.renderMode = "invisible";
|
|
165
|
+
break;
|
|
166
|
+
case FPDF_TEXTRENDERMODE_FILL_CLIP:
|
|
167
|
+
data_.renderMode = "fillClip";
|
|
168
|
+
break;
|
|
169
|
+
case FPDF_TEXTRENDERMODE_STROKE_CLIP:
|
|
170
|
+
data_.renderMode = "strokeClip";
|
|
171
|
+
break;
|
|
172
|
+
case FPDF_TEXTRENDERMODE_FILL_STROKE_CLIP:
|
|
173
|
+
data_.renderMode = "fillStrokeClip";
|
|
174
|
+
break;
|
|
175
|
+
case FPDF_TEXTRENDERMODE_CLIP:
|
|
176
|
+
data_.renderMode = "clip";
|
|
177
|
+
break;
|
|
178
|
+
default:
|
|
179
|
+
data_.renderMode = "unknown";
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (type == FPDF_PAGEOBJ_IMAGE) {
|
|
185
|
+
FPDFImageObj_GetImagePixelSize(obj, &data_.imageWidth,
|
|
186
|
+
&data_.imageHeight);
|
|
187
|
+
|
|
188
|
+
// rich image metadata
|
|
189
|
+
FPDF_IMAGEOBJ_METADATA meta;
|
|
190
|
+
if (FPDFImageObj_GetImageMetadata(obj, page_, &meta)) {
|
|
191
|
+
data_.hasImageMetadata = true;
|
|
192
|
+
data_.horizontalDpi = meta.horizontal_dpi;
|
|
193
|
+
data_.verticalDpi = meta.vertical_dpi;
|
|
194
|
+
data_.bitsPerPixel = meta.bits_per_pixel;
|
|
195
|
+
switch (meta.colorspace) {
|
|
196
|
+
case FPDF_COLORSPACE_DEVICEGRAY:
|
|
197
|
+
data_.colorspace = "deviceGray";
|
|
198
|
+
break;
|
|
199
|
+
case FPDF_COLORSPACE_DEVICERGB:
|
|
200
|
+
data_.colorspace = "deviceRGB";
|
|
201
|
+
break;
|
|
202
|
+
case FPDF_COLORSPACE_DEVICECMYK:
|
|
203
|
+
data_.colorspace = "deviceCMYK";
|
|
204
|
+
break;
|
|
205
|
+
case FPDF_COLORSPACE_CALGRAY:
|
|
206
|
+
data_.colorspace = "calGray";
|
|
207
|
+
break;
|
|
208
|
+
case FPDF_COLORSPACE_CALRGB:
|
|
209
|
+
data_.colorspace = "calRGB";
|
|
210
|
+
break;
|
|
211
|
+
case FPDF_COLORSPACE_LAB:
|
|
212
|
+
data_.colorspace = "lab";
|
|
213
|
+
break;
|
|
214
|
+
case FPDF_COLORSPACE_ICCBASED:
|
|
215
|
+
data_.colorspace = "iccBased";
|
|
216
|
+
break;
|
|
217
|
+
case FPDF_COLORSPACE_SEPARATION:
|
|
218
|
+
data_.colorspace = "separation";
|
|
219
|
+
break;
|
|
220
|
+
case FPDF_COLORSPACE_DEVICEN:
|
|
221
|
+
data_.colorspace = "deviceN";
|
|
222
|
+
break;
|
|
223
|
+
case FPDF_COLORSPACE_INDEXED:
|
|
224
|
+
data_.colorspace = "indexed";
|
|
225
|
+
break;
|
|
226
|
+
case FPDF_COLORSPACE_PATTERN:
|
|
227
|
+
data_.colorspace = "pattern";
|
|
228
|
+
break;
|
|
229
|
+
default:
|
|
230
|
+
data_.colorspace = "unknown";
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// compression filters
|
|
236
|
+
int filterCount = FPDFImageObj_GetImageFilterCount(obj);
|
|
237
|
+
for (int fi = 0; fi < filterCount; fi++) {
|
|
238
|
+
unsigned long fLen = FPDFImageObj_GetImageFilter(obj, fi, nullptr, 0);
|
|
239
|
+
if (fLen > 1) {
|
|
240
|
+
std::vector<char> fBuf(fLen);
|
|
241
|
+
FPDFImageObj_GetImageFilter(obj, fi, fBuf.data(), fLen);
|
|
242
|
+
data_.filters.emplace_back(fBuf.data(), fLen - 1);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
void OnOK() override {
|
|
249
|
+
Napi::Env env = Env();
|
|
250
|
+
Napi::Object result = Napi::Object::New(env);
|
|
251
|
+
result.Set("type", Napi::String::New(env, data_.type));
|
|
252
|
+
result.Set("bounds", CreateBoundsObject(env, data_.left, data_.bottom,
|
|
253
|
+
data_.right, data_.top));
|
|
254
|
+
|
|
255
|
+
if (data_.hasFillColor) {
|
|
256
|
+
result.Set("fillColor", CreateColorObject(env, data_.fillR, data_.fillG,
|
|
257
|
+
data_.fillB, data_.fillA));
|
|
258
|
+
} else {
|
|
259
|
+
result.Set("fillColor", env.Null());
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (data_.hasStrokeColor) {
|
|
263
|
+
result.Set("strokeColor",
|
|
264
|
+
CreateColorObject(env, data_.strokeR, data_.strokeG,
|
|
265
|
+
data_.strokeB, data_.strokeA));
|
|
266
|
+
} else {
|
|
267
|
+
result.Set("strokeColor", env.Null());
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (data_.type == "text") {
|
|
271
|
+
SetU16(result, "text", env, data_.text);
|
|
272
|
+
result.Set("fontSize", Napi::Number::New(env, data_.fontSize));
|
|
273
|
+
result.Set("fontName", Napi::String::New(env, data_.fontName));
|
|
274
|
+
if (data_.fontWeight >= 0)
|
|
275
|
+
result.Set("fontWeight", Napi::Number::New(env, data_.fontWeight));
|
|
276
|
+
if (data_.hasItalicAngle)
|
|
277
|
+
result.Set("italicAngle", Napi::Number::New(env, data_.italicAngle));
|
|
278
|
+
if (!data_.renderMode.empty())
|
|
279
|
+
result.Set("renderMode", Napi::String::New(env, data_.renderMode));
|
|
280
|
+
if (!data_.fontFamily.empty())
|
|
281
|
+
result.Set("fontFamily", Napi::String::New(env, data_.fontFamily));
|
|
282
|
+
if (data_.isEmbedded >= 0)
|
|
283
|
+
result.Set("isEmbedded",
|
|
284
|
+
Napi::Boolean::New(env, data_.isEmbedded != 0));
|
|
285
|
+
if (data_.fontFlags >= 0)
|
|
286
|
+
result.Set("fontFlags", Napi::Number::New(env, data_.fontFlags));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (data_.type == "image") {
|
|
290
|
+
result.Set("imageWidth", Napi::Number::New(env, data_.imageWidth));
|
|
291
|
+
result.Set("imageHeight", Napi::Number::New(env, data_.imageHeight));
|
|
292
|
+
if (data_.hasImageMetadata) {
|
|
293
|
+
result.Set("horizontalDpi",
|
|
294
|
+
Napi::Number::New(env, data_.horizontalDpi));
|
|
295
|
+
result.Set("verticalDpi", Napi::Number::New(env, data_.verticalDpi));
|
|
296
|
+
result.Set("bitsPerPixel", Napi::Number::New(env, data_.bitsPerPixel));
|
|
297
|
+
if (!data_.colorspace.empty())
|
|
298
|
+
result.Set("colorspace", Napi::String::New(env, data_.colorspace));
|
|
299
|
+
}
|
|
300
|
+
if (!data_.filters.empty()) {
|
|
301
|
+
Napi::Array fArr = Napi::Array::New(env, data_.filters.size());
|
|
302
|
+
for (uint32_t fi = 0; fi < data_.filters.size(); fi++)
|
|
303
|
+
fArr.Set(fi, Napi::String::New(env, data_.filters[fi]));
|
|
304
|
+
result.Set("filters", fArr);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
deferred_.Resolve(result);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
void OnError(const Napi::Error &err) override {
|
|
312
|
+
deferred_.Reject(err.Value());
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
private:
|
|
316
|
+
Napi::Promise::Deferred deferred_;
|
|
317
|
+
FPDF_PAGE page_;
|
|
318
|
+
int index_;
|
|
319
|
+
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
320
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
321
|
+
PageObjectData data_;
|
|
322
|
+
};
|
package/src/page.h
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
|
-
#include "
|
|
3
|
+
#include "annotations_worker.h"
|
|
4
|
+
#include "form_fields_worker.h"
|
|
5
|
+
#include "links_worker.h"
|
|
6
|
+
#include "objects_worker.h"
|
|
4
7
|
#include "render_worker.h"
|
|
8
|
+
#include "search_worker.h"
|
|
9
|
+
#include "text_worker.h"
|
|
5
10
|
|
|
6
|
-
#include <algorithm>
|
|
7
11
|
#include <atomic>
|
|
8
12
|
#include <memory>
|
|
9
13
|
#include <string>
|
|
@@ -29,6 +33,7 @@ public:
|
|
|
29
33
|
InstanceMethod<&PDFiumPage::GetLinks>("getLinks"),
|
|
30
34
|
InstanceMethod<&PDFiumPage::Search>("search"),
|
|
31
35
|
InstanceMethod<&PDFiumPage::GetAnnotations>("getAnnotations"),
|
|
36
|
+
InstanceMethod<&PDFiumPage::GetFormFields>("getFormFields"),
|
|
32
37
|
InstanceMethod<&PDFiumPage::Close>("close"),
|
|
33
38
|
});
|
|
34
39
|
}
|
|
@@ -116,6 +121,9 @@ private:
|
|
|
116
121
|
int renderHeight = 0;
|
|
117
122
|
int format = IMAGE_FORMAT_JPEG;
|
|
118
123
|
int quality = 100;
|
|
124
|
+
int rotation = 0;
|
|
125
|
+
bool transparent = false;
|
|
126
|
+
int renderFlags = FPDF_ANNOT | FPDF_PRINTING;
|
|
119
127
|
|
|
120
128
|
std::string outputPath;
|
|
121
129
|
|
|
@@ -142,6 +150,25 @@ private:
|
|
|
142
150
|
quality = opts.Get("quality").As<Napi::Number>().Int32Value();
|
|
143
151
|
if (opts.Has("output"))
|
|
144
152
|
outputPath = opts.Get("output").As<Napi::String>().Utf8Value();
|
|
153
|
+
if (opts.Has("rotation")) {
|
|
154
|
+
rotation = opts.Get("rotation").As<Napi::Number>().Int32Value();
|
|
155
|
+
if (rotation != 0 && rotation != 1 && rotation != 2 && rotation != 3) {
|
|
156
|
+
Napi::RangeError::New(env, "rotation must be 0, 1, 2, or 3")
|
|
157
|
+
.ThrowAsJavaScriptException();
|
|
158
|
+
return env.Null();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (opts.Has("transparent"))
|
|
162
|
+
transparent = opts.Get("transparent").As<Napi::Boolean>().Value();
|
|
163
|
+
if (opts.Has("renderAnnotations") &&
|
|
164
|
+
!opts.Get("renderAnnotations").As<Napi::Boolean>().Value())
|
|
165
|
+
renderFlags &= ~FPDF_ANNOT;
|
|
166
|
+
if (opts.Has("grayscale") &&
|
|
167
|
+
opts.Get("grayscale").As<Napi::Boolean>().Value())
|
|
168
|
+
renderFlags |= FPDF_GRAYSCALE;
|
|
169
|
+
if (opts.Has("lcdText") &&
|
|
170
|
+
opts.Get("lcdText").As<Napi::Boolean>().Value())
|
|
171
|
+
renderFlags |= FPDF_LCD_TEXT;
|
|
145
172
|
}
|
|
146
173
|
|
|
147
174
|
if (renderWidth == 0)
|
|
@@ -166,9 +193,9 @@ private:
|
|
|
166
193
|
// clamp quality to valid range
|
|
167
194
|
quality = std::clamp(quality, 1, 100);
|
|
168
195
|
|
|
169
|
-
auto *worker =
|
|
170
|
-
|
|
171
|
-
|
|
196
|
+
auto *worker = new RenderWorker(env, page_, renderWidth, renderHeight,
|
|
197
|
+
format, quality, std::move(outputPath),
|
|
198
|
+
rotation, transparent, renderFlags, alive_);
|
|
172
199
|
auto promise = worker->Promise();
|
|
173
200
|
worker->Queue();
|
|
174
201
|
return promise;
|
|
@@ -236,6 +263,9 @@ private:
|
|
|
236
263
|
if (opts.Has("wholeWord") &&
|
|
237
264
|
opts.Get("wholeWord").As<Napi::Boolean>().Value())
|
|
238
265
|
flags |= FPDF_MATCHWHOLEWORD;
|
|
266
|
+
if (opts.Has("consecutive") &&
|
|
267
|
+
opts.Get("consecutive").As<Napi::Boolean>().Value())
|
|
268
|
+
flags |= FPDF_CONSECUTIVE;
|
|
239
269
|
}
|
|
240
270
|
|
|
241
271
|
auto *worker = new SearchWorker(env, page_, std::move(needle), flags,
|
|
@@ -261,6 +291,22 @@ private:
|
|
|
261
291
|
return promise;
|
|
262
292
|
}
|
|
263
293
|
|
|
294
|
+
/**
|
|
295
|
+
* Returns all form fields on the page (async). Returns a
|
|
296
|
+
* Promise<FormField[]>.
|
|
297
|
+
*/
|
|
298
|
+
Napi::Value GetFormFields(const Napi::CallbackInfo &info) {
|
|
299
|
+
Napi::Env env = info.Env();
|
|
300
|
+
EnsureOpen(env);
|
|
301
|
+
if (env.IsExceptionPending())
|
|
302
|
+
return env.Null();
|
|
303
|
+
|
|
304
|
+
auto *worker = new GetFormFieldsWorker(env, page_, doc_, alive_, docAlive_);
|
|
305
|
+
auto promise = worker->Promise();
|
|
306
|
+
worker->Queue();
|
|
307
|
+
return promise;
|
|
308
|
+
}
|
|
309
|
+
|
|
264
310
|
/**
|
|
265
311
|
* Closes the page and releases its resources.
|
|
266
312
|
*/
|
package/src/pdfium_addon.cc
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
#include "document.h"
|
|
2
2
|
|
|
3
|
+
#include <climits>
|
|
4
|
+
|
|
5
|
+
#include "fpdf_attachment.h"
|
|
6
|
+
#include "fpdf_catalog.h"
|
|
7
|
+
#include "fpdf_signature.h"
|
|
8
|
+
|
|
3
9
|
Napi::FunctionReference PDFiumDocument::pageConstructor;
|
|
4
10
|
|
|
5
11
|
// ---------------------------------------------------------------------------
|
|
@@ -71,6 +77,10 @@ protected:
|
|
|
71
77
|
if (useFile_) {
|
|
72
78
|
doc_ = FPDF_LoadDocument(filePath_.c_str(), pw);
|
|
73
79
|
} else {
|
|
80
|
+
if (bufferData_.size() > static_cast<size_t>(INT_MAX)) {
|
|
81
|
+
SetError("FORMAT:Buffer too large");
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
74
84
|
doc_ = FPDF_LoadMemDocument(bufferData_.data(),
|
|
75
85
|
static_cast<int>(bufferData_.size()), pw);
|
|
76
86
|
}
|
|
@@ -86,16 +96,53 @@ protected:
|
|
|
86
96
|
const char *metaTags[] = {"Title", "Author", "Subject", "Keywords",
|
|
87
97
|
"Creator", "Producer", "CreationDate", "ModDate"};
|
|
88
98
|
for (int i = 0; i < 8; i++) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
99
|
+
meta_[i] = ReadU16(
|
|
100
|
+
[&](auto *, unsigned long) {
|
|
101
|
+
return FPDF_GetMetaText(doc_, metaTags[i], nullptr, 0);
|
|
102
|
+
},
|
|
103
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
104
|
+
return FPDF_GetMetaText(doc_, metaTags[i], buf, len);
|
|
105
|
+
});
|
|
97
106
|
}
|
|
98
107
|
FPDF_GetFileVersion(doc_, &pdfVersion_);
|
|
108
|
+
permissions_ = FPDF_GetDocPermissions(doc_);
|
|
109
|
+
|
|
110
|
+
// tagged PDF flag
|
|
111
|
+
isTagged_ = FPDFCatalog_IsTagged(doc_) != 0;
|
|
112
|
+
|
|
113
|
+
// document language (UTF-16LE encoded)
|
|
114
|
+
language_ = ReadU16(
|
|
115
|
+
[&](auto *, unsigned long) {
|
|
116
|
+
return FPDFCatalog_GetLanguage(doc_, nullptr, 0);
|
|
117
|
+
},
|
|
118
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
119
|
+
return FPDFCatalog_GetLanguage(doc_, buf, len);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// signature and attachment counts
|
|
123
|
+
signatureCount_ = FPDF_GetSignatureCount(doc_);
|
|
124
|
+
attachmentCount_ = FPDFDoc_GetAttachmentCount(doc_);
|
|
125
|
+
|
|
126
|
+
// file identifiers (raw byte strings → hex)
|
|
127
|
+
auto readFileId = [&](FPDF_FILEIDTYPE idType) -> std::string {
|
|
128
|
+
unsigned long len = FPDF_GetFileIdentifier(doc_, idType, nullptr, 0);
|
|
129
|
+
if (len <= 1)
|
|
130
|
+
return "";
|
|
131
|
+
std::vector<char> buf(len);
|
|
132
|
+
FPDF_GetFileIdentifier(doc_, idType, buf.data(), len);
|
|
133
|
+
// convert raw bytes to hex string
|
|
134
|
+
static const char hex[] = "0123456789abcdef";
|
|
135
|
+
std::string result;
|
|
136
|
+
result.reserve((len - 1) * 2);
|
|
137
|
+
for (unsigned long j = 0; j < len - 1; j++) {
|
|
138
|
+
unsigned char c = static_cast<unsigned char>(buf[j]);
|
|
139
|
+
result.push_back(hex[c >> 4]);
|
|
140
|
+
result.push_back(hex[c & 0x0F]);
|
|
141
|
+
}
|
|
142
|
+
return result;
|
|
143
|
+
};
|
|
144
|
+
permanentId_ = readFileId(FILEIDTYPE_PERMANENT);
|
|
145
|
+
changingId_ = readFileId(FILEIDTYPE_CHANGING);
|
|
99
146
|
}
|
|
100
147
|
|
|
101
148
|
void OnOK() override {
|
|
@@ -111,17 +158,39 @@ protected:
|
|
|
111
158
|
"creator", "producer", "creationDate", "modDate"};
|
|
112
159
|
Napi::Object metaObj = Napi::Object::New(env);
|
|
113
160
|
for (int i = 0; i < 8; i++) {
|
|
114
|
-
|
|
115
|
-
metaObj.Set(metaKeys[i], Napi::String::New(env, ""));
|
|
116
|
-
} else {
|
|
117
|
-
metaObj.Set(metaKeys[i],
|
|
118
|
-
Napi::String::New(
|
|
119
|
-
env,
|
|
120
|
-
reinterpret_cast<const char16_t *>(meta_[i].data()),
|
|
121
|
-
meta_[i].size()));
|
|
122
|
-
}
|
|
161
|
+
SetU16(metaObj, metaKeys[i], env, meta_[i]);
|
|
123
162
|
}
|
|
124
163
|
metaObj.Set("pdfVersion", Napi::Number::New(env, pdfVersion_));
|
|
164
|
+
|
|
165
|
+
// permission flags (ISO 32000-1:2008, Table 22)
|
|
166
|
+
Napi::Object permObj = Napi::Object::New(env);
|
|
167
|
+
permObj.Set("print",
|
|
168
|
+
Napi::Boolean::New(env, (permissions_ & (1 << 2)) != 0));
|
|
169
|
+
permObj.Set("modify",
|
|
170
|
+
Napi::Boolean::New(env, (permissions_ & (1 << 3)) != 0));
|
|
171
|
+
permObj.Set("copy",
|
|
172
|
+
Napi::Boolean::New(env, (permissions_ & (1 << 4)) != 0));
|
|
173
|
+
permObj.Set("annotate",
|
|
174
|
+
Napi::Boolean::New(env, (permissions_ & (1 << 5)) != 0));
|
|
175
|
+
permObj.Set("fillForms",
|
|
176
|
+
Napi::Boolean::New(env, (permissions_ & (1 << 8)) != 0));
|
|
177
|
+
permObj.Set("extractForAccessibility",
|
|
178
|
+
Napi::Boolean::New(env, (permissions_ & (1 << 9)) != 0));
|
|
179
|
+
permObj.Set("assemble",
|
|
180
|
+
Napi::Boolean::New(env, (permissions_ & (1 << 10)) != 0));
|
|
181
|
+
permObj.Set("printHighQuality",
|
|
182
|
+
Napi::Boolean::New(env, (permissions_ & (1 << 11)) != 0));
|
|
183
|
+
metaObj.Set("permissions", permObj);
|
|
184
|
+
|
|
185
|
+
metaObj.Set("isTagged", Napi::Boolean::New(env, isTagged_));
|
|
186
|
+
SetU16(metaObj, "language", env, language_);
|
|
187
|
+
metaObj.Set("signatureCount", Napi::Number::New(env, signatureCount_));
|
|
188
|
+
metaObj.Set("attachmentCount", Napi::Number::New(env, attachmentCount_));
|
|
189
|
+
if (!permanentId_.empty())
|
|
190
|
+
metaObj.Set("permanentId", Napi::String::New(env, permanentId_));
|
|
191
|
+
if (!changingId_.empty())
|
|
192
|
+
metaObj.Set("changingId", Napi::String::New(env, changingId_));
|
|
193
|
+
|
|
125
194
|
docObj.Set("metadata", metaObj);
|
|
126
195
|
|
|
127
196
|
// for buffer variant, transfer ownership of the copied data
|
|
@@ -147,6 +216,13 @@ private:
|
|
|
147
216
|
int pageCount_ = 0;
|
|
148
217
|
std::u16string meta_[8];
|
|
149
218
|
int pdfVersion_ = 0;
|
|
219
|
+
unsigned long permissions_ = 0xFFFFFFFF;
|
|
220
|
+
bool isTagged_ = false;
|
|
221
|
+
std::u16string language_;
|
|
222
|
+
int signatureCount_ = 0;
|
|
223
|
+
int attachmentCount_ = 0;
|
|
224
|
+
std::string permanentId_;
|
|
225
|
+
std::string changingId_;
|
|
150
226
|
};
|
|
151
227
|
|
|
152
228
|
/**
|