@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
package/README.md CHANGED
@@ -3,6 +3,15 @@
3
3
  This version is forked from [Hopding/pdf-lib](https://github.com/Hopding/pdf-lib).
4
4
  Incorporated several bug fixes and additional features into the original code.
5
5
 
6
+ <details>
7
+ <summary><a href="https://github.com/pdfme/pdf-lib/pull/9">2025/03/09 - Add `radius` option for drawRectangle. This enables to render rounded corner rectangles</a></summary>
8
+ Draw rounded corners with the specified radius using the Bezier curve function. The radius must be a positive number. If it exceeds half of the width or height, it will be clipped.
9
+ </details>
10
+ <details>
11
+ <summary><a href="https://github.com/cantoo-scribe/pdf-lib/pull/42">2023/12/22 - Incorporate @cantoo/pdf-lib Refactor drawSvg PR</a></summary>
12
+
13
+ PR: [Refactor drawSvg to handle transfomations as matrix and to crop elements #42](https://github.com/cantoo-scribe/pdf-lib/pull/42)
14
+ </details>
6
15
  <details>
7
16
  <summary><a href="https://github.com/cantoo-scribe/pdf-lib">2023/12/20 - Incorporate @cantoo/pdf-lib that is fork of pdf-lib to use drawSvg method</a></summary>
8
17
 
@@ -53,9 +62,9 @@ pdfDoc.registerFontkit(fontkit)
53
62
 
54
63
  <div align="center">
55
64
  <!-- NPM Version -->
56
- <a href="https://www.npmjs.com/package/pdf-lib">
65
+ <a href="https://www.npmjs.com/package/@pdfme/pdf-lib">
57
66
  <img
58
- src="https://img.shields.io/npm/v/pdf-lib.svg?style=flat-square"
67
+ src="https://img.shields.io/npm/v/@pdfme/pdf-lib.svg?style=flat-square"
59
68
  alt="NPM Version"
60
69
  />
61
70
  </a>
@@ -115,42 +115,38 @@ class PDFDocument {
115
115
  * @param options The options to be used when loading the document.
116
116
  * @returns Resolves with a document loaded from the input.
117
117
  */
118
- static load(pdf, options = {}) {
119
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
120
- const { ignoreEncryption = false, parseSpeed = PDFDocumentOptions_1.ParseSpeeds.Slow, throwOnInvalidObject = false, updateMetadata = true, capNumbers = false, password, } = options;
121
- (0, utils_1.assertIs)(pdf, 'pdf', ['string', Uint8Array, ArrayBuffer]);
122
- (0, utils_1.assertIs)(ignoreEncryption, 'ignoreEncryption', ['boolean']);
123
- (0, utils_1.assertIs)(parseSpeed, 'parseSpeed', ['number']);
124
- (0, utils_1.assertIs)(throwOnInvalidObject, 'throwOnInvalidObject', ['boolean']);
125
- (0, utils_1.assertIs)(password, 'password', ['string', 'undefined']);
126
- const bytes = (0, utils_1.toUint8Array)(pdf);
127
- const context = yield core_1.PDFParser.forBytesWithOptions(bytes, parseSpeed, throwOnInvalidObject, capNumbers).parseDocument();
128
- if (!!context.lookup(context.trailerInfo.Encrypt) && password !== undefined) {
129
- // Decrypt
130
- const fileIds = context.lookup(context.trailerInfo.ID, core_1.PDFArray);
131
- const encryptDict = context.lookup(context.trailerInfo.Encrypt, core_1.PDFDict);
132
- const decryptedContext = yield core_1.PDFParser.forBytesWithOptions(bytes, parseSpeed, throwOnInvalidObject, capNumbers, new crypto_1.CipherTransformFactory(encryptDict, fileIds.get(0).asBytes(), password)).parseDocument();
133
- return new PDFDocument(decryptedContext, true, updateMetadata);
134
- }
135
- else {
136
- return new PDFDocument(context, ignoreEncryption, updateMetadata);
137
- }
138
- });
118
+ static async load(pdf, options = {}) {
119
+ const { ignoreEncryption = false, parseSpeed = PDFDocumentOptions_1.ParseSpeeds.Slow, throwOnInvalidObject = false, updateMetadata = true, capNumbers = false, password, } = options;
120
+ (0, utils_1.assertIs)(pdf, 'pdf', ['string', Uint8Array, ArrayBuffer]);
121
+ (0, utils_1.assertIs)(ignoreEncryption, 'ignoreEncryption', ['boolean']);
122
+ (0, utils_1.assertIs)(parseSpeed, 'parseSpeed', ['number']);
123
+ (0, utils_1.assertIs)(throwOnInvalidObject, 'throwOnInvalidObject', ['boolean']);
124
+ (0, utils_1.assertIs)(password, 'password', ['string', 'undefined']);
125
+ const bytes = (0, utils_1.toUint8Array)(pdf);
126
+ const context = await core_1.PDFParser.forBytesWithOptions(bytes, parseSpeed, throwOnInvalidObject, capNumbers).parseDocument();
127
+ if (!!context.lookup(context.trailerInfo.Encrypt) && password !== undefined) {
128
+ // Decrypt
129
+ const fileIds = context.lookup(context.trailerInfo.ID, core_1.PDFArray);
130
+ const encryptDict = context.lookup(context.trailerInfo.Encrypt, core_1.PDFDict);
131
+ const decryptedContext = await core_1.PDFParser.forBytesWithOptions(bytes, parseSpeed, throwOnInvalidObject, capNumbers, new crypto_1.CipherTransformFactory(encryptDict, fileIds.get(0).asBytes(), password)).parseDocument();
132
+ return new PDFDocument(decryptedContext, true, updateMetadata);
133
+ }
134
+ else {
135
+ return new PDFDocument(context, ignoreEncryption, updateMetadata);
136
+ }
139
137
  }
140
138
  /**
141
139
  * Create a new [[PDFDocument]].
142
140
  * @returns Resolves with the newly created document.
143
141
  */
144
- static create(options = {}) {
145
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
146
- const { updateMetadata = true } = options;
147
- const context = core_1.PDFContext.create();
148
- const pageTree = core_1.PDFPageTree.withContext(context);
149
- const pageTreeRef = context.register(pageTree);
150
- const catalog = core_1.PDFCatalog.withContextAndPages(context, pageTreeRef);
151
- context.trailerInfo.Root = context.register(catalog);
152
- return new PDFDocument(context, false, updateMetadata);
153
- });
142
+ static async create(options = {}) {
143
+ const { updateMetadata = true } = options;
144
+ const context = core_1.PDFContext.create();
145
+ const pageTree = core_1.PDFPageTree.withContext(context);
146
+ const pageTreeRef = context.register(pageTree);
147
+ const catalog = core_1.PDFCatalog.withContextAndPages(context, pageTreeRef);
148
+ context.trailerInfo.Root = context.register(catalog);
149
+ return new PDFDocument(context, false, updateMetadata);
154
150
  }
155
151
  /**
156
152
  * Register a fontkit instance. This must be done before custom fonts can
@@ -634,22 +630,20 @@ class PDFDocument {
634
630
  * @param indices The indices of the pages that should be copied.
635
631
  * @returns Resolves with an array of pages copied into this document.
636
632
  */
637
- copyPages(srcDoc, indices) {
638
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
639
- (0, utils_1.assertIs)(srcDoc, 'srcDoc', [[PDFDocument, 'PDFDocument']]);
640
- (0, utils_1.assertIs)(indices, 'indices', [Array]);
641
- yield srcDoc.flush();
642
- const copier = core_1.PDFObjectCopier.for(srcDoc.context, this.context);
643
- const srcPages = srcDoc.getPages();
644
- const copiedPages = new Array(indices.length);
645
- for (let idx = 0, len = indices.length; idx < len; idx++) {
646
- const srcPage = srcPages[indices[idx]];
647
- const copiedPage = copier.copy(srcPage.node);
648
- const ref = this.context.register(copiedPage);
649
- copiedPages[idx] = PDFPage_1.default.of(copiedPage, ref, this);
650
- }
651
- return copiedPages;
652
- });
633
+ async copyPages(srcDoc, indices) {
634
+ (0, utils_1.assertIs)(srcDoc, 'srcDoc', [[PDFDocument, 'PDFDocument']]);
635
+ (0, utils_1.assertIs)(indices, 'indices', [Array]);
636
+ await srcDoc.flush();
637
+ const copier = core_1.PDFObjectCopier.for(srcDoc.context, this.context);
638
+ const srcPages = srcDoc.getPages();
639
+ const copiedPages = new Array(indices.length);
640
+ for (let idx = 0, len = indices.length; idx < len; idx++) {
641
+ const srcPage = srcPages[indices[idx]];
642
+ const copiedPage = copier.copy(srcPage.node);
643
+ const ref = this.context.register(copiedPage);
644
+ copiedPages[idx] = PDFPage_1.default.of(copiedPage, ref, this);
645
+ }
646
+ return copiedPages;
653
647
  }
