@libpdf/core 0.0.1-beta.4 → 0.0.1-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -5535,13 +5535,13 @@ declare class PathBuilder {
5535
5535
  */
5536
5536
  interface Rectangle {
5537
5537
  /** Left x coordinate */
5538
- x1: number;
5538
+ x: number;
5539
5539
  /** Bottom y coordinate */
5540
- y1: number;
5541
- /** Right x coordinate */
5542
- x2: number;
5543
- /** Top y coordinate */
5544
- y2: number;
5540
+ y: number;
5541
+ /** Width */
5542
+ width: number;
5543
+ /** Height */
5544
+ height: number;
5545
5545
  }
5546
5546
  /**
5547
5547
  * Options for drawing an embedded page.
@@ -8869,6 +8869,44 @@ declare function layoutText(text: string, font: FontInput, fontSize: number, max
8869
8869
  */
8870
8870
  declare function layoutJustifiedLine(words: string[], font: FontInput, fontSize: number, targetWidth: number): PositionedWord[];
8871
8871
  //#endregion
8872
+ //#region src/helpers/pem.d.ts
8873
+ /**
8874
+ * PEM encoding/decoding utilities.
8875
+ *
8876
+ * PEM (Privacy-Enhanced Mail) is a Base64 encoding format with header/footer
8877
+ * lines used for certificates, keys, and other cryptographic data.
8878
+ */
8879
+ /**
8880
+ * A single PEM block parsed from a PEM file.
8881
+ */
8882
+ interface PemBlock {
8883
+ /** The label from the BEGIN statement (e.g., "CERTIFICATE", "PRIVATE KEY") */
8884
+ label: string;
8885
+ /** The DER-encoded binary data */
8886
+ der: Uint8Array;
8887
+ }
8888
+ /**
8889
+ * Parse a PEM file into a series of DER blocks with their types.
8890
+ *
8891
+ * PEM files can contain multiple blocks (e.g., certificate chains). This function
8892
+ * extracts each block along with its label from the BEGIN statement.
8893
+ *
8894
+ * @param pem - The PEM-encoded string (may contain multiple blocks)
8895
+ * @returns Array of PEM blocks, each containing a label and DER data
8896
+ * @throws {Error} if the PEM format is invalid (mismatched labels or missing markers)
8897
+ *
8898
+ * @example
8899
+ * ```typescript
8900
+ * const blocks = parsePem(pemString);
8901
+ * // [
8902
+ * // { label: "CERTIFICATE", der: Uint8Array(...) },
8903
+ * // { label: "CERTIFICATE", der: Uint8Array(...) },
8904
+ * // { label: "PRIVATE KEY", der: Uint8Array(...) }
8905
+ * // ]
8906
+ * ```
8907
+ */
8908
+ declare function parsePem(pem: string): PemBlock[];
8909
+ //#endregion
8872
8910
  //#region src/annotations/factory.d.ts
8873
8911
  /**
8874
8912
  * Unknown annotation - fallback for unsupported types.
@@ -8892,5 +8930,5 @@ declare function isWidgetAnnotation(dict: PdfDict): boolean;
8892
8930
  */
8893
8931
  declare function isPopupAnnotation(dict: PdfDict): boolean;
8894
8932
  //#endregion
8895
- export { AnnotationFlags, type AnnotationSubtype, type AuthenticationResult, type BorderStyle, type BorderStyleType, type ButtonField, type CMYK, type CaretAnnotationOptions, type CaretSymbol, CertificateChainError, type CheckboxField, type CheckboxOptions, type CheckboxSymbol, type CircleAnnotationOptions, type Color, type CopyPagesOptions, CryptoKeySigner, type Degrees, type DestinationType, type DigestAlgorithm, type DocumentMetadata, type DrawCircleOptions, type DrawEllipseOptions, type DrawFieldOptions, type DrawImageOptions, type DrawLineOptions, type DrawPageOptions, type DrawRectangleOptions, type DrawTextOptions, type DropdownField, type DropdownOptions, type EmbedFontOptions, type EmbeddedFont, type EncryptionAlgorithmOption, type ExtractPagesOptions, type FieldOptions, type FieldType, type FieldValue, type FileAttachmentIcon, type FlattenAnnotationsOptions, type FlattenLayersResult, type FlattenOptions, type FontInput, type FormField, type FormProperties, type FreeTextAnnotationOptions, type FreeTextJustification, GoogleKmsSigner, type Grayscale, type HighlightMode, HttpTimestampAuthority, type HttpTimestampAuthorityOptions, type InkAnnotationOptions, type KeyType, KmsSignerError, type LayerInfo, type LayoutResult, type LineAnnotationOptions, type LineCap, type LineEndingStyle, type LineJoin, type LinkAction, type LinkAnnotationOptions, type LinkDestination, type ListBoxField, type ListboxOptions, type LoadOptions, type MergeOptions, P12Signer, type PAdESLevel, PDF, PDFAnnotation, PDFCaretAnnotation, PDFCircleAnnotation, PDFEmbeddedPage, PDFFileAttachmentAnnotation, PDFForm, PDFFreeTextAnnotation, PDFHighlightAnnotation, PDFImage, PDFInkAnnotation, PDFLineAnnotation, PDFLinkAnnotation, PDFMarkupAnnotation, PDFPage, PDFPolygonAnnotation, PDFPolylineAnnotation, PDFPopupAnnotation, PDFSquareAnnotation, PDFSquigglyAnnotation, PDFStampAnnotation, PDFStrikeOutAnnotation, PDFTextAnnotation, PDFTextMarkupAnnotation, PDFUnderlineAnnotation, PDFUnknownAnnotation, PathBuilder, type PathOptions, PdfArray, PdfBool, PdfDict, PdfName, PdfNull, PdfNumber, type PdfObject, PdfRef, PdfStream, PdfString, PermissionDeniedError, type PermissionOptions, type Permissions, PlaceholderError, type Point, type PolygonAnnotationOptions, type PolylineAnnotationOptions, type PopupOptions, type PositionedWord, type ProtectionOptions, type RGB, type RadioField, type RadioGroupOptions, type RadioSymbol, type Rect, type Rectangle, type RemoveAnnotationsOptions, RevocationError, type RevocationProvider, type Rotation, STANDARD_STAMPS, type SaveOptions, SecurityError, type SecurityInfo, type SetTitleOptions, type SignOptions, type SignResult, type SignWarning, type SignatureAlgorithm, SignatureError, type SignatureField, type SignatureFieldOptions, type Signer, SignerError, type SquareAnnotationOptions, type StampAnnotationOptions, type StampName, type Standard14FontName, StandardFonts, type SubFilter, TextAlignment, type TextAnnotationIcon, type TextAnnotationOptions, type TextAnnotationState, type TextAnnotationStateModel, type TextField, type TextFieldOptions, type TextLine, type TextMarkupAnnotationOptions, type TimestampAuthority, TimestampError, type TrappedStatus, black, blue, cmyk, createAnnotation, degrees, grayscale, green, isPopupAnnotation, isWidgetAnnotation, layoutJustifiedLine, layoutText, lineCapToNumber, lineJoinToNumber, measureText, rectToQuadPoints, rectsToQuadPoints, red, rgb, version, white };
8933
+ export { AnnotationFlags, type AnnotationSubtype, type AuthenticationResult, type BorderStyle, type BorderStyleType, type ButtonField, type CMYK, type CaretAnnotationOptions, type CaretSymbol, CertificateChainError, type CheckboxField, type CheckboxOptions, type CheckboxSymbol, type CircleAnnotationOptions, type Color, type CopyPagesOptions, CryptoKeySigner, type Degrees, type DestinationType, type DigestAlgorithm, type DocumentMetadata, type DrawCircleOptions, type DrawEllipseOptions, type DrawFieldOptions, type DrawImageOptions, type DrawLineOptions, type DrawPageOptions, type DrawRectangleOptions, type DrawTextOptions, type DropdownField, type DropdownOptions, type EmbedFontOptions, type EmbeddedFont, type EncryptionAlgorithmOption, type ExtractPagesOptions, type FieldOptions, type FieldType, type FieldValue, type FileAttachmentIcon, type FlattenAnnotationsOptions, type FlattenLayersResult, type FlattenOptions, type FontInput, type FormField, type FormProperties, type FreeTextAnnotationOptions, type FreeTextJustification, GoogleKmsSigner, type Grayscale, type HighlightMode, HttpTimestampAuthority, type HttpTimestampAuthorityOptions, type InkAnnotationOptions, type KeyType, KmsSignerError, type LayerInfo, type LayoutResult, type LineAnnotationOptions, type LineCap, type LineEndingStyle, type LineJoin, type LinkAction, type LinkAnnotationOptions, type LinkDestination, type ListBoxField, type ListboxOptions, type LoadOptions, type MergeOptions, P12Signer, type PAdESLevel, PDF, PDFAnnotation, PDFCaretAnnotation, PDFCircleAnnotation, PDFEmbeddedPage, PDFFileAttachmentAnnotation, PDFForm, PDFFreeTextAnnotation, PDFHighlightAnnotation, PDFImage, PDFInkAnnotation, PDFLineAnnotation, PDFLinkAnnotation, PDFMarkupAnnotation, PDFPage, PDFPolygonAnnotation, PDFPolylineAnnotation, PDFPopupAnnotation, PDFSquareAnnotation, PDFSquigglyAnnotation, PDFStampAnnotation, PDFStrikeOutAnnotation, PDFTextAnnotation, PDFTextMarkupAnnotation, PDFUnderlineAnnotation, PDFUnknownAnnotation, PathBuilder, type PathOptions, PdfArray, PdfBool, PdfDict, PdfName, PdfNull, PdfNumber, type PdfObject, PdfRef, PdfStream, PdfString, type PemBlock, PermissionDeniedError, type PermissionOptions, type Permissions, PlaceholderError, type Point, type PolygonAnnotationOptions, type PolylineAnnotationOptions, type PopupOptions, type PositionedWord, type ProtectionOptions, type RGB, type RadioField, type RadioGroupOptions, type RadioSymbol, type Rect, type Rectangle, type RemoveAnnotationsOptions, RevocationError, type RevocationProvider, type Rotation, STANDARD_STAMPS, type SaveOptions, SecurityError, type SecurityInfo, type SetTitleOptions, type SignOptions, type SignResult, type SignWarning, type SignatureAlgorithm, SignatureError, type SignatureField, type SignatureFieldOptions, type Signer, SignerError, type SquareAnnotationOptions, type StampAnnotationOptions, type StampName, type Standard14FontName, StandardFonts, type SubFilter, TextAlignment, type TextAnnotationIcon, type TextAnnotationOptions, type TextAnnotationState, type TextAnnotationStateModel, type TextField, type TextFieldOptions, type TextLine, type TextMarkupAnnotationOptions, type TimestampAuthority, TimestampError, type TrappedStatus, black, blue, cmyk, createAnnotation, degrees, grayscale, green, isPopupAnnotation, isWidgetAnnotation, layoutJustifiedLine, layoutText, lineCapToNumber, lineJoinToNumber, measureText, parsePem, rectToQuadPoints, rectsToQuadPoints, red, rgb, version, white };
8896
8934
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -9,7 +9,7 @@ import * as pkijs from "pkijs";
9
9
  import { base64 } from "@scure/base";
10
10
 
11
11
  //#region package.json
12
- var version = "0.0.1-beta.4";
12
+ var version = "0.0.1-beta.6";
13
13
 
14
14
  //#endregion
15
15
  //#region src/objects/pdf-array.ts
@@ -32774,10 +32774,10 @@ var PDFPage = class PDFPage {
32774
32774
  */
32775
32775
  getMediaBox() {
32776
32776
  return this.getBox("MediaBox") ?? {
32777
- x1: 0,
32778
- y1: 0,
32779
- x2: 612,
32780
- y2: 792
32777
+ x: 0,
32778
+ y: 0,
32779
+ width: 612,
32780
+ height: 792
32781
32781
  };
32782
32782
  }
32783
32783
  /**
@@ -32819,10 +32819,13 @@ var PDFPage = class PDFPage {
32819
32819
  * returns the height of the MediaBox instead.
32820
32820
  */
32821
32821
  get width() {
32822
- const box = this.getMediaBox();
32822
+ const mediaBox = this.getMediaBox();
32823
+ const cropBox = this.getCropBox();
32824
+ let box = mediaBox;
32825
+ if (cropBox.width < mediaBox.width || cropBox.height < mediaBox.height) box = cropBox;
32823
32826
  const rotation = this.rotation;
32824
- if (rotation === 90 || rotation === 270) return Math.abs(box.y2 - box.y1);
32825
- return Math.abs(box.x2 - box.x1);
32827
+ if (rotation === 90 || rotation === 270) return Math.abs(box.height);
32828
+ return Math.abs(box.width);
32826
32829
  }
32827
32830
  /**
32828
32831
  * Page height in points (based on MediaBox).
@@ -32831,10 +32834,13 @@ var PDFPage = class PDFPage {
32831
32834
  * returns the width of the MediaBox instead.
32832
32835
  */
32833
32836
  get height() {
32834
- const box = this.getMediaBox();
32837
+ const mediaBox = this.getMediaBox();
32838
+ const cropBox = this.getCropBox();
32839
+ let box = mediaBox;
32840
+ if (cropBox.width < mediaBox.width || cropBox.height < mediaBox.height) box = cropBox;
32835
32841
  const rotation = this.rotation;
32836
- if (rotation === 90 || rotation === 270) return Math.abs(box.x2 - box.x1);
32837
- return Math.abs(box.y2 - box.y1);
32842
+ if (rotation === 90 || rotation === 270) return Math.abs(box.width);
32843
+ return Math.abs(box.height);
32838
32844
  }
32839
32845
  /**
32840
32846
  * Whether the page is in landscape orientation.
@@ -32935,8 +32941,8 @@ var PDFPage = class PDFPage {
32935
32941
  });
32936
32942
  ops.push(`/${gsName} gs`);
32937
32943
  }
32938
- const translateX = x - embedded.box.x1 * scaleX;
32939
- const translateY = y - embedded.box.y1 * scaleY;
32944
+ const translateX = x - embedded.box.x * scaleX;
32945
+ const translateY = y - embedded.box.y * scaleY;
32940
32946
  ops.push(`${this.formatNumber(scaleX)} 0 0 ${this.formatNumber(scaleY)} ${this.formatNumber(translateX)} ${this.formatNumber(translateY)} cm`);
32941
32947
  ops.push(`/${xobjectName} Do`);
32942
32948
  ops.push("Q");
@@ -34016,10 +34022,10 @@ var PDFPage = class PDFPage {
34016
34022
  const y2 = box.at(3);
34017
34023
  if (!(x1 instanceof PdfNumber) || !(y1 instanceof PdfNumber) || !(x2 instanceof PdfNumber) || !(y2 instanceof PdfNumber)) return null;
34018
34024
  return {
34019
- x1: x1.value,
34020
- y1: y1.value,
34021
- x2: x2.value,
34022
- y2: y2.value
34025
+ x: x1.value,
34026
+ y: y1.value,
34027
+ width: x2.value,
34028
+ height: y2.value
34023
34029
  };
34024
34030
  }
34025
34031
  /**
@@ -37572,10 +37578,10 @@ var PDF = class PDF {
37572
37578
  Subtype: PdfName.of("Form"),
37573
37579
  FormType: PdfNumber.of(1),
37574
37580
  BBox: new PdfArray([
37575
- PdfNumber.of(mediaBox.x1),
37576
- PdfNumber.of(mediaBox.y1),
37577
- PdfNumber.of(mediaBox.x2),
37578
- PdfNumber.of(mediaBox.y2)
37581
+ PdfNumber.of(mediaBox.x),
37582
+ PdfNumber.of(mediaBox.y),
37583
+ PdfNumber.of(mediaBox.x + mediaBox.width),
37584
+ PdfNumber.of(mediaBox.y + mediaBox.height)
37579
37585
  ]),
37580
37586
  Resources: resources
37581
37587
  }, contentData);
@@ -38385,6 +38391,51 @@ var CryptoKeySigner = class {
38385
38391
  * lines used for certificates, keys, and other cryptographic data.
38386
38392
  */
38387
38393
  /**
38394
+ * Parse a PEM file into a series of DER blocks with their types.
38395
+ *
38396
+ * PEM files can contain multiple blocks (e.g., certificate chains). This function
38397
+ * extracts each block along with its label from the BEGIN statement.
38398
+ *
38399
+ * @param pem - The PEM-encoded string (may contain multiple blocks)
38400
+ * @returns Array of PEM blocks, each containing a label and DER data
38401
+ * @throws {Error} if the PEM format is invalid (mismatched labels or missing markers)
38402
+ *
38403
+ * @example
38404
+ * ```typescript
38405
+ * const blocks = parsePem(pemString);
38406
+ * // [
38407
+ * // { label: "CERTIFICATE", der: Uint8Array(...) },
38408
+ * // { label: "CERTIFICATE", der: Uint8Array(...) },
38409
+ * // { label: "PRIVATE KEY", der: Uint8Array(...) }
38410
+ * // ]
38411
+ * ```
38412
+ */
38413
+ function parsePem(pem) {
38414
+ const blocks = [];
38415
+ const blockRegex = /-----BEGIN ([A-Z0-9 ]+)-----([\s\S]*?)-----END ([A-Z0-9 ]+)-----/g;
38416
+ let match;
38417
+ while ((match = blockRegex.exec(pem)) !== null) {
38418
+ const beginLabel = match[1];
38419
+ const content = match[2];
38420
+ const endLabel = match[3];
38421
+ if (beginLabel !== endLabel) throw new Error(`PEM label mismatch: BEGIN ${beginLabel} but END ${endLabel}`);
38422
+ const b64 = content.replace(/\s/g, "");
38423
+ if (b64.length === 0) {
38424
+ blocks.push({
38425
+ label: beginLabel,
38426
+ der: new Uint8Array(0)
38427
+ });
38428
+ continue;
38429
+ }
38430
+ const der = base64.decode(b64);
38431
+ blocks.push({
38432
+ label: beginLabel,
38433
+ der
38434
+ });
38435
+ }
38436
+ return blocks;
38437
+ }
38438
+ /**
38388
38439
  * Convert DER (binary) bytes to PEM format.
38389
38440
  *
38390
38441
  * @param der - The DER-encoded binary data
@@ -40875,5 +40926,5 @@ var HttpTimestampAuthority = class {
40875
40926
  };
40876
40927
 
40877
40928
  //#endregion
40878
- export { AnnotationFlags, CertificateChainError, CryptoKeySigner, GoogleKmsSigner, HttpTimestampAuthority, KmsSignerError, P12Signer, PDF, PDFAnnotation, PDFCaretAnnotation, PDFCircleAnnotation, PDFEmbeddedPage, PDFFileAttachmentAnnotation, PDFForm, PDFFreeTextAnnotation, PDFHighlightAnnotation, PDFImage, PDFInkAnnotation, PDFLineAnnotation, PDFLinkAnnotation, PDFMarkupAnnotation, PDFPage, PDFPolygonAnnotation, PDFPolylineAnnotation, PDFPopupAnnotation, PDFSquareAnnotation, PDFSquigglyAnnotation, PDFStampAnnotation, PDFStrikeOutAnnotation, PDFTextAnnotation, PDFTextMarkupAnnotation, PDFUnderlineAnnotation, PDFUnknownAnnotation, PathBuilder, PdfArray, PdfBool, PdfDict, PdfName, PdfNull, PdfNumber, PdfRef, PdfStream, PdfString, PermissionDeniedError, PlaceholderError, RevocationError, STANDARD_STAMPS, SecurityError, SignatureError, SignerError, StandardFonts, TextAlignment, TimestampError, black, blue, cmyk, createAnnotation, degrees, grayscale, green, isPopupAnnotation, isWidgetAnnotation, layoutJustifiedLine, layoutText, lineCapToNumber, lineJoinToNumber, measureText, rectToQuadPoints, rectsToQuadPoints, red, rgb, version, white };
40929
+ export { AnnotationFlags, CertificateChainError, CryptoKeySigner, GoogleKmsSigner, HttpTimestampAuthority, KmsSignerError, P12Signer, PDF, PDFAnnotation, PDFCaretAnnotation, PDFCircleAnnotation, PDFEmbeddedPage, PDFFileAttachmentAnnotation, PDFForm, PDFFreeTextAnnotation, PDFHighlightAnnotation, PDFImage, PDFInkAnnotation, PDFLineAnnotation, PDFLinkAnnotation, PDFMarkupAnnotation, PDFPage, PDFPolygonAnnotation, PDFPolylineAnnotation, PDFPopupAnnotation, PDFSquareAnnotation, PDFSquigglyAnnotation, PDFStampAnnotation, PDFStrikeOutAnnotation, PDFTextAnnotation, PDFTextMarkupAnnotation, PDFUnderlineAnnotation, PDFUnknownAnnotation, PathBuilder, PdfArray, PdfBool, PdfDict, PdfName, PdfNull, PdfNumber, PdfRef, PdfStream, PdfString, PermissionDeniedError, PlaceholderError, RevocationError, STANDARD_STAMPS, SecurityError, SignatureError, SignerError, StandardFonts, TextAlignment, TimestampError, black, blue, cmyk, createAnnotation, degrees, grayscale, green, isPopupAnnotation, isWidgetAnnotation, layoutJustifiedLine, layoutText, lineCapToNumber, lineJoinToNumber, measureText, parsePem, rectToQuadPoints, rectsToQuadPoints, red, rgb, version, white };
40879
40930
  //# sourceMappingURL=index.mjs.map