@pdfme/pdf-lib 6.0.3 → 6.0.4-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (449) hide show
  1. package/{src/api/Embeddable.ts → dist/api/Embeddable.d.ts} +1 -1
  2. package/dist/api/PDFDocument.d.ts +778 -0
  3. package/dist/api/PDFDocumentOptions.d.ts +38 -0
  4. package/dist/api/PDFEmbeddedFile.d.ts +38 -0
  5. package/dist/api/PDFEmbeddedPage.d.ts +73 -0
  6. package/dist/api/PDFFont.d.ts +94 -0
  7. package/dist/api/PDFImage.d.ts +95 -0
  8. package/dist/api/PDFJavaScript.d.ts +38 -0
  9. package/dist/api/PDFPage.d.ts +910 -0
  10. package/dist/api/PDFPageOptions.d.ts +172 -0
  11. package/dist/api/StandardFonts.d.ts +16 -0
  12. package/dist/api/colors.d.ts +34 -0
  13. package/dist/api/errors.d.ts +42 -0
  14. package/dist/api/form/PDFButton.d.ts +137 -0
  15. package/dist/api/form/PDFCheckBox.d.ts +142 -0
  16. package/dist/api/form/PDFDropdown.d.ts +402 -0
  17. package/dist/api/form/PDFField.d.ts +203 -0
  18. package/dist/api/form/PDFForm.d.ts +411 -0
  19. package/dist/api/form/PDFOptionList.d.ts +335 -0
  20. package/dist/api/form/PDFRadioGroup.d.ts +252 -0
  21. package/dist/api/form/PDFSignature.d.ts +29 -0
  22. package/dist/api/form/PDFTextField.d.ts +537 -0
  23. package/dist/api/form/appearances.d.ts +41 -0
  24. package/dist/api/image/alignment.d.ts +5 -0
  25. package/dist/api/objects.d.ts +4 -0
  26. package/dist/api/operations.d.ts +226 -0
  27. package/dist/api/operators.d.ts +90 -0
  28. package/dist/api/rotations.d.ts +38 -0
  29. package/dist/api/sizes.d.ts +52 -0
  30. package/dist/api/svg.d.ts +70 -0
  31. package/dist/api/svgPath.d.ts +2 -0
  32. package/dist/api/text/alignment.d.ts +5 -0
  33. package/dist/api/text/layout.d.ts +54 -0
  34. package/dist/core/PDFContext.d.ts +84 -0
  35. package/dist/core/PDFObjectCopier.d.ts +37 -0
  36. package/dist/core/acroform/PDFAcroButton.d.ts +21 -0
  37. package/dist/core/acroform/PDFAcroCheckBox.d.ts +13 -0
  38. package/dist/core/acroform/PDFAcroChoice.d.ts +20 -0
  39. package/dist/core/acroform/PDFAcroComboBox.d.ts +9 -0
  40. package/dist/core/acroform/PDFAcroField.d.ts +36 -0
  41. package/dist/core/acroform/PDFAcroForm.d.ts +20 -0
  42. package/dist/core/acroform/PDFAcroListBox.d.ts +9 -0
  43. package/dist/core/acroform/PDFAcroNonTerminal.d.ts +13 -0
  44. package/dist/core/acroform/PDFAcroPushButton.d.ts +9 -0
  45. package/dist/core/acroform/PDFAcroRadioButton.d.ts +13 -0
  46. package/{src/core/acroform/PDFAcroSignature.ts → dist/core/acroform/PDFAcroSignature.d.ts} +2 -4
  47. package/dist/core/acroform/PDFAcroTerminal.d.ts +16 -0
  48. package/dist/core/acroform/PDFAcroText.d.ts +22 -0
  49. package/dist/core/acroform/flags.d.ts +141 -0
  50. package/dist/core/acroform/utils.d.ts +6 -0
  51. package/dist/core/annotation/AppearanceCharacteristics.d.ts +33 -0
  52. package/dist/core/annotation/BorderStyle.d.ts +11 -0
  53. package/dist/core/annotation/PDFAnnotation.d.ts +51 -0
  54. package/dist/core/annotation/PDFWidgetAnnotation.d.ts +26 -0
  55. package/dist/core/annotation/flags.d.ts +79 -0
  56. package/dist/core/crypto.d.ts +95 -0
  57. package/dist/core/document/PDFCrossRefSection.d.ts +27 -0
  58. package/dist/core/document/PDFHeader.d.ts +10 -0
  59. package/dist/core/document/PDFTrailer.d.ts +9 -0
  60. package/dist/core/document/PDFTrailerDict.d.ts +10 -0
  61. package/dist/core/embedders/CMap.d.ts +3 -0
  62. package/dist/core/embedders/CustomFontEmbedder.d.ts +44 -0
  63. package/dist/core/embedders/CustomFontSubsetEmbedder.d.ts +20 -0
  64. package/dist/core/embedders/FileEmbedder.d.ts +33 -0
  65. package/dist/core/embedders/FontFlags.d.ts +13 -0
  66. package/dist/core/embedders/JavaScriptEmbedder.d.ts +10 -0
  67. package/dist/core/embedders/JpegEmbedder.d.ts +23 -0
  68. package/dist/core/embedders/PDFPageEmbedder.d.ts +37 -0
  69. package/dist/core/embedders/PngEmbedder.d.ts +19 -0
  70. package/dist/core/embedders/StandardFontEmbedder.d.ts +36 -0
  71. package/dist/core/errors.d.ts +94 -0
  72. package/{src/core/index.ts → dist/core/index.d.ts} +1 -17
  73. package/dist/core/interactive/ViewerPreferences.d.ts +347 -0
  74. package/dist/core/objects/PDFArray.d.ts +63 -0
  75. package/dist/core/objects/PDFBool.d.ts +13 -0
  76. package/dist/core/objects/PDFDict.d.ts +62 -0
  77. package/dist/core/objects/PDFHexString.d.ts +16 -0
  78. package/dist/core/objects/PDFInvalidObject.d.ts +11 -0
  79. package/dist/core/objects/PDFName.d.ts +41 -0
  80. package/dist/core/objects/PDFNull.d.ts +10 -0
  81. package/dist/core/objects/PDFNumber.d.ts +15 -0
  82. package/dist/core/objects/PDFObject.d.ts +8 -0
  83. package/dist/core/objects/PDFRawStream.d.ts +16 -0
  84. package/dist/core/objects/PDFRef.d.ts +13 -0
  85. package/dist/core/objects/PDFStream.d.ts +16 -0
  86. package/dist/core/objects/PDFString.d.ts +16 -0
  87. package/dist/core/operators/PDFOperator.d.ts +19 -0
  88. package/dist/core/operators/PDFOperatorNames.d.ts +76 -0
  89. package/dist/core/parser/BaseParser.d.ts +14 -0
  90. package/dist/core/parser/ByteStream.d.ts +26 -0
  91. package/dist/core/parser/PDFObjectParser.d.ts +31 -0
  92. package/dist/core/parser/PDFObjectStreamParser.d.ts +13 -0
  93. package/dist/core/parser/PDFParser.d.ts +46 -0
  94. package/dist/core/parser/PDFXRefStreamParser.d.ts +21 -0
  95. package/dist/core/streams/Ascii85Stream.d.ts +9 -0
  96. package/dist/core/streams/AsciiHexStream.d.ts +9 -0
  97. package/dist/core/streams/DecodeStream.d.ts +26 -0
  98. package/dist/core/streams/DecryptStream.d.ts +12 -0
  99. package/dist/core/streams/FlateStream.d.ts +13 -0
  100. package/dist/core/streams/LZWStream.d.ts +12 -0
  101. package/dist/core/streams/RunLengthStream.d.ts +8 -0
  102. package/dist/core/streams/Stream.d.ts +34 -0
  103. package/dist/core/streams/decode.d.ts +3 -0
  104. package/dist/core/structures/PDFCatalog.d.ts +27 -0
  105. package/dist/core/structures/PDFContentStream.d.ts +15 -0
  106. package/dist/core/structures/PDFCrossRefStream.d.ts +53 -0
  107. package/dist/core/structures/PDFFlateStream.d.ts +13 -0
  108. package/dist/core/structures/PDFObjectStream.d.ts +20 -0
  109. package/dist/core/structures/PDFPageLeaf.d.ts +54 -0
  110. package/dist/core/structures/PDFPageTree.d.ts +41 -0
  111. package/dist/core/syntax/CharCodes.d.ts +61 -0
  112. package/dist/core/syntax/Delimiters.d.ts +1 -0
  113. package/dist/core/syntax/Irregular.d.ts +1 -0
  114. package/dist/core/syntax/Keywords.d.ts +22 -0
  115. package/dist/core/syntax/Numeric.d.ts +3 -0
  116. package/dist/core/syntax/Whitespace.d.ts +1 -0
  117. package/dist/core/writers/PDFStreamWriter.d.ts +19 -0
  118. package/dist/core/writers/PDFWriter.d.ts +29 -0
  119. package/dist/index.js +19124 -0
  120. package/dist/index.js.map +1 -0
  121. package/dist/types/fontkit.d.ts +597 -0
  122. package/{src/types/index.ts → dist/types/index.d.ts} +8 -13
  123. package/{src/types/matrix.ts → dist/types/matrix.d.ts} +1 -2
  124. package/dist/utils/Cache.d.ts +10 -0
  125. package/dist/utils/arrays.d.ts +16 -0
  126. package/dist/utils/async.d.ts +5 -0
  127. package/dist/utils/base64.d.ts +10 -0
  128. package/dist/utils/elements/Arc.d.ts +22 -0
  129. package/dist/utils/elements/Circle.d.ts +14 -0
  130. package/dist/utils/elements/Ellipse.d.ts +20 -0
  131. package/dist/utils/elements/GraphElement.d.ts +6 -0
  132. package/dist/utils/elements/Line.d.ts +23 -0
  133. package/dist/utils/elements/Plot.d.ts +11 -0
  134. package/dist/utils/elements/Point.d.ts +18 -0
  135. package/dist/utils/elements/Rectangle.d.ts +21 -0
  136. package/dist/utils/elements/Segment.d.ts +19 -0
  137. package/dist/utils/errors.d.ts +1 -0
  138. package/dist/utils/intersections.d.ts +9 -0
  139. package/dist/utils/maths.d.ts +45 -0
  140. package/dist/utils/numbers.d.ts +23 -0
  141. package/dist/utils/objects.d.ts +15 -0
  142. package/dist/utils/pdfDocEncoding.d.ts +7 -0
  143. package/dist/utils/png.d.ts +17 -0
  144. package/dist/utils/rng.d.ts +13 -0
  145. package/dist/utils/strings.d.ts +25 -0
  146. package/{src/utils/unicode.ts → dist/utils/unicode.d.ts} +8 -195
  147. package/dist/utils/validators.d.ts +26 -0
  148. package/package.json +6 -1
  149. package/__tests__/api/PDFDocument.spec.ts +0 -576
  150. package/__tests__/api/PDFImage.spec.ts +0 -60
  151. package/__tests__/api/PDFPage.spec.ts +0 -151
  152. package/__tests__/api/form/PDFCheckBox.spec.ts +0 -71
  153. package/__tests__/api/form/PDFDropdown.spec.ts +0 -118
  154. package/__tests__/api/form/PDFForm.spec.ts +0 -370
  155. package/__tests__/api/form/PDFOptionList.spec.ts +0 -104
  156. package/__tests__/api/form/PDFRadioGroup.spec.ts +0 -183
  157. package/__tests__/api/form/PDFTextField.spec.ts +0 -148
  158. package/__tests__/api/svgPath.spec.ts +0 -153
  159. package/__tests__/api/text/layout.spec.ts +0 -166
  160. package/__tests__/core/PDFContext.spec.ts +0 -194
  161. package/__tests__/core/PDFObjectCopier.spec.ts +0 -421
  162. package/__tests__/core/acroform/PDFAcroField.spec.ts +0 -98
  163. package/__tests__/core/acroform/PDFAcroTerminal.spec.ts +0 -39
  164. package/__tests__/core/annotation/PDFWidgetAnnotation.spec.ts +0 -40
  165. package/__tests__/core/document/PDFCrossRefSection.spec.ts +0 -88
  166. package/__tests__/core/document/PDFHeader.spec.ts +0 -22
  167. package/__tests__/core/document/PDFTrailer.spec.ts +0 -28
  168. package/__tests__/core/document/PDFTrailerDict.spec.ts +0 -24
  169. package/__tests__/core/embedders/CMap.spec.ts +0 -47
  170. package/__tests__/core/embedders/CustomFontEmbedder.spec.ts +0 -96
  171. package/__tests__/core/embedders/CustomFontSubsetEmbedder.spec.ts +0 -62
  172. package/__tests__/core/embedders/FileEmbedder.spec.ts +0 -54
  173. package/__tests__/core/embedders/JpegEmbedder.spec.ts +0 -67
  174. package/__tests__/core/embedders/PDFPageEmbedder.spec.ts +0 -65
  175. package/__tests__/core/embedders/PngEmbedder.spec.ts +0 -87
  176. package/__tests__/core/embedders/StandardFontEmbedder.spec.ts +0 -79
  177. package/__tests__/core/embedders/data/SourceHanSerifJP-Regular.otf.cmap +0 -16096
  178. package/__tests__/core/embedders/data/Ubuntu-R.ttf.cmap +0 -1211
  179. package/__tests__/core/objects/PDFArray.spec.ts +0 -134
  180. package/__tests__/core/objects/PDFBool.spec.ts +0 -42
  181. package/__tests__/core/objects/PDFDict.spec.ts +0 -195
  182. package/__tests__/core/objects/PDFHexString.spec.ts +0 -168
  183. package/__tests__/core/objects/PDFInvalidObject.spec.ts +0 -32
  184. package/__tests__/core/objects/PDFName.spec.ts +0 -122
  185. package/__tests__/core/objects/PDFNull.spec.ts +0 -30
  186. package/__tests__/core/objects/PDFNumber.spec.ts +0 -50
  187. package/__tests__/core/objects/PDFObject.spec.ts +0 -29
  188. package/__tests__/core/objects/PDFRawStream.spec.ts +0 -45
  189. package/__tests__/core/objects/PDFRef.spec.ts +0 -60
  190. package/__tests__/core/objects/PDFString.spec.ts +0 -237
  191. package/__tests__/core/operators/PDFOperator.spec.ts +0 -77
  192. package/__tests__/core/parser/PDFObjectParser.spec.ts +0 -730
  193. package/__tests__/core/parser/PDFObjectStreamParser.spec.ts +0 -158
  194. package/__tests__/core/parser/PDFParser.spec.ts +0 -398
  195. package/__tests__/core/parser/PDFXRefStreamParser.spec.ts +0 -187
  196. package/__tests__/core/parser/data/object-stream-invalid +0 -2
  197. package/__tests__/core/parser/data/object-stream1 +0 -16
  198. package/__tests__/core/parser/data/object-stream2 +0 -10
  199. package/__tests__/core/parser/data/object-stream3 +0 -0
  200. package/__tests__/core/parser/data/object-stream4 +0 -0
  201. package/__tests__/core/parser/data/xref-stream1 +0 -0
  202. package/__tests__/core/parser/data/xref-stream2 +0 -0
  203. package/__tests__/core/parser/data/xref-stream3 +0 -0
  204. package/__tests__/core/parser/data/xref-stream4 +0 -0
  205. package/__tests__/core/streams/Ascii85Stream.spec.ts +0 -20
  206. package/__tests__/core/streams/AsciiHexStream.spec.ts +0 -20
  207. package/__tests__/core/streams/FlateStream.spec.ts +0 -20
  208. package/__tests__/core/streams/LZWStream.spec.ts +0 -20
  209. package/__tests__/core/streams/RunLengthStream.spec.ts +0 -20
  210. package/__tests__/core/streams/Stream.spec.ts +0 -128
  211. package/__tests__/core/streams/data/ascii85/1.decoded +0 -0
  212. package/__tests__/core/streams/data/ascii85/1.encoded +0 -43
  213. package/__tests__/core/streams/data/asciihex/1.decoded +0 -1
  214. package/__tests__/core/streams/data/asciihex/1.encoded +0 -1
  215. package/__tests__/core/streams/data/asciihex/2.decoded +0 -7
  216. package/__tests__/core/streams/data/asciihex/2.encoded +0 -1
  217. package/__tests__/core/streams/data/flate/1.corrupt.encoded +0 -1
  218. package/__tests__/core/streams/data/flate/1.decoded +0 -0
  219. package/__tests__/core/streams/data/flate/1.encoded +0 -2
  220. package/__tests__/core/streams/data/flate/2.decoded +0 -33
  221. package/__tests__/core/streams/data/flate/2.encoded +0 -0
  222. package/__tests__/core/streams/data/flate/3.decoded +0 -15
  223. package/__tests__/core/streams/data/flate/3.encoded +0 -0
  224. package/__tests__/core/streams/data/flate/4.decoded +0 -16
  225. package/__tests__/core/streams/data/flate/4.encoded +0 -0
  226. package/__tests__/core/streams/data/flate/5.decoded +0 -0
  227. package/__tests__/core/streams/data/flate/5.encoded +0 -0
  228. package/__tests__/core/streams/data/flate/6.decoded +0 -17
  229. package/__tests__/core/streams/data/flate/6.encoded +0 -0
  230. package/__tests__/core/streams/data/flate/7.decoded +0 -20
  231. package/__tests__/core/streams/data/flate/7.encoded +0 -1
  232. package/__tests__/core/streams/data/lzw/1.decoded +0 -0
  233. package/__tests__/core/streams/data/lzw/1.encoded +0 -0
  234. package/__tests__/core/streams/data/lzw/2.decoded +0 -0
  235. package/__tests__/core/streams/data/lzw/2.encoded +0 -0
  236. package/__tests__/core/streams/data/lzw/3.decoded +0 -14
  237. package/__tests__/core/streams/data/lzw/3.encoded +0 -2
  238. package/__tests__/core/streams/data/lzw/4.decoded +0 -14
  239. package/__tests__/core/streams/data/lzw/4.encoded +0 -2
  240. package/__tests__/core/streams/data/runlength/1.decoded +0 -1
  241. package/__tests__/core/streams/data/runlength/1.encoded +0 -1
  242. package/__tests__/core/streams/data/runlength/2.decoded +0 -1
  243. package/__tests__/core/streams/data/runlength/2.encoded +0 -1
  244. package/__tests__/core/streams/data/runlength/3.decoded +0 -0
  245. package/__tests__/core/streams/data/runlength/3.encoded +0 -0
  246. package/__tests__/core/streams/data/runlength/4.decoded +0 -1
  247. package/__tests__/core/streams/data/runlength/4.encoded +0 -1
  248. package/__tests__/core/streams/data/runlength/5.decoded +0 -1
  249. package/__tests__/core/streams/data/runlength/5.encoded +0 -1
  250. package/__tests__/core/streams/data/runlength/empty.encoded +0 -1
  251. package/__tests__/core/structures/PDFCatalog.spec.ts +0 -108
  252. package/__tests__/core/structures/PDFContentStream.spec.ts +0 -117
  253. package/__tests__/core/structures/PDFCrossRefStream.spec.ts +0 -145
  254. package/__tests__/core/structures/PDFObjectStream.spec.ts +0 -138
  255. package/__tests__/core/structures/PDFPageLeaf.spec.ts +0 -355
  256. package/__tests__/core/structures/PDFPageTree.spec.ts +0 -592
  257. package/__tests__/core/writers/PDFStreamWriter.spec.ts +0 -71
  258. package/__tests__/core/writers/PDFWriter.spec.ts +0 -144
  259. package/__tests__/core/writers/data/stream-writer-1.pdf +0 -0
  260. package/__tests__/index.test.ts +0 -12
  261. package/__tests__/utils/base64.spec.ts +0 -61
  262. package/__tests__/utils/data/simple.pdf +0 -201
  263. package/__tests__/utils/data/simple.pdf.base64 +0 -1
  264. package/__tests__/utils/pdfDocEncoding.spec.ts +0 -82
  265. package/__tests__/utils/png.spec.ts +0 -53
  266. package/__tests__/utils/rng.spec.ts +0 -14
  267. package/__tests__/utils/strings.spec.ts +0 -98
  268. package/__tests__/utils/unicode.spec.ts +0 -337
  269. package/assets/fonts/source_hans_jp/SourceHanSerifJP-Regular.otf +0 -0
  270. package/assets/fonts/ubuntu/Ubuntu-B.ttf +0 -0
  271. package/assets/fonts/ubuntu/Ubuntu-R.ttf +0 -0
  272. package/assets/images/cat_riding_unicorn.jpg +0 -0
  273. package/assets/images/cmyk_colorspace.jpg +0 -0
  274. package/assets/images/greyscale_bird.png +0 -0
  275. package/assets/images/minions_banana_alpha.png +0 -0
  276. package/assets/images/minions_banana_no_alpha.png +0 -0
  277. package/assets/images/minions_laughing.jpg +0 -0
  278. package/assets/pdfs/bixby_guide.pdf +0 -0
  279. package/assets/pdfs/encrypted_new.pdf +0 -0
  280. package/assets/pdfs/encrypted_old.pdf +0 -0
  281. package/assets/pdfs/fancy_fields.pdf +0 -0
  282. package/assets/pdfs/giraffe.pdf +0 -0
  283. package/assets/pdfs/invalid_root_ref.pdf +0 -0
  284. package/assets/pdfs/just_metadata.pdf +0 -49
  285. package/assets/pdfs/missing_endobj_keyword.pdf +0 -70
  286. package/assets/pdfs/missing_xref_trailer_dict.pdf +0 -141
  287. package/assets/pdfs/normal.pdf +0 -0
  288. package/assets/pdfs/pdf20examples/PDF 2.0 with offset start.pdf +0 -179
  289. package/assets/pdfs/stuff_following_header.pdf +0 -0
  290. package/assets/pdfs/us_constitution.pdf +0 -0
  291. package/assets/pdfs/with_comments.pdf +0 -0
  292. package/assets/pdfs/with_invalid_objects.pdf +0 -53
  293. package/assets/pdfs/with_invalid_stream_EOL.pdf +0 -0
  294. package/assets/pdfs/with_null_parent_entry.pdf +0 -0
  295. package/assets/pdfs/with_signature.pdf +0 -0
  296. package/assets/pdfs/with_update_sections.pdf +0 -0
  297. package/assets/pdfs/with_viewer_prefs.pdf +0 -0
  298. package/assets/pdfs/with_xfa_fields.pdf +0 -0
  299. package/src/api/PDFDocument.ts +0 -1389
  300. package/src/api/PDFDocumentOptions.ts +0 -45
  301. package/src/api/PDFEmbeddedFile.ts +0 -87
  302. package/src/api/PDFEmbeddedPage.ts +0 -100
  303. package/src/api/PDFFont.ts +0 -149
  304. package/src/api/PDFImage.ts +0 -143
  305. package/src/api/PDFJavaScript.ts +0 -75
  306. package/src/api/PDFPage.ts +0 -1604
  307. package/src/api/PDFPageOptions.ts +0 -175
  308. package/src/api/StandardFonts.ts +0 -16
  309. package/src/api/colors.ts +0 -109
  310. package/src/api/errors.ts +0 -110
  311. package/src/api/form/PDFButton.ts +0 -242
  312. package/src/api/form/PDFCheckBox.ts +0 -247
  313. package/src/api/form/PDFDropdown.ts +0 -637
  314. package/src/api/form/PDFField.ts +0 -491
  315. package/src/api/form/PDFForm.ts +0 -842
  316. package/src/api/form/PDFOptionList.ts +0 -555
  317. package/src/api/form/PDFRadioGroup.ts +0 -455
  318. package/src/api/form/PDFSignature.ts +0 -44
  319. package/src/api/form/PDFTextField.ts +0 -811
  320. package/src/api/form/appearances.ts +0 -655
  321. package/src/api/image/alignment.ts +0 -5
  322. package/src/api/objects.ts +0 -10
  323. package/src/api/operations.ts +0 -873
  324. package/src/api/operators.ts +0 -335
  325. package/src/api/rotations.ts +0 -84
  326. package/src/api/sizes.ts +0 -52
  327. package/src/api/svg.ts +0 -888
  328. package/src/api/svgPath.ts +0 -460
  329. package/src/api/text/alignment.ts +0 -5
  330. package/src/api/text/layout.ts +0 -328
  331. package/src/core/PDFContext.ts +0 -274
  332. package/src/core/PDFObjectCopier.ts +0 -142
  333. package/src/core/acroform/PDFAcroButton.ts +0 -104
  334. package/src/core/acroform/PDFAcroCheckBox.ts +0 -48
  335. package/src/core/acroform/PDFAcroChoice.ts +0 -143
  336. package/src/core/acroform/PDFAcroComboBox.ts +0 -21
  337. package/src/core/acroform/PDFAcroField.ts +0 -168
  338. package/src/core/acroform/PDFAcroForm.ts +0 -96
  339. package/src/core/acroform/PDFAcroListBox.ts +0 -19
  340. package/src/core/acroform/PDFAcroNonTerminal.ts +0 -33
  341. package/src/core/acroform/PDFAcroPushButton.ts +0 -21
  342. package/src/core/acroform/PDFAcroRadioButton.ts +0 -57
  343. package/src/core/acroform/PDFAcroTerminal.ts +0 -70
  344. package/src/core/acroform/PDFAcroText.ts +0 -76
  345. package/src/core/acroform/flags.ts +0 -162
  346. package/src/core/acroform/utils.ts +0 -129
  347. package/src/core/annotation/AppearanceCharacteristics.ts +0 -133
  348. package/src/core/annotation/BorderStyle.ts +0 -31
  349. package/src/core/annotation/PDFAnnotation.ts +0 -148
  350. package/src/core/annotation/PDFWidgetAnnotation.ts +0 -111
  351. package/src/core/annotation/flags.ts +0 -90
  352. package/src/core/crypto.ts +0 -1844
  353. package/src/core/document/PDFCrossRefSection.ts +0 -161
  354. package/src/core/document/PDFHeader.ts +0 -48
  355. package/src/core/document/PDFTrailer.ts +0 -48
  356. package/src/core/document/PDFTrailerDict.ts +0 -39
  357. package/src/core/embedders/CMap.ts +0 -65
  358. package/src/core/embedders/CustomFontEmbedder.ts +0 -237
  359. package/src/core/embedders/CustomFontSubsetEmbedder.ts +0 -89
  360. package/src/core/embedders/FileEmbedder.ts +0 -77
  361. package/src/core/embedders/FontFlags.ts +0 -45
  362. package/src/core/embedders/JavaScriptEmbedder.ts +0 -34
  363. package/src/core/embedders/JpegEmbedder.ts +0 -118
  364. package/src/core/embedders/PDFPageEmbedder.ts +0 -139
  365. package/src/core/embedders/PngEmbedder.ts +0 -69
  366. package/src/core/embedders/StandardFontEmbedder.ts +0 -121
  367. package/src/core/errors.ts +0 -226
  368. package/src/core/interactive/ViewerPreferences.ts +0 -565
  369. package/src/core/objects/PDFArray.ts +0 -179
  370. package/src/core/objects/PDFBool.ts +0 -53
  371. package/src/core/objects/PDFDict.ts +0 -220
  372. package/src/core/objects/PDFHexString.ts +0 -94
  373. package/src/core/objects/PDFInvalidObject.ts +0 -34
  374. package/src/core/objects/PDFName.ts +0 -152
  375. package/src/core/objects/PDFNull.ts +0 -30
  376. package/src/core/objects/PDFNumber.ts +0 -44
  377. package/src/core/objects/PDFObject.ts +0 -22
  378. package/src/core/objects/PDFRawStream.ts +0 -41
  379. package/src/core/objects/PDFRef.ts +0 -51
  380. package/src/core/objects/PDFStream.ts +0 -87
  381. package/src/core/objects/PDFString.ts +0 -118
  382. package/src/core/operators/PDFOperator.ts +0 -72
  383. package/src/core/operators/PDFOperatorNames.ts +0 -92
  384. package/src/core/parser/BaseParser.ts +0 -119
  385. package/src/core/parser/ByteStream.ts +0 -76
  386. package/src/core/parser/PDFObjectParser.ts +0 -302
  387. package/src/core/parser/PDFObjectStreamParser.ts +0 -65
  388. package/src/core/parser/PDFParser.ts +0 -363
  389. package/src/core/parser/PDFXRefStreamParser.ts +0 -129
  390. package/src/core/streams/Ascii85Stream.ts +0 -97
  391. package/src/core/streams/AsciiHexStream.ts +0 -77
  392. package/src/core/streams/DecodeStream.ts +0 -188
  393. package/src/core/streams/DecryptStream.ts +0 -49
  394. package/src/core/streams/FlateStream.ts +0 -397
  395. package/src/core/streams/LZWStream.ts +0 -157
  396. package/src/core/streams/RunLengthStream.ts +0 -55
  397. package/src/core/streams/Stream.ts +0 -126
  398. package/src/core/streams/decode.ts +0 -70
  399. package/src/core/structures/PDFCatalog.ts +0 -81
  400. package/src/core/structures/PDFContentStream.ts +0 -58
  401. package/src/core/structures/PDFCrossRefStream.ts +0 -238
  402. package/src/core/structures/PDFFlateStream.ts +0 -40
  403. package/src/core/structures/PDFObjectStream.ts +0 -91
  404. package/src/core/structures/PDFPageLeaf.ts +0 -244
  405. package/src/core/structures/PDFPageTree.ts +0 -192
  406. package/src/core/syntax/CharCodes.ts +0 -62
  407. package/src/core/syntax/Delimiters.ts +0 -14
  408. package/src/core/syntax/Irregular.ts +0 -10
  409. package/src/core/syntax/Keywords.ts +0 -57
  410. package/src/core/syntax/Numeric.ts +0 -26
  411. package/src/core/syntax/Whitespace.ts +0 -10
  412. package/src/core/writers/PDFStreamWriter.ts +0 -114
  413. package/src/core/writers/PDFWriter.ts +0 -146
  414. package/src/types/fontkit.ts +0 -643
  415. package/src/utils/Cache.ts +0 -26
  416. package/src/utils/arrays.ts +0 -153
  417. package/src/utils/async.ts +0 -8
  418. package/src/utils/base64.ts +0 -98
  419. package/src/utils/elements/Arc.ts +0 -97
  420. package/src/utils/elements/Circle.ts +0 -47
  421. package/src/utils/elements/Ellipse.ts +0 -117
  422. package/src/utils/elements/GraphElement.ts +0 -14
  423. package/src/utils/elements/Line.ts +0 -83
  424. package/src/utils/elements/Plot.ts +0 -50
  425. package/src/utils/elements/Point.ts +0 -37
  426. package/src/utils/elements/Rectangle.ts +0 -65
  427. package/src/utils/elements/Segment.ts +0 -81
  428. package/src/utils/errors.ts +0 -3
  429. package/src/utils/intersections.ts +0 -237
  430. package/src/utils/maths.ts +0 -96
  431. package/src/utils/numbers.ts +0 -55
  432. package/src/utils/objects.ts +0 -13
  433. package/src/utils/pdfDocEncoding.ts +0 -69
  434. package/src/utils/png.ts +0 -100
  435. package/src/utils/rng.ts +0 -21
  436. package/src/utils/strings.ts +0 -191
  437. package/src/utils/validators.ts +0 -192
  438. package/tsconfig.build.json +0 -14
  439. package/tsconfig.json +0 -15
  440. package/vite.config.mts +0 -39
  441. /package/{src/api/form/index.ts → dist/api/form/index.d.ts} +0 -0
  442. /package/{src/api/image/index.ts → dist/api/image/index.d.ts} +0 -0
  443. /package/{src/api/index.ts → dist/api/index.d.ts} +0 -0
  444. /package/{src/api/text/index.ts → dist/api/text/index.d.ts} +0 -0
  445. /package/{src/core/acroform/index.ts → dist/core/acroform/index.d.ts} +0 -0
  446. /package/{src/core/annotation/index.ts → dist/core/annotation/index.d.ts} +0 -0
  447. /package/{src/index.ts → dist/index.d.ts} +0 -0
  448. /package/{src/utils/elements/index.ts → dist/utils/elements/index.d.ts} +0 -0
  449. /package/{src/utils/index.ts → dist/utils/index.d.ts} +0 -0