654
648
  /**
655
649
  * Get a copy of this document.
@@ -665,37 +659,35 @@ class PDFDocument {
665
659
  *
666
660
  * @returns Resolves with a copy this document.
667
661
  */
668
- copy() {
669
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
670
- const pdfCopy = yield PDFDocument.create();
671
- const contentPages = yield pdfCopy.copyPages(this, this.getPageIndices());
672
- for (let idx = 0, len = contentPages.length; idx < len; idx++) {
673
- pdfCopy.addPage(contentPages[idx]);
674
- }
675
- if (this.getAuthor() !== undefined) {
676
- pdfCopy.setAuthor(this.getAuthor());
677
- }
678
- if (this.getCreationDate() !== undefined) {
679
- pdfCopy.setCreationDate(this.getCreationDate());
680
- }
681
- if (this.getCreator() !== undefined) {
682
- pdfCopy.setCreator(this.getCreator());
683
- }
684
- if (this.getModificationDate() !== undefined) {
685
- pdfCopy.setModificationDate(this.getModificationDate());
686
- }
687
- if (this.getProducer() !== undefined) {
688
- pdfCopy.setProducer(this.getProducer());
689
- }
690
- if (this.getSubject() !== undefined) {
691
- pdfCopy.setSubject(this.getSubject());
692
- }
693
- if (this.getTitle() !== undefined) {
694
- pdfCopy.setTitle(this.getTitle());
695
- }
696
- pdfCopy.defaultWordBreaks = this.defaultWordBreaks;
697
- return pdfCopy;
698
- });
662
+ async copy() {
663
+ const pdfCopy = await PDFDocument.create();
664
+ const contentPages = await pdfCopy.copyPages(this, this.getPageIndices());
665
+ for (let idx = 0, len = contentPages.length; idx < len; idx++) {
666
+ pdfCopy.addPage(contentPages[idx]);
667
+ }
668
+ if (this.getAuthor() !== undefined) {
669
+ pdfCopy.setAuthor(this.getAuthor());
670
+ }
671
+ if (this.getCreationDate() !== undefined) {
672
+ pdfCopy.setCreationDate(this.getCreationDate());
673
+ }
674
+ if (this.getCreator() !== undefined) {
675
+ pdfCopy.setCreator(this.getCreator());
676
+ }
677
+ if (this.getModificationDate() !== undefined) {
678
+ pdfCopy.setModificationDate(this.getModificationDate());
679
+ }
680
+ if (this.getProducer() !== undefined) {
681
+ pdfCopy.setProducer(this.getProducer());
682
+ }
683
+ if (this.getSubject() !== undefined) {
684
+ pdfCopy.setSubject(this.getSubject());
685
+ }
686
+ if (this.getTitle() !== undefined) {
687
+ pdfCopy.setTitle(this.getTitle());
688
+ }
689
+ pdfCopy.defaultWordBreaks = this.defaultWordBreaks;
690
+ return pdfCopy;
699
691
  }
