pdfium-native 0.1.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/LICENSE +21 -0
- package/README.md +227 -0
- package/binding.gyp +85 -0
- package/dist/document.d.ts +23 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +36 -0
- package/dist/document.js.map +1 -0
- package/dist/errors.d.ts +23 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +58 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/page.d.ts +40 -0
- package/dist/page.d.ts.map +1 -0
- package/dist/page.js +59 -0
- package/dist/page.js.map +1 -0
- package/dist/types.d.ts +120 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/lib/__tests__/document.test.ts +99 -0
- package/lib/__tests__/features.test.ts +72 -0
- package/lib/__tests__/fixtures.ts +71 -0
- package/lib/__tests__/load.test.ts +68 -0
- package/lib/__tests__/page.test.ts +123 -0
- package/lib/__tests__/render.test.ts +103 -0
- package/lib/__tests__/search.test.ts +44 -0
- package/lib/document.ts +40 -0
- package/lib/errors.ts +63 -0
- package/lib/index.ts +63 -0
- package/lib/page.ts +80 -0
- package/lib/types.ts +154 -0
- package/package.json +80 -0
- package/scripts/bundle-lib.mjs +66 -0
- package/scripts/download-pdfium.mjs +84 -0
- package/scripts/generate-fixtures.mjs +194 -0
- package/scripts/install.mjs +80 -0
- package/src/document.h +254 -0
- package/src/napi_helpers.h +50 -0
- package/src/page.h +576 -0
- package/src/pdfium_addon.cc +218 -0
- package/src/render_worker.h +141 -0
- package/src/stb_image_write.c +2 -0
- package/src/stb_image_write.h +1724 -0
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
#include "document.h"
|
|
2
|
+
|
|
3
|
+
Napi::FunctionReference PDFiumDocument::pageConstructor;
|
|
4
|
+
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Module-level: loadDocument (async)
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
|
|
9
|
+
static Napi::FunctionReference g_docConstructor;
|
|
10
|
+
|
|
11
|
+
static std::string GetPdfiumErrorMessage() {
|
|
12
|
+
unsigned long err = FPDF_GetLastError();
|
|
13
|
+
const char *code;
|
|
14
|
+
const char *msg;
|
|
15
|
+
switch (err) {
|
|
16
|
+
case FPDF_ERR_FILE:
|
|
17
|
+
code = "FILE";
|
|
18
|
+
msg = "File not found or could not be opened";
|
|
19
|
+
break;
|
|
20
|
+
case FPDF_ERR_FORMAT:
|
|
21
|
+
code = "FORMAT";
|
|
22
|
+
msg = "Not a valid PDF or corrupted";
|
|
23
|
+
break;
|
|
24
|
+
case FPDF_ERR_PASSWORD:
|
|
25
|
+
code = "PASSWORD";
|
|
26
|
+
msg = "Password required or incorrect";
|
|
27
|
+
break;
|
|
28
|
+
case FPDF_ERR_SECURITY:
|
|
29
|
+
code = "SECURITY";
|
|
30
|
+
msg = "Unsupported security scheme";
|
|
31
|
+
break;
|
|
32
|
+
case FPDF_ERR_PAGE:
|
|
33
|
+
code = "PAGE";
|
|
34
|
+
msg = "Page error";
|
|
35
|
+
break;
|
|
36
|
+
default:
|
|
37
|
+
code = "UNKNOWN";
|
|
38
|
+
msg = "Unknown error";
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
return std::string(code) + ":" + msg;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
// LoadDocumentWorker — async document loading
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
class LoadDocumentWorker : public Napi::AsyncWorker {
|
|
49
|
+
public:
|
|
50
|
+
// buffer variant — copies data since buffer may be GC'd
|
|
51
|
+
LoadDocumentWorker(Napi::Env env, std::vector<uint8_t> data,
|
|
52
|
+
std::string password)
|
|
53
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
54
|
+
bufferData_(std::move(data)), password_(std::move(password)),
|
|
55
|
+
useFile_(false) {}
|
|
56
|
+
|
|
57
|
+
// file path variant
|
|
58
|
+
LoadDocumentWorker(Napi::Env env, std::string path, std::string password)
|
|
59
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
60
|
+
filePath_(std::move(path)), password_(std::move(password)),
|
|
61
|
+
useFile_(true) {}
|
|
62
|
+
|
|
63
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
64
|
+
|
|
65
|
+
protected:
|
|
66
|
+
void Execute() override {
|
|
67
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
68
|
+
|
|
69
|
+
const char *pw = password_.empty() ? nullptr : password_.c_str();
|
|
70
|
+
|
|
71
|
+
if (useFile_) {
|
|
72
|
+
doc_ = FPDF_LoadDocument(filePath_.c_str(), pw);
|
|
73
|
+
} else {
|
|
74
|
+
doc_ = FPDF_LoadMemDocument(bufferData_.data(),
|
|
75
|
+
static_cast<int>(bufferData_.size()), pw);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!doc_) {
|
|
79
|
+
SetError(GetPdfiumErrorMessage());
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
pageCount_ = FPDF_GetPageCount(doc_);
|
|
84
|
+
|
|
85
|
+
// read metadata under the mutex
|
|
86
|
+
const char *metaTags[] = {"Title", "Author", "Subject", "Keywords",
|
|
87
|
+
"Creator", "Producer", "CreationDate", "ModDate"};
|
|
88
|
+
for (int i = 0; i < 8; i++) {
|
|
89
|
+
unsigned long len = FPDF_GetMetaText(doc_, metaTags[i], nullptr, 0);
|
|
90
|
+
if (len > 2) {
|
|
91
|
+
std::vector<unsigned short> buf(len / sizeof(unsigned short));
|
|
92
|
+
FPDF_GetMetaText(doc_, metaTags[i], buf.data(), len);
|
|
93
|
+
size_t charCount = len / sizeof(unsigned short) - 1;
|
|
94
|
+
meta_[i] = std::u16string(
|
|
95
|
+
reinterpret_cast<const char16_t *>(buf.data()), charCount);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
FPDF_GetFileVersion(doc_, &pdfVersion_);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
void OnOK() override {
|
|
102
|
+
Napi::Env env = Env();
|
|
103
|
+
Napi::Object docObj = g_docConstructor.New({});
|
|
104
|
+
PDFiumDocument *docWrapper = PDFiumDocument::Unwrap(docObj);
|
|
105
|
+
docWrapper->SetDocument(doc_);
|
|
106
|
+
|
|
107
|
+
docObj.Set("pageCount", Napi::Number::New(env, pageCount_));
|
|
108
|
+
|
|
109
|
+
// set metadata as a plain JS property
|
|
110
|
+
const char *metaKeys[] = {"title", "author", "subject", "keywords",
|
|
111
|
+
"creator", "producer", "creationDate", "modDate"};
|
|
112
|
+
Napi::Object metaObj = Napi::Object::New(env);
|
|
113
|
+
for (int i = 0; i < 8; i++) {
|
|
114
|
+
if (meta_[i].empty()) {
|
|
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
|
+
}
|
|
123
|
+
}
|
|
124
|
+
metaObj.Set("pdfVersion", Napi::Number::New(env, pdfVersion_));
|
|
125
|
+
docObj.Set("metadata", metaObj);
|
|
126
|
+
|
|
127
|
+
// for buffer variant, transfer ownership of the copied data
|
|
128
|
+
// the data must stay alive as long as the document is open
|
|
129
|
+
if (!useFile_ && !bufferData_.empty()) {
|
|
130
|
+
docWrapper->SetOwnedBuffer(std::move(bufferData_));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
deferred_.Resolve(docObj);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
void OnError(const Napi::Error &err) override {
|
|
137
|
+
deferred_.Reject(err.Value());
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private:
|
|
141
|
+
Napi::Promise::Deferred deferred_;
|
|
142
|
+
std::vector<uint8_t> bufferData_;
|
|
143
|
+
std::string filePath_;
|
|
144
|
+
std::string password_;
|
|
145
|
+
bool useFile_;
|
|
146
|
+
FPDF_DOCUMENT doc_ = nullptr;
|
|
147
|
+
int pageCount_ = 0;
|
|
148
|
+
std::u16string meta_[8];
|
|
149
|
+
int pdfVersion_ = 0;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Opens a PDF document from a Buffer or file path. Returns a
|
|
154
|
+
* Promise<PDFiumDocument>.
|
|
155
|
+
*/
|
|
156
|
+
Napi::Value LoadDocument(const Napi::CallbackInfo &info) {
|
|
157
|
+
Napi::Env env = info.Env();
|
|
158
|
+
|
|
159
|
+
if (info.Length() < 1) {
|
|
160
|
+
Napi::TypeError::New(env, "Expected a Buffer or string path argument")
|
|
161
|
+
.ThrowAsJavaScriptException();
|
|
162
|
+
return env.Null();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
Napi::Value arg = info[0];
|
|
166
|
+
std::string password;
|
|
167
|
+
if (info.Length() > 1 && info[1].IsString()) {
|
|
168
|
+
password = info[1].As<Napi::String>().Utf8Value();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
LoadDocumentWorker *worker = nullptr;
|
|
172
|
+
|
|
173
|
+
if (arg.IsBuffer()) {
|
|
174
|
+
auto buffer = arg.As<Napi::Buffer<uint8_t>>();
|
|
175
|
+
// copy buffer data — original JS buffer may be GC'd before worker runs
|
|
176
|
+
std::vector<uint8_t> data(buffer.Data(), buffer.Data() + buffer.Length());
|
|
177
|
+
worker = new LoadDocumentWorker(env, std::move(data), std::move(password));
|
|
178
|
+
} else if (arg.IsString()) {
|
|
179
|
+
std::string path = arg.As<Napi::String>().Utf8Value();
|
|
180
|
+
worker = new LoadDocumentWorker(env, std::move(path), std::move(password));
|
|
181
|
+
} else {
|
|
182
|
+
Napi::TypeError::New(env, "Expected a Buffer or string path argument")
|
|
183
|
+
.ThrowAsJavaScriptException();
|
|
184
|
+
return env.Null();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
auto promise = worker->Promise();
|
|
188
|
+
worker->Queue();
|
|
189
|
+
return promise;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ---------------------------------------------------------------------------
|
|
193
|
+
// Module init
|
|
194
|
+
// ---------------------------------------------------------------------------
|
|
195
|
+
|
|
196
|
+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
197
|
+
if (!g_initialized) {
|
|
198
|
+
FPDF_InitLibrary();
|
|
199
|
+
g_initialized = true;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// register classes
|
|
203
|
+
Napi::Function pageCtor = PDFiumPage::Init(env);
|
|
204
|
+
PDFiumDocument::pageConstructor = Napi::Persistent(pageCtor);
|
|
205
|
+
PDFiumDocument::pageConstructor.SuppressDestruct();
|
|
206
|
+
|
|
207
|
+
Napi::Function docCtor = PDFiumDocument::Init(env);
|
|
208
|
+
g_docConstructor = Napi::Persistent(docCtor);
|
|
209
|
+
g_docConstructor.SuppressDestruct();
|
|
210
|
+
|
|
211
|
+
exports.Set("PDFiumDocument", docCtor);
|
|
212
|
+
exports.Set("PDFiumPage", pageCtor);
|
|
213
|
+
exports.Set("loadDocument", Napi::Function::New(env, LoadDocument));
|
|
214
|
+
|
|
215
|
+
return exports;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
NODE_API_MODULE(pdfium, Init)
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "napi_helpers.h"
|
|
4
|
+
|
|
5
|
+
#include <atomic>
|
|
6
|
+
#include <cstdio>
|
|
7
|
+
#include <filesystem>
|
|
8
|
+
#include <memory>
|
|
9
|
+
|
|
10
|
+
#include "stb_image_write.h"
|
|
11
|
+
|
|
12
|
+
// stb write callback — appends bytes to a std::vector
|
|
13
|
+
inline void stb_write_callback(void *context, void *data, int size) {
|
|
14
|
+
auto *out = static_cast<std::vector<uint8_t> *>(context);
|
|
15
|
+
auto *bytes = static_cast<uint8_t *>(data);
|
|
16
|
+
out->insert(out->end(), bytes, bytes + size);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// RenderWorker — async page rendering
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
class RenderWorker : public Napi::AsyncWorker {
|
|
24
|
+
public:
|
|
25
|
+
RenderWorker(Napi::Env env, FPDF_PAGE page, int renderWidth, int renderHeight,
|
|
26
|
+
int format, int quality, std::string outputPath,
|
|
27
|
+
std::shared_ptr<std::atomic<bool>> pageAlive)
|
|
28
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
29
|
+
page_(page), renderWidth_(renderWidth), renderHeight_(renderHeight),
|
|
30
|
+
format_(format), quality_(quality), outputPath_(std::move(outputPath)),
|
|
31
|
+
pageAlive_(std::move(pageAlive)) {}
|
|
32
|
+
|
|
33
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
34
|
+
|
|
35
|
+
protected:
|
|
36
|
+
void Execute() override {
|
|
37
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
38
|
+
|
|
39
|
+
// check that the page hasn't been closed while we were queued
|
|
40
|
+
if (!pageAlive_ || !pageAlive_->load()) {
|
|
41
|
+
SetError("Page was closed before render completed");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
FPDF_BITMAP bitmap = FPDFBitmap_Create(renderWidth_, renderHeight_, 0);
|
|
46
|
+
if (!bitmap) {
|
|
47
|
+
SetError("Failed to create bitmap");
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
FPDFBitmap_FillRect(bitmap, 0, 0, renderWidth_, renderHeight_, 0xFFFFFFFF);
|
|
52
|
+
FPDF_RenderPageBitmap(bitmap, page_, 0, 0, renderWidth_, renderHeight_, 0,
|
|
53
|
+
FPDF_ANNOT | FPDF_PRINTING);
|
|
54
|
+
|
|
55
|
+
// convert BGRA → RGB for stb (which expects RGB)
|
|
56
|
+
void *bufferData = FPDFBitmap_GetBuffer(bitmap);
|
|
57
|
+
int stride = FPDFBitmap_GetStride(bitmap);
|
|
58
|
+
std::vector<uint8_t> rgb(static_cast<size_t>(renderWidth_) * renderHeight_ *
|
|
59
|
+
3);
|
|
60
|
+
|
|
61
|
+
for (int y = 0; y < renderHeight_; y++) {
|
|
62
|
+
auto *row = static_cast<uint8_t *>(bufferData) + y * stride;
|
|
63
|
+
for (int x = 0; x < renderWidth_; x++) {
|
|
64
|
+
int srcIdx = x * 4;
|
|
65
|
+
int dstIdx = (y * renderWidth_ + x) * 3;
|
|
66
|
+
rgb[dstIdx + 0] = row[srcIdx + 2]; // R ← BGRA[2]
|
|
67
|
+
rgb[dstIdx + 1] = row[srcIdx + 1]; // G ← BGRA[1]
|
|
68
|
+
rgb[dstIdx + 2] = row[srcIdx + 0]; // B ← BGRA[0]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
FPDFBitmap_Destroy(bitmap);
|
|
73
|
+
|
|
74
|
+
// encode to JPEG or PNG
|
|
75
|
+
int ok;
|
|
76
|
+
if (format_ == IMAGE_FORMAT_PNG) {
|
|
77
|
+
ok = stbi_write_png_to_func(stb_write_callback, &encodedData_,
|
|
78
|
+
renderWidth_, renderHeight_, 3, rgb.data(),
|
|
79
|
+
renderWidth_ * 3);
|
|
80
|
+
} else {
|
|
81
|
+
ok = stbi_write_jpg_to_func(stb_write_callback, &encodedData_,
|
|
82
|
+
renderWidth_, renderHeight_, 3, rgb.data(),
|
|
83
|
+
quality_);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!ok) {
|
|
87
|
+
SetError("Failed to encode image");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// write to file if output path was specified
|
|
92
|
+
if (!outputPath_.empty()) {
|
|
93
|
+
// verify parent directory exists
|
|
94
|
+
std::filesystem::path outPath(outputPath_);
|
|
95
|
+
auto parentDir = outPath.parent_path();
|
|
96
|
+
if (!parentDir.empty() && !std::filesystem::is_directory(parentDir)) {
|
|
97
|
+
SetError("Parent directory does not exist: " + parentDir.string());
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
FILE *f = fopen(outputPath_.c_str(), "wb");
|
|
102
|
+
if (!f) {
|
|
103
|
+
SetError("Failed to open output file: " + outputPath_);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
size_t written = fwrite(encodedData_.data(), 1, encodedData_.size(), f);
|
|
107
|
+
fclose(f);
|
|
108
|
+
if (written != encodedData_.size()) {
|
|
109
|
+
SetError("Failed to write output file: " + outputPath_);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
encodedData_.clear();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
void OnOK() override {
|
|
117
|
+
Napi::Env env = Env();
|
|
118
|
+
if (outputPath_.empty()) {
|
|
119
|
+
auto data = Napi::Buffer<uint8_t>::Copy(env, encodedData_.data(),
|
|
120
|
+
encodedData_.size());
|
|
121
|
+
deferred_.Resolve(data);
|
|
122
|
+
} else {
|
|
123
|
+
deferred_.Resolve(env.Undefined());
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
void OnError(const Napi::Error &err) override {
|
|
128
|
+
deferred_.Reject(err.Value());
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private:
|
|
132
|
+
Napi::Promise::Deferred deferred_;
|
|
133
|
+
FPDF_PAGE page_;
|
|
134
|
+
int renderWidth_;
|
|
135
|
+
int renderHeight_;
|
|
136
|
+
int format_;
|
|
137
|
+
int quality_;
|
|
138
|
+
std::string outputPath_;
|
|
139
|
+
std::vector<uint8_t> encodedData_;
|
|
140
|
+
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
141
|
+
};
|