pdfium-native 0.2.0 → 0.2.3
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 +172 -41
- package/binding.gyp +27 -1
- 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 +16 -3
- package/scripts/bundle-lib.mjs +24 -3
- package/scripts/download-pdfium.mjs +2 -1
- package/scripts/generate-fixtures.mjs +308 -1
- package/scripts/install.mjs +19 -1
- package/src/annotations_worker.h +274 -0
- package/src/bookmarks_worker.h +162 -0
- package/src/document.h +48 -5
- package/src/form_fields_worker.h +250 -0
- package/src/links_worker.h +158 -0
- package/src/napi_helpers.h +76 -1
- package/src/objects_worker.h +322 -0
- package/src/page.h +62 -9
- 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
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
import { writeFileSync, mkdirSync } from 'node:fs';
|
|
6
6
|
import { resolve, dirname } from 'node:path';
|
|
7
7
|
import { fileURLToPath } from 'node:url';
|
|
8
|
-
import {
|
|
8
|
+
import { Buffer } from 'node:buffer';
|
|
9
|
+
import { PDFDocument, StandardFonts, rgb, degrees } from 'pdf-lib';
|
|
9
10
|
|
|
10
11
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
11
12
|
const fixturesDir = resolve(__dirname, '..', 'test', 'fixtures');
|
|
@@ -202,15 +203,321 @@ async function createTextPdf() {
|
|
|
202
203
|
return doc.save();
|
|
203
204
|
}
|
|
204
205
|
|
|
206
|
+
// --- PDF with rich annotation metadata (author, subject, dates, flags) ---
|
|
207
|
+
async function createRichAnnotationPdf() {
|
|
208
|
+
const doc = await PDFDocument.create();
|
|
209
|
+
const page = doc.addPage([612, 792]);
|
|
210
|
+
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
211
|
+
page.drawText('Page with rich annotations', { x: 50, y: 700, size: 16, font });
|
|
212
|
+
|
|
213
|
+
const { PDFName, PDFString, PDFHexString } = await import('pdf-lib');
|
|
214
|
+
|
|
215
|
+
const annot = doc.context.obj({
|
|
216
|
+
Type: 'Annot',
|
|
217
|
+
Subtype: 'Text',
|
|
218
|
+
Rect: [100, 600, 120, 620],
|
|
219
|
+
Contents: PDFHexString.fromText('Test note'),
|
|
220
|
+
T: PDFHexString.fromText('John Doe'),
|
|
221
|
+
Subj: PDFHexString.fromText('Review Comment'),
|
|
222
|
+
CreationDate: PDFString.of('D:20250101120000Z'),
|
|
223
|
+
M: PDFString.of('D:20250102120000Z'),
|
|
224
|
+
C: [1, 0, 0],
|
|
225
|
+
F: 4, // print flag
|
|
226
|
+
});
|
|
227
|
+
const annotRef = doc.context.register(annot);
|
|
228
|
+
|
|
229
|
+
page.node.set(PDFName.of('Annots'), doc.context.obj([annotRef]));
|
|
230
|
+
return doc.save();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// --- PDF with a rotated page ---
|
|
234
|
+
async function createRotatedPagePdf() {
|
|
235
|
+
const doc = await PDFDocument.create();
|
|
236
|
+
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
237
|
+
const page = doc.addPage([612, 792]);
|
|
238
|
+
page.drawText('This page is rotated 90° clockwise', { x: 50, y: 700, size: 16, font });
|
|
239
|
+
page.setRotation(degrees(90));
|
|
240
|
+
return doc.save();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// --- PDF with CropBox and TrimBox ---
|
|
244
|
+
async function createPageBoxesPdf() {
|
|
245
|
+
const doc = await PDFDocument.create();
|
|
246
|
+
const { PDFName } = await import('pdf-lib');
|
|
247
|
+
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
248
|
+
const page = doc.addPage([612, 792]);
|
|
249
|
+
page.drawText('Page with CropBox and TrimBox', { x: 80, y: 700, size: 14, font });
|
|
250
|
+
page.node.set(PDFName.of('CropBox'), doc.context.obj([50, 50, 562, 742]));
|
|
251
|
+
page.node.set(PDFName.of('TrimBox'), doc.context.obj([72, 72, 540, 720]));
|
|
252
|
+
return doc.save();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// --- Tagged PDF with language ---
|
|
256
|
+
async function createTaggedPdf() {
|
|
257
|
+
const doc = await PDFDocument.create();
|
|
258
|
+
const { PDFName, PDFString } = await import('pdf-lib');
|
|
259
|
+
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
260
|
+
const page = doc.addPage([612, 792]);
|
|
261
|
+
page.drawText('Tagged PDF with language', { x: 50, y: 700, size: 16, font });
|
|
262
|
+
const markInfo = doc.context.obj({ Marked: true });
|
|
263
|
+
doc.catalog.set(PDFName.of('MarkInfo'), markInfo);
|
|
264
|
+
doc.catalog.set(PDFName.of('Lang'), PDFString.of('en-US'));
|
|
265
|
+
return doc.save();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// --- PDF with an embedded image ---
|
|
269
|
+
async function createImagePdf() {
|
|
270
|
+
const doc = await PDFDocument.create();
|
|
271
|
+
// create a minimal 2x2 red PNG manually (smallest valid PNG)
|
|
272
|
+
const signature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
273
|
+
// IHDR chunk: 2x2, 8-bit RGB
|
|
274
|
+
const ihdr = Buffer.alloc(25);
|
|
275
|
+
ihdr.writeUInt32BE(13, 0); // length
|
|
276
|
+
ihdr.write('IHDR', 4);
|
|
277
|
+
ihdr.writeUInt32BE(2, 8); // width
|
|
278
|
+
ihdr.writeUInt32BE(2, 12); // height
|
|
279
|
+
ihdr[16] = 8; // bit depth
|
|
280
|
+
ihdr[17] = 2; // color type RGB
|
|
281
|
+
const { crc32, deflateSync } = await import('node:zlib');
|
|
282
|
+
ihdr.writeUInt32BE(crc32(ihdr.subarray(4, 21)) >>> 0, 21);
|
|
283
|
+
// IDAT chunk: zlib-compressed scanlines (filter byte + 3 bytes per pixel × 2 pixels × 2 rows)
|
|
284
|
+
const raw = Buffer.from([
|
|
285
|
+
0,
|
|
286
|
+
255,
|
|
287
|
+
0,
|
|
288
|
+
0,
|
|
289
|
+
255,
|
|
290
|
+
0,
|
|
291
|
+
0, // row1: filter=0, red, red
|
|
292
|
+
0,
|
|
293
|
+
0,
|
|
294
|
+
0,
|
|
295
|
+
255,
|
|
296
|
+
0,
|
|
297
|
+
0,
|
|
298
|
+
255, // row2: filter=0, blue, blue
|
|
299
|
+
]);
|
|
300
|
+
const compressed = deflateSync(raw);
|
|
301
|
+
const idat = Buffer.alloc(compressed.length + 12);
|
|
302
|
+
idat.writeUInt32BE(compressed.length, 0);
|
|
303
|
+
idat.write('IDAT', 4);
|
|
304
|
+
compressed.copy(idat, 8);
|
|
305
|
+
idat.writeUInt32BE(crc32(idat.subarray(4, 8 + compressed.length)) >>> 0, 8 + compressed.length);
|
|
306
|
+
// IEND chunk
|
|
307
|
+
const iend = Buffer.from([0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130]);
|
|
308
|
+
const pngData = Buffer.concat([signature, ihdr, idat, iend]);
|
|
309
|
+
|
|
310
|
+
const img = await doc.embedPng(pngData);
|
|
311
|
+
const page = doc.addPage([612, 792]);
|
|
312
|
+
page.drawImage(img, { x: 50, y: 600, width: 100, height: 100 });
|
|
313
|
+
return doc.save();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// --- PDF with rich bookmarks (XYZ dest, open/closed, nested, URI) ---
|
|
317
|
+
async function createRichBookmarkPdf() {
|
|
318
|
+
const doc = await PDFDocument.create();
|
|
319
|
+
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
320
|
+
|
|
321
|
+
const page1 = doc.addPage([612, 792]);
|
|
322
|
+
page1.drawText('Chapter 1: Introduction', { x: 50, y: 700, size: 20, font });
|
|
323
|
+
const page2 = doc.addPage([612, 792]);
|
|
324
|
+
page2.drawText('Section 1.1: Background', { x: 70, y: 700, size: 16, font });
|
|
325
|
+
const page3 = doc.addPage([612, 792]);
|
|
326
|
+
page3.drawText('Chapter 2: Resources', { x: 50, y: 700, size: 20, font });
|
|
327
|
+
|
|
328
|
+
const { PDFName, PDFString, PDFNumber } = await import('pdf-lib');
|
|
329
|
+
|
|
330
|
+
const page1Ref = doc.getPages()[0].ref;
|
|
331
|
+
const page2Ref = doc.getPages()[1].ref;
|
|
332
|
+
const page3Ref = doc.getPages()[2].ref;
|
|
333
|
+
|
|
334
|
+
// child bookmark for chapter 1 (section 1.1)
|
|
335
|
+
const child1 = doc.context.obj({
|
|
336
|
+
Title: PDFString.of('Section 1.1'),
|
|
337
|
+
Dest: doc.context.obj([
|
|
338
|
+
page2Ref,
|
|
339
|
+
PDFName.of('XYZ'),
|
|
340
|
+
PDFNumber.of(70),
|
|
341
|
+
PDFNumber.of(700),
|
|
342
|
+
PDFNumber.of(1.5),
|
|
343
|
+
]),
|
|
344
|
+
});
|
|
345
|
+
const child1Ref = doc.context.register(child1);
|
|
346
|
+
|
|
347
|
+
// chapter 1 (open, with child, XYZ dest)
|
|
348
|
+
const item1 = doc.context.obj({
|
|
349
|
+
Title: PDFString.of('Chapter 1'),
|
|
350
|
+
Dest: doc.context.obj([
|
|
351
|
+
page1Ref,
|
|
352
|
+
PDFName.of('XYZ'),
|
|
353
|
+
PDFNumber.of(50),
|
|
354
|
+
PDFNumber.of(700),
|
|
355
|
+
PDFNumber.of(1),
|
|
356
|
+
]),
|
|
357
|
+
First: child1Ref,
|
|
358
|
+
Last: child1Ref,
|
|
359
|
+
Count: 1, // positive = open
|
|
360
|
+
});
|
|
361
|
+
const item1Ref = doc.context.register(item1);
|
|
362
|
+
|
|
363
|
+
// chapter 2 (closed, URI action)
|
|
364
|
+
const uriAction = doc.context.obj({
|
|
365
|
+
Type: 'Action',
|
|
366
|
+
S: 'URI',
|
|
367
|
+
URI: PDFString.of('https://example.com/resources'),
|
|
368
|
+
});
|
|
369
|
+
const uriActionRef = doc.context.register(uriAction);
|
|
370
|
+
const item2 = doc.context.obj({
|
|
371
|
+
Title: PDFString.of('Resources (link)'),
|
|
372
|
+
A: uriActionRef,
|
|
373
|
+
});
|
|
374
|
+
const item2Ref = doc.context.register(item2);
|
|
375
|
+
|
|
376
|
+
// chapter 3 (Fit dest)
|
|
377
|
+
const item3 = doc.context.obj({
|
|
378
|
+
Title: PDFString.of('Chapter 2'),
|
|
379
|
+
Dest: doc.context.obj([page3Ref, PDFName.of('Fit')]),
|
|
380
|
+
});
|
|
381
|
+
const item3Ref = doc.context.register(item3);
|
|
382
|
+
|
|
383
|
+
// link siblings
|
|
384
|
+
item1.set(PDFName.of('Next'), item2Ref);
|
|
385
|
+
item2.set(PDFName.of('Prev'), item1Ref);
|
|
386
|
+
item2.set(PDFName.of('Next'), item3Ref);
|
|
387
|
+
item3.set(PDFName.of('Prev'), item2Ref);
|
|
388
|
+
|
|
389
|
+
// child parent
|
|
390
|
+
child1.set(PDFName.of('Parent'), item1Ref);
|
|
391
|
+
|
|
392
|
+
// outline root
|
|
393
|
+
const outlines = doc.context.obj({
|
|
394
|
+
Type: 'Outlines',
|
|
395
|
+
First: item1Ref,
|
|
396
|
+
Last: item3Ref,
|
|
397
|
+
Count: 4,
|
|
398
|
+
});
|
|
399
|
+
const outlinesRef = doc.context.register(outlines);
|
|
400
|
+
|
|
401
|
+
item1.set(PDFName.of('Parent'), outlinesRef);
|
|
402
|
+
item2.set(PDFName.of('Parent'), outlinesRef);
|
|
403
|
+
item3.set(PDFName.of('Parent'), outlinesRef);
|
|
404
|
+
|
|
405
|
+
doc.catalog.set(PDFName.of('Outlines'), outlinesRef);
|
|
406
|
+
|
|
407
|
+
return doc.save();
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// --- PDF with annotations that have border and interior color ---
|
|
411
|
+
async function createBorderAnnotationPdf() {
|
|
412
|
+
const doc = await PDFDocument.create();
|
|
413
|
+
const page = doc.addPage([612, 792]);
|
|
414
|
+
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
415
|
+
page.drawText('Annotations with borders and fill', { x: 50, y: 700, size: 16, font });
|
|
416
|
+
|
|
417
|
+
const { PDFName, PDFString } = await import('pdf-lib');
|
|
418
|
+
|
|
419
|
+
// square annotation with interior color and border
|
|
420
|
+
const squareAnnot = doc.context.obj({
|
|
421
|
+
Type: 'Annot',
|
|
422
|
+
Subtype: 'Square',
|
|
423
|
+
Rect: [100, 500, 250, 600],
|
|
424
|
+
Contents: PDFString.of('Filled square'),
|
|
425
|
+
C: [0, 0, 1], // blue stroke
|
|
426
|
+
IC: [1, 1, 0], // yellow fill
|
|
427
|
+
Border: [5, 5, 3], // hRadius, vRadius, width
|
|
428
|
+
});
|
|
429
|
+
const squareRef = doc.context.register(squareAnnot);
|
|
430
|
+
|
|
431
|
+
// circle annotation with interior color
|
|
432
|
+
const circleAnnot = doc.context.obj({
|
|
433
|
+
Type: 'Annot',
|
|
434
|
+
Subtype: 'Circle',
|
|
435
|
+
Rect: [300, 500, 450, 600],
|
|
436
|
+
Contents: PDFString.of('Filled circle'),
|
|
437
|
+
C: [1, 0, 0], // red stroke
|
|
438
|
+
IC: [0, 1, 0], // green fill
|
|
439
|
+
Border: [0, 0, 2],
|
|
440
|
+
});
|
|
441
|
+
const circleRef = doc.context.register(circleAnnot);
|
|
442
|
+
|
|
443
|
+
page.node.set(PDFName.of('Annots'), doc.context.obj([squareRef, circleRef]));
|
|
444
|
+
return doc.save();
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// --- PDF with form fields ---
|
|
448
|
+
async function createFormFieldsPdf() {
|
|
449
|
+
const doc = await PDFDocument.create();
|
|
450
|
+
const page = doc.addPage([612, 792]);
|
|
451
|
+
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
452
|
+
page.drawText('Form Fields Test', { x: 50, y: 750, size: 16, font });
|
|
453
|
+
|
|
454
|
+
const form = doc.getForm();
|
|
455
|
+
|
|
456
|
+
// text field with default value
|
|
457
|
+
const textField = form.createTextField('fullName');
|
|
458
|
+
textField.setText('John Doe');
|
|
459
|
+
textField.addToPage(page, { x: 50, y: 680, width: 200, height: 24 });
|
|
460
|
+
|
|
461
|
+
// empty text field with alternate name
|
|
462
|
+
const emailField = form.createTextField('email');
|
|
463
|
+
emailField.addToPage(page, { x: 50, y: 640, width: 200, height: 24 });
|
|
464
|
+
|
|
465
|
+
// checkbox (checked)
|
|
466
|
+
const checkbox = form.createCheckBox('agree');
|
|
467
|
+
checkbox.addToPage(page, { x: 50, y: 600, width: 16, height: 16 });
|
|
468
|
+
checkbox.check();
|
|
469
|
+
|
|
470
|
+
// ensure checkbox /V and widget /AS are set (pdf-lib may not set /V properly)
|
|
471
|
+
const { PDFName: FormPDFName } = await import('pdf-lib');
|
|
472
|
+
checkbox.acroField.dict.set(FormPDFName.of('V'), FormPDFName.of('Yes'));
|
|
473
|
+
const checkboxWidgets = checkbox.acroField.getWidgets();
|
|
474
|
+
for (const w of checkboxWidgets) {
|
|
475
|
+
w.dict.set(FormPDFName.of('AS'), FormPDFName.of('Yes'));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// checkbox (unchecked)
|
|
479
|
+
const checkboxUnchecked = form.createCheckBox('newsletter');
|
|
480
|
+
checkboxUnchecked.addToPage(page, { x: 50, y: 570, width: 16, height: 16 });
|
|
481
|
+
|
|
482
|
+
// radio group
|
|
483
|
+
const radioGroup = form.createRadioGroup('color');
|
|
484
|
+
radioGroup.addOptionToPage('red', page, { x: 50, y: 530, width: 16, height: 16 });
|
|
485
|
+
radioGroup.addOptionToPage('green', page, { x: 100, y: 530, width: 16, height: 16 });
|
|
486
|
+
radioGroup.addOptionToPage('blue', page, { x: 150, y: 530, width: 16, height: 16 });
|
|
487
|
+
radioGroup.select('green');
|
|
488
|
+
|
|
489
|
+
// dropdown (combo box) with options
|
|
490
|
+
const dropdown = form.createDropdown('country');
|
|
491
|
+
dropdown.setOptions(['Germany', 'France', 'Italy', 'Spain']);
|
|
492
|
+
dropdown.select('France');
|
|
493
|
+
dropdown.addToPage(page, { x: 50, y: 480, width: 200, height: 24 });
|
|
494
|
+
|
|
495
|
+
// list box with options
|
|
496
|
+
const listBox = form.createOptionList('fruits');
|
|
497
|
+
listBox.setOptions(['Apple', 'Banana', 'Cherry', 'Date']);
|
|
498
|
+
listBox.select('Cherry');
|
|
499
|
+
listBox.addToPage(page, { x: 50, y: 400, width: 200, height: 60 });
|
|
500
|
+
|
|
501
|
+
return doc.save();
|
|
502
|
+
}
|
|
503
|
+
|
|
205
504
|
// Generate all fixtures
|
|
206
505
|
const fixtures = [
|
|
207
506
|
['metadata.pdf', createMetadataPdf],
|
|
208
507
|
['annotations.pdf', createAnnotationPdf],
|
|
508
|
+
['rich-annotations.pdf', createRichAnnotationPdf],
|
|
209
509
|
['links.pdf', createLinkPdf],
|
|
210
510
|
['bookmarks.pdf', createBookmarkPdf],
|
|
211
511
|
['minimal.pdf', createMinimalPdf],
|
|
212
512
|
['two-page.pdf', createTwoPagePdf],
|
|
213
513
|
['text.pdf', createTextPdf],
|
|
514
|
+
['rotated-page.pdf', createRotatedPagePdf],
|
|
515
|
+
['page-boxes.pdf', createPageBoxesPdf],
|
|
516
|
+
['tagged.pdf', createTaggedPdf],
|
|
517
|
+
['image.pdf', createImagePdf],
|
|
518
|
+
['rich-bookmarks.pdf', createRichBookmarkPdf],
|
|
519
|
+
['border-annotations.pdf', createBorderAnnotationPdf],
|
|
520
|
+
['form-fields.pdf', createFormFieldsPdf],
|
|
214
521
|
];
|
|
215
522
|
|
|
216
523
|
for (const [name, generator] of fixtures) {
|
package/scripts/install.mjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Falls back to compiling from source if no prebuilt is available.
|
|
4
4
|
*
|
|
5
5
|
* Prebuilt tarball naming: pdfium-v{version}-{platform}-{arch}.tar.gz
|
|
6
|
+
* Musl (Alpine) naming: pdfium-v{version}-linux-musl-{arch}.tar.gz
|
|
6
7
|
* Contents: build/Release/pdfium.node + build/Release/libpdfium.{dylib,so,dll}
|
|
7
8
|
*/
|
|
8
9
|
import { existsSync, mkdirSync, createWriteStream, readFileSync, unlinkSync } from 'node:fs';
|
|
@@ -20,9 +21,26 @@ if (!/^\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?$/.test(version)) {
|
|
|
20
21
|
process.exit(1);
|
|
21
22
|
}
|
|
22
23
|
|
|
24
|
+
// detect musl libc (Alpine, Void, etc.)
|
|
25
|
+
function isMusl() {
|
|
26
|
+
if (process.platform !== 'linux') return false;
|
|
27
|
+
try {
|
|
28
|
+
const ldd = execSync('ldd --version 2>&1 || true', { encoding: 'utf8' });
|
|
29
|
+
return ldd.toLowerCase().includes('musl');
|
|
30
|
+
} catch {
|
|
31
|
+
return (
|
|
32
|
+
existsSync('/lib/ld-musl-x86_64.so.1') ||
|
|
33
|
+
existsSync('/lib/ld-musl-aarch64.so.1') ||
|
|
34
|
+
existsSync('/lib/ld-musl-armhf.so.1')
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
23
39
|
const platform = process.platform;
|
|
24
40
|
const arch = process.arch;
|
|
25
|
-
const
|
|
41
|
+
const musl = isMusl();
|
|
42
|
+
const platformKey = musl ? `${platform}-musl` : platform;
|
|
43
|
+
const tarName = `pdfium-v${version}-${platformKey}-${arch}.tar.gz`;
|
|
26
44
|
|
|
27
45
|
// hardcoded origin ensures the URL can never point to an attacker-controlled host
|
|
28
46
|
const releaseOrigin = 'https://github.com';
|
|
@@ -0,0 +1,274 @@
|
|
|
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_annot.h"
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// GetAnnotationsWorker — async annotation extraction
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
struct AnnotationData {
|
|
17
|
+
std::string type;
|
|
18
|
+
double left = 0, bottom = 0, right = 0, top = 0;
|
|
19
|
+
bool hasBounds = false;
|
|
20
|
+
std::u16string contents;
|
|
21
|
+
unsigned int r = 0, g = 0, b = 0, a = 0;
|
|
22
|
+
bool hasColor = false;
|
|
23
|
+
unsigned int ir = 0, ig = 0, ib = 0, ia = 0;
|
|
24
|
+
bool hasInteriorColor = false;
|
|
25
|
+
float borderHRadius = 0, borderVRadius = 0, borderWidth = 0;
|
|
26
|
+
bool hasBorder = false;
|
|
27
|
+
std::u16string author;
|
|
28
|
+
std::u16string subject;
|
|
29
|
+
std::u16string creationDate;
|
|
30
|
+
std::u16string modDate;
|
|
31
|
+
int flags = 0;
|
|
32
|
+
struct QuadPoints {
|
|
33
|
+
float x1, y1, x2, y2, x3, y3, x4, y4;
|
|
34
|
+
};
|
|
35
|
+
std::vector<QuadPoints> quadPoints;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
class GetAnnotationsWorker : public Napi::AsyncWorker {
|
|
39
|
+
public:
|
|
40
|
+
GetAnnotationsWorker(Napi::Env env, FPDF_PAGE page,
|
|
41
|
+
std::shared_ptr<std::atomic<bool>> pageAlive,
|
|
42
|
+
std::shared_ptr<std::atomic<bool>> docAlive)
|
|
43
|
+
: Napi::AsyncWorker(env), deferred_(Napi::Promise::Deferred::New(env)),
|
|
44
|
+
page_(page), pageAlive_(std::move(pageAlive)),
|
|
45
|
+
docAlive_(std::move(docAlive)) {}
|
|
46
|
+
|
|
47
|
+
Napi::Promise Promise() { return deferred_.Promise(); }
|
|
48
|
+
|
|
49
|
+
protected:
|
|
50
|
+
void Execute() override {
|
|
51
|
+
std::lock_guard<std::mutex> lock(g_pdfium_mutex);
|
|
52
|
+
CHECK_ALIVE();
|
|
53
|
+
|
|
54
|
+
int count = FPDFPage_GetAnnotCount(page_);
|
|
55
|
+
for (int i = 0; i < count; i++) {
|
|
56
|
+
FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page_, i);
|
|
57
|
+
if (!annot)
|
|
58
|
+
continue;
|
|
59
|
+
|
|
60
|
+
AnnotationData data;
|
|
61
|
+
|
|
62
|
+
FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot);
|
|
63
|
+
switch (subtype) {
|
|
64
|
+
case FPDF_ANNOT_TEXT:
|
|
65
|
+
data.type = "text";
|
|
66
|
+
break;
|
|
67
|
+
case FPDF_ANNOT_LINK:
|
|
68
|
+
data.type = "link";
|
|
69
|
+
break;
|
|
70
|
+
case FPDF_ANNOT_FREETEXT:
|
|
71
|
+
data.type = "freetext";
|
|
72
|
+
break;
|
|
73
|
+
case FPDF_ANNOT_LINE:
|
|
74
|
+
data.type = "line";
|
|
75
|
+
break;
|
|
76
|
+
case FPDF_ANNOT_SQUARE:
|
|
77
|
+
data.type = "square";
|
|
78
|
+
break;
|
|
79
|
+
case FPDF_ANNOT_CIRCLE:
|
|
80
|
+
data.type = "circle";
|
|
81
|
+
break;
|
|
82
|
+
case FPDF_ANNOT_POLYGON:
|
|
83
|
+
data.type = "polygon";
|
|
84
|
+
break;
|
|
85
|
+
case FPDF_ANNOT_POLYLINE:
|
|
86
|
+
data.type = "polyline";
|
|
87
|
+
break;
|
|
88
|
+
case FPDF_ANNOT_HIGHLIGHT:
|
|
89
|
+
data.type = "highlight";
|
|
90
|
+
break;
|
|
91
|
+
case FPDF_ANNOT_UNDERLINE:
|
|
92
|
+
data.type = "underline";
|
|
93
|
+
break;
|
|
94
|
+
case FPDF_ANNOT_SQUIGGLY:
|
|
95
|
+
data.type = "squiggly";
|
|
96
|
+
break;
|
|
97
|
+
case FPDF_ANNOT_STRIKEOUT:
|
|
98
|
+
data.type = "strikeout";
|
|
99
|
+
break;
|
|
100
|
+
case FPDF_ANNOT_STAMP:
|
|
101
|
+
data.type = "stamp";
|
|
102
|
+
break;
|
|
103
|
+
case FPDF_ANNOT_CARET:
|
|
104
|
+
data.type = "caret";
|
|
105
|
+
break;
|
|
106
|
+
case FPDF_ANNOT_INK:
|
|
107
|
+
data.type = "ink";
|
|
108
|
+
break;
|
|
109
|
+
case FPDF_ANNOT_POPUP:
|
|
110
|
+
data.type = "popup";
|
|
111
|
+
break;
|
|
112
|
+
case FPDF_ANNOT_FILEATTACHMENT:
|
|
113
|
+
data.type = "fileattachment";
|
|
114
|
+
break;
|
|
115
|
+
case FPDF_ANNOT_SOUND:
|
|
116
|
+
data.type = "sound";
|
|
117
|
+
break;
|
|
118
|
+
case FPDF_ANNOT_WIDGET:
|
|
119
|
+
data.type = "widget";
|
|
120
|
+
break;
|
|
121
|
+
case FPDF_ANNOT_REDACT:
|
|
122
|
+
data.type = "redact";
|
|
123
|
+
break;
|
|
124
|
+
case FPDF_ANNOT_WATERMARK:
|
|
125
|
+
data.type = "watermark";
|
|
126
|
+
break;
|
|
127
|
+
default:
|
|
128
|
+
data.type = "unknown";
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
FS_RECTF rect;
|
|
133
|
+
if (FPDFAnnot_GetRect(annot, &rect)) {
|
|
134
|
+
data.hasBounds = true;
|
|
135
|
+
data.left = rect.left;
|
|
136
|
+
data.bottom = rect.bottom;
|
|
137
|
+
data.right = rect.right;
|
|
138
|
+
data.top = rect.top;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// helper to read annotation string values via ReadU16
|
|
142
|
+
auto readAnnotStr = [&](const char *key) {
|
|
143
|
+
return ReadU16(
|
|
144
|
+
[&](auto *, unsigned long) {
|
|
145
|
+
return FPDFAnnot_GetStringValue(annot, key, nullptr, 0);
|
|
146
|
+
},
|
|
147
|
+
[&](FPDF_WCHAR *buf, unsigned long len) {
|
|
148
|
+
return FPDFAnnot_GetStringValue(annot, key, buf, len);
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
data.contents = readAnnotStr("Contents");
|
|
153
|
+
|
|
154
|
+
unsigned int cr, cg, cb, ca;
|
|
155
|
+
if (FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &cr, &cg, &cb,
|
|
156
|
+
&ca)) {
|
|
157
|
+
data.hasColor = true;
|
|
158
|
+
data.r = cr;
|
|
159
|
+
data.g = cg;
|
|
160
|
+
data.b = cb;
|
|
161
|
+
data.a = ca;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// interior color (fill color for markup annotations)
|
|
165
|
+
unsigned int icr, icg, icb, ica;
|
|
166
|
+
if (FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_InteriorColor, &icr,
|
|
167
|
+
&icg, &icb, &ica)) {
|
|
168
|
+
data.hasInteriorColor = true;
|
|
169
|
+
data.ir = icr;
|
|
170
|
+
data.ig = icg;
|
|
171
|
+
data.ib = icb;
|
|
172
|
+
data.ia = ica;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// border style
|
|
176
|
+
float hRadius, vRadius, bWidth;
|
|
177
|
+
if (FPDFAnnot_GetBorder(annot, &hRadius, &vRadius, &bWidth)) {
|
|
178
|
+
data.hasBorder = true;
|
|
179
|
+
data.borderHRadius = hRadius;
|
|
180
|
+
data.borderVRadius = vRadius;
|
|
181
|
+
data.borderWidth = bWidth;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// quad points (for highlight, underline, squiggly, strikeout, link,
|
|
185
|
+
// redact)
|
|
186
|
+
if (FPDFAnnot_HasAttachmentPoints(annot)) {
|
|
187
|
+
size_t qpCount = FPDFAnnot_CountAttachmentPoints(annot);
|
|
188
|
+
for (size_t qi = 0; qi < qpCount; qi++) {
|
|
189
|
+
FS_QUADPOINTSF qp;
|
|
190
|
+
if (FPDFAnnot_GetAttachmentPoints(annot, qi, &qp)) {
|
|
191
|
+
data.quadPoints.push_back(
|
|
192
|
+
{qp.x1, qp.y1, qp.x2, qp.y2, qp.x3, qp.y3, qp.x4, qp.y4});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
data.author = readAnnotStr("T");
|
|
198
|
+
data.subject = readAnnotStr("Subj");
|
|
199
|
+
data.creationDate = readAnnotStr("CreationDate");
|
|
200
|
+
data.modDate = readAnnotStr("M");
|
|
201
|
+
|
|
202
|
+
// annotation flags
|
|
203
|
+
data.flags = FPDFAnnot_GetFlags(annot);
|
|
204
|
+
|
|
205
|
+
FPDFPage_CloseAnnot(annot);
|
|
206
|
+
annotations_.push_back(std::move(data));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
void OnOK() override {
|
|
211
|
+
Napi::Env env = Env();
|
|
212
|
+
Napi::Array arr = Napi::Array::New(env, annotations_.size());
|
|
213
|
+
for (uint32_t i = 0; i < annotations_.size(); i++) {
|
|
214
|
+
auto &d = annotations_[i];
|
|
215
|
+
Napi::Object obj = Napi::Object::New(env);
|
|
216
|
+
obj.Set("type", Napi::String::New(env, d.type));
|
|
217
|
+
if (d.hasBounds)
|
|
218
|
+
obj.Set("bounds",
|
|
219
|
+
CreateBoundsObject(env, d.left, d.bottom, d.right, d.top));
|
|
220
|
+
SetU16(obj, "contents", env, d.contents);
|
|
221
|
+
if (d.hasColor) {
|
|
222
|
+
obj.Set("color", CreateColorObject(env, d.r, d.g, d.b, d.a));
|
|
223
|
+
} else {
|
|
224
|
+
obj.Set("color", env.Null());
|
|
225
|
+
}
|
|
226
|
+
if (d.hasInteriorColor) {
|
|
227
|
+
obj.Set("interiorColor",
|
|
228
|
+
CreateColorObject(env, d.ir, d.ig, d.ib, d.ia));
|
|
229
|
+
}
|
|
230
|
+
SetU16(obj, "author", env, d.author);
|
|
231
|
+
SetU16(obj, "subject", env, d.subject);
|
|
232
|
+
SetU16(obj, "creationDate", env, d.creationDate);
|
|
233
|
+
SetU16(obj, "modDate", env, d.modDate);
|
|
234
|
+
obj.Set("flags", Napi::Number::New(env, d.flags));
|
|
235
|
+
if (d.hasBorder) {
|
|
236
|
+
Napi::Object border = Napi::Object::New(env);
|
|
237
|
+
border.Set("horizontalRadius", Napi::Number::New(env, d.borderHRadius));
|
|
238
|
+
border.Set("verticalRadius", Napi::Number::New(env, d.borderVRadius));
|
|
239
|
+
border.Set("width", Napi::Number::New(env, d.borderWidth));
|
|
240
|
+
obj.Set("border", border);
|
|
241
|
+
}
|
|
242
|
+
if (!d.quadPoints.empty()) {
|
|
243
|
+
Napi::Array qpArr = Napi::Array::New(env, d.quadPoints.size());
|
|
244
|
+
for (uint32_t qi = 0; qi < d.quadPoints.size(); qi++) {
|
|
245
|
+
auto &qp = d.quadPoints[qi];
|
|
246
|
+
Napi::Object qpObj = Napi::Object::New(env);
|
|
247
|
+
qpObj.Set("x1", Napi::Number::New(env, qp.x1));
|
|
248
|
+
qpObj.Set("y1", Napi::Number::New(env, qp.y1));
|
|
249
|
+
qpObj.Set("x2", Napi::Number::New(env, qp.x2));
|
|
250
|
+
qpObj.Set("y2", Napi::Number::New(env, qp.y2));
|
|
251
|
+
qpObj.Set("x3", Napi::Number::New(env, qp.x3));
|
|
252
|
+
qpObj.Set("y3", Napi::Number::New(env, qp.y3));
|
|
253
|
+
qpObj.Set("x4", Napi::Number::New(env, qp.x4));
|
|
254
|
+
qpObj.Set("y4", Napi::Number::New(env, qp.y4));
|
|
255
|
+
qpArr.Set(qi, qpObj);
|
|
256
|
+
}
|
|
257
|
+
obj.Set("quadPoints", qpArr);
|
|
258
|
+
}
|
|
259
|
+
arr.Set(i, obj);
|
|
260
|
+
}
|
|
261
|
+
deferred_.Resolve(arr);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
void OnError(const Napi::Error &err) override {
|
|
265
|
+
deferred_.Reject(err.Value());
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
private:
|
|
269
|
+
Napi::Promise::Deferred deferred_;
|
|
270
|
+
FPDF_PAGE page_;
|
|
271
|
+
std::shared_ptr<std::atomic<bool>> pageAlive_;
|
|
272
|
+
std::shared_ptr<std::atomic<bool>> docAlive_;
|
|
273
|
+
std::vector<AnnotationData> annotations_;
|
|
274
|
+
};
|