700
692
  /**
701
693
  * Add JavaScript to this document. The supplied `script` is executed when the
@@ -784,23 +776,21 @@ class PDFDocument {
784
776
  * @param name The name of the file to be attached.
785
777
  * @returns Resolves when the attachment is complete.
786
778
  */
787
- attach(attachment, name, options = {}) {
788
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
789
- (0, utils_1.assertIs)(attachment, 'attachment', ['string', Uint8Array, ArrayBuffer]);
790
- (0, utils_1.assertIs)(name, 'name', ['string']);
791
- (0, utils_1.assertOrUndefined)(options.mimeType, 'mimeType', ['string']);
792
- (0, utils_1.assertOrUndefined)(options.description, 'description', ['string']);
793
- (0, utils_1.assertOrUndefined)(options.creationDate, 'options.creationDate', [Date]);
794
- (0, utils_1.assertOrUndefined)(options.modificationDate, 'options.modificationDate', [
795
- Date,
796
- ]);
797
- (0, utils_1.assertIsOneOfOrUndefined)(options.afRelationship, 'options.afRelationship', FileEmbedder_1.AFRelationship);
798
- const bytes = (0, utils_1.toUint8Array)(attachment);
799
- const embedder = FileEmbedder_1.default.for(bytes, name, options);
800
- const ref = this.context.nextRef();
801
- const embeddedFile = PDFEmbeddedFile_1.default.of(ref, this, embedder);
802
- this.embeddedFiles.push(embeddedFile);
803
- });
779
+ async attach(attachment, name, options = {}) {
780
+ (0, utils_1.assertIs)(attachment, 'attachment', ['string', Uint8Array, ArrayBuffer]);
781
+ (0, utils_1.assertIs)(name, 'name', ['string']);
782
+ (0, utils_1.assertOrUndefined)(options.mimeType, 'mimeType', ['string']);
783
+ (0, utils_1.assertOrUndefined)(options.description, 'description', ['string']);
784
+ (0, utils_1.assertOrUndefined)(options.creationDate, 'options.creationDate', [Date]);
785
+ (0, utils_1.assertOrUndefined)(options.modificationDate, 'options.modificationDate', [
786
+ Date,
787
+ ]);
788
+ (0, utils_1.assertIsOneOfOrUndefined)(options.afRelationship, 'options.afRelationship', FileEmbedder_1.AFRelationship);
789
+ const bytes = (0, utils_1.toUint8Array)(attachment);
790
+ const embedder = FileEmbedder_1.default.for(bytes, name, options);
791
+ const ref = this.context.nextRef();
792
+ const embeddedFile = PDFEmbeddedFile_1.default.of(ref, this, embedder);
793
+ this.embeddedFiles.push(embeddedFile);
804
794
  }
