@pdfme/pdf-lib 1.18.1 → 1.18.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.
Files changed (143) hide show
  1. package/README.md +11 -2
  2. package/cjs/api/PDFDocument.js +195 -225
  3. package/cjs/api/PDFDocument.js.map +1 -1
  4. package/cjs/api/PDFEmbeddedFile.js +30 -33
  5. package/cjs/api/PDFEmbeddedFile.js.map +1 -1
  6. package/cjs/api/PDFEmbeddedPage.js +5 -7
  7. package/cjs/api/PDFEmbeddedPage.js.map +1 -1
  8. package/cjs/api/PDFFont.js +6 -8
  9. package/cjs/api/PDFFont.js.map +1 -1
  10. package/cjs/api/PDFImage.js +14 -16
  11. package/cjs/api/PDFImage.js.map +1 -1
  12. package/cjs/api/PDFJavaScript.js +19 -22
  13. package/cjs/api/PDFJavaScript.js.map +1 -1
  14. package/cjs/api/PDFPage.d.ts +1 -0
  15. package/cjs/api/PDFPage.d.ts.map +1 -1
  16. package/cjs/api/PDFPage.js +30 -17
  17. package/cjs/api/PDFPage.js.map +1 -1
  18. package/cjs/api/PDFPageOptions.d.ts +15 -8
  19. package/cjs/api/PDFPageOptions.d.ts.map +1 -1
  20. package/cjs/api/PDFPageOptions.js.map +1 -1
  21. package/cjs/api/form/PDFField.js +1 -1
  22. package/cjs/api/form/PDFField.js.map +1 -1
  23. package/cjs/api/form/PDFForm.js +1 -1
  24. package/cjs/api/form/PDFForm.js.map +1 -1
  25. package/cjs/api/form/appearances.js +56 -16
  26. package/cjs/api/form/appearances.js.map +1 -1
  27. package/cjs/api/operations.d.ts +16 -0
  28. package/cjs/api/operations.d.ts.map +1 -1
  29. package/cjs/api/operations.js +53 -5
  30. package/cjs/api/operations.js.map +1 -1
  31. package/cjs/api/svg.d.ts +7 -1
  32. package/cjs/api/svg.d.ts.map +1 -1
  33. package/cjs/api/svg.js +332 -1016
  34. package/cjs/api/svg.js.map +1 -1
  35. package/cjs/core/PDFContext.js +11 -2
  36. package/cjs/core/PDFContext.js.map +1 -1
  37. package/cjs/core/embedders/CustomFontEmbedder.js +62 -74
  38. package/cjs/core/embedders/CustomFontEmbedder.js.map +1 -1
  39. package/cjs/core/embedders/CustomFontSubsetEmbedder.js +3 -5
  40. package/cjs/core/embedders/CustomFontSubsetEmbedder.js.map +1 -1
  41. package/cjs/core/embedders/FileEmbedder.js +30 -32
  42. package/cjs/core/embedders/FileEmbedder.js.map +1 -1
  43. package/cjs/core/embedders/JavaScriptEmbedder.js +12 -14
  44. package/cjs/core/embedders/JavaScriptEmbedder.js.map +1 -1
  45. package/cjs/core/embedders/JpegEmbedder.js +54 -59
  46. package/cjs/core/embedders/JpegEmbedder.js.map +1 -1
  47. package/cjs/core/embedders/PDFPageEmbedder.js +22 -26
  48. package/cjs/core/embedders/PDFPageEmbedder.js.map +1 -1
  49. package/cjs/core/embedders/PngEmbedder.js +20 -25
  50. package/cjs/core/embedders/PngEmbedder.js.map +1 -1
  51. package/cjs/core/parser/PDFObjectStreamParser.js +15 -17
  52. package/cjs/core/parser/PDFObjectStreamParser.js.map +1 -1
  53. package/cjs/core/parser/PDFParser.js +66 -74
  54. package/cjs/core/parser/PDFParser.js.map +1 -1
  55. package/cjs/core/writers/PDFStreamWriter.js +53 -55
  56. package/cjs/core/writers/PDFStreamWriter.js.map +1 -1
  57. package/cjs/core/writers/PDFWriter.js +62 -66
  58. package/cjs/core/writers/PDFWriter.js.map +1 -1
  59. package/cjs/types/index.d.ts +4 -4
  60. package/cjs/types/index.d.ts.map +1 -1
  61. package/dist/pdf-lib.esm.js +1153 -2590
  62. package/dist/pdf-lib.esm.js.map +1 -1
  63. package/dist/pdf-lib.esm.min.js +1 -15
  64. package/dist/pdf-lib.esm.min.js.map +1 -1
  65. package/dist/pdf-lib.js +1153 -2590
  66. package/dist/pdf-lib.js.map +1 -1
  67. package/dist/pdf-lib.min.js +1 -15
  68. package/dist/pdf-lib.min.js.map +1 -1
  69. package/es/api/PDFDocument.js +195 -226
  70. package/es/api/PDFDocument.js.map +1 -1
  71. package/es/api/PDFEmbeddedFile.js +30 -33
  72. package/es/api/PDFEmbeddedFile.js.map +1 -1
  73. package/es/api/PDFEmbeddedPage.js +5 -8
  74. package/es/api/PDFEmbeddedPage.js.map +1 -1
  75. package/es/api/PDFFont.js +6 -9
  76. package/es/api/PDFFont.js.map +1 -1
  77. package/es/api/PDFImage.js +14 -17
  78. package/es/api/PDFImage.js.map +1 -1
  79. package/es/api/PDFJavaScript.js +19 -22
  80. package/es/api/PDFJavaScript.js.map +1 -1
  81. package/es/api/PDFPage.d.ts +1 -0
  82. package/es/api/PDFPage.d.ts.map +1 -1
  83. package/es/api/PDFPage.js +30 -18
  84. package/es/api/PDFPage.js.map +1 -1
  85. package/es/api/PDFPageOptions.d.ts +15 -8
  86. package/es/api/PDFPageOptions.d.ts.map +1 -1
  87. package/es/api/PDFPageOptions.js.map +1 -1
  88. package/es/api/form/PDFField.js +1 -1
  89. package/es/api/form/PDFField.js.map +1 -1
  90. package/es/api/form/PDFForm.js +1 -1
  91. package/es/api/form/PDFForm.js.map +1 -1
  92. package/es/api/form/appearances.js +56 -16
  93. package/es/api/form/appearances.js.map +1 -1
  94. package/es/api/operations.d.ts +16 -0
  95. package/es/api/operations.d.ts.map +1 -1
  96. package/es/api/operations.js +54 -6
  97. package/es/api/operations.js.map +1 -1
  98. package/es/api/svg.d.ts +7 -1
  99. package/es/api/svg.d.ts.map +1 -1
  100. package/es/api/svg.js +333 -1017
  101. package/es/api/svg.js.map +1 -1
  102. package/es/core/PDFContext.js +11 -2
  103. package/es/core/PDFContext.js.map +1 -1
  104. package/es/core/embedders/CustomFontEmbedder.js +62 -75
  105. package/es/core/embedders/CustomFontEmbedder.js.map +1 -1
  106. package/es/core/embedders/CustomFontSubsetEmbedder.js +3 -6
  107. package/es/core/embedders/CustomFontSubsetEmbedder.js.map +1 -1
  108. package/es/core/embedders/FileEmbedder.js +30 -33
  109. package/es/core/embedders/FileEmbedder.js.map +1 -1
  110. package/es/core/embedders/JavaScriptEmbedder.js +12 -15
  111. package/es/core/embedders/JavaScriptEmbedder.js.map +1 -1
  112. package/es/core/embedders/JpegEmbedder.js +54 -59
  113. package/es/core/embedders/JpegEmbedder.js.map +1 -1
  114. package/es/core/embedders/PDFPageEmbedder.js +22 -27
  115. package/es/core/embedders/PDFPageEmbedder.js.map +1 -1
  116. package/es/core/embedders/PngEmbedder.js +20 -25
  117. package/es/core/embedders/PngEmbedder.js.map +1 -1
  118. package/es/core/parser/PDFObjectStreamParser.js +15 -18
  119. package/es/core/parser/PDFObjectStreamParser.js.map +1 -1
  120. package/es/core/parser/PDFParser.js +66 -75
  121. package/es/core/parser/PDFParser.js.map +1 -1
  122. package/es/core/writers/PDFStreamWriter.js +53 -56
  123. package/es/core/writers/PDFStreamWriter.js.map +1 -1
  124. package/es/core/writers/PDFWriter.js +62 -67
  125. package/es/core/writers/PDFWriter.js.map +1 -1
  126. package/es/types/index.d.ts +4 -4
  127. package/es/types/index.d.ts.map +1 -1
  128. package/package.json +1 -1
  129. package/src/api/PDFPage.ts +15 -0
  130. package/src/api/PDFPageOptions.ts +15 -8
  131. package/src/api/operations.ts +82 -8
  132. package/src/api/svg.ts +305 -1086
  133. package/src/types/index.ts +6 -1
  134. package/ts3.4/cjs/api/PDFPage.d.ts +1 -0
  135. package/ts3.4/cjs/api/PDFPageOptions.d.ts +15 -8
  136. package/ts3.4/cjs/api/operations.d.ts +16 -0
  137. package/ts3.4/cjs/api/svg.d.ts +7 -1
  138. package/ts3.4/cjs/types/index.d.ts +4 -4
  139. package/ts3.4/es/api/PDFPage.d.ts +1 -0
  140. package/ts3.4/es/api/PDFPageOptions.d.ts +15 -8
  141. package/ts3.4/es/api/operations.d.ts +16 -0
  142. package/ts3.4/es/api/svg.d.ts +7 -1
  143. package/ts3.4/es/types/index.d.ts +4 -4