@@ -1,1604 +0,0 @@
1
- import { Color, rgb } from './colors';
2
- import {
3
- drawImage,
4
- drawLine,
5
- drawLinesOfText,
6
- drawPage,
7
- drawRectangle,
8
- drawSvgPath,
9
- drawEllipse,
10
- } from './operations';
11
- import {
12
- popGraphicsState,
13
- pushGraphicsState,
14
- translate,
15
- LineCapStyle,
16
- scale,
17
- FillRule,
18
- } from './operators';
19
- import PDFDocument from './PDFDocument';
20
- import PDFEmbeddedPage from './PDFEmbeddedPage';
21
- import PDFFont from './PDFFont';
22
- import PDFImage from './PDFImage';
23
- import {
24
- PDFPageDrawCircleOptions,
25
- PDFPageDrawEllipseOptions,
26
- PDFPageDrawImageOptions,
27
- PDFPageDrawLineOptions,
28
- PDFPageDrawPageOptions,
29
- PDFPageDrawRectangleOptions,
30
- PDFPageDrawSquareOptions,
31
- PDFPageDrawSVGOptions,
32
- PDFPageDrawTextOptions,
33
- BlendMode,
34
- PDFPageDrawSVGElementOptions,
35
- } from './PDFPageOptions';
36
- import { degrees, Rotation, toDegrees } from './rotations';
37
- import { StandardFonts } from './StandardFonts';
38
- import {
39
- PDFContentStream,
40
- PDFHexString,
41
- PDFName,
42
- PDFOperator,
43
- PDFPageLeaf,
44
- PDFRef,
45
- PDFDict,
46
- PDFArray,
47
- } from '../core';
48
- import {
49
- assertEachIs,
50
- assertIs,
51
- assertMultiple,
52
- assertOrUndefined,
53
- breakTextIntoLines,
54
- cleanText,
55
- rectanglesAreEqual,
56
- lineSplit,
57
- assertRangeOrUndefined,
58
- assertIsOneOfOrUndefined,
59
- } from '../utils';
60
- import { drawSvg } from './svg';
61
-
62
- /**
63
- * Represents a single page of a [[PDFDocument]].
64
- */
65
- export default class PDFPage {
66
- /**
67
- * > **NOTE:** You probably don't want to call this method directly. Instead,
68
- * > consider using the [[PDFDocument.addPage]] and [[PDFDocument.insertPage]]
69
- * > methods, which can create instances of [[PDFPage]] for you.
70
- *
71
- * Create an instance of [[PDFPage]] from an existing leaf node.
72
- *
73
- * @param leafNode The leaf node to be wrapped.
74
- * @param ref The unique reference for the page.
75
- * @param doc The document to which the page will belong.
76
- */
77
- static of = (leafNode: PDFPageLeaf, ref: PDFRef, doc: PDFDocument) =>
78
- new PDFPage(leafNode, ref, doc);
79
-
80
- /**
81
- * > **NOTE:** You probably don't want to call this method directly. Instead,
82
- * > consider using the [[PDFDocument.addPage]] and [[PDFDocument.insertPage]]
83
- * > methods, which can create instances of [[PDFPage]] for you.
84
- *
85
- * Create an instance of [[PDFPage]].
86
- *
87
- * @param doc The document to which the page will belong.
88
- */
89
- static create = (doc: PDFDocument) => {
90
- assertIs(doc, 'doc', [[PDFDocument, 'PDFDocument']]);
91
- const dummyRef = PDFRef.of(-1);
92
- const pageLeaf = PDFPageLeaf.withContextAndParent(doc.context, dummyRef);
93
- const pageRef = doc.context.register(pageLeaf);
94
- return new PDFPage(pageLeaf, pageRef, doc);
95
- };
96
-
97
- /** The low-level PDFDictionary wrapped by this page. */
98
- readonly node: PDFPageLeaf;
99
-
100
- /** The unique reference assigned to this page within the document. */
101
- readonly ref: PDFRef;
102
-
103
- /** The document to which this page belongs. */
104
- readonly doc: PDFDocument;
105
-
106
- private fontKey?: PDFName;
107
- private font?: PDFFont;
108
- private fontSize = 24;
109
- private fontColor = rgb(0, 0, 0) as Color;
110
- private lineHeight = 24;
111
- private x = 0;
112
- private y = 0;
113
- private contentStream?: PDFContentStream;
114
- private contentStreamRef?: PDFRef;
115
-
116
- private constructor(leafNode: PDFPageLeaf, ref: PDFRef, doc: PDFDocument) {
117
- assertIs(leafNode, 'leafNode', [[PDFPageLeaf, 'PDFPageLeaf']]);
118
- assertIs(ref, 'ref', [[PDFRef, 'PDFRef']]);
119
- assertIs(doc, 'doc', [[PDFDocument, 'PDFDocument']]);
120
-
121
- this.node = leafNode;
122
- this.ref = ref;
123
- this.doc = doc;
124
- }
125
-
126
- /**
127
- * Rotate this page by a multiple of 90 degrees. For example:
128
- * ```js
129
- * import { degrees } from 'pdf-lib'
130
- *
131
- * page.setRotation(degrees(-90))
132
- * page.setRotation(degrees(0))
133
- * page.setRotation(degrees(90))
134
- * page.setRotation(degrees(180))
135
- * page.setRotation(degrees(270))
136
- * ```
137
- * @param angle The angle to rotate this page.
138
- */
139
- setRotation(angle: Rotation): void {
140
- const degreesAngle = toDegrees(angle);
141
- assertMultiple(degreesAngle, 'degreesAngle', 90);
142
- this.node.set(PDFName.of('Rotate'), this.doc.context.obj(degreesAngle));
143
- }
144
-
145
- /**
146
- * Get this page's rotation angle in degrees. For example:
147
- * ```js
148
- * const rotationAngle = page.getRotation().angle;
149
- * ```
150
- * @returns The rotation angle of the page in degrees (always a multiple of
151
- * 90 degrees).
152
- */
153
- getRotation(): Rotation {
154
- const Rotate = this.node.Rotate();
155
- return degrees(Rotate ? Rotate.asNumber() : 0);
156
- }
157
-
158
- /**
159
- * Resize this page by increasing or decreasing its width and height. For
160
- * example:
161
- * ```js
162
- * page.setSize(250, 500)
163
- * page.setSize(page.getWidth() + 50, page.getHeight() + 100)
164
- * page.setSize(page.getWidth() - 50, page.getHeight() - 100)
165
- * ```
166
- *
167
- * Note that the PDF specification does not allow for pages to have explicit
168
- * widths and heights. Instead it defines the "size" of a page in terms of
169
- * five rectangles: the MediaBox, CropBox, BleedBox, TrimBox, and ArtBox. As a
170
- * result, this method cannot directly change the width and height of a page.
171
- * Instead, it works by adjusting these five boxes.
172
- *
173
- * This method performs the following steps:
174
- * 1. Set width & height of MediaBox.
175
- * 2. Set width & height of CropBox, if it has same dimensions as MediaBox.
176
- * 3. Set width & height of BleedBox, if it has same dimensions as MediaBox.
177
- * 4. Set width & height of TrimBox, if it has same dimensions as MediaBox.
178
- * 5. Set width & height of ArtBox, if it has same dimensions as MediaBox.
179
- *
180
- * This approach works well for most PDF documents as all PDF pages must
181
- * have a MediaBox, but relatively few have a CropBox, BleedBox, TrimBox, or
182
- * ArtBox. And when they do have these additional boxes, they often have the
183
- * same dimensions as the MediaBox. However, if you find this method does not
184
- * work for your document, consider setting the boxes directly:
185
- * * [[PDFPage.setMediaBox]]
186
- * * [[PDFPage.setCropBox]]
187
- * * [[PDFPage.setBleedBox]]
188
- * * [[PDFPage.setTrimBox]]
189
- * * [[PDFPage.setArtBox]]
190
- *
191
- * @param width The new width of the page.
192
- * @param height The new height of the page.
193
- */
194
- setSize(width: number, height: number): void {
195
- assertIs(width, 'width', ['number']);
196
- assertIs(height, 'height', ['number']);
197
-
198
- const mediaBox = this.getMediaBox();
199
- this.setMediaBox(mediaBox.x, mediaBox.y, width, height);
200
-
201
- const cropBox = this.getCropBox();
202
- const bleedBox = this.getBleedBox();
203
- const trimBox = this.getTrimBox();
204
- const artBox = this.getArtBox();
205
-
206
- const hasCropBox = Boolean(this.node.CropBox());
207
- const hasBleedBox = Boolean(this.node.BleedBox());
208
- const hasTrimBox = Boolean(this.node.TrimBox());
209
- const hasArtBox = Boolean(this.node.ArtBox());
210
-
211
- if (hasCropBox && rectanglesAreEqual(cropBox, mediaBox)) {
212
- this.setCropBox(mediaBox.x, mediaBox.y, width, height);
213
- }
214
- if (hasBleedBox && rectanglesAreEqual(bleedBox, mediaBox)) {
215
- this.setBleedBox(mediaBox.x, mediaBox.y, width, height);
216
- }
217
- if (hasTrimBox && rectanglesAreEqual(trimBox, mediaBox)) {
218
- this.setTrimBox(mediaBox.x, mediaBox.y, width, height);
219
- }
220
- if (hasArtBox && rectanglesAreEqual(artBox, mediaBox)) {
221
- this.setArtBox(mediaBox.x, mediaBox.y, width, height);
222
- }
223
- }
224
-
225
- /**
226
- * Resize this page by increasing or decreasing its width. For example:
227
- * ```js
228
- * page.setWidth(250)
229
- * page.setWidth(page.getWidth() + 50)
230
- * page.setWidth(page.getWidth() - 50)
231
- * ```
232
- *
233
- * This method uses [[PDFPage.setSize]] to set the page's width.
234
- *
235
- * @param width The new width of the page.
236
- */
237
- setWidth(width: number): void {
238
- assertIs(width, 'width', ['number']);
239
- this.setSize(width, this.getSize().height);
240
- }
241
-
242
- /**
243
- * Resize this page by increasing or decreasing its height. For example:
244
- * ```js
245
- * page.setHeight(500)
246
- * page.setHeight(page.getWidth() + 100)
247
- * page.setHeight(page.getWidth() - 100)
248
- * ```
249
- *
250
- * This method uses [[PDFPage.setSize]] to set the page's height.
251
- *
252
- * @param height The new height of the page.
253
- */
254
- setHeight(height: number): void {
255
- assertIs(height, 'height', ['number']);
256
- this.setSize(this.getSize().width, height);
257
- }
258
-
259
- /**
260
- * Set the MediaBox of this page. For example:
261
- * ```js
262
- * const mediaBox = page.getMediaBox()
263
- *
264
- * page.setMediaBox(0, 0, 250, 500)
265
- * page.setMediaBox(mediaBox.x, mediaBox.y, 50, 100)
266
- * page.setMediaBox(15, 5, mediaBox.width - 50, mediaBox.height - 100)
267
- * ```
268
- *
269
- * See [[PDFPage.getMediaBox]] for details about what the MediaBox represents.
270
- *
271
- * @param x The x coordinate of the lower left corner of the new MediaBox.
272
- * @param y The y coordinate of the lower left corner of the new MediaBox.
273
- * @param width The width of the new MediaBox.
274
- * @param height The height of the new MediaBox.
275
- */
276
- setMediaBox(x: number, y: number, width: number, height: number): void {
277
- assertIs(x, 'x', ['number']);
278
- assertIs(y, 'y', ['number']);
279
- assertIs(width, 'width', ['number']);
280
- assertIs(height, 'height', ['number']);
281
- const mediaBox = this.doc.context.obj([x, y, x + width, y + height]);
282
- this.node.set(PDFName.MediaBox, mediaBox);
283
- }
284
-
285
- /**
286
- * Set the CropBox of this page. For example:
287
- * ```js
288
- * const cropBox = page.getCropBox()
289
- *
290
- * page.setCropBox(0, 0, 250, 500)
291
- * page.setCropBox(cropBox.x, cropBox.y, 50, 100)
292
- * page.setCropBox(15, 5, cropBox.width - 50, cropBox.height - 100)
293
- * ```
294
- *
295
- * See [[PDFPage.getCropBox]] for details about what the CropBox represents.
296
- *
297
- * @param x The x coordinate of the lower left corner of the new CropBox.
298
- * @param y The y coordinate of the lower left corner of the new CropBox.
299
- * @param width The width of the new CropBox.
300
- * @param height The height of the new CropBox.
301
- */
302
- setCropBox(x: number, y: number, width: number, height: number): void {
303
- assertIs(x, 'x', ['number']);
304
- assertIs(y, 'y', ['number']);
305
- assertIs(width, 'width', ['number']);
306
- assertIs(height, 'height', ['number']);
307
- const cropBox = this.doc.context.obj([x, y, x + width, y + height]);
308
- this.node.set(PDFName.CropBox, cropBox);
309
- }
310
-
311
- /**
312
- * Set the BleedBox of this page. For example:
313
- * ```js
314
- * const bleedBox = page.getBleedBox()
315
- *
316
- * page.setBleedBox(0, 0, 250, 500)
317
- * page.setBleedBox(bleedBox.x, bleedBox.y, 50, 100)
318
- * page.setBleedBox(15, 5, bleedBox.width - 50, bleedBox.height - 100)
319
- * ```
320
- *
321
- * See [[PDFPage.getBleedBox]] for details about what the BleedBox represents.
322
- *
323
- * @param x The x coordinate of the lower left corner of the new BleedBox.
324
- * @param y The y coordinate of the lower left corner of the new BleedBox.
325
- * @param width The width of the new BleedBox.
326
- * @param height The height of the new BleedBox.
327
- */
328
- setBleedBox(x: number, y: number, width: number, height: number): void {
329
- assertIs(x, 'x', ['number']);
330
- assertIs(y, 'y', ['number']);
331
- assertIs(width, 'width', ['number']);
332
- assertIs(height, 'height', ['number']);
333
- const bleedBox = this.doc.context.obj([x, y, x + width, y + height]);
334
- this.node.set(PDFName.BleedBox, bleedBox);
335
- }
336
-
337
- /**
338
- * Set the TrimBox of this page. For example:
339
- * ```js
340
- * const trimBox = page.getTrimBox()
341
- *
342
- * page.setTrimBox(0, 0, 250, 500)
343
- * page.setTrimBox(trimBox.x, trimBox.y, 50, 100)
344
- * page.setTrimBox(15, 5, trimBox.width - 50, trimBox.height - 100)
345
- * ```
346
- *
347
- * See [[PDFPage.getTrimBox]] for details about what the TrimBox represents.
348
- *
349
- * @param x The x coordinate of the lower left corner of the new TrimBox.
350
- * @param y The y coordinate of the lower left corner of the new TrimBox.
351
- * @param width The width of the new TrimBox.
352
- * @param height The height of the new TrimBox.
353
- */
354
- setTrimBox(x: number, y: number, width: number, height: number): void {
355
- assertIs(x, 'x', ['number']);
356
- assertIs(y, 'y', ['number']);
357
- assertIs(width, 'width', ['number']);
358
- assertIs(height, 'height', ['number']);
359
- const trimBox = this.doc.context.obj([x, y, x + width, y + height]);
360
- this.node.set(PDFName.TrimBox, trimBox);
361
- }
362
-
363
- /**
364
- * Set the ArtBox of this page. For example:
365
- * ```js
366
- * const artBox = page.getArtBox()
367
- *
368
- * page.setArtBox(0, 0, 250, 500)
369
- * page.setArtBox(artBox.x, artBox.y, 50, 100)
370
- * page.setArtBox(15, 5, artBox.width - 50, artBox.height - 100)
371
- * ```
372
- *
373
- * See [[PDFPage.getArtBox]] for details about what the ArtBox represents.
374
- *
375
- * @param x The x coordinate of the lower left corner of the new ArtBox.
376
- * @param y The y coordinate of the lower left corner of the new ArtBox.
377
- * @param width The width of the new ArtBox.
378
- * @param height The height of the new ArtBox.
379
- */
380
- setArtBox(x: number, y: number, width: number, height: number): void {
381
- assertIs(x, 'x', ['number']);
382
- assertIs(y, 'y', ['number']);
383
- assertIs(width, 'width', ['number']);
384
- assertIs(height, 'height', ['number']);
385
- const artBox = this.doc.context.obj([x, y, x + width, y + height]);
386
- this.node.set(PDFName.ArtBox, artBox);
387
- }
388
-
389
- /**
390
- * Get this page's width and height. For example:
391
- * ```js
392
- * const { width, height } = page.getSize()
393
- * ```
394
- *
395
- * This method uses [[PDFPage.getMediaBox]] to obtain the page's
396
- * width and height.
397
- *
398
- * @returns The width and height of the page.
399
- */
400
- getSize(): { width: number; height: number } {
401
- const { width, height } = this.getMediaBox();
402
- return { width, height };
403
- }
404
-
405
- /**
406
- * Get this page's width. For example:
407
- * ```js
408
- * const width = page.getWidth()
409
- * ```
410
- *
411
- * This method uses [[PDFPage.getSize]] to obtain the page's size.
412
- *
413
- * @returns The width of the page.
414
- */
415
- getWidth(): number {
416
- return this.getSize().width;
417
- }
418
-
419
- /**
420
- * Get this page's height. For example:
421
- * ```js
422
- * const height = page.getHeight()
423
- * ```
424
- *
425
- * This method uses [[PDFPage.getSize]] to obtain the page's size.
426
- *
427
- * @returns The height of the page.
428
- */
429
- getHeight(): number {
430
- return this.getSize().height;
431
- }
432
-
433
- /**
434
- * Get the rectangle defining this page's MediaBox. For example:
435
- * ```js
436
- * const { x, y, width, height } = page.getMediaBox()
437
- * ```
438
- *
439
- * The MediaBox of a page defines the boundaries of the physical medium on
440
- * which the page is to be displayed/printed. It may include extended area
441
- * surrounding the page content for bleed marks, printing marks, etc...
442
- * It may also include areas close to the edges of the medium that cannot be
443
- * marked because of physical limitations of the output device. Content
444
- * falling outside this boundary may safely be discarded without affecting
445
- * the meaning of the PDF file.
446
- *
447
- * @returns An object defining the lower left corner of the MediaBox and its
448
- * width & height.
449
- */
450
- getMediaBox(): { x: number; y: number; width: number; height: number } {
451
- const mediaBox = this.node.MediaBox();
452
- return mediaBox.asRectangle();
453
- }
454
-
455
- /**
456
- * Get the rectangle defining this page's CropBox. For example:
457
- * ```js
458
- * const { x, y, width, height } = page.getCropBox()
459
- * ```
460
- *
461
- * The CropBox of a page defines the region to which the contents of the page
462
- * shall be clipped when displayed or printed. Unlike the other boxes, the
463
- * CropBox does not necessarily represent the physical page geometry. It
464
- * merely imposes clipping on the page contents.
465
- *
466
- * The CropBox's default value is the page's MediaBox.
467
- *
468
- * @returns An object defining the lower left corner of the CropBox and its
469
- * width & height.
470
- */
471
- getCropBox(): { x: number; y: number; width: number; height: number } {
472
- const cropBox = this.node.CropBox();
473
- return cropBox?.asRectangle() ?? this.getMediaBox();
474
- }
475
-
476
- /**
477
- * Get the rectangle defining this page's BleedBox. For example:
478
- * ```js
479
- * const { x, y, width, height } = page.getBleedBox()
480
- * ```
481
- *
482
- * The BleedBox of a page defines the region to which the contents of the
483
- * page shall be clipped when output in a production environment. This may
484
- * include any extra bleed area needed to accommodate the physical
485
- * limitations of cutting, folding, and trimming equipment. The actual
486
- * printed page may include printing marks that fall outside the BleedBox.
487
- *
488
- * The BleedBox's default value is the page's CropBox.
489
- *
490
- * @returns An object defining the lower left corner of the BleedBox and its
491
- * width & height.
492
- */
493
- getBleedBox(): { x: number; y: number; width: number; height: number } {
494
- const bleedBox = this.node.BleedBox();
495
- return bleedBox?.asRectangle() ?? this.getCropBox();
496
- }
497
-
498
- /**
499
- * Get the rectangle defining this page's TrimBox. For example:
500
- * ```js
501
- * const { x, y, width, height } = page.getTrimBox()
502
- * ```
503
- *
504
- * The TrimBox of a page defines the intended dimensions of the finished
505
- * page after trimming. It may be smaller than the MediaBox to allow for
506
- * production-related content, such as printing instructions, cut marks, or
507
- * color bars.
508
- *
509
- * The TrimBox's default value is the page's CropBox.
510
- *
511
- * @returns An object defining the lower left corner of the TrimBox and its
512
- * width & height.
513
- */
514
- getTrimBox(): { x: number; y: number; width: number; height: number } {
515
- const trimBox = this.node.TrimBox();
516
- return trimBox?.asRectangle() ?? this.getCropBox();
517
- }
518
-
519
- /**
520
- * Get the rectangle defining this page's ArtBox. For example:
521
- * ```js
522
- * const { x, y, width, height } = page.getArtBox()
523
- * ```
524
- *
525
- * The ArtBox of a page defines the extent of the page's meaningful content
526
- * (including potential white space).
527
- *
528
- * The ArtBox's default value is the page's CropBox.
529
- *
530
- * @returns An object defining the lower left corner of the ArtBox and its
531
- * width & height.
532
- */
533
- getArtBox(): { x: number; y: number; width: number; height: number } {
534
- const artBox = this.node.ArtBox();
535
- return artBox?.asRectangle() ?? this.getCropBox();
536
- }
537
-
538
- /**
539
- * Translate this page's content to a new location on the page. This operation
540
- * is often useful after resizing the page with [[setSize]]. For example:
541
- * ```js
542
- * // Add 50 units of whitespace to the top and right of the page
543
- * page.setSize(page.getWidth() + 50, page.getHeight() + 50)
544
- *
545
- * // Move the page's content from the lower-left corner of the page
546
- * // to the top-right corner.
547
- * page.translateContent(50, 50)
548
- *
549
- * // Now there are 50 units of whitespace to the left and bottom of the page
550
- * ```
551
- * See also: [[resetPosition]]
552
- * @param x The new position on the x-axis for this page's content.
553
- * @param y The new position on the y-axis for this page's content.
554
- */
555
- translateContent(x: number, y: number): void {
556
- assertIs(x, 'x', ['number']);
557
- assertIs(y, 'y', ['number']);
558
-
559
- this.node.normalize();
560
- this.getContentStream();
561
-
562
- const start = this.createContentStream(pushGraphicsState(), translate(x, y));
563
- const startRef = this.doc.context.register(start);
564
-
565
- const end = this.createContentStream(popGraphicsState());
566
- const endRef = this.doc.context.register(end);
567
-
568
- this.node.wrapContentStreams(startRef, endRef);
569
- }
570
-
571
- /**
572
- * Scale the size, content, and annotations of a page.
573
- *
574
- * For example:
575
- * ```js
576
- * page.scale(0.5, 0.5);
577
- * ```
578
- *
579
- * @param x The factor by which the width for the page should be scaled
580
- * (e.g. `0.5` is 50%).
581
- * @param y The factor by which the height for the page should be scaled
582
- * (e.g. `2.0` is 200%).
583
- */
584
- scale(x: number, y: number): void {
585
- assertIs(x, 'x', ['number']);
586
- assertIs(y, 'y', ['number']);
587
- this.setSize(this.getWidth() * x, this.getHeight() * y);
588
- this.scaleContent(x, y);
589
- this.scaleAnnotations(x, y);
590
- }
591
-
592
- /**
593
- * Scale the content of a page. This is useful after resizing an existing
594
- * page. This scales only the content, not the annotations.
595
- *
596
- * For example:
597
- * ```js
598
- * // Bisect the size of the page
599
- * page.setSize(page.getWidth() / 2, page.getHeight() / 2);
600
- *
601
- * // Scale the content of the page down by 50% in x and y
602
- * page.scaleContent(0.5, 0.5);
603
- * ```
604
- * See also: [[scaleAnnotations]]
605
- * @param x The factor by which the x-axis for the content should be scaled
606
- * (e.g. `0.5` is 50%).
607
- * @param y The factor by which the y-axis for the content should be scaled
608
- * (e.g. `2.0` is 200%).
609
- */
610
- scaleContent(x: number, y: number): void {
611
- assertIs(x, 'x', ['number']);
612
- assertIs(y, 'y', ['number']);
613
-
614
- this.node.normalize();
615
- this.getContentStream();
616
-
617
- const start = this.createContentStream(pushGraphicsState(), scale(x, y));
618
- const startRef = this.doc.context.register(start);
619
-
620
- const end = this.createContentStream(popGraphicsState());
621
- const endRef = this.doc.context.register(end);
622
-
623
- this.node.wrapContentStreams(startRef, endRef);
624
- }
625
-
626
- /**
627
- * Scale the annotations of a page. This is useful if you want to scale a
628
- * page with comments or other annotations.
629
- * ```js
630
- * // Scale the content of the page down by 50% in x and y
631
- * page.scaleContent(0.5, 0.5);
632
- *
633
- * // Scale the content of the page down by 50% in x and y
634
- * page.scaleAnnotations(0.5, 0.5);
635
- * ```
636
- * See also: [[scaleContent]]
637
- * @param x The factor by which the x-axis for the annotations should be
638
- * scaled (e.g. `0.5` is 50%).
639
- * @param y The factor by which the y-axis for the annotations should be
640
- * scaled (e.g. `2.0` is 200%).
641
- */
642
- scaleAnnotations(x: number, y: number) {
643
- assertIs(x, 'x', ['number']);
644
- assertIs(y, 'y', ['number']);
645
-
646
- const annots = this.node.Annots();
647
- if (!annots) return;
648
-
649
- for (let idx = 0; idx < annots.size(); idx++) {
650
- const annot = annots.lookup(idx);
651
- if (annot instanceof PDFDict) this.scaleAnnot(annot, x, y);
652
- }
653
- }
654
-
655
- /**
656
- * Reset the x and y coordinates of this page to `(0, 0)`. This operation is
657
- * often useful after calling [[translateContent]]. For example:
658
- * ```js
659
- * // Shift the page's contents up and to the right by 50 units
660
- * page.translateContent(50, 50)
661
- *
662
- * // This text will shifted - it will be drawn at (50, 50)
663
- * page.drawText('I am shifted')
664
- *
665
- * // Move back to (0, 0)
666
- * page.resetPosition()
667
- *
668
- * // This text will not be shifted - it will be drawn at (0, 0)
669
- * page.drawText('I am not shifted')
670
- * ```
671
- */
672
- resetPosition(): void {
673
- this.getContentStream(false);
674
- this.x = 0;
675
- this.y = 0;
676
- }
677
-
678
- /**
679
- * Choose a default font for this page. The default font will be used whenever
680
- * text is drawn on this page and no font is specified. For example:
681
- * ```js
682
- * import { StandardFonts } from 'pdf-lib'
683
- *
684
- * const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
685
- * const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica)
686
- * const courierFont = await pdfDoc.embedFont(StandardFonts.Courier)
687
- *
688
- * const page = pdfDoc.addPage()
689
- *
690
- * page.setFont(helveticaFont)
691
- * page.drawText('I will be drawn in Helvetica')
692
- *
693
- * page.setFont(timesRomanFont)
694
- * page.drawText('I will be drawn in Courier', { font: courierFont })
695
- * ```
696
- * @param font The default font to be used when drawing text on this page.
697
- */
698
- setFont(font: PDFFont): void {
699
- // TODO: Reuse image Font name if we've already added this image to Resources.Fonts
700
- assertIs(font, 'font', [[PDFFont, 'PDFFont']]);
701
- this.font = font;
702
- this.fontKey = this.node.newFontDictionary(this.font.name, this.font.ref);
703
- }
704
-
705
- /**
706
- * Choose a default font size for this page. The default font size will be
707
- * used whenever text is drawn on this page and no font size is specified.
708
- * For example:
709
- * ```js
710
- * page.setFontSize(12)
711
- * page.drawText('I will be drawn in size 12')
712
- *
713
- * page.setFontSize(36)
714
- * page.drawText('I will be drawn in size 24', { fontSize: 24 })
715
- * ```
716
- * @param fontSize The default font size to be used when drawing text on this
717
- * page.
718
- */
719
- setFontSize(fontSize: number): void {
720
- assertIs(fontSize, 'fontSize', ['number']);
721
- this.fontSize = fontSize;
722
- }
723
-
724
- /**
725
- * Choose a default font color for this page. The default font color will be
726
- * used whenever text is drawn on this page and no font color is specified.
727
- * For example:
728
- * ```js
729
- * import { rgb, cmyk, grayscale } from 'pdf-lib'
730
- *
731
- * page.setFontColor(rgb(0.97, 0.02, 0.97))
732
- * page.drawText('I will be drawn in pink')
733
- *
734
- * page.setFontColor(cmyk(0.4, 0.7, 0.39, 0.15))
735
- * page.drawText('I will be drawn in gray', { color: grayscale(0.5) })
736
- * ```
737
- * @param fontColor The default font color to be used when drawing text on
738
- * this page.
739
- */
740
- setFontColor(fontColor: Color): void {
741
- assertIs(fontColor, 'fontColor', [[Object, 'Color']]);
742
- this.fontColor = fontColor;
743
- }
744
-
745
- /**
746
- * Choose a default line height for this page. The default line height will be
747
- * used whenever text is drawn on this page and no line height is specified.
748
- * For example:
749
- * ```js
750
- * page.setLineHeight(12);
751
- * page.drawText('These lines will be vertically \n separated by 12 units')
752
- *
753
- * page.setLineHeight(36);
754
- * page.drawText('These lines will be vertically \n separated by 24 units', {
755
- * lineHeight: 24
756
- * })
757
- * ```
758
- * @param lineHeight The default line height to be used when drawing text on
759
- * this page.
760
- */
761
- setLineHeight(lineHeight: number): void {
762
- assertIs(lineHeight, 'lineHeight', ['number']);
763
- this.lineHeight = lineHeight;
764
- }
765
-
766
- /**
767
- * Get the default position of this page. For example:
768
- * ```js
769
- * const { x, y } = page.getPosition()
770
- * ```
771
- * @returns The default position of the page.
772
- */
773
- getPosition(): { x: number; y: number } {
774
- return { x: this.x, y: this.y };
775
- }
776
-
777
- /**
778
- * Get the default x coordinate of this page. For example:
779
- * ```js
780
- * const x = page.getX()
781
- * ```
782
- * @returns The default x coordinate of the page.
783
- */
784
- getX(): number {
785
- return this.x;
786
- }
787
-
788
- /**
789
- * Get the default y coordinate of this page. For example:
790
- * ```js
791
- * const y = page.getY()
792
- * ```
793
- * @returns The default y coordinate of the page.
794
- */
795
- getY(): number {
796
- return this.y;
797
- }
798
-
799
- /**
800
- * Change the default position of this page. For example:
801
- * ```js
802
- * page.moveTo(0, 0)
803
- * page.drawText('I will be drawn at the origin')
804
- *
805
- * page.moveTo(0, 25)
806
- * page.drawText('I will be drawn 25 units up')
807
- *
808
- * page.moveTo(25, 25)
809
- * page.drawText('I will be drawn 25 units up and 25 units to the right')
810
- * ```
811
- * @param x The new default position on the x-axis for this page.
812
- * @param y The new default position on the y-axis for this page.
813
- */
814
- moveTo(x: number, y: number): void {
815
- assertIs(x, 'x', ['number']);
816
- assertIs(y, 'y', ['number']);
817
- this.x = x;
818
- this.y = y;
819
- }
820
-
821
- /**
822
- * Change the default position of this page to be further down the y-axis.
823
- * For example:
824
- * ```js
825
- * page.moveTo(50, 50)
826
- * page.drawText('I will be drawn at (50, 50)')
827
- *
828
- * page.moveDown(10)
829
- * page.drawText('I will be drawn at (50, 40)')
830
- * ```
831
- * @param yDecrease The amount by which the page's default position along the
832
- * y-axis should be decreased.
833
- */
834
- moveDown(yDecrease: number): void {
835
- assertIs(yDecrease, 'yDecrease', ['number']);
836
- this.y -= yDecrease;
837
- }
838
-
839
- /**
840
- * Change the default position of this page to be further up the y-axis.
841
- * For example:
842
- * ```js
843
- * page.moveTo(50, 50)
844
- * page.drawText('I will be drawn at (50, 50)')
845
- *
846
- * page.moveUp(10)
847
- * page.drawText('I will be drawn at (50, 60)')
848
- * ```
849
- * @param yIncrease The amount by which the page's default position along the
850
- * y-axis should be increased.
851
- */
852
- moveUp(yIncrease: number): void {
853
- assertIs(yIncrease, 'yIncrease', ['number']);
854
- this.y += yIncrease;
855
- }
856
-
857
- /**
858
- * Change the default position of this page to be further left on the x-axis.
859
- * For example:
860
- * ```js
861
- * page.moveTo(50, 50)
862
- * page.drawText('I will be drawn at (50, 50)')
863
- *
864
- * page.moveLeft(10)
865
- * page.drawText('I will be drawn at (40, 50)')
866
- * ```
867
- * @param xDecrease The amount by which the page's default position along the
868
- * x-axis should be decreased.
869
- */
870
- moveLeft(xDecrease: number): void {
871
- assertIs(xDecrease, 'xDecrease', ['number']);
872
- this.x -= xDecrease;
873
- }
874
-
875
- /**
876
- * Change the default position of this page to be further right on the y-axis.
877
- * For example:
878
- * ```js
879
- * page.moveTo(50, 50)
880
- * page.drawText('I will be drawn at (50, 50)')
881
- *
882
- * page.moveRight(10)
883
- * page.drawText('I will be drawn at (60, 50)')
884
- * ```
885
- * @param xIncrease The amount by which the page's default position along the
886
- * x-axis should be increased.
887
- */
888
- moveRight(xIncrease: number): void {
889
- assertIs(xIncrease, 'xIncrease', ['number']);
890
- this.x += xIncrease;
891
- }
892
-
893
- /**
894
- * Push one or more operators to the end of this page's current content
895
- * stream. For example:
896
- * ```js
897
- * import {
898
- * pushGraphicsState,
899
- * moveTo,
900
- * lineTo,
901
- * closePath,
902
- * setFillingColor,
903
- * rgb,
904
- * fill,
905
- * popGraphicsState,
906
- * } from 'pdf-lib'
907
- *
908
- * // Draw a green triangle in the lower-left corner of the page
909
- * page.pushOperators(
910
- * pushGraphicsState(),
911
- * moveTo(0, 0),
912
- * lineTo(100, 0),
913
- * lineTo(50, 100),
914
- * closePath(),
915
- * setFillingColor(rgb(0.0, 1.0, 0.0)),
916
- * fill(),
917
- * popGraphicsState(),
918
- * )
919
- * ```
920
- * @param operator The operators to be pushed.
921
- */
922
- pushOperators(...operator: PDFOperator[]): void {
923
- assertEachIs(operator, 'operator', [[PDFOperator, 'PDFOperator']]);
924
- const contentStream = this.getContentStream();
925
- contentStream.push(...operator);
926
- }
927
-
928
- /**
929
- * Draw one or more lines of text on this page. For example:
930
- * ```js
931
- * import { StandardFonts, rgb } from 'pdf-lib'
932
- *
933
- * const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica)
934
- * const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman)
935
- *
936
- * const page = pdfDoc.addPage()
937
- *
938
- * page.setFont(helveticaFont)
939
- *
940
- * page.moveTo(5, 200)
941
- * page.drawText('The Life of an Egg', { size: 36 })
942
- *
943
- * page.moveDown(36)
944
- * page.drawText('An Epic Tale of Woe', { size: 30 })
945
- *
946
- * page.drawText(
947
- * `Humpty Dumpty sat on a wall \n` +
948
- * `Humpty Dumpty had a great fall; \n` +
949
- * `All the king's horses and all the king's men \n` +
950
- * `Couldn't put Humpty together again. \n`,
951
- * {
952
- * x: 25,
953
- * y: 100,
954
- * font: timesRomanFont,
955
- * size: 24,
956
- * color: rgb(1, 0, 0),
957
- * lineHeight: 24,
958
- * opacity: 0.75,
959
- * },
960
- * )
961
- * ```
962
- * @param text The text to be drawn.
963
- * @param options The options to be used when drawing the text.
964
- */
965
- drawText(text: string, options: PDFPageDrawTextOptions = {}): void {
966
- assertIs(text, 'text', ['string']);
967
- assertOrUndefined(options.color, 'options.color', [[Object, 'Color']]);
968
- assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
969
- assertOrUndefined(options.font, 'options.font', [[PDFFont, 'PDFFont']]);
970
- assertOrUndefined(options.size, 'options.size', ['number']);
971
- assertOrUndefined(options.rotate, 'options.rotate', [[Object, 'Rotation']]);
972
- assertOrUndefined(options.xSkew, 'options.xSkew', [[Object, 'Rotation']]);
973
- assertOrUndefined(options.ySkew, 'options.ySkew', [[Object, 'Rotation']]);
974
- assertOrUndefined(options.x, 'options.x', ['number']);
975
- assertOrUndefined(options.y, 'options.y', ['number']);
976
- assertOrUndefined(options.lineHeight, 'options.lineHeight', ['number']);
977
- assertOrUndefined(options.maxWidth, 'options.maxWidth', ['number']);
978
- assertOrUndefined(options.wordBreaks, 'options.wordBreaks', [Array]);
979
- assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
980
-
981
- const { oldFont, newFont, newFontKey } = this.setOrEmbedFont(options.font);
982
- const fontSize = options.size || this.fontSize;
983
-
984
- const wordBreaks = options.wordBreaks || this.doc.defaultWordBreaks;
985
- const textWidth = (t: string) => newFont.widthOfTextAtSize(t, fontSize);
986
- const lines =
987
- options.maxWidth === undefined
988
- ? lineSplit(cleanText(text))
989
- : breakTextIntoLines(text, wordBreaks, options.maxWidth, textWidth);
990
-
991
- const encodedLines = Array<PDFHexString>(lines.length);
992
- for (let idx = 0, len = lines.length; idx < len; idx++) {
993
- encodedLines[idx] = newFont.encodeText(lines[idx]);
994
- }
995
-
996
- const graphicsStateKey = this.maybeEmbedGraphicsState({
997
- opacity: options.opacity,
998
- blendMode: options.blendMode,
999
- });
1000
-
1001
- const contentStream = this.getContentStream();
1002
- contentStream.push(
1003
- ...drawLinesOfText(encodedLines, {
1004
- color: options.color ?? this.fontColor,
1005
- font: newFontKey,
1006
- size: fontSize,
1007
- rotate: options.rotate ?? degrees(0),
1008
- xSkew: options.xSkew ?? degrees(0),
1009
- ySkew: options.ySkew ?? degrees(0),
1010
- x: options.x ?? this.x,
1011
- y: options.y ?? this.y,
1012
- lineHeight: options.lineHeight ?? this.lineHeight,
1013
- graphicsState: graphicsStateKey,
1014
- matrix: options.matrix,
1015
- clipSpaces: options.clipSpaces,
1016
- }),
1017
- );
1018
-
1019
- if (options.font) {
1020
- if (oldFont) this.setFont(oldFont);
1021
- else this.resetFont();
1022
- }
1023
- }
1024
-
1025
- /**
1026
- * Draw an image on this page. For example:
1027
- * ```js
1028
- * import { degrees } from 'pdf-lib'
1029
- *
1030
- * const jpgUrl = 'https://pdf-lib.js.org/assets/cat_riding_unicorn.jpg'
1031
- * const jpgImageBytes = await fetch(jpgUrl).then((res) => res.arrayBuffer())
1032
- *
1033
- * const jpgImage = await pdfDoc.embedJpg(jpgImageBytes)
1034
- * const jpgDims = jpgImage.scale(0.5)
1035
- *
1036
- * const page = pdfDoc.addPage()
1037
- *
1038
- * page.drawImage(jpgImage, {
1039
- * x: 25,
1040
- * y: 25,
1041
- * width: jpgDims.width,
1042
- * height: jpgDims.height,
1043
- * rotate: degrees(30),
1044
- * opacity: 0.75,
1045
- * })
1046
- * ```
1047
- * @param image The image to be drawn.
1048
- * @param options The options to be used when drawing the image.
1049
- */
1050
- drawImage(image: PDFImage, options: PDFPageDrawImageOptions = {}): void {
1051
- // TODO: Reuse image XObject name if we've already added this image to Resources.XObjects
1052
- assertIs(image, 'image', [[PDFImage, 'PDFImage']]);
1053
- assertOrUndefined(options.x, 'options.x', ['number']);
1054
- assertOrUndefined(options.y, 'options.y', ['number']);
1055
- assertOrUndefined(options.width, 'options.width', ['number']);
1056
- assertOrUndefined(options.height, 'options.height', ['number']);
1057
- assertOrUndefined(options.rotate, 'options.rotate', [[Object, 'Rotation']]);
1058
- assertOrUndefined(options.xSkew, 'options.xSkew', [[Object, 'Rotation']]);
1059
- assertOrUndefined(options.ySkew, 'options.ySkew', [[Object, 'Rotation']]);
1060
- assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
1061
- assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
1062
-
1063
- const xObjectKey = this.node.newXObject('Image', image.ref);
1064
-
1065
- const graphicsStateKey = this.maybeEmbedGraphicsState({
1066
- opacity: options.opacity,
1067
- blendMode: options.blendMode,
1068
- });
1069
-
1070
- const contentStream = this.getContentStream();
1071
- contentStream.push(
1072
- ...drawImage(xObjectKey, {
1073
- x: options.x ?? this.x,
1074
- y: options.y ?? this.y,
1075
- width: options.width ?? image.size().width,
1076
- height: options.height ?? image.size().height,
1077
- rotate: options.rotate ?? degrees(0),
1078
- xSkew: options.xSkew ?? degrees(0),
1079
- ySkew: options.ySkew ?? degrees(0),
1080
- graphicsState: graphicsStateKey,
1081
- matrix: options.matrix,
1082
- clipSpaces: options.clipSpaces,
1083
- }),
1084
- );
1085
- }
1086
-
1087
- /**
1088
- * Draw an embedded PDF page on this page. For example:
1089
- * ```js
1090
- * import { degrees } from 'pdf-lib'
1091
- *
1092
- * const pdfDoc = await PDFDocument.create()
1093
- * const page = pdfDoc.addPage()
1094
- *
1095
- * const sourcePdfUrl = 'https://pdf-lib.js.org/assets/with_large_page_count.pdf'
1096
- * const sourcePdf = await fetch(sourcePdfUrl).then((res) => res.arrayBuffer())
1097
- *
1098
- * // Embed page 74 from the PDF
1099
- * const [embeddedPage] = await pdfDoc.embedPdf(sourcePdf, 73)
1100
- *
1101
- * page.drawPage(embeddedPage, {
1102
- * x: 250,
1103
- * y: 200,
1104
- * xScale: 0.5,
1105
- * yScale: 0.5,
1106
- * rotate: degrees(30),
1107
- * opacity: 0.75,
1108
- * })
1109
- * ```
1110
- *
1111
- * The `options` argument accepts both `width`/`height` and `xScale`/`yScale`
1112
- * as options. Since each of these options defines the size of the drawn page,
1113
- * if both options are given, `width` and `height` take precedence and the
1114
- * corresponding scale variants are ignored.
1115
- *
1116
- * @param embeddedPage The embedded page to be drawn.
1117
- * @param options The options to be used when drawing the embedded page.
1118
- */
1119
- drawPage(embeddedPage: PDFEmbeddedPage, options: PDFPageDrawPageOptions = {}): void {
1120
- // TODO: Reuse embeddedPage XObject name if we've already added this embeddedPage to Resources.XObjects
1121
- assertIs(embeddedPage, 'embeddedPage', [[PDFEmbeddedPage, 'PDFEmbeddedPage']]);
1122
- assertOrUndefined(options.x, 'options.x', ['number']);
1123
- assertOrUndefined(options.y, 'options.y', ['number']);
1124
- assertOrUndefined(options.xScale, 'options.xScale', ['number']);
1125
- assertOrUndefined(options.yScale, 'options.yScale', ['number']);
1126
- assertOrUndefined(options.width, 'options.width', ['number']);
1127
- assertOrUndefined(options.height, 'options.height', ['number']);
1128
- assertOrUndefined(options.rotate, 'options.rotate', [[Object, 'Rotation']]);
1129
- assertOrUndefined(options.xSkew, 'options.xSkew', [[Object, 'Rotation']]);
1130
- assertOrUndefined(options.ySkew, 'options.ySkew', [[Object, 'Rotation']]);
1131
- assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
1132
- assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
1133
-
1134
- const xObjectKey = this.node.newXObject('EmbeddedPdfPage', embeddedPage.ref);
1135
-
1136
- const graphicsStateKey = this.maybeEmbedGraphicsState({
1137
- opacity: options.opacity,
1138
- blendMode: options.blendMode,
1139
- });
1140
-
1141
- // prettier-ignore
1142
- const xScale = (
1143
- options.width !== undefined ? options.width / embeddedPage.width
1144
- : options.xScale !== undefined ? options.xScale
1145
- : 1
1146
- );
1147
-
1148
- // prettier-ignore
1149
- const yScale = (
1150
- options.height !== undefined ? options.height / embeddedPage.height
1151
- : options.yScale !== undefined ? options.yScale
1152
- : 1
1153
- );
1154
-
1155
- const contentStream = this.getContentStream();
1156
- contentStream.push(
1157
- ...drawPage(xObjectKey, {
1158
- x: options.x ?? this.x,
1159
- y: options.y ?? this.y,
1160
- xScale,
1161
- yScale,
1162
- rotate: options.rotate ?? degrees(0),
1163
- xSkew: options.xSkew ?? degrees(0),
1164
- ySkew: options.ySkew ?? degrees(0),
1165
- graphicsState: graphicsStateKey,
1166
- }),
1167
- );
1168
- }
1169
-
1170
- /**
1171
- * Draw an SVG path on this page. For example:
1172
- * ```js
1173
- * import { rgb } from 'pdf-lib'
1174
- *
1175
- * const svgPath = 'M 0,20 L 100,160 Q 130,200 150,120 C 190,-40 200,200 300,150 L 400,90'
1176
- *
1177
- * // Draw path as black line
1178
- * page.drawSvgPath(svgPath, { x: 25, y: 75 })
1179
- *
1180
- * // Change border style and opacity
1181
- * page.drawSvgPath(svgPath, {
1182
- * x: 25,
1183
- * y: 275,
1184
- * borderColor: rgb(0.5, 0.5, 0.5),
1185
- * borderWidth: 2,
1186
- * borderOpacity: 0.75,
1187
- * })
1188
- *
1189
- * // Set fill color and opacity
1190
- * page.drawSvgPath(svgPath, {
1191
- * x: 25,
1192
- * y: 475,
1193
- * color: rgb(1.0, 0, 0),
1194
- * opacity: 0.75,
1195
- * })
1196
- *
1197
- * // Draw 50% of original size
1198
- * page.drawSvgPath(svgPath, {
1199
- * x: 25,
1200
- * y: 675,
1201
- * scale: 0.5,
1202
- * })
1203
- * ```
1204
- * @param path The SVG path to be drawn.
1205
- * @param options The options to be used when drawing the SVG path.
1206
- */
1207
- drawSvgPath(path: string, options: PDFPageDrawSVGOptions = {}): void {
1208
- assertIs(path, 'path', ['string']);
1209
- assertOrUndefined(options.x, 'options.x', ['number']);
1210
- assertOrUndefined(options.y, 'options.y', ['number']);
1211
- assertOrUndefined(options.scale, 'options.scale', ['number']);
1212
- assertOrUndefined(options.rotate, 'options.rotate', [[Object, 'Rotation']]);
1213
- assertOrUndefined(options.borderWidth, 'options.borderWidth', ['number']);
1214
- assertOrUndefined(options.color, 'options.color', [[Object, 'Color']]);
1215
- assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
1216
- assertOrUndefined(options.borderColor, 'options.borderColor', [[Object, 'Color']]);
1217
- assertOrUndefined(options.borderDashArray, 'options.borderDashArray', [Array]);
1218
- assertOrUndefined(options.borderDashPhase, 'options.borderDashPhase', ['number']);
1219
- assertIsOneOfOrUndefined(options.borderLineCap, 'options.borderLineCap', LineCapStyle);
1220
- assertRangeOrUndefined(options.borderOpacity, 'options.borderOpacity', 0, 1);
1221
- assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
1222
- assertIsOneOfOrUndefined(options.fillRule, 'options.fillRule', FillRule);
1223
-
1224
- const graphicsStateKey = this.maybeEmbedGraphicsState({
1225
- opacity: options.opacity,
1226
- borderOpacity: options.borderOpacity,
1227
- blendMode: options.blendMode,
1228
- });
1229
-
1230
- if (!('color' in options) && !('borderColor' in options)) {
1231
- options.borderColor = rgb(0, 0, 0);
1232
- }
1233
-
1234
- const contentStream = this.getContentStream();
1235
- contentStream.push(
1236
- ...drawSvgPath(path, {
1237
- x: options.x ?? this.x,
1238
- y: options.y ?? this.y,
1239
- scale: options.scale,
1240
- rotate: options.rotate ?? degrees(0),
1241
- color: options.color ?? undefined,
1242
- borderColor: options.borderColor ?? undefined,
1243
- borderWidth: options.borderWidth ?? 0,
1244
- borderDashArray: options.borderDashArray ?? undefined,
1245
- borderDashPhase: options.borderDashPhase ?? undefined,
1246
- borderLineCap: options.borderLineCap ?? undefined,
1247
- graphicsState: graphicsStateKey,
1248
- fillRule: options.fillRule,
1249
- matrix: options.matrix,
1250
- clipSpaces: options.clipSpaces,
1251
- }),
1252
- );
1253
- }
1254
-
1255
- /**
1256
- * Draw a line on this page. For example:
1257
- * ```js
1258
- * import { rgb } from 'pdf-lib'
1259
- *
1260
- * page.drawLine({
1261
- * start: { x: 25, y: 75 },
1262
- * end: { x: 125, y: 175 },
1263
- * thickness: 2,
1264
- * color: rgb(0.75, 0.2, 0.2),
1265
- * opacity: 0.75,
1266
- * })
1267
- * ```
1268
- * @param options The options to be used when drawing the line.
1269
- */
1270
- drawLine(options: PDFPageDrawLineOptions): void {
1271
- assertIs(options.start, 'options.start', [[Object, '{ x: number, y: number }']]);
1272
- assertIs(options.end, 'options.end', [[Object, '{ x: number, y: number }']]);
1273
- assertIs(options.start.x, 'options.start.x', ['number']);
1274
- assertIs(options.start.y, 'options.start.y', ['number']);
1275
- assertIs(options.end.x, 'options.end.x', ['number']);
1276
- assertIs(options.end.y, 'options.end.y', ['number']);
1277
- assertOrUndefined(options.thickness, 'options.thickness', ['number']);
1278
- assertOrUndefined(options.color, 'options.color', [[Object, 'Color']]);
1279
- assertOrUndefined(options.dashArray, 'options.dashArray', [Array]);
1280
- assertOrUndefined(options.dashPhase, 'options.dashPhase', ['number']);
1281
- assertIsOneOfOrUndefined(options.lineCap, 'options.lineCap', LineCapStyle);
1282
- assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
1283
- assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
1284
-
1285
- const graphicsStateKey = this.maybeEmbedGraphicsState({
1286
- borderOpacity: options.opacity,
1287
- blendMode: options.blendMode,
1288
- });
1289
-
1290
- if (!('color' in options)) {
1291
- options.color = rgb(0, 0, 0);
1292
- }
1293
-
1294
- const contentStream = this.getContentStream();
1295
- contentStream.push(
1296
- ...drawLine({
1297
- start: options.start,
1298
- end: options.end,
1299
- thickness: options.thickness ?? 1,
1300
- color: options.color ?? undefined,
1301
- dashArray: options.dashArray ?? undefined,
1302
- dashPhase: options.dashPhase ?? undefined,
1303
- lineCap: options.lineCap ?? undefined,
1304
- graphicsState: graphicsStateKey,
1305
- matrix: options.matrix,
1306
- clipSpaces: options.clipSpaces,
1307
- }),
1308
- );
1309
- }
1310
-
1311
- /**
1312
- * Draw a rectangle on this page. For example:
1313
- * ```js
1314
- * import { degrees, grayscale, rgb } from 'pdf-lib'
1315
- *
1316
- * page.drawRectangle({
1317
- * x: 25,
1318
- * y: 75,
1319
- * width: 250,
1320
- * height: 75,
1321
- * rotate: degrees(-15),
1322
- * borderWidth: 5,
1323
- * borderColor: grayscale(0.5),
1324
- * color: rgb(0.75, 0.2, 0.2),
1325
- * opacity: 0.5,
1326
- * borderOpacity: 0.75,
1327
- * radius: 0.1,
1328
- * })
1329
- * ```
1330
- * @param options The options to be used when drawing the rectangle.
1331
- */
1332
- drawRectangle(options: PDFPageDrawRectangleOptions = {}): void {
1333
- assertOrUndefined(options.x, 'options.x', ['number']);
1334
- assertOrUndefined(options.y, 'options.y', ['number']);
1335
- assertOrUndefined(options.width, 'options.width', ['number']);
1336
- assertOrUndefined(options.height, 'options.height', ['number']);
1337
- assertOrUndefined(options.rotate, 'options.rotate', [[Object, 'Rotation']]);
1338
- assertOrUndefined(options.xSkew, 'options.xSkew', [[Object, 'Rotation']]);
1339
- assertOrUndefined(options.ySkew, 'options.ySkew', [[Object, 'Rotation']]);
1340
- assertOrUndefined(options.borderWidth, 'options.borderWidth', ['number']);
1341
- assertOrUndefined(options.color, 'options.color', [[Object, 'Color']]);
1342
- assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
1343
- assertOrUndefined(options.borderColor, 'options.borderColor', [[Object, 'Color']]);
1344
- assertOrUndefined(options.borderDashArray, 'options.borderDashArray', [Array]);
1345
- assertOrUndefined(options.borderDashPhase, 'options.borderDashPhase', ['number']);
1346
- assertIsOneOfOrUndefined(options.borderLineCap, 'options.borderLineCap', LineCapStyle);
1347
- assertRangeOrUndefined(options.borderOpacity, 'options.borderOpacity', 0, 1);
1348
- assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
1349
- assertOrUndefined(options.radius, 'options.radius', ['number']);
1350
-
1351
- const graphicsStateKey = this.maybeEmbedGraphicsState({
1352
- opacity: options.opacity,
1353
- borderOpacity: options.borderOpacity,
1354
- blendMode: options.blendMode,
1355
- });
1356
-
1357
- if (!('color' in options) && !('borderColor' in options)) {
1358
- options.color = rgb(0, 0, 0);
1359
- }
1360
-
1361
- const contentStream = this.getContentStream();
1362
- contentStream.push(
1363
- ...drawRectangle({
1364
- x: options.x ?? this.x,
1365
- y: options.y ?? this.y,
1366
- width: options.width ?? 150,
1367
- height: options.height ?? 100,
1368
- rotate: options.rotate ?? degrees(0),
1369
- xSkew: options.xSkew ?? degrees(0),
1370
- ySkew: options.ySkew ?? degrees(0),
1371
- borderWidth: options.borderWidth ?? 0,
1372
- color: options.color ?? undefined,
1373
- borderColor: options.borderColor ?? undefined,
1374
- borderDashArray: options.borderDashArray ?? undefined,
1375
- borderDashPhase: options.borderDashPhase ?? undefined,
1376
- graphicsState: graphicsStateKey,
1377
- borderLineCap: options.borderLineCap ?? undefined,
1378
- matrix: options.matrix,
1379
- clipSpaces: options.clipSpaces,
1380
- radius: options.radius ?? 0,
1381
- }),
1382
- );
1383
- }
1384
-
1385
- /**
1386
- * Draw a square on this page. For example:
1387
- * ```js
1388
- * import { degrees, grayscale, rgb } from 'pdf-lib'
1389
- *
1390
- * page.drawSquare({
1391
- * x: 25,
1392
- * y: 75,
1393
- * size: 100,
1394
- * rotate: degrees(-15),
1395
- * borderWidth: 5,
1396
- * borderColor: grayscale(0.5),
1397
- * color: rgb(0.75, 0.2, 0.2),
1398
- * opacity: 0.5,
1399
- * borderOpacity: 0.75,
1400
- * })
1401
- * ```
1402
- * @param options The options to be used when drawing the square.
1403
- */
1404
- drawSquare(options: PDFPageDrawSquareOptions = {}): void {
1405
- const { size } = options;
1406
- assertOrUndefined(size, 'size', ['number']);
1407
- this.drawRectangle({ ...options, width: size, height: size });
1408
- }
1409
-
1410
- /**
1411
- * Draw an ellipse on this page. For example:
1412
- * ```js
1413
- * import { grayscale, rgb } from 'pdf-lib'
1414
- *
1415
- * page.drawEllipse({
1416
- * x: 200,
1417
- * y: 75,
1418
- * xScale: 100,
1419
- * yScale: 50,
1420
- * borderWidth: 5,
1421
- * borderColor: grayscale(0.5),
1422
- * color: rgb(0.75, 0.2, 0.2),
1423
- * opacity: 0.5,
1424
- * borderOpacity: 0.75,
1425
- * })
1426
- * ```
1427
- * @param options The options to be used when drawing the ellipse.
1428
- */
1429
- drawEllipse(options: PDFPageDrawEllipseOptions = {}): void {
1430
- assertOrUndefined(options.x, 'options.x', ['number']);
1431
- assertOrUndefined(options.y, 'options.y', ['number']);
1432
- assertOrUndefined(options.xScale, 'options.xScale', ['number']);
1433
- assertOrUndefined(options.yScale, 'options.yScale', ['number']);
1434
- assertOrUndefined(options.rotate, 'options.rotate', [[Object, 'Rotation']]);
1435
- assertOrUndefined(options.color, 'options.color', [[Object, 'Color']]);
1436
- assertRangeOrUndefined(options.opacity, 'opacity.opacity', 0, 1);
1437
- assertOrUndefined(options.borderColor, 'options.borderColor', [[Object, 'Color']]);
1438
- assertRangeOrUndefined(options.borderOpacity, 'options.borderOpacity', 0, 1);
1439
- assertOrUndefined(options.borderWidth, 'options.borderWidth', ['number']);
1440
- assertOrUndefined(options.borderDashArray, 'options.borderDashArray', [Array]);
1441
- assertOrUndefined(options.borderDashPhase, 'options.borderDashPhase', ['number']);
1442
- assertIsOneOfOrUndefined(options.borderLineCap, 'options.borderLineCap', LineCapStyle);
1443
- assertIsOneOfOrUndefined(options.blendMode, 'options.blendMode', BlendMode);
1444
- const graphicsStateKey = this.maybeEmbedGraphicsState({
1445
- opacity: options.opacity,
1446
- borderOpacity: options.borderOpacity,
1447
- blendMode: options.blendMode,
1448
- });
1449
-
1450
- if (!('color' in options) && !('borderColor' in options)) {
1451
- options.color = rgb(0, 0, 0);
1452
- }
1453
-
1454
- const contentStream = this.getContentStream();
1455
- contentStream.push(
1456
- ...drawEllipse({
1457
- x: options.x ?? this.x,
1458
- y: options.y ?? this.y,
1459
- xScale: options.xScale ?? 100,
1460
- yScale: options.yScale ?? 100,
1461
- rotate: options.rotate ?? undefined,
1462
- color: options.color ?? undefined,
1463
- borderColor: options.borderColor ?? undefined,
1464
- borderWidth: options.borderWidth ?? 0,
1465
- borderDashArray: options.borderDashArray ?? undefined,
1466
- borderDashPhase: options.borderDashPhase ?? undefined,
1467
- borderLineCap: options.borderLineCap ?? undefined,
1468
- graphicsState: graphicsStateKey,
1469
- matrix: options.matrix,
1470
- clipSpaces: options.clipSpaces,
1471
- }),
1472
- );
1473
- }
1474
-
1475
- /**
1476
- * Draw a circle on this page. For example:
1477
- * ```js
1478
- * import { grayscale, rgb } from 'pdf-lib'
1479
- *
1480
- * page.drawCircle({
1481
- * x: 200,
1482
- * y: 150,
1483
- * size: 100,
1484
- * borderWidth: 5,
1485
- * borderColor: grayscale(0.5),
1486
- * color: rgb(0.75, 0.2, 0.2),
1487
- * opacity: 0.5,
1488
- * borderOpacity: 0.75,
1489
- * })
1490
- * ```
1491
- * @param options The options to be used when drawing the ellipse.
1492
- */
1493
- drawCircle(options: PDFPageDrawCircleOptions = {}): void {
1494
- const { size = 100 } = options;
1495
- assertOrUndefined(size, 'size', ['number']);
1496
- this.drawEllipse({ ...options, xScale: size, yScale: size });
1497
- }
1498
-
1499
- private setOrEmbedFont(font?: PDFFont) {
1500
- const oldFont = this.font;
1501
- const oldFontKey = this.fontKey;
1502
-
1503
- if (font) this.setFont(font);
1504
- else this.getFont();
1505
-
1506
- const newFont = this.font!;
1507
- const newFontKey = this.fontKey!;
1508
-
1509
- return { oldFont, oldFontKey, newFont, newFontKey };
1510
- }
1511
-
1512
- /**
1513
- * Draw an SVG on this page. For example:
1514
- * ```js
1515
- * const svg = '<svg><path d="M 0,20 L 100,160 Q 130,200 150,120 C 190,-40 200,200 300,150 L 400,90"></path></svg>'
1516
- *
1517
- * // Draw svg
1518
- * page.drawSvg(svg, { x: 25, y: 75 })
1519
- * ```
1520
- * @param svg The SVG to be drawn.
1521
- * @param options The options to be used when drawing the SVG.
1522
- */
1523
- async drawSvg(svg: string, options: PDFPageDrawSVGElementOptions = {}): Promise<void> {
1524
- assertIs(svg, 'svg', ['string']);
1525
- assertOrUndefined(options.x, 'options.x', ['number']);
1526
- assertOrUndefined(options.y, 'options.y', ['number']);
1527
- assertOrUndefined(options.width, 'options.width', ['number']);
1528
- assertOrUndefined(options.height, 'options.height', ['number']);
1529
-
1530
- await drawSvg(this, svg, {
1531
- x: options.x ?? this.x,
1532
- y: options.y ?? this.y,
1533
- fonts: options.fonts,
1534
- width: options.width,
1535
- height: options.height,
1536
- });
1537
- }
1538
-
1539
- getFont(): [PDFFont, PDFName] {
1540
- if (!this.font || !this.fontKey) {
1541
- const font = this.doc.embedStandardFont(StandardFonts.Helvetica);
1542
- this.setFont(font);
1543
- }
1544
- return [this.font!, this.fontKey!];
1545
- }
1546
-
1547
- private resetFont(): void {
1548
- this.font = undefined;
1549
- this.fontKey = undefined;
1550
- }
1551
-
1552
- private getContentStream(useExisting = true): PDFContentStream {
1553
- if (useExisting && this.contentStream) return this.contentStream;
1554
- this.contentStream = this.createContentStream();
1555
- this.contentStreamRef = this.doc.context.register(this.contentStream);
1556
- this.node.addContentStream(this.contentStreamRef);
1557
- return this.contentStream;
1558
- }
1559
-
1560
- private createContentStream(...operators: PDFOperator[]): PDFContentStream {
1561
- const dict = this.doc.context.obj({});
1562
- const contentStream = PDFContentStream.of(dict, operators);
1563
- return contentStream;
1564
- }
1565
-
1566
- private maybeEmbedGraphicsState(options: {
1567
- opacity?: number;
1568
- borderOpacity?: number;
1569
- blendMode?: BlendMode;
1570
- }): PDFName | undefined {
1571
- const { opacity, borderOpacity, blendMode } = options;
1572
-
1573
- if (opacity === undefined && borderOpacity === undefined && blendMode === undefined) {
1574
- return undefined;
1575
- }
1576
-
1577
- const graphicsState = this.doc.context.obj({
1578
- Type: 'ExtGState',
1579
- ca: opacity,
1580
- CA: borderOpacity,
1581
- BM: blendMode,
1582
- });
1583
-
1584
- const key = this.node.newExtGState('GS', graphicsState);
1585
-
1586
- return key;
1587
- }
1588
-
1589
- private scaleAnnot(annot: PDFDict, x: number, y: number) {
1590
- const selectors = ['RD', 'CL', 'Vertices', 'QuadPoints', 'L', 'Rect'];
1591
- for (let idx = 0, len = selectors.length; idx < len; idx++) {
1592
- const list = annot.lookup(PDFName.of(selectors[idx]));
1593
- if (list instanceof PDFArray) list.scalePDFNumbers(x, y);
1594
- }
1595
-
1596
- const inkLists = annot.lookup(PDFName.of('InkList'));
1597
- if (inkLists instanceof PDFArray) {
1598
- for (let idx = 0, len = inkLists.size(); idx < len; idx++) {
1599
- const arr = inkLists.lookup(idx);
1600
- if (arr instanceof PDFArray) arr.scalePDFNumbers(x, y);
1601
- }
1602
- }
1603
- }
1604
- }