805
795
  /**
806
796
  * Embed a font into this document. The input data can be provided in multiple
@@ -837,30 +827,28 @@ class PDFDocument {
837
827
  * @param options The options to be used when embedding the font.
838
828
  * @returns Resolves with the embedded font.
839
829
  */
840
- embedFont(font, options = {}) {
841
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
842
- const { subset = false, customName, features } = options;
843
- (0, utils_1.assertIs)(font, 'font', ['string', Uint8Array, ArrayBuffer]);
844
- (0, utils_1.assertIs)(subset, 'subset', ['boolean']);
845
- let embedder;
846
- if ((0, utils_1.isStandardFont)(font)) {
847
- embedder = core_1.StandardFontEmbedder.for(font, customName);
848
- }
849
- else if ((0, utils_1.canBeConvertedToUint8Array)(font)) {
850
- const bytes = (0, utils_1.toUint8Array)(font);
851
- const fontkit = this.assertFontkit();
852
- embedder = subset
853
- ? yield core_1.CustomFontSubsetEmbedder.for(fontkit, bytes, customName, features)
854
- : yield core_1.CustomFontEmbedder.for(fontkit, bytes, customName, features);
855
- }
856
- else {
857
- throw new TypeError('`font` must be one of `StandardFonts | string | Uint8Array | ArrayBuffer`');
858
- }
859
- const ref = this.context.nextRef();
860
- const pdfFont = PDFFont_1.default.of(ref, this, embedder);
861
- this.fonts.push(pdfFont);
862
- return pdfFont;
863
- });
830
+ async embedFont(font, options = {}) {
831
+ const { subset = false, customName, features } = options;
832
+ (0, utils_1.assertIs)(font, 'font', ['string', Uint8Array, ArrayBuffer]);
833
+ (0, utils_1.assertIs)(subset, 'subset', ['boolean']);
834
+ let embedder;
835
+ if ((0, utils_1.isStandardFont)(font)) {
836
+ embedder = core_1.StandardFontEmbedder.for(font, customName);
837
+ }
838
+ else if ((0, utils_1.canBeConvertedToUint8Array)(font)) {
839
+ const bytes = (0, utils_1.toUint8Array)(font);
840
+ const fontkit = this.assertFontkit();
841
+ embedder = subset
842
+ ? await core_1.CustomFontSubsetEmbedder.for(fontkit, bytes, customName, features)
843
+ : await core_1.CustomFontEmbedder.for(fontkit, bytes, customName, features);
844
+ }
845
+ else {
846
+ throw new TypeError('`font` must be one of `StandardFonts | string | Uint8Array | ArrayBuffer`');
847
+ }
848
+ const ref = this.context.nextRef();
849
+ const pdfFont = PDFFont_1.default.of(ref, this, embedder);
850
+ this.fonts.push(pdfFont);
851
+ return pdfFont;
864
852
  }