@@ -1,4 +1,3 @@
1
- import { __awaiter, __rest } from "tslib";
2
1
  import { EncryptedPDFError, FontkitNotRegisteredError, ForeignPageError, RemovePageFromEmptyDocumentError, } from './errors.js';
3
2
  import PDFEmbeddedPage from './PDFEmbeddedPage.js';
4
3
  import PDFFont from './PDFFont.js';
@@ -113,42 +112,38 @@ export default class PDFDocument {
113
112
  * @param options The options to be used when loading the document.
114
113
  * @returns Resolves with a document loaded from the input.
115
114
  */
116
- static load(pdf, options = {}) {
117
- return __awaiter(this, void 0, void 0, function* () {
118
- const { ignoreEncryption = false, parseSpeed = ParseSpeeds.Slow, throwOnInvalidObject = false, updateMetadata = true, capNumbers = false, password, } = options;
119
- assertIs(pdf, 'pdf', ['string', Uint8Array, ArrayBuffer]);
120
- assertIs(ignoreEncryption, 'ignoreEncryption', ['boolean']);
121
- assertIs(parseSpeed, 'parseSpeed', ['number']);
122
- assertIs(throwOnInvalidObject, 'throwOnInvalidObject', ['boolean']);
123
- assertIs(password, 'password', ['string', 'undefined']);
124
- const bytes = toUint8Array(pdf);
125
- const context = yield PDFParser.forBytesWithOptions(bytes, parseSpeed, throwOnInvalidObject, capNumbers).parseDocument();
126
- if (!!context.lookup(context.trailerInfo.Encrypt) && password !== undefined) {
127
- // Decrypt
128
- const fileIds = context.lookup(context.trailerInfo.ID, PDFArray);
129
- const encryptDict = context.lookup(context.trailerInfo.Encrypt, PDFDict);
130
- const decryptedContext = yield PDFParser.forBytesWithOptions(bytes, parseSpeed, throwOnInvalidObject, capNumbers, new CipherTransformFactory(encryptDict, fileIds.get(0).asBytes(), password)).parseDocument();
131
- return new PDFDocument(decryptedContext, true, updateMetadata);
132
- }
133
- else {
134
- return new PDFDocument(context, ignoreEncryption, updateMetadata);
135
- }
136
- });
115
+ static async load(pdf, options = {}) {
116
+ const { ignoreEncryption = false, parseSpeed = ParseSpeeds.Slow, throwOnInvalidObject = false, updateMetadata = true, capNumbers = false, password, } = options;
117
+ assertIs(pdf, 'pdf', ['string', Uint8Array, ArrayBuffer]);
118
+ assertIs(ignoreEncryption, 'ignoreEncryption', ['boolean']);
119
+ assertIs(parseSpeed, 'parseSpeed', ['number']);
120
+ assertIs(throwOnInvalidObject, 'throwOnInvalidObject', ['boolean']);
121
+ assertIs(password, 'password', ['string', 'undefined']);
122
+ const bytes = toUint8Array(pdf);
123
+ const context = await PDFParser.forBytesWithOptions(bytes, parseSpeed, throwOnInvalidObject, capNumbers).parseDocument();
124
+ if (!!context.lookup(context.trailerInfo.Encrypt) && password !== undefined) {
125
+ // Decrypt
126
+ const fileIds = context.lookup(context.trailerInfo.ID, PDFArray);
127
+ const encryptDict = context.lookup(context.trailerInfo.Encrypt, PDFDict);
128
+ const decryptedContext = await PDFParser.forBytesWithOptions(bytes, parseSpeed, throwOnInvalidObject, capNumbers, new CipherTransformFactory(encryptDict, fileIds.get(0).asBytes(), password)).parseDocument();
129
+ return new PDFDocument(decryptedContext, true, updateMetadata);
130
+ }
131
+ else {
132
+ return new PDFDocument(context, ignoreEncryption, updateMetadata);
133
+ }
137
134
  }
138
135
  /**
139
136
  * Create a new [[PDFDocument]].
140
137
  * @returns Resolves with the newly created document.
141
138
  */
142
- static create(options = {}) {
143
- return __awaiter(this, void 0, void 0, function* () {
144
- const { updateMetadata = true } = options;
145
- const context = PDFContext.create();
146
- const pageTree = PDFPageTree.withContext(context);
147
- const pageTreeRef = context.register(pageTree);
148
- const catalog = PDFCatalog.withContextAndPages(context, pageTreeRef);
149
- context.trailerInfo.Root = context.register(catalog);
150
- return new PDFDocument(context, false, updateMetadata);
151
- });
139
+ static async create(options = {}) {
140
+ const { updateMetadata = true } = options;
141
+ const context = PDFContext.create();
142
+ const pageTree = PDFPageTree.withContext(context);
143
+ const pageTreeRef = context.register(pageTree);
144
+ const catalog = PDFCatalog.withContextAndPages(context, pageTreeRef);
145
+ context.trailerInfo.Root = context.register(catalog);
146
+ return new PDFDocument(context, false, updateMetadata);
152
147
  }
153
148
  /**
154
149
  * Register a fontkit instance. This must be done before custom fonts can
@@ -632,22 +627,20 @@ export default class PDFDocument {
632
627
  * @param indices The indices of the pages that should be copied.
633
628
  * @returns Resolves with an array of pages copied into this document.
634
629
  */
635
- copyPages(srcDoc, indices) {
636
- return __awaiter(this, void 0, void 0, function* () {
637
- assertIs(srcDoc, 'srcDoc', [[PDFDocument, 'PDFDocument']]);
638
- assertIs(indices, 'indices', [Array]);
639
- yield srcDoc.flush();
640
- const copier = PDFObjectCopier.for(srcDoc.context, this.context);
641
- const srcPages = srcDoc.getPages();
642
- const copiedPages = new Array(indices.length);
643
- for (let idx = 0, len = indices.length; idx < len; idx++) {
644
- const srcPage = srcPages[indices[idx]];
645
- const copiedPage = copier.copy(srcPage.node);
646
- const ref = this.context.register(copiedPage);
647
- copiedPages[idx] = PDFPage.of(copiedPage, ref, this);
648
- }
649
- return copiedPages;
650
- });
630
+ async copyPages(srcDoc, indices) {
631
+ assertIs(srcDoc, 'srcDoc', [[PDFDocument, 'PDFDocument']]);
632
+ assertIs(indices, 'indices', [Array]);
633
+ await srcDoc.flush();
634
+ const copier = PDFObjectCopier.for(srcDoc.context, this.context);
635
+ const srcPages = srcDoc.getPages();
636
+ const copiedPages = new Array(indices.length);
637
+ for (let idx = 0, len = indices.length; idx < len; idx++) {
638
+ const srcPage = srcPages[indices[idx]];
639
+ const copiedPage = copier.copy(srcPage.node);
640
+ const ref = this.context.register(copiedPage);
641
+ copiedPages[idx] = PDFPage.of(copiedPage, ref, this);
642
+ }
643
+ return copiedPages;
651
644
  }
652
645
  /**
653
646
  * Get a copy of this document.
@@ -663,37 +656,35 @@ export default class PDFDocument {
663
656
  *
664
657
  * @returns Resolves with a copy this document.
665
658
  */
666
- copy() {
667
- return __awaiter(this, void 0, void 0, function* () {
668
- const pdfCopy = yield PDFDocument.create();
669
- const contentPages = yield pdfCopy.copyPages(this, this.getPageIndices());
670
- for (let idx = 0, len = contentPages.length; idx < len; idx++) {
671
- pdfCopy.addPage(contentPages[idx]);
672
- }
673
- if (this.getAuthor() !== undefined) {
674
- pdfCopy.setAuthor(this.getAuthor());
675
- }
676
- if (this.getCreationDate() !== undefined) {
677
- pdfCopy.setCreationDate(this.getCreationDate());
678
- }
679
- if (this.getCreator() !== undefined) {
680
- pdfCopy.setCreator(this.getCreator());
681
- }
682
- if (this.getModificationDate() !== undefined) {
683
- pdfCopy.setModificationDate(this.getModificationDate());
684
- }
685
- if (this.getProducer() !== undefined) {
686
- pdfCopy.setProducer(this.getProducer());
687
- }
688
- if (this.getSubject() !== undefined) {
689
- pdfCopy.setSubject(this.getSubject());
690
- }
691
- if (this.getTitle() !== undefined) {
692
- pdfCopy.setTitle(this.getTitle());
693
- }
694
- pdfCopy.defaultWordBreaks = this.defaultWordBreaks;
695
- return pdfCopy;
696
- });
659
+ async copy() {
660
+ const pdfCopy = await PDFDocument.create();
661
+ const contentPages = await pdfCopy.copyPages(this, this.getPageIndices());
662
+ for (let idx = 0, len = contentPages.length; idx < len; idx++) {
663
+ pdfCopy.addPage(contentPages[idx]);
664
+ }
665
+ if (this.getAuthor() !== undefined) {
666
+ pdfCopy.setAuthor(this.getAuthor());
667
+ }
668
+ if (this.getCreationDate() !== undefined) {
669
+ pdfCopy.setCreationDate(this.getCreationDate());
670
+ }
671
+ if (this.getCreator() !== undefined) {
672
+ pdfCopy.setCreator(this.getCreator());
673
+ }
674
+ if (this.getModificationDate() !== undefined) {
675
+ pdfCopy.setModificationDate(this.getModificationDate());
676
+ }
677
+ if (this.getProducer() !== undefined) {
678
+ pdfCopy.setProducer(this.getProducer());
679
+ }
680
+ if (this.getSubject() !== undefined) {
681
+ pdfCopy.setSubject(this.getSubject());
682
+ }
683
+ if (this.getTitle() !== undefined) {
684
+ pdfCopy.setTitle(this.getTitle());
685
+ }
686
+ pdfCopy.defaultWordBreaks = this.defaultWordBreaks;
687
+ return pdfCopy;
697
688
  }
698
689
  /**
699
690
  * Add JavaScript to this document. The supplied `script` is executed when the
@@ -782,23 +773,21 @@ export default class PDFDocument {
782
773
  * @param name The name of the file to be attached.
783
774
  * @returns Resolves when the attachment is complete.
784
775
  */
785
- attach(attachment, name, options = {}) {
786
- return __awaiter(this, void 0, void 0, function* () {
787
- assertIs(attachment, 'attachment', ['string', Uint8Array, ArrayBuffer]);
788
- assertIs(name, 'name', ['string']);
789
- assertOrUndefined(options.mimeType, 'mimeType', ['string']);
790
- assertOrUndefined(options.description, 'description', ['string']);
791
- assertOrUndefined(options.creationDate, 'options.creationDate', [Date]);
792
- assertOrUndefined(options.modificationDate, 'options.modificationDate', [
793
- Date,
794
- ]);
795
- assertIsOneOfOrUndefined(options.afRelationship, 'options.afRelationship', AFRelationship);
796
- const bytes = toUint8Array(attachment);
797
- const embedder = FileEmbedder.for(bytes, name, options);
798
- const ref = this.context.nextRef();
799
- const embeddedFile = PDFEmbeddedFile.of(ref, this, embedder);
800
- this.embeddedFiles.push(embeddedFile);
801
- });
776
+ async attach(attachment, name, options = {}) {
777
+ assertIs(attachment, 'attachment', ['string', Uint8Array, ArrayBuffer]);
778
+ assertIs(name, 'name', ['string']);
779
+ assertOrUndefined(options.mimeType, 'mimeType', ['string']);
780
+ assertOrUndefined(options.description, 'description', ['string']);
781
+ assertOrUndefined(options.creationDate, 'options.creationDate', [Date]);
782
+ assertOrUndefined(options.modificationDate, 'options.modificationDate', [
783
+ Date,
784
+ ]);
785
+ assertIsOneOfOrUndefined(options.afRelationship, 'options.afRelationship', AFRelationship);
786
+ const bytes = toUint8Array(attachment);
787
+ const embedder = FileEmbedder.for(bytes, name, options);
788
+ const ref = this.context.nextRef();
789
+ const embeddedFile = PDFEmbeddedFile.of(ref, this, embedder);
790
+ this.embeddedFiles.push(embeddedFile);
802
791
  }
803
792
  /**
804
793
  * Embed a font into this document. The input data can be provided in multiple
@@ -835,30 +824,28 @@ export default class PDFDocument {
835
824
  * @param options The options to be used when embedding the font.
836
825
  * @returns Resolves with the embedded font.
837
826
  */
838
- embedFont(font, options = {}) {
839
- return __awaiter(this, void 0, void 0, function* () {
840
- const { subset = false, customName, features } = options;
841
- assertIs(font, 'font', ['string', Uint8Array, ArrayBuffer]);
842
- assertIs(subset, 'subset', ['boolean']);
843
- let embedder;
844
- if (isStandardFont(font)) {
845
- embedder = StandardFontEmbedder.for(font, customName);
846
- }
847
- else if (canBeConvertedToUint8Array(font)) {
848
- const bytes = toUint8Array(font);
849
- const fontkit = this.assertFontkit();
850
- embedder = subset
851
- ? yield CustomFontSubsetEmbedder.for(fontkit, bytes, customName, features)
852
- : yield CustomFontEmbedder.for(fontkit, bytes, customName, features);
853
- }
854
- else {
855
- throw new TypeError('`font` must be one of `StandardFonts | string | Uint8Array | ArrayBuffer`');
856
- }
857
- const ref = this.context.nextRef();
858
- const pdfFont = PDFFont.of(ref, this, embedder);
859
- this.fonts.push(pdfFont);
860
- return pdfFont;
861
- });
827
+ async embedFont(font, options = {}) {
828
+ const { subset = false, customName, features } = options;
829
+ assertIs(font, 'font', ['string', Uint8Array, ArrayBuffer]);
830
+ assertIs(subset, 'subset', ['boolean']);
831
+ let embedder;
832
+ if (isStandardFont(font)) {
833
+ embedder = StandardFontEmbedder.for(font, customName);
834
+ }
835
+ else if (canBeConvertedToUint8Array(font)) {
836
+ const bytes = toUint8Array(font);
837
+ const fontkit = this.assertFontkit();
838
+ embedder = subset
839
+ ? await CustomFontSubsetEmbedder.for(fontkit, bytes, customName, features)
840
+ : await CustomFontEmbedder.for(fontkit, bytes, customName, features);
841
+ }
842
+ else {
843
+ throw new TypeError('`font` must be one of `StandardFonts | string | Uint8Array | ArrayBuffer`');
844
+ }
845
+ const ref = this.context.nextRef();
846
+ const pdfFont = PDFFont.of(ref, this, embedder);
847
+ this.fonts.push(pdfFont);
848
+ return pdfFont;
862
849
  }
863
850
  /**
864
851
  * Embed a standard font into this document.
@@ -912,16 +899,14 @@ export default class PDFDocument {
912
899
  * @param jpg The input data for a JPEG image.
913
900
  * @returns Resolves with the embedded image.
914
901
  */
915
- embedJpg(jpg) {
916
- return __awaiter(this, void 0, void 0, function* () {
917
- assertIs(jpg, 'jpg', ['string', Uint8Array, ArrayBuffer]);
918
- const bytes = toUint8Array(jpg);
919
- const embedder = yield JpegEmbedder.for(bytes);
920
- const ref = this.context.nextRef();
921
- const pdfImage = PDFImage.of(ref, this, embedder);
922
- this.images.push(pdfImage);
923
- return pdfImage;
924
- });
902
+ async embedJpg(jpg) {
903
+ assertIs(jpg, 'jpg', ['string', Uint8Array, ArrayBuffer]);
904
+ const bytes = toUint8Array(jpg);
905
+ const embedder = await JpegEmbedder.for(bytes);
906
+ const ref = this.context.nextRef();
907
+ const pdfImage = PDFImage.of(ref, this, embedder);
908
+ this.images.push(pdfImage);
909
+ return pdfImage;
925
910
  }
926
911
  /**
927
912
  * Embed a PNG image into this document. The input data can be provided in
@@ -953,16 +938,14 @@ export default class PDFDocument {
953
938
  * @param png The input data for a PNG image.
954
939
  * @returns Resolves with the embedded image.
955
940
  */
956
- embedPng(png) {
957
- return __awaiter(this, void 0, void 0, function* () {
958
- assertIs(png, 'png', ['string', Uint8Array, ArrayBuffer]);
959
- const bytes = toUint8Array(png);
960
- const embedder = yield PngEmbedder.for(bytes);
961
- const ref = this.context.nextRef();
962
- const pdfImage = PDFImage.of(ref, this, embedder);
963
- this.images.push(pdfImage);
964
- return pdfImage;
965
- });
941
+ async embedPng(png) {
942
+ assertIs(png, 'png', ['string', Uint8Array, ArrayBuffer]);
943
+ const bytes = toUint8Array(png);
944
+ const embedder = await PngEmbedder.for(bytes);
945
+ const ref = this.context.nextRef();
946
+ const pdfImage = PDFImage.of(ref, this, embedder);
947
+ this.images.push(pdfImage);
948
+ return pdfImage;
966
949
  }
967
950
  /**
968
951
  * Embed one or more PDF pages into this document.
@@ -984,19 +967,17 @@ export default class PDFDocument {
984
967
  * @param indices The indices of the pages that should be embedded.
985
968
  * @returns Resolves with an array of the embedded pages.
986
969
  */
987
- embedPdf(pdf, indices = [0]) {
988
- return __awaiter(this, void 0, void 0, function* () {
989
- assertIs(pdf, 'pdf', [
990
- 'string',
991
- Uint8Array,
992
- ArrayBuffer,
993
- [PDFDocument, 'PDFDocument'],
994
- ]);
995
- assertIs(indices, 'indices', [Array]);
996
- const srcDoc = pdf instanceof PDFDocument ? pdf : yield PDFDocument.load(pdf);
997
- const srcPages = pluckIndices(srcDoc.getPages(), indices);
998
- return this.embedPages(srcPages);
999
- });
970
+ async embedPdf(pdf, indices = [0]) {
971
+ assertIs(pdf, 'pdf', [
972
+ 'string',
973
+ Uint8Array,
974
+ ArrayBuffer,
975
+ [PDFDocument, 'PDFDocument'],
976
+ ]);
977
+ assertIs(indices, 'indices', [Array]);
978
+ const srcDoc = pdf instanceof PDFDocument ? pdf : await PDFDocument.load(pdf);
979
+ const srcPages = pluckIndices(srcDoc.getPages(), indices);
980
+ return this.embedPages(srcPages);
1000
981
  }
1001
982
  /**
1002
983
  * Embed a single PDF page into this document.
@@ -1030,12 +1011,10 @@ export default class PDFDocument {
1030
1011
  * page anywhere it is drawn.
1031
1012
  * @returns Resolves with the embedded pdf page.
1032
1013
  */
1033
- embedPage(page, boundingBox, transformationMatrix) {
1034
- return __awaiter(this, void 0, void 0, function* () {
1035
- assertIs(page, 'page', [[PDFPage, 'PDFPage']]);
1036
- const [embeddedPage] = yield this.embedPages([page], [boundingBox], [transformationMatrix]);
1037
- return embeddedPage;
1038
- });
1014
+ async embedPage(page, boundingBox, transformationMatrix) {
1015
+ assertIs(page, 'page', [[PDFPage, 'PDFPage']]);
1016
+ const [embeddedPage] = await this.embedPages([page], [boundingBox], [transformationMatrix]);
1017
+ return embeddedPage;
1039
1018
  }
1040
1019
  /**
1041
1020
  * Embed one or more PDF pages into this document.
@@ -1065,34 +1044,32 @@ export default class PDFDocument {
1065
1044
  * (each page's transformation will apply anywhere it is drawn).
1066
1045
  * @returns Resolves with an array of the embedded pdf pages.
1067
1046
  */
1068
- embedPages(pages, boundingBoxes = [], transformationMatrices = []) {
1069
- return __awaiter(this, void 0, void 0, function* () {
1070
- if (pages.length === 0)
1071
- return [];
1072
- // Assert all pages have the same context
1073
- for (let idx = 0, len = pages.length - 1; idx < len; idx++) {
1074
- const currPage = pages[idx];
1075
- const nextPage = pages[idx + 1];
1076
- if (currPage.node.context !== nextPage.node.context) {
1077
- throw new PageEmbeddingMismatchedContextError();
1078
- }
1047
+ async embedPages(pages, boundingBoxes = [], transformationMatrices = []) {
1048
+ if (pages.length === 0)
1049
+ return [];
1050
+ // Assert all pages have the same context
1051
+ for (let idx = 0, len = pages.length - 1; idx < len; idx++) {
1052
+ const currPage = pages[idx];
1053
+ const nextPage = pages[idx + 1];
1054
+ if (currPage.node.context !== nextPage.node.context) {
1055
+ throw new PageEmbeddingMismatchedContextError();
1079
1056
  }
1080
- const context = pages[0].node.context;
1081
- const maybeCopyPage = context === this.context
1082
- ? (p) => p
1083
- : PDFObjectCopier.for(context, this.context).copy;
1084
- const embeddedPages = new Array(pages.length);
1085
- for (let idx = 0, len = pages.length; idx < len; idx++) {
1086
- const page = maybeCopyPage(pages[idx].node);
1087
- const box = boundingBoxes[idx];
1088
- const matrix = transformationMatrices[idx];
1089
- const embedder = yield PDFPageEmbedder.for(page, box, matrix);
1090
- const ref = this.context.nextRef();
1091
- embeddedPages[idx] = PDFEmbeddedPage.of(ref, this, embedder);
1092
- }
1093
- this.embeddedPages.push(...embeddedPages);
1094
- return embeddedPages;
1095
- });
1057
+ }
1058
+ const context = pages[0].node.context;
1059
+ const maybeCopyPage = context === this.context
1060
+ ? (p) => p
1061
+ : PDFObjectCopier.for(context, this.context).copy;
1062
+ const embeddedPages = new Array(pages.length);
1063
+ for (let idx = 0, len = pages.length; idx < len; idx++) {
1064
+ const page = maybeCopyPage(pages[idx].node);
1065
+ const box = boundingBoxes[idx];
1066
+ const matrix = transformationMatrices[idx];
1067
+ const embedder = await PDFPageEmbedder.for(page, box, matrix);
1068
+ const ref = this.context.nextRef();
1069
+ embeddedPages[idx] = PDFEmbeddedPage.of(ref, this, embedder);
1070
+ }
1071
+ this.embeddedPages.push(...embeddedPages);
1072
+ return embeddedPages;
1096
1073
  }
1097
1074
  /**
1098
1075
  * > **NOTE:** You shouldn't need to call this method directly. The [[save]]
@@ -1104,14 +1081,12 @@ export default class PDFDocument {
1104
1081
  *
1105
1082
  * @returns Resolves when the flush is complete.
1106
1083
  */
1107
- flush() {
1108
- return __awaiter(this, void 0, void 0, function* () {
1109
- yield this.embedAll(this.fonts);
1110
- yield this.embedAll(this.images);
1111
- yield this.embedAll(this.embeddedPages);
1112
- yield this.embedAll(this.embeddedFiles);
1113
- yield this.embedAll(this.javaScripts);
1114
- });
1084
+ async flush() {
1085
+ await this.embedAll(this.fonts);
1086
+ await this.embedAll(this.images);
1087
+ await this.embedAll(this.embeddedPages);
1088
+ await this.embedAll(this.embeddedFiles);
1089
+ await this.embedAll(this.javaScripts);
1115
1090
  }
1116
1091
  /**
1117
1092
  * Serialize this document to an array of bytes making up a PDF file.
@@ -1129,24 +1104,22 @@ export default class PDFDocument {
1129
1104
  * @param options The options to be used when saving the document.
1130
1105
  * @returns Resolves with the bytes of the serialized document.
1131
1106
  */
1132
- save(options = {}) {
1133
- return __awaiter(this, void 0, void 0, function* () {
1134
- const { useObjectStreams = true, addDefaultPage = true, objectsPerTick = 50, updateFieldAppearances = true, } = options;
1135
- assertIs(useObjectStreams, 'useObjectStreams', ['boolean']);
1136
- assertIs(addDefaultPage, 'addDefaultPage', ['boolean']);
1137
- assertIs(objectsPerTick, 'objectsPerTick', ['number']);
1138
- assertIs(updateFieldAppearances, 'updateFieldAppearances', ['boolean']);
1139
- if (addDefaultPage && this.getPageCount() === 0)
1140
- this.addPage();
1141
- if (updateFieldAppearances) {
1142
- const form = this.formCache.getValue();
1143
- if (form)
1144
- form.updateFieldAppearances();
1145
- }
1146
- yield this.flush();
1147
- const Writer = useObjectStreams ? PDFStreamWriter : PDFWriter;
1148
- return Writer.forContext(this.context, objectsPerTick).serializeToBuffer();
1149
- });
1107
+ async save(options = {}) {
1108
+ const { useObjectStreams = true, addDefaultPage = true, objectsPerTick = 50, updateFieldAppearances = true, } = options;
1109
+ assertIs(useObjectStreams, 'useObjectStreams', ['boolean']);
1110
+ assertIs(addDefaultPage, 'addDefaultPage', ['boolean']);
1111
+ assertIs(objectsPerTick, 'objectsPerTick', ['number']);
1112
+ assertIs(updateFieldAppearances, 'updateFieldAppearances', ['boolean']);
1113
+ if (addDefaultPage && this.getPageCount() === 0)
1114
+ this.addPage();
1115
+ if (updateFieldAppearances) {
1116
+ const form = this.formCache.getValue();
1117
+ if (form)
1118
+ form.updateFieldAppearances();
1119
+ }
1120
+ await this.flush();
1121
+ const Writer = useObjectStreams ? PDFStreamWriter : PDFWriter;
1122
+ return Writer.forContext(this.context, objectsPerTick).serializeToBuffer();
1150
1123
  }
1151
1124
  /**
1152
1125
  * Serialize this document to a base64 encoded string or data URI making up a
@@ -1163,14 +1136,12 @@ export default class PDFDocument {
1163
1136
  * @returns Resolves with a base64 encoded string or data URI of the
1164
1137
  * serialized document.
1165
1138
  */
1166
- saveAsBase64(options = {}) {
1167
- return __awaiter(this, void 0, void 0, function* () {
1168
- const { dataUri = false } = options, otherOptions = __rest(options, ["dataUri"]);
1169
- assertIs(dataUri, 'dataUri', ['boolean']);
1170
- const bytes = yield this.save(otherOptions);
1171
- const base64 = encodeToBase64(bytes);
1172
- return dataUri ? `data:application/pdf;base64,${base64}` : base64;
1173
- });
1139
+ async saveAsBase64(options = {}) {
1140
+ const { dataUri = false, ...otherOptions } = options;
1141
+ assertIs(dataUri, 'dataUri', ['boolean']);
1142
+ const bytes = await this.save(otherOptions);
1143
+ const base64 = encodeToBase64(bytes);
1144
+ return dataUri ? `data:application/pdf;base64,${base64}` : base64;
1174
1145
  }
1175
1146
  findPageForAnnotationRef(ref) {
1176
1147
  const pages = this.getPages();
@@ -1183,12 +1154,10 @@ export default class PDFDocument {
1183
1154
  }
1184
1155
  return undefined;
1185
1156
  }
1186
- embedAll(embeddables) {
1187
- return __awaiter(this, void 0, void 0, function* () {
1188
- for (let idx = 0, len = embeddables.length; idx < len; idx++) {
1189
- yield embeddables[idx].embed();
1190
- }
1191
- });
1157
+ async embedAll(embeddables) {
1158
+ for (let idx = 0, len = embeddables.length; idx < len; idx++) {
1159
+ await embeddables[idx].embed();
1160
+ }
1192
1161
  }
1193
1162
  updateInfoDict() {
1194
1163
  const pdfLib = `pdf-lib (https://github.com/Hopding/pdf-lib)`;