@adnsistemas/pdf-lib 2.6.4 → 2.7.0
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 +44 -0
- package/cjs/api/PDFDocument.d.ts +22 -0
- package/cjs/api/PDFDocument.d.ts.map +1 -1
- package/cjs/api/PDFDocument.js +45 -0
- package/cjs/api/PDFDocument.js.map +1 -1
- package/cjs/api/PDFFont.d.ts +1 -1
- package/cjs/api/PDFFont.d.ts.map +1 -1
- package/cjs/api/PDFFont.js +3 -5
- package/cjs/api/PDFFont.js.map +1 -1
- package/cjs/core/PDFContext.d.ts +2 -0
- package/cjs/core/PDFContext.d.ts.map +1 -1
- package/cjs/core/PDFContext.js +43 -2
- package/cjs/core/PDFContext.js.map +1 -1
- package/cjs/core/objects/PDFArray.d.ts +1 -0
- package/cjs/core/objects/PDFArray.d.ts.map +1 -1
- package/cjs/core/objects/PDFArray.js +7 -0
- package/cjs/core/objects/PDFArray.js.map +1 -1
- package/cjs/core/objects/PDFRawStream.d.ts.map +1 -1
- package/cjs/core/objects/PDFRawStream.js +1 -0
- package/cjs/core/objects/PDFRawStream.js.map +1 -1
- package/dist/pdf-lib.esm.js +99 -7
- package/dist/pdf-lib.esm.js.map +1 -1
- package/dist/pdf-lib.esm.min.js +2 -2
- package/dist/pdf-lib.esm.min.js.map +1 -1
- package/dist/pdf-lib.js +99 -7
- package/dist/pdf-lib.js.map +1 -1
- package/dist/pdf-lib.min.js +2 -2
- package/dist/pdf-lib.min.js.map +1 -1
- package/es/api/PDFDocument.d.ts +22 -0
- package/es/api/PDFDocument.d.ts.map +1 -1
- package/es/api/PDFDocument.js +45 -0
- package/es/api/PDFDocument.js.map +1 -1
- package/es/api/PDFFont.d.ts +1 -1
- package/es/api/PDFFont.d.ts.map +1 -1
- package/es/api/PDFFont.js +3 -5
- package/es/api/PDFFont.js.map +1 -1
- package/es/core/PDFContext.d.ts +2 -0
- package/es/core/PDFContext.d.ts.map +1 -1
- package/es/core/PDFContext.js +43 -2
- package/es/core/PDFContext.js.map +1 -1
- package/es/core/objects/PDFArray.d.ts +1 -0
- package/es/core/objects/PDFArray.d.ts.map +1 -1
- package/es/core/objects/PDFArray.js +7 -0
- package/es/core/objects/PDFArray.js.map +1 -1
- package/es/core/objects/PDFRawStream.d.ts.map +1 -1
- package/es/core/objects/PDFRawStream.js +1 -0
- package/es/core/objects/PDFRawStream.js.map +1 -1
- package/package.json +3 -2
- package/src/api/PDFDocument.ts +60 -0
- package/src/api/PDFFont.ts +3 -5
- package/src/core/PDFContext.ts +42 -1
- package/src/core/objects/PDFArray.ts +8 -0
- package/src/core/objects/PDFRawStream.ts +1 -0
- package/ts3.4/cjs/api/PDFDocument.d.ts +22 -0
- package/ts3.4/cjs/api/PDFFont.d.ts +1 -1
- package/ts3.4/cjs/core/PDFContext.d.ts +2 -0
- package/ts3.4/cjs/core/objects/PDFArray.d.ts +1 -0
- package/ts3.4/es/api/PDFDocument.d.ts +22 -0
- package/ts3.4/es/api/PDFFont.d.ts +1 -1
- package/ts3.4/es/core/PDFContext.d.ts +2 -0
- package/ts3.4/es/core/objects/PDFArray.d.ts +1 -0
package/src/api/PDFDocument.ts
CHANGED
|
@@ -1697,6 +1697,66 @@ export default class PDFDocument {
|
|
|
1697
1697
|
.filter((ov) => ov.actual || ov.previous.length);
|
|
1698
1698
|
}
|
|
1699
1699
|
|
|
1700
|
+
/**
|
|
1701
|
+
* Saves the current changes to the document as an incremental update, returns the full document,
|
|
1702
|
+
* like save method, and modifies the internal state to be able to continue editing the document
|
|
1703
|
+
* for another incremental update.
|
|
1704
|
+
* This allows you to save multiple incremental updates without reloading the PDF.
|
|
1705
|
+
*
|
|
1706
|
+
* For example:
|
|
1707
|
+
* ```js
|
|
1708
|
+
* const pdfDoc = await PDFDocument.load(pdfBytes, { forIncrementalUpdate: true })
|
|
1709
|
+
*
|
|
1710
|
+
* const page = pdfDoc.getPage(0)
|
|
1711
|
+
* page.drawText('First update')
|
|
1712
|
+
* const firstsave = await pdfDoc.saveAndContinue()
|
|
1713
|
+
*
|
|
1714
|
+
* page.drawText('Second update', { y: 100 })
|
|
1715
|
+
* const secondsave = await pdfDoc.saveAndContinue()
|
|
1716
|
+
* ```
|
|
1717
|
+
*
|
|
1718
|
+
* @param options The options to be used when saving changes.
|
|
1719
|
+
* @returns Resolves with the complete PDF bytes including all updates.
|
|
1720
|
+
*/
|
|
1721
|
+
async saveAndContinue(
|
|
1722
|
+
options: IncrementalSaveOptions = {},
|
|
1723
|
+
): Promise<Uint8Array> {
|
|
1724
|
+
if (!this.context.pdfFileDetails.originalBytes || !this.context.snapshot) {
|
|
1725
|
+
throw new Error(
|
|
1726
|
+
'saveAndContinue() requires the document to be loaded with forIncrementalUpdate: true',
|
|
1727
|
+
);
|
|
1728
|
+
}
|
|
1729
|
+
const originalBytes = this.context.pdfFileDetails.originalBytes;
|
|
1730
|
+
const incrementalBytes = await this.saveIncremental(
|
|
1731
|
+
this.context.snapshot,
|
|
1732
|
+
options,
|
|
1733
|
+
);
|
|
1734
|
+
|
|
1735
|
+
const newPdfBytes = new Uint8Array(
|
|
1736
|
+
originalBytes.byteLength + incrementalBytes.byteLength,
|
|
1737
|
+
);
|
|
1738
|
+
newPdfBytes.set(originalBytes);
|
|
1739
|
+
newPdfBytes.set(incrementalBytes, originalBytes.byteLength);
|
|
1740
|
+
|
|
1741
|
+
this.context.pdfFileDetails.originalBytes = newPdfBytes;
|
|
1742
|
+
this.context.pdfFileDetails.pdfSize = newPdfBytes.byteLength;
|
|
1743
|
+
|
|
1744
|
+
const incrementalStr = new TextDecoder('latin1').decode(incrementalBytes);
|
|
1745
|
+
const startxrefMatch = incrementalStr.match(/startxref\s+(\d+)/);
|
|
1746
|
+
if (startxrefMatch) {
|
|
1747
|
+
this.context.pdfFileDetails.prevStartXRef = parseInt(
|
|
1748
|
+
startxrefMatch[1],
|
|
1749
|
+
10,
|
|
1750
|
+
);
|
|
1751
|
+
} else {
|
|
1752
|
+
this.context.pdfFileDetails.prevStartXRef = originalBytes.byteLength;
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
this.context.snapshot = this.takeSnapshot();
|
|
1756
|
+
|
|
1757
|
+
return newPdfBytes;
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1700
1760
|
private async prepareForSave(options: SaveOptions): Promise<void> {
|
|
1701
1761
|
const { addDefaultPage = true, updateFieldAppearances = true } = options;
|
|
1702
1762
|
|
package/src/api/PDFFont.ts
CHANGED
|
@@ -38,7 +38,7 @@ export default class PDFFont implements Embeddable {
|
|
|
38
38
|
/** The name of this font. */
|
|
39
39
|
readonly name: string;
|
|
40
40
|
|
|
41
|
-
private
|
|
41
|
+
private alreadyEmbedded = false;
|
|
42
42
|
private readonly embedder: FontEmbedder;
|
|
43
43
|
|
|
44
44
|
private constructor(ref: PDFRef, doc: PDFDocument, embedder: FontEmbedder) {
|
|
@@ -68,7 +68,6 @@ export default class PDFFont implements Embeddable {
|
|
|
68
68
|
*/
|
|
69
69
|
encodeText(text: string): PDFHexString {
|
|
70
70
|
assertIs(text, 'text', ['string']);
|
|
71
|
-
this.modified = true;
|
|
72
71
|
return this.embedder.encodeText(text);
|
|
73
72
|
}
|
|
74
73
|
|
|
@@ -145,10 +144,9 @@ export default class PDFFont implements Embeddable {
|
|
|
145
144
|
* @returns Resolves when the embedding is complete.
|
|
146
145
|
*/
|
|
147
146
|
async embed(): Promise<void> {
|
|
148
|
-
|
|
149
|
-
if (this.modified) {
|
|
147
|
+
if (!this.alreadyEmbedded) {
|
|
150
148
|
await this.embedder.embedIntoContext(this.doc.context, this.ref);
|
|
151
|
-
this.
|
|
149
|
+
this.alreadyEmbedded = true;
|
|
152
150
|
}
|
|
153
151
|
}
|
|
154
152
|
}
|
package/src/core/PDFContext.ts
CHANGED
|
@@ -429,7 +429,48 @@ class PDFContext {
|
|
|
429
429
|
}
|
|
430
430
|
|
|
431
431
|
registerObjectChange(obj: PDFObject) {
|
|
432
|
-
if (this.snapshot)
|
|
432
|
+
if (!this.snapshot) return;
|
|
433
|
+
|
|
434
|
+
const ref = this.getObjectRef(obj);
|
|
435
|
+
if (ref) {
|
|
436
|
+
this.snapshot.markRefForSave(ref);
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const containingRef = this.findContainingIndirectObject(obj);
|
|
441
|
+
if (containingRef) {
|
|
442
|
+
this.snapshot.markRefForSave(containingRef);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
private findContainingIndirectObject(target: PDFObject): PDFRef | undefined {
|
|
447
|
+
const entries = Array.from(this.indirectObjects.entries());
|
|
448
|
+
for (let idx = 0, len = entries.length; idx < len; idx++) {
|
|
449
|
+
const [ref, object] = entries[idx];
|
|
450
|
+
if (this.objectContains(object, target)) {
|
|
451
|
+
return ref;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return undefined;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
private objectContains(container: PDFObject, target: PDFObject): boolean {
|
|
458
|
+
if (container === target) return true;
|
|
459
|
+
|
|
460
|
+
if (container instanceof PDFDict) {
|
|
461
|
+
const values = container.values();
|
|
462
|
+
for (let i = 0, len = values.length; i < len; i++) {
|
|
463
|
+
if (this.objectContains(values[i], target)) return true;
|
|
464
|
+
}
|
|
465
|
+
} else if (container instanceof PDFArray) {
|
|
466
|
+
for (let i = 0, len = container.size(); i < len; i++) {
|
|
467
|
+
if (this.objectContains(container.get(i), target)) return true;
|
|
468
|
+
}
|
|
469
|
+
} else if (container instanceof PDFStream) {
|
|
470
|
+
if (this.objectContains(container.dict, target)) return true;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return false;
|
|
433
474
|
}
|
|
434
475
|
|
|
435
476
|
getObjectVersions(ref: PDFRef): PDFObject[] {
|
|
@@ -30,10 +30,12 @@ class PDFArray extends PDFObject {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
push(object: PDFObject): void {
|
|
33
|
+
this.registerChange();
|
|
33
34
|
this.array.push(object);
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
insert(index: number, object: PDFObject): void {
|
|
38
|
+
this.registerChange();
|
|
37
39
|
this.array.splice(index, 0, object);
|
|
38
40
|
}
|
|
39
41
|
|
|
@@ -43,10 +45,12 @@ class PDFArray extends PDFObject {
|
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
remove(index: number): void {
|
|
48
|
+
this.registerChange();
|
|
46
49
|
this.array.splice(index, 1);
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
set(idx: number, object: PDFObject): void {
|
|
53
|
+
this.registerChange();
|
|
50
54
|
this.array[idx] = object;
|
|
51
55
|
}
|
|
52
56
|
|
|
@@ -180,6 +184,10 @@ class PDFArray extends PDFObject {
|
|
|
180
184
|
}
|
|
181
185
|
}
|
|
182
186
|
}
|
|
187
|
+
|
|
188
|
+
registerChange(): void {
|
|
189
|
+
this.context.registerObjectChange(this);
|
|
190
|
+
}
|
|
183
191
|
}
|
|
184
192
|
|
|
185
193
|
export default PDFArray;
|
|
@@ -842,6 +842,28 @@ export default class PDFDocument {
|
|
|
842
842
|
* @returns {PDFObjectVersions[]} Objects modified in the update, and previous versions
|
|
843
843
|
*/
|
|
844
844
|
getChangedObjects(lastUpdateMinusX?: number): PDFObjectVersions[];
|
|
845
|
+
/**
|
|
846
|
+
* Saves the current changes to the document as an incremental update, returns the full document,
|
|
847
|
+
* like save method, and modifies the internal state to be able to continue editing the document
|
|
848
|
+
* for another incremental update.
|
|
849
|
+
* This allows you to save multiple incremental updates without reloading the PDF.
|
|
850
|
+
*
|
|
851
|
+
* For example:
|
|
852
|
+
* ```js
|
|
853
|
+
* const pdfDoc = await PDFDocument.load(pdfBytes, { forIncrementalUpdate: true })
|
|
854
|
+
*
|
|
855
|
+
* const page = pdfDoc.getPage(0)
|
|
856
|
+
* page.drawText('First update')
|
|
857
|
+
* const firstsave = await pdfDoc.saveAndContinue()
|
|
858
|
+
*
|
|
859
|
+
* page.drawText('Second update', { y: 100 })
|
|
860
|
+
* const secondsave = await pdfDoc.saveAndContinue()
|
|
861
|
+
* ```
|
|
862
|
+
*
|
|
863
|
+
* @param options The options to be used when saving changes.
|
|
864
|
+
* @returns Resolves with the complete PDF bytes including all updates.
|
|
865
|
+
*/
|
|
866
|
+
saveAndContinue(options?: IncrementalSaveOptions): Promise<Uint8Array>;
|
|
845
867
|
private prepareForSave;
|
|
846
868
|
private embedAll;
|
|
847
869
|
private updateInfoDict;
|
|
@@ -119,6 +119,8 @@ declare class PDFContext {
|
|
|
119
119
|
getPopGraphicsStateContentStream(): PDFRef;
|
|
120
120
|
addRandomSuffix(prefix: string, suffixLength?: number): string;
|
|
121
121
|
registerObjectChange(obj: PDFObject): void;
|
|
122
|
+
private findContainingIndirectObject;
|
|
123
|
+
private objectContains;
|
|
122
124
|
getObjectVersions(ref: PDFRef): PDFObject[];
|
|
123
125
|
listXrefEntries(xrefIndex?: number): Entry[];
|
|
124
126
|
}
|
|
@@ -59,6 +59,7 @@ declare class PDFArray extends PDFObject {
|
|
|
59
59
|
sizeInBytes(): number;
|
|
60
60
|
copyBytesInto(buffer: Uint8Array, offset: number): number;
|
|
61
61
|
scalePDFNumbers(x: number, y: number): void;
|
|
62
|
+
registerChange(): void;
|
|
62
63
|
}
|
|
63
64
|
export default PDFArray;
|
|
64
65
|
//# sourceMappingURL=PDFArray.d.ts.map
|
|
@@ -842,6 +842,28 @@ export default class PDFDocument {
|
|
|
842
842
|
* @returns {PDFObjectVersions[]} Objects modified in the update, and previous versions
|
|
843
843
|
*/
|
|
844
844
|
getChangedObjects(lastUpdateMinusX?: number): PDFObjectVersions[];
|
|
845
|
+
/**
|
|
846
|
+
* Saves the current changes to the document as an incremental update, returns the full document,
|
|
847
|
+
* like save method, and modifies the internal state to be able to continue editing the document
|
|
848
|
+
* for another incremental update.
|
|
849
|
+
* This allows you to save multiple incremental updates without reloading the PDF.
|
|
850
|
+
*
|
|
851
|
+
* For example:
|
|
852
|
+
* ```js
|
|
853
|
+
* const pdfDoc = await PDFDocument.load(pdfBytes, { forIncrementalUpdate: true })
|
|
854
|
+
*
|
|
855
|
+
* const page = pdfDoc.getPage(0)
|
|
856
|
+
* page.drawText('First update')
|
|
857
|
+
* const firstsave = await pdfDoc.saveAndContinue()
|
|
858
|
+
*
|
|
859
|
+
* page.drawText('Second update', { y: 100 })
|
|
860
|
+
* const secondsave = await pdfDoc.saveAndContinue()
|
|
861
|
+
* ```
|
|
862
|
+
*
|
|
863
|
+
* @param options The options to be used when saving changes.
|
|
864
|
+
* @returns Resolves with the complete PDF bytes including all updates.
|
|
865
|
+
*/
|
|
866
|
+
saveAndContinue(options?: IncrementalSaveOptions): Promise<Uint8Array>;
|
|
845
867
|
private prepareForSave;
|
|
846
868
|
private embedAll;
|
|
847
869
|
private updateInfoDict;
|
|
@@ -119,6 +119,8 @@ declare class PDFContext {
|
|
|
119
119
|
getPopGraphicsStateContentStream(): PDFRef;
|
|
120
120
|
addRandomSuffix(prefix: string, suffixLength?: number): string;
|
|
121
121
|
registerObjectChange(obj: PDFObject): void;
|
|
122
|
+
private findContainingIndirectObject;
|
|
123
|
+
private objectContains;
|
|
122
124
|
getObjectVersions(ref: PDFRef): PDFObject[];
|
|
123
125
|
listXrefEntries(xrefIndex?: number): Entry[];
|
|
124
126
|
}
|
|
@@ -59,6 +59,7 @@ declare class PDFArray extends PDFObject {
|
|
|
59
59
|
sizeInBytes(): number;
|
|
60
60
|
copyBytesInto(buffer: Uint8Array, offset: number): number;
|
|
61
61
|
scalePDFNumbers(x: number, y: number): void;
|
|
62
|
+
registerChange(): void;
|
|
62
63
|
}
|
|
63
64
|
export default PDFArray;
|
|
64
65
|
//# sourceMappingURL=PDFArray.d.ts.map
|