865
853
  /**
866
854
  * Embed a standard font into this document.
@@ -914,16 +902,14 @@ class PDFDocument {
914
902
  * @param jpg The input data for a JPEG image.
915
903
  * @returns Resolves with the embedded image.
916
904
  */
917
- embedJpg(jpg) {
918
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
919
- (0, utils_1.assertIs)(jpg, 'jpg', ['string', Uint8Array, ArrayBuffer]);
920
- const bytes = (0, utils_1.toUint8Array)(jpg);
921
- const embedder = yield core_1.JpegEmbedder.for(bytes);
922
- const ref = this.context.nextRef();
923
- const pdfImage = PDFImage_1.default.of(ref, this, embedder);
924
- this.images.push(pdfImage);
925
- return pdfImage;
926
- });
905
+ async embedJpg(jpg) {
906
+ (0, utils_1.assertIs)(jpg, 'jpg', ['string', Uint8Array, ArrayBuffer]);
907
+ const bytes = (0, utils_1.toUint8Array)(jpg);
908
+ const embedder = await core_1.JpegEmbedder.for(bytes);
909
+ const ref = this.context.nextRef();
910
+ const pdfImage = PDFImage_1.default.of(ref, this, embedder);
911
+ this.images.push(pdfImage);
912
+ return pdfImage;
927
913
  }
928
914
  /**
929
915
  * Embed a PNG image into this document. The input data can be provided in
@@ -955,16 +941,14 @@ class PDFDocument {
955
941
  * @param png The input data for a PNG image.
956
942
  * @returns Resolves with the embedded image.
957
943
  */
958
- embedPng(png) {
959
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
960
- (0, utils_1.assertIs)(png, 'png', ['string', Uint8Array, ArrayBuffer]);
961
- const bytes = (0, utils_1.toUint8Array)(png);
962
- const embedder = yield core_1.PngEmbedder.for(bytes);
963
- const ref = this.context.nextRef();
964
- const pdfImage = PDFImage_1.default.of(ref, this, embedder);
965
- this.images.push(pdfImage);
966
- return pdfImage;
967
- });
944
+ async embedPng(png) {
945
+ (0, utils_1.assertIs)(png, 'png', ['string', Uint8Array, ArrayBuffer]);
946
+ const bytes = (0, utils_1.toUint8Array)(png);
947
+ const embedder = await core_1.PngEmbedder.for(bytes);
948
+ const ref = this.context.nextRef();
949
+ const pdfImage = PDFImage_1.default.of(ref, this, embedder);
950
+ this.images.push(pdfImage);
951
+ return pdfImage;
968
952
  }
