@libpdf/core 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # LibPDF
2
2
 
3
+ [![npm](https://img.shields.io/npm/v/@libpdf/core)](https://www.npmjs.com/package/@libpdf/core)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@libpdf/core)](https://www.npmjs.com/package/@libpdf/core)
5
+ [![CI](https://github.com/LibPDF-js/core/actions/workflows/ci.yml/badge.svg)](https://github.com/LibPDF-js/core/actions/workflows/ci.yml)
6
+ [![GitHub stars](https://img.shields.io/github/stars/libpdf-js/core?style=flat)](https://github.com/LibPDF-js/core)
7
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
8
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0-blue?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
9
+
3
10
  A modern PDF library for TypeScript. Parse, modify, and generate PDFs with a clean, intuitive API.
4
11
 
5
12
  > **Beta Software**: LibPDF is under active development and APIs may change between minor versions, but we use it in production at [Documenso](https://documenso.com) and consider it ready for real-world use.
@@ -8,7 +15,7 @@ A modern PDF library for TypeScript. Parse, modify, and generate PDFs with a cle
8
15
 
9
16
  LibPDF was born from frustration. At [Documenso](https://documenso.com), we found ourselves wrestling with the JavaScript PDF ecosystem:
10
17
 
11
- - **PDF.js** is excellent for rendering, but it's read-only
18
+ - **PDF.js** is excellent for rendering and even has annotation editing — but it requires a browser
12
19
  - **pdf-lib** has a great API, but chokes on slightly malformed documents
13
20
  - **pdfkit** only generates, no parsing at all
14
21
 
@@ -51,7 +58,7 @@ bun add @libpdf/core
51
58
  import { PDF } from "@libpdf/core";
52
59
 
53
60
  const pdf = await PDF.load(bytes);
54
- const pages = await pdf.getPages();
61
+ const pages = pdf.getPages();
55
62
 
56
63
  console.log(`${pages.length} pages`);
57
64
  ```
@@ -66,7 +73,7 @@ const pdf = await PDF.load(bytes, { credentials: "password" });
66
73
 
67
74
  ```typescript
68
75
  const pdf = await PDF.load(bytes);
69
- const form = await pdf.getForm();
76
+ const form = pdf.getForm();
70
77
 
71
78
  form.fill({
72
79
  name: "Jane Doe",
package/dist/index.d.mts CHANGED
@@ -3732,20 +3732,41 @@ interface FindTextOptions {
3732
3732
  wholeWord?: boolean;
3733
3733
  }
3734
3734
  //#endregion
3735
- //#region src/fonts/standard-14.d.ts
3735
+ //#region src/svg/path-executor.d.ts
3736
3736
  /**
3737
- * Standard 14 PDF Fonts
3738
- *
3739
- * These are the 14 fonts that every PDF reader must support.
3740
- * They don't require embedding - the reader provides them.
3741
- *
3742
- * Width tables are stored by glyph name (like pdf.js) which works
3743
- * with any encoding. Use getWidthByGlyphName() for lookup.
3744
- *
3745
- * Widths are in glyph units (1000 units = 1 em).
3746
- *
3747
- * Data extracted from pdf.js metrics.js (Mozilla, Apache 2.0 License)
3737
+ * Options for SVG path execution.
3748
3738
  */
3739
+ interface SvgPathExecutorOptions {
3740
+ /**
3741
+ * Flip Y coordinates (negate Y values).
3742
+ *
3743
+ * SVG uses a top-left origin with Y increasing downward.
3744
+ * PDF uses a bottom-left origin with Y increasing upward.
3745
+ *
3746
+ * When true (default), Y coordinates are negated to convert
3747
+ * SVG paths to PDF coordinate space.
3748
+ *
3749
+ * @default true
3750
+ */
3751
+ flipY?: boolean;
3752
+ /**
3753
+ * Scale factor to apply to all coordinates.
3754
+ * @default 1
3755
+ */
3756
+ scale?: number;
3757
+ /**
3758
+ * X offset to add after scaling and flipping.
3759
+ * @default 0
3760
+ */
3761
+ translateX?: number;
3762
+ /**
3763
+ * Y offset to add after scaling and flipping.
3764
+ * @default 0
3765
+ */
3766
+ translateY?: number;
3767
+ }
3768
+ //#endregion
3769
+ //#region src/fonts/standard-14.d.ts
3749
3770
  /**
3750
3771
  * Standard 14 font names.
3751
3772
  */
@@ -3971,6 +3992,65 @@ interface DrawEllipseOptions {
3971
3992
  /** Rotation */
3972
3993
  rotate?: Rotation;
3973
3994
  }
3995
+ /**
3996
+ * Options for drawing an SVG path.
3997
+ *
3998
+ * SVG paths are automatically transformed from SVG coordinate space
3999
+ * (Y-down, origin at top-left) to PDF coordinate space (Y-up, origin
4000
+ * at bottom-left). Use `x`, `y`, and `scale` to position and size
4001
+ * the path on the page.
4002
+ */
4003
+ interface DrawSvgPathOptions {
4004
+ /**
4005
+ * X position on the page (left edge of the path's bounding box).
4006
+ * @default 0
4007
+ */
4008
+ x?: number;
4009
+ /**
4010
+ * Y position on the page (bottom edge of the path's bounding box after transform).
4011
+ * @default 0
4012
+ */
4013
+ y?: number;
4014
+ /**
4015
+ * Scale factor to apply to the path.
4016
+ * Useful for SVG icons with large viewBox (e.g., 512x512).
4017
+ * A scale of 0.1 would make a 512-unit icon ~51 points.
4018
+ * @default 1
4019
+ */
4020
+ scale?: number;
4021
+ /**
4022
+ * Whether to flip the Y-axis to convert from SVG coordinates (Y-down)
4023
+ * to PDF coordinates (Y-up).
4024
+ *
4025
+ * Set to `true` (default) when using SVG paths from icon libraries.
4026
+ * Set to `false` when using paths already in PDF coordinate space.
4027
+ *
4028
+ * @default true
4029
+ */
4030
+ flipY?: boolean;
4031
+ /** Fill color (default: black; omit to stroke only if borderColor set) */
4032
+ color?: Color;
4033
+ /** Stroke color (omit for no stroke) */
4034
+ borderColor?: Color;
4035
+ /** Stroke width in points (default: 1 if borderColor set) */
4036
+ borderWidth?: number;
4037
+ /** Line cap style */
4038
+ lineCap?: LineCap;
4039
+ /** Line join style */
4040
+ lineJoin?: LineJoin;
4041
+ /** Miter limit for miter joins */
4042
+ miterLimit?: number;
4043
+ /** Dash pattern array */
4044
+ dashArray?: number[];
4045
+ /** Dash pattern phase */
4046
+ dashPhase?: number;
4047
+ /** Fill opacity 0-1 (default: 1) */
4048
+ opacity?: number;
4049
+ /** Stroke opacity 0-1 (default: 1) */
4050
+ borderOpacity?: number;
4051
+ /** Winding rule for fill (default: "nonzero") */
4052
+ windingRule?: "nonzero" | "evenodd";
4053
+ }
3974
4054
  /**
3975
4055
  * Options for path painting.
3976
4056
  */
@@ -4010,8 +4090,9 @@ declare function lineJoinToNumber(join: LineJoin): 0 | 1 | 2;
4010
4090
  //#region src/api/drawing/path-builder.d.ts
4011
4091
  /**
4012
4092
  * Callback type for appending content to a page.
4093
+ * Accepts a string (for ASCII-only content) or raw bytes.
4013
4094
  */
4014
- type ContentAppender = (content: string) => void;
4095
+ type ContentAppender = (content: string | Uint8Array) => void;
4015
4096
  /**
4016
4097
  * Callback type for registering a graphics state and returning its name.
4017
4098
  */
@@ -4093,6 +4174,40 @@ declare class PathBuilder {
4093
4174
  * Add an ellipse to the current path.
4094
4175
  */
4095
4176
  ellipse(cx: number, cy: number, rx: number, ry: number): this;
4177
+ /**
4178
+ * Append an SVG path string to the current path.
4179
+ *
4180
+ * Parses the SVG path `d` attribute string and adds all commands to this path.
4181
+ * Relative commands (lowercase) are converted to absolute coordinates based on
4182
+ * the current point. Smooth curves (S, T) and arcs (A) are converted to
4183
+ * cubic bezier curves.
4184
+ *
4185
+ * By default, this method does NOT transform coordinates (flipY: false).
4186
+ * Use the options to apply scale, translation, and Y-flip for SVG paths.
4187
+ *
4188
+ * @param pathData - SVG path `d` attribute string
4189
+ * @param options - Execution options (flipY, scale, translate)
4190
+ * @returns This PathBuilder for chaining
4191
+ *
4192
+ * @example
4193
+ * ```typescript
4194
+ * // Simple path (no transform)
4195
+ * page.drawPath()
4196
+ * .appendSvgPath("M 10 10 L 100 10 L 55 90 Z")
4197
+ * .fill({ color: rgb(1, 0, 0) });
4198
+ *
4199
+ * // SVG icon with full transform
4200
+ * page.drawPath()
4201
+ * .appendSvgPath(iconPath, {
4202
+ * flipY: true,
4203
+ * scale: 0.1,
4204
+ * translateX: 100,
4205
+ * translateY: 500,
4206
+ * })
4207
+ * .fill({ color: rgb(0, 0, 0) });
4208
+ * ```
4209
+ */
4210
+ appendSvgPath(pathData: string, options?: SvgPathExecutorOptions): this;
4096
4211
  /**
4097
4212
  * Stroke the path with the given options.
4098
4213
  */
@@ -5204,6 +5319,49 @@ declare class PDFPage {
5204
5319
  * ```
5205
5320
  */
5206
5321
  drawPath(): PathBuilder;
5322
+ /**
5323
+ * Draw an SVG path on the page.
5324
+ *
5325
+ * This is a convenience method that parses an SVG path `d` attribute string
5326
+ * and draws it with the specified options. For more control, use `drawPath()`
5327
+ * with `appendSvgPath()`.
5328
+ *
5329
+ * By default, the path is filled with black. Specify `borderColor` without
5330
+ * `color` to stroke without filling.
5331
+ *
5332
+ * SVG paths are automatically transformed from SVG coordinate space (Y-down)
5333
+ * to PDF coordinate space (Y-up). Use `x`, `y` to position the path, and
5334
+ * `scale` to resize it.
5335
+ *
5336
+ * @param pathData - SVG path `d` attribute string
5337
+ * @param options - Drawing options (x, y, scale, color, etc.)
5338
+ *
5339
+ * @example
5340
+ * ```typescript
5341
+ * // Draw a Font Awesome heart icon at position (100, 500)
5342
+ * // Icon is 512x512 in SVG, scale to ~50pt
5343
+ * page.drawSvgPath(faHeartPath, {
5344
+ * x: 100,
5345
+ * y: 500,
5346
+ * scale: 0.1,
5347
+ * color: rgb(1, 0, 0),
5348
+ * });
5349
+ *
5350
+ * // Draw a simple triangle at default position (0, 0)
5351
+ * page.drawSvgPath("M 0 0 L 50 0 L 25 40 Z", {
5352
+ * color: rgb(0, 0, 1),
5353
+ * });
5354
+ *
5355
+ * // Stroke a curve
5356
+ * page.drawSvgPath("M 0 0 C 10 10, 30 10, 40 0", {
5357
+ * x: 200,
5358
+ * y: 300,
5359
+ * borderColor: rgb(0, 0, 0),
5360
+ * borderWidth: 2,
5361
+ * });
5362
+ * ```
5363
+ */
5364
+ drawSvgPath(pathData: string, options?: DrawSvgPathOptions): void;
5207
5365
  /** Cached annotations for this page */
5208
5366
  private _annotationCache;
5209
5367
  /**
@@ -5492,6 +5650,9 @@ declare class PDFPage {
5492
5650
  private formatNumber;
5493
5651
  /**
5494
5652
  * Create and register a content stream.
5653
+ *
5654
+ * Accepts either a string (for ASCII-only content like operator names and numbers)
5655
+ * or raw bytes (for content that may contain non-ASCII data).
5495
5656
  */
5496
5657
  private createContentStream;
5497
5658
  /**
@@ -5519,6 +5680,9 @@ declare class PDFPage {
5519
5680
  private registerGraphicsStateForOpacity;
5520
5681
  /**
5521
5682
  * Append operators to the page content stream.
5683
+ *
5684
+ * Uses Operator.toBytes() directly to avoid UTF-8 round-trip corruption
5685
+ * of non-ASCII bytes in PdfString operands (e.g., WinAnsi-encoded text).
5522
5686
  */
5523
5687
  private appendOperators;
5524
5688
  /**
@@ -5527,6 +5691,10 @@ declare class PDFPage {
5527
5691
  private addFontResource;
5528
5692
  /**
5529
5693
  * Encode text to a PDF string for the given font.
5694
+ *
5695
+ * Standard 14 fonts use WinAnsiEncoding (or SymbolEncoding/ZapfDingbatsEncoding).
5696
+ * Unencodable characters are substituted with .notdef (byte 0x00).
5697
+ * Embedded fonts use Identity-H encoding with glyph IDs.
5530
5698
  */
5531
5699
  private encodeTextForFont;
5532
5700
  /**
@@ -9260,5 +9428,5 @@ declare function isWidgetAnnotation(dict: PdfDict, registry?: ObjectRegistry): b
9260
9428
  */
9261
9429
  declare function isPopupAnnotation(dict: PdfDict, registry?: ObjectRegistry): boolean;
9262
9430
  //#endregion
9263
- 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 FlattenAllOptions, type FlattenAllResult, 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, type RotationOrigin, type RotationOriginName, 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, Standard14Font, type Standard14FontName, StandardFonts, type SubFilter, type 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 };
9431
+ 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 DrawSvgPathOptions, type DrawTextOptions, type DropdownField, type DropdownOptions, type EmbedFontOptions, type EmbeddedFont, type EncryptionAlgorithmOption, type ExtractPagesOptions, type FieldOptions, type FieldType, type FieldValue, type FileAttachmentIcon, type FlattenAllOptions, type FlattenAllResult, 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, type RotationOrigin, type RotationOriginName, 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, Standard14Font, type Standard14FontName, StandardFonts, type SubFilter, type 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 };
9264
9432
  //# sourceMappingURL=index.d.mts.map