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
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,313 @@ 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
|
+
bool hasPageSelection = false;
|
|
631
|
+
std::string outputPath;
|
|
632
|
+
std::string password;
|
|
633
|
+
|
|
634
|
+
if (info.Length() > 1 && info[1].IsObject()) {
|
|
635
|
+
Napi::Object opts = info[1].As<Napi::Object>();
|
|
636
|
+
if (opts.Has("usage") && opts.Get("usage").IsString()) {
|
|
637
|
+
std::string usage = opts.Get("usage").As<Napi::String>().Utf8Value();
|
|
638
|
+
if (usage == "print") {
|
|
639
|
+
flag = FLAT_PRINT;
|
|
640
|
+
} else if (usage != "display") {
|
|
641
|
+
Napi::TypeError::New(env, "usage must be 'display' or 'print'")
|
|
642
|
+
.ThrowAsJavaScriptException();
|
|
643
|
+
return env.Null();
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
if (opts.Has("pages") && opts.Get("pages").IsArray()) {
|
|
647
|
+
hasPageSelection = true;
|
|
648
|
+
Napi::Array jsPages = opts.Get("pages").As<Napi::Array>();
|
|
649
|
+
pages.reserve(jsPages.Length());
|
|
650
|
+
for (uint32_t i = 0; i < jsPages.Length(); i++) {
|
|
651
|
+
pages.push_back(jsPages.Get(i).As<Napi::Number>().Int32Value());
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
if (opts.Has("output") && opts.Get("output").IsString()) {
|
|
655
|
+
outputPath = opts.Get("output").As<Napi::String>().Utf8Value();
|
|
656
|
+
}
|
|
657
|
+
if (opts.Has("password") && opts.Get("password").IsString()) {
|
|
658
|
+
password = opts.Get("password").As<Napi::String>().Utf8Value();
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
Napi::Value arg = info[0];
|
|
663
|
+
FlattenDocumentWorker *worker = nullptr;
|
|
664
|
+
|
|
665
|
+
if (arg.IsBuffer()) {
|
|
666
|
+
auto buffer = arg.As<Napi::Buffer<uint8_t>>();
|
|
667
|
+
std::vector<uint8_t> data(buffer.Data(), buffer.Data() + buffer.Length());
|
|
668
|
+
worker = new FlattenDocumentWorker(env, std::move(data), flag,
|
|
669
|
+
std::move(pages), hasPageSelection,
|
|
670
|
+
std::move(outputPath),
|
|
671
|
+
std::move(password));
|
|
672
|
+
} else if (arg.IsString()) {
|
|
673
|
+
std::string path = arg.As<Napi::String>().Utf8Value();
|
|
674
|
+
worker = new FlattenDocumentWorker(env, std::move(path), flag,
|
|
675
|
+
std::move(pages), hasPageSelection,
|
|
676
|
+
std::move(outputPath),
|
|
677
|
+
std::move(password));
|
|
678
|
+
} else {
|
|
679
|
+
Napi::TypeError::New(env, "Expected a Buffer or string path argument")
|
|
680
|
+
.ThrowAsJavaScriptException();
|
|
681
|
+
return env.Null();
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
auto promise = worker->Promise();
|
|
685
|
+
worker->Queue();
|
|
686
|
+
return promise;
|
|
687
|
+
}
|
|
688
|
+
|
|
379
689
|
// ---------------------------------------------------------------------------
|
|
380
690
|
// Module init
|
|
381
691
|
// ---------------------------------------------------------------------------
|
|
@@ -430,6 +740,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
|
430
740
|
exports.Set("loadDocument", Napi::Function::New(env, LoadDocument));
|
|
431
741
|
exports.Set("splitDocument", Napi::Function::New(env, SplitDocument));
|
|
432
742
|
exports.Set("mergeDocuments", Napi::Function::New(env, MergeDocuments));
|
|
743
|
+
exports.Set("assemblePages", Napi::Function::New(env, AssemblePages));
|
|
744
|
+
exports.Set("nUpPages", Napi::Function::New(env, NUpPages));
|
|
745
|
+
exports.Set("addAttachments", Napi::Function::New(env, AddAttachments));
|
|
746
|
+
exports.Set("flattenDocument", Napi::Function::New(env, FlattenDocument));
|
|
433
747
|
exports.Set("prepareShutdown", Napi::Function::New(env, PrepareShutdown));
|
|
434
748
|
|
|
435
749
|
return exports;
|
|
@@ -0,0 +1,248 @@
|
|
|
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_signature.h"
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Digital signature helpers
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// PDFium reads the signature *dictionary* only — it performs no cryptographic
|
|
16
|
+
// verification whatsoever. These workers surface what the PDF itself declares:
|
|
17
|
+
// the encoding (/SubFilter), the signing reason and time, the DocMDP
|
|
18
|
+
// certification level and the byte ranges the digest covers. Verifying the
|
|
19
|
+
// PKCS#7 blob against those ranges is the caller's job;
|
|
20
|
+
// GetSignatureContentsWorker hands the blob over untouched.
|
|
21
|
+
|
|
22
|
+
// read a signature's /SubFilter, /M etc. — these are 7-bit ASCII, not UTF-16LE
|
|
23
|
+
template <typename Fn> inline std::string ReadSignatureString(Fn fn) {
|
|
24
|
+
return ReadUtf8([&](char *, unsigned long) { return fn(nullptr, 0); },
|
|
25
|
+
[&](char *buf, unsigned long len) { return fn(buf, len); });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Reads the raw /Contents bytes of a signature (a DER-encoded PKCS#1 or PKCS#7
|
|
29
|
+
// binary) via the two-pass protocol. An empty /Contents yields an empty vector
|
|
30
|
+
// and success. Returns false and sets `err` on failure.
|
|
31
|
+
inline bool ReadSignatureContentsBytes(FPDF_SIGNATURE signature,
|
|
32
|
+
std::vector<uint8_t> &out,
|
|
33
|
+
std::string &err) {
|
|
34
|
+
unsigned long len = FPDFSignatureObj_GetContents(signature, nullptr, 0);
|
|
35
|
+
out.clear();
|
|
36
|
+
if (len == 0)
|
|
37
|
+
return true;
|
|
38
|
+
|
|
39
|
+
out.resize(len);
|
|
40
|
+
if (FPDFSignatureObj_GetContents(signature, out.data(), len) != len) {
|
|
41
|
+
err = "Failed to read signature contents";
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// GetSignaturesWorker — async signature metadata listing
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
// Deliberately cheap in the same sense as GetAttachmentsWorker: it reads
|
|
51
|
+
// dictionary entries and the byte-range array, but reports /Contents only by
|
|
52
|
+
// its length. Use GetSignatureContentsWorker to fetch the blob itself.
|
|
53
|
+
|
|
54
|
+
struct SignatureInfo {
|
|
55
|
+
int index = 0;
|
|
56
|
+
std::string subFilter;
|
|
57
|
+
std::u16string reason;
|
|
58
|
+
std::string time;
|
|
59
|
+
unsigned int docMdpPermission = 0;
|
|
60
|
+
std::vector<int> byteRange;
|
|
61
|
+
unsigned long contentsLength = 0;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
class GetSignaturesWorker : public SafeAsyncWorker {
|
|
65
|
+
public:
|
|
66
|
+
GetSignaturesWorker(Napi::Env env, FPDF_DOCUMENT doc,
|
|
67
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
68
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
69
|
+
doc_(doc), docAlive_(std::move(docAlive)) {}
|
|
70
|
+
|
|
71
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
72
|
+
|
|
73
|
+
protected:
|
|
74
|
+
void Execute() override {
|
|
75
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
76
|
+
if (!docAlive_ || !docAlive_->load()) {
|
|
77
|
+
SetError("Document was destroyed");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
int count = FPDF_GetSignatureCount(doc_);
|
|
82
|
+
if (count <= 0)
|
|
83
|
+
return;
|
|
84
|
+
|
|
85
|
+
signatures_.reserve(count);
|
|
86
|
+
for (int i = 0; i < count; i++) {
|
|
87
|
+
FPDF_SIGNATURE signature = FPDF_GetSignatureObject(doc_, i);
|
|
88
|
+
if (!signature)
|
|
89
|
+
continue;
|
|
90
|
+
|
|
91
|
+
SignatureInfo info;
|
|
92
|
+
info.index = i;
|
|
93
|
+
|
|
94
|
+
info.subFilter = ReadSignatureString(
|
|
95
|
+
[&](char *buf, unsigned long len) {
|
|
96
|
+
return FPDFSignatureObj_GetSubFilter(signature, buf, len);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
info.reason = ReadU16(
|
|
100
|
+
[&](auto *, unsigned long) {
|
|
101
|
+
return FPDFSignatureObj_GetReason(signature, nullptr, 0);
|
|
102
|
+
},
|
|
103
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
104
|
+
return FPDFSignatureObj_GetReason(signature, buf, len);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
info.time = ReadSignatureString([&](char *buf, unsigned long len) {
|
|
108
|
+
return FPDFSignatureObj_GetTime(signature, buf, len);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// 1-3 for a certification (DocMDP) signature, 0 for an ordinary one
|
|
112
|
+
info.docMdpPermission =
|
|
113
|
+
FPDFSignatureObj_GetDocMDPPermission(signature);
|
|
114
|
+
|
|
115
|
+
// /ByteRange is a flat array of (offset, length) pairs; the length here
|
|
116
|
+
// counts ints, not bytes
|
|
117
|
+
unsigned long rangeLen =
|
|
118
|
+
FPDFSignatureObj_GetByteRange(signature, nullptr, 0);
|
|
119
|
+
if (rangeLen > 0) {
|
|
120
|
+
info.byteRange.resize(rangeLen);
|
|
121
|
+
if (FPDFSignatureObj_GetByteRange(signature, info.byteRange.data(),
|
|
122
|
+
rangeLen) != rangeLen) {
|
|
123
|
+
info.byteRange.clear();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
info.contentsLength =
|
|
128
|
+
FPDFSignatureObj_GetContents(signature, nullptr, 0);
|
|
129
|
+
|
|
130
|
+
signatures_.push_back(std::move(info));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
void OnOK() override {
|
|
135
|
+
Napi::Env env = Env();
|
|
136
|
+
Napi::Array arr = Napi::Array::New(env, signatures_.size());
|
|
137
|
+
for (uint32_t i = 0; i < signatures_.size(); i++) {
|
|
138
|
+
const auto &info = signatures_[i];
|
|
139
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
140
|
+
obj.Set("index", Napi::Number::New(env, info.index));
|
|
141
|
+
obj.Set("subFilter", Napi::String::New(env, info.subFilter));
|
|
142
|
+
SetU16IfPresent(obj, "reason", env, info.reason);
|
|
143
|
+
if (!info.time.empty())
|
|
144
|
+
obj.Set("time", Napi::String::New(env, info.time));
|
|
145
|
+
if (info.docMdpPermission > 0) {
|
|
146
|
+
obj.Set("docMdpPermission",
|
|
147
|
+
Napi::Number::New(env, info.docMdpPermission));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
Napi::Array range = Napi::Array::New(env, info.byteRange.size());
|
|
151
|
+
for (uint32_t j = 0; j < info.byteRange.size(); j++) {
|
|
152
|
+
range.Set(j, Napi::Number::New(env, info.byteRange[j]));
|
|
153
|
+
}
|
|
154
|
+
obj.Set("byteRange", range);
|
|
155
|
+
|
|
156
|
+
obj.Set("contentsLength",
|
|
157
|
+
Napi::Number::New(env, static_cast<double>(info.contentsLength)));
|
|
158
|
+
arr.Set(i, obj);
|
|
159
|
+
}
|
|
160
|
+
deferred_.Resolve(arr);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
void OnError(const Napi::Error &err) override {
|
|
164
|
+
deferred_.Reject(err.Value());
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private:
|
|
168
|
+
Napi::Promise::Deferred deferred_;
|
|
169
|
+
FPDF_DOCUMENT doc_;
|
|
170
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
171
|
+
std::vector<SignatureInfo> signatures_;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
// ---------------------------------------------------------------------------
|
|
175
|
+
// GetSignatureContentsWorker — async read of a single signature's /Contents
|
|
176
|
+
// ---------------------------------------------------------------------------
|
|
177
|
+
|
|
178
|
+
class GetSignatureContentsWorker : public SafeAsyncWorker {
|
|
179
|
+
public:
|
|
180
|
+
GetSignatureContentsWorker(Napi::Env env, FPDF_DOCUMENT doc, int index,
|
|
181
|
+
std::string outputPath,
|
|
182
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
183
|
+
: SafeAsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
184
|
+
doc_(doc), index_(index), outputPath_(std::move(outputPath)),
|
|
185
|
+
docAlive_(std::move(docAlive)) {}
|
|
186
|
+
|
|
187
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
188
|
+
|
|
189
|
+
protected:
|
|
190
|
+
void Execute() override {
|
|
191
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
192
|
+
if (!docAlive_ || !docAlive_->load()) {
|
|
193
|
+
SetError("Document was destroyed");
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
int count = FPDF_GetSignatureCount(doc_);
|
|
198
|
+
if (index_ < 0 || index_ >= count) {
|
|
199
|
+
SetError("Signature index out of range");
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
FPDF_SIGNATURE signature = FPDF_GetSignatureObject(doc_, index_);
|
|
204
|
+
if (!signature) {
|
|
205
|
+
SetError("Failed to get signature");
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
std::string err;
|
|
210
|
+
if (!ReadSignatureContentsBytes(signature, data_, err)) {
|
|
211
|
+
SetError(err);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (!outputPath_.empty()) {
|
|
216
|
+
if (!WriteBytesToFile(outputPath_, data_, err)) {
|
|
217
|
+
SetError(err);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
data_.clear();
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
void OnOK() override {
|
|
225
|
+
Napi::Env env = Env();
|
|
226
|
+
if (!outputPath_.empty()) {
|
|
227
|
+
deferred_.Resolve(env.Undefined());
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
auto *vec = new std::vector<uint8_t>(std::move(data_));
|
|
231
|
+
auto buffer = Napi::Buffer<uint8_t>::New(
|
|
232
|
+
env, vec->data(), vec->size(),
|
|
233
|
+
[](Napi::Env, uint8_t *, std::vector<uint8_t> *v) { delete v; }, vec);
|
|
234
|
+
deferred_.Resolve(buffer);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
void OnError(const Napi::Error &err) override {
|
|
238
|
+
deferred_.Reject(err.Value());
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private:
|
|
242
|
+
Napi::Promise::Deferred deferred_;
|
|
243
|
+
FPDF_DOCUMENT doc_;
|
|
244
|
+
int index_;
|
|
245
|
+
std::string outputPath_;
|
|
246
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
247
|
+
std::vector<uint8_t> data_;
|
|
248
|
+
};
|
package/src/split_merge_worker.h
CHANGED
|
@@ -1,103 +1,12 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
|
-
#include "
|
|
3
|
+
#include "document_io.h"
|
|
4
4
|
|
|
5
|
-
#include <
|
|
6
|
-
#include <cstdio>
|
|
5
|
+
#include <string>
|
|
7
6
|
#include <vector>
|
|
8
7
|
|
|
9
|
-
#ifdef _WIN32
|
|
10
|
-
#include <io.h>
|
|
11
|
-
#define F_OK 0
|
|
12
|
-
#define access _access
|
|
13
|
-
#else
|
|
14
|
-
#include <unistd.h>
|
|
15
|
-
#endif
|
|
16
|
-
|
|
17
8
|
#include "fpdf_edit.h"
|
|
18
9
|
#include "fpdf_ppo.h"
|
|
19
|
-
#include "fpdf_save.h"
|
|
20
|
-
|
|
21
|
-
// ---------------------------------------------------------------------------
|
|
22
|
-
// FPDF_FILEWRITE adapter — collects saved PDF bytes into a std::vector
|
|
23
|
-
// ---------------------------------------------------------------------------
|
|
24
|
-
|
|
25
|
-
struct VectorFileWrite : FPDF_FILEWRITE {
|
|
26
|
-
std::vector<uint8_t> data;
|
|
27
|
-
|
|
28
|
-
VectorFileWrite() {
|
|
29
|
-
version = 1;
|
|
30
|
-
WriteBlock = [](FPDF_FILEWRITE_ *self, const void *buf,
|
|
31
|
-
unsigned long size) -> int {
|
|
32
|
-
auto *w = static_cast<VectorFileWrite *>(self);
|
|
33
|
-
auto *bytes = static_cast<const uint8_t *>(buf);
|
|
34
|
-
w->data.insert(w->data.end(), bytes, bytes + size);
|
|
35
|
-
return 1;
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// helper: save a document to buffer or file, returning the buffer data
|
|
41
|
-
static inline bool SaveDocument(FPDF_DOCUMENT doc,
|
|
42
|
-
const std::string &outputPath,
|
|
43
|
-
std::vector<uint8_t> &outData,
|
|
44
|
-
std::string &outError) {
|
|
45
|
-
VectorFileWrite writer;
|
|
46
|
-
if (!FPDF_SaveAsCopy(doc, &writer, 0)) {
|
|
47
|
-
outError = "Failed to save PDF document";
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (!outputPath.empty()) {
|
|
52
|
-
auto slash = outputPath.rfind('/');
|
|
53
|
-
if (slash != std::string::npos && slash > 0) {
|
|
54
|
-
std::string parentDir = outputPath.substr(0, slash);
|
|
55
|
-
if (access(parentDir.c_str(), F_OK) != 0) {
|
|
56
|
-
outError = "Parent directory does not exist: " + parentDir;
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
FILE *f = fopen(outputPath.c_str(), "wb");
|
|
61
|
-
if (!f) {
|
|
62
|
-
outError = "Failed to open output file: " + outputPath;
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
size_t written = fwrite(writer.data.data(), 1, writer.data.size(), f);
|
|
66
|
-
fclose(f);
|
|
67
|
-
if (written != writer.data.size()) {
|
|
68
|
-
outError = "Failed to write output file: " + outputPath;
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
} else {
|
|
72
|
-
outData = std::move(writer.data);
|
|
73
|
-
}
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// helper: load a document from buffer or file path
|
|
78
|
-
static inline FPDF_DOCUMENT LoadDoc(const std::vector<uint8_t> &bufferData,
|
|
79
|
-
const std::string &filePath, bool useFile,
|
|
80
|
-
const std::string &password,
|
|
81
|
-
std::string &outError) {
|
|
82
|
-
const char *pw = password.empty() ? nullptr : password.c_str();
|
|
83
|
-
FPDF_DOCUMENT doc;
|
|
84
|
-
|
|
85
|
-
if (useFile) {
|
|
86
|
-
doc = FPDF_LoadDocument(filePath.c_str(), pw);
|
|
87
|
-
} else {
|
|
88
|
-
if (bufferData.size() > static_cast<size_t>(INT_MAX)) {
|
|
89
|
-
outError = "FORMAT:Buffer too large";
|
|
90
|
-
return nullptr;
|
|
91
|
-
}
|
|
92
|
-
doc = FPDF_LoadMemDocument(bufferData.data(),
|
|
93
|
-
static_cast<int>(bufferData.size()), pw);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (!doc) {
|
|
97
|
-
outError = GetPdfiumErrorMessage();
|
|
98
|
-
}
|
|
99
|
-
return doc;
|
|
100
|
-
}
|
|
101
10
|
|
|
102
11
|
// ---------------------------------------------------------------------------
|
|
103
12
|
// SplitDocumentWorker — split a PDF into multiple documents at given indices
|