969
953
  /**
970
954
  * Embed one or more PDF pages into this document.
@@ -986,19 +970,17 @@ class PDFDocument {
986
970
  * @param indices The indices of the pages that should be embedded.
987
971
  * @returns Resolves with an array of the embedded pages.
988
972
  */
989
- embedPdf(pdf, indices = [0]) {
990
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
991
- (0, utils_1.assertIs)(pdf, 'pdf', [
992
- 'string',
993
- Uint8Array,
994
- ArrayBuffer,
995
- [PDFDocument, 'PDFDocument'],
996
- ]);
997
- (0, utils_1.assertIs)(indices, 'indices', [Array]);
998
- const srcDoc = pdf instanceof PDFDocument ? pdf : yield PDFDocument.load(pdf);
999
- const srcPages = (0, utils_1.pluckIndices)(srcDoc.getPages(), indices);
1000
- return this.embedPages(srcPages);
1001
- });
973
+ async embedPdf(pdf, indices = [0]) {
974
+ (0, utils_1.assertIs)(pdf, 'pdf', [
975
+ 'string',
976
+ Uint8Array,
977
+ ArrayBuffer,
978
+ [PDFDocument, 'PDFDocument'],
979
+ ]);
980
+ (0, utils_1.assertIs)(indices, 'indices', [Array]);
981
+ const srcDoc = pdf instanceof PDFDocument ? pdf : await PDFDocument.load(pdf);
982
+ const srcPages = (0, utils_1.pluckIndices)(srcDoc.getPages(), indices);
983
+ return this.embedPages(srcPages);
1002
984
  }
1003
985
  /**
1004
986
  * Embed a single PDF page into this document.
@@ -1032,12 +1014,10 @@ class PDFDocument {
1032
1014
  * page anywhere it is drawn.
1033
1015
  * @returns Resolves with the embedded pdf page.
1034
1016
  */
1035
- embedPage(page, boundingBox, transformationMatrix) {
1036
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
1037
- (0, utils_1.assertIs)(page, 'page', [[PDFPage_1.default, 'PDFPage']]);
1038
- const [embeddedPage] = yield this.embedPages([page], [boundingBox], [transformationMatrix]);
1039
- return embeddedPage;
1040
- });
1017
+ async embedPage(page, boundingBox, transformationMatrix) {
1018
+ (0, utils_1.assertIs)(page, 'page', [[PDFPage_1.default, 'PDFPage']]);
1019
+ const [embeddedPage] = await this.embedPages([page], [boundingBox], [transformationMatrix]);
1020
+ return embeddedPage;
1041
1021
  }
1042
1022
  /**
1043
1023
  * Embed one or more PDF pages into this document.
@@ -1067,34 +1047,32 @@ class PDFDocument {
1067
1047
  * (each page's transformation will apply anywhere it is drawn).
1068
1048
  * @returns Resolves with an array of the embedded pdf pages.
1069
1049
  */
1070
- embedPages(pages, boundingBoxes = [], transformationMatrices = []) {
1071
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
1072
- if (pages.length === 0)
1073
- return [];
1074
- // Assert all pages have the same context
1075
- for (let idx = 0, len = pages.length - 1; idx < len; idx++) {
1076
- const currPage = pages[idx];
1077
- const nextPage = pages[idx + 1];
1078
- if (currPage.node.context !== nextPage.node.context) {
1079
- throw new core_1.PageEmbeddingMismatchedContextError();
1080
- }
1050
+ async embedPages(pages, boundingBoxes = [], transformationMatrices = []) {
1051
+ if (pages.length === 0)
1052
+ return [];
1053
+ // Assert all pages have the same context
1054
+ for (let idx = 0, len = pages.length - 1; idx < len; idx++) {
1055
+ const currPage = pages[idx];
1056
+ const nextPage = pages[idx + 1];
1057
+ if (currPage.node.context !== nextPage.node.context) {
1058
+ throw new core_1.PageEmbeddingMismatchedContextError();
1081
1059
  }
1082
- const context = pages[0].node.context;
1083
- const maybeCopyPage = context === this.context
1084
- ? (p) => p
1085
- : core_1.PDFObjectCopier.for(context, this.context).copy;
1086
- const embeddedPages = new Array(pages.length);
1087
- for (let idx = 0, len = pages.length; idx < len; idx++) {
1088
- const page = maybeCopyPage(pages[idx].node);
1089
- const box = boundingBoxes[idx];
1090
- const matrix = transformationMatrices[idx];
1091
- const embedder = yield core_1.PDFPageEmbedder.for(page, box, matrix);
1092
- const ref = this.context.nextRef();
1093
- embeddedPages[idx] = PDFEmbeddedPage_1.default.of(ref, this, embedder);
1094
- }
1095
- this.embeddedPages.push(...embeddedPages);
1096
- return embeddedPages;
1097
- });
1060
+ }
1061
+ const context = pages[0].node.context;
1062
+ const maybeCopyPage = context === this.context
1063
+ ? (p) => p
1064
+ : core_1.PDFObjectCopier.for(context, this.context).copy;
1065
+ const embeddedPages = new Array(pages.length);
1066
+ for (let idx = 0, len = pages.length; idx < len; idx++) {
1067
+ const page = maybeCopyPage(pages[idx].node);
1068
+ const box = boundingBoxes[idx];
1069
+ const matrix = transformationMatrices[idx];
1070
+ const embedder = await core_1.PDFPageEmbedder.for(page, box, matrix);
1071
+ const ref = this.context.nextRef();
1072
+ embeddedPages[idx] = PDFEmbeddedPage_1.default.of(ref, this, embedder);
1073
+ }
1074
+ this.embeddedPages.push(...embeddedPages);
1075
+ return embeddedPages;
1098
1076
  }
1099
1077
  /**
1100
1078
  * > **NOTE:** You shouldn't need to call this method directly. The [[save]]
@@ -1106,14 +1084,12 @@ class PDFDocument {
1106
1084
  *
1107
1085
  * @returns Resolves when the flush is complete.
1108
1086
  */
1109
- flush() {
1110
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
1111
- yield this.embedAll(this.fonts);
1112
- yield this.embedAll(this.images);
1113
- yield this.embedAll(this.embeddedPages);
1114
- yield this.embedAll(this.embeddedFiles);
1115
- yield this.embedAll(this.javaScripts);
1116
- });
1087
+ async flush() {
1088
+ await this.embedAll(this.fonts);
1089
+ await this.embedAll(this.images);
1090
+ await this.embedAll(this.embeddedPages);
1091
+ await this.embedAll(this.embeddedFiles);
1092
+ await this.embedAll(this.javaScripts);
1117
1093
  }
1118
1094
  /**
1119
1095
  * Serialize this document to an array of bytes making up a PDF file.
@@ -1131,24 +1107,22 @@ class PDFDocument {
1131
1107
  * @param options The options to be used when saving the document.
1132
1108
  * @returns Resolves with the bytes of the serialized document.
1133
1109
  */
1134
- save(options = {}) {
1135
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
1136
- const { useObjectStreams = true, addDefaultPage = true, objectsPerTick = 50, updateFieldAppearances = true, } = options;
1137
- (0, utils_1.assertIs)(useObjectStreams, 'useObjectStreams', ['boolean']);
1138
- (0, utils_1.assertIs)(addDefaultPage, 'addDefaultPage', ['boolean']);
1139
- (0, utils_1.assertIs)(objectsPerTick, 'objectsPerTick', ['number']);
1140
- (0, utils_1.assertIs)(updateFieldAppearances, 'updateFieldAppearances', ['boolean']);
1141
- if (addDefaultPage && this.getPageCount() === 0)
1142
- this.addPage();
1143
- if (updateFieldAppearances) {
1144
- const form = this.formCache.getValue();
1145
- if (form)
1146
- form.updateFieldAppearances();
1147
- }
1148
- yield this.flush();
1149
- const Writer = useObjectStreams ? core_1.PDFStreamWriter : core_1.PDFWriter;
1150
- return Writer.forContext(this.context, objectsPerTick).serializeToBuffer();
1151
- });
1110
+ async save(options = {}) {
1111
+ const { useObjectStreams = true, addDefaultPage = true, objectsPerTick = 50, updateFieldAppearances = true, } = options;
1112
+ (0, utils_1.assertIs)(useObjectStreams, 'useObjectStreams', ['boolean']);
1113
+ (0, utils_1.assertIs)(addDefaultPage, 'addDefaultPage', ['boolean']);
1114
+ (0, utils_1.assertIs)(objectsPerTick, 'objectsPerTick', ['number']);
1115
+ (0, utils_1.assertIs)(updateFieldAppearances, 'updateFieldAppearances', ['boolean']);
1116
+ if (addDefaultPage && this.getPageCount() === 0)
1117
+ this.addPage();
1118
+ if (updateFieldAppearances) {
1119
+ const form = this.formCache.getValue();
1120
+ if (form)
1121
+ form.updateFieldAppearances();
1122
+ }
1123
+ await this.flush();
1124
+ const Writer = useObjectStreams ? core_1.PDFStreamWriter : core_1.PDFWriter;
1125
+ return Writer.forContext(this.context, objectsPerTick).serializeToBuffer();
1152
1126
  }
1153
1127
  /**
1154
1128
  * Serialize this document to a base64 encoded string or data URI making up a
@@ -1165,14 +1139,12 @@ class PDFDocument {
1165
1139
  * @returns Resolves with a base64 encoded string or data URI of the
1166
1140
  * serialized document.
1167
1141
  */
1168
- saveAsBase64(options = {}) {
1169
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
1170
- const { dataUri = false } = options, otherOptions = (0, tslib_1.__rest)(options, ["dataUri"]);
1171
- (0, utils_1.assertIs)(dataUri, 'dataUri', ['boolean']);
1172
- const bytes = yield this.save(otherOptions);
1173
- const base64 = (0, utils_1.encodeToBase64)(bytes);
1174
- return dataUri ? `data:application/pdf;base64,${base64}` : base64;
1175
- });
1142
+ async saveAsBase64(options = {}) {
1143
+ const { dataUri = false, ...otherOptions } = options;
1144
+ (0, utils_1.assertIs)(dataUri, 'dataUri', ['boolean']);
1145
+ const bytes = await this.save(otherOptions);
1146
+ const base64 = (0, utils_1.encodeToBase64)(bytes);
1147
+ return dataUri ? `data:application/pdf;base64,${base64}` : base64;
1176
1148
  }
1177
1149
  findPageForAnnotationRef(ref) {
1178
1150
  const pages = this.getPages();
@@ -1185,12 +1157,10 @@ class PDFDocument {
1185
1157
  }
1186
1158
  return undefined;
1187
1159
  }
1188
- embedAll(embeddables) {
1189
- return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
1190
- for (let idx = 0, len = embeddables.length; idx < len; idx++) {
1191
- yield embeddables[idx].embed();
1192
- }
1193
- });
1160
+ async embedAll(embeddables) {
1161
+ for (let idx = 0, len = embeddables.length; idx < len; idx++) {
1162
+ await embeddables[idx].embed();
1163
+ }
1194
1164
  }
1195
1165
  updateInfoDict() {
1196
1166
  const pdfLib = `pdf-lib (https://github.com/Hopding/pdf-lib)`;