pdf-lite 1.7.4-alpha.4 → 1.7.4-alpha.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.
@@ -14,6 +14,7 @@ export declare class PdfAppearanceStream extends PdfIndirectObject<PdfStream> {
14
14
  height?: number;
15
15
  contentStream?: string;
16
16
  resources?: PdfDictionary;
17
+ matrix?: [number, number, number, number, number, number];
17
18
  });
18
19
  get contentStream(): string;
19
20
  set contentStream(newContent: string);
@@ -20,6 +20,9 @@ export class PdfAppearanceStream extends PdfIndirectObject {
20
20
  new PdfNumber(options.width ?? 100),
21
21
  new PdfNumber(options.height ?? 100),
22
22
  ]));
23
+ if (options.matrix) {
24
+ appearanceDict.set('Matrix', new PdfArray(options.matrix.map((v) => new PdfNumber(v))));
25
+ }
23
26
  if (options.resources) {
24
27
  appearanceDict.set('Resources', options.resources);
25
28
  }
@@ -16,5 +16,6 @@ export declare class PdfChoiceAppearanceStream extends PdfAppearanceStream {
16
16
  displayOptions?: string[];
17
17
  selectedIndex?: number;
18
18
  quadding?: number;
19
+ rotation?: number;
19
20
  });
20
21
  }
@@ -14,13 +14,35 @@ function calcTextX(quadding, padding, availableWidth, textWidth) {
14
14
  }
15
15
  export class PdfChoiceAppearanceStream extends PdfAppearanceStream {
16
16
  constructor(ctx) {
17
- const [x1, y1, x2, y2] = ctx.rect;
18
- const width = x2 - x1;
19
- const height = y2 - y1;
17
+ // Normalize rect coordinates: some PDFs have inverted y-axis where y1 > y2
18
+ let [x1, y1, x2, y2] = ctx.rect;
19
+ if (x2 < x1)
20
+ [x1, x2] = [x2, x1];
21
+ if (y2 < y1)
22
+ [y1, y2] = [y2, y1];
23
+ const rectWidth = x2 - x1;
24
+ const rectHeight = y2 - y1;
25
+ const rotation = ctx.rotation ?? 0;
26
+ const needsSwap = rotation === 90 || rotation === 270;
27
+ const width = needsSwap ? rectHeight : rectWidth;
28
+ const height = needsSwap ? rectWidth : rectHeight;
29
+ // For rotated pages, the BBox uses the displayed (swapped)
30
+ // dimensions and a Matrix entry maps back to annotation space.
31
+ let matrix;
32
+ if (rotation === 90) {
33
+ matrix = [0, 1, -1, 0, rectWidth, 0];
34
+ }
35
+ else if (rotation === 270) {
36
+ matrix = [0, -1, 1, 0, 0, rectHeight];
37
+ }
38
+ else if (rotation === 180) {
39
+ matrix = [-1, 0, 0, -1, rectWidth, rectHeight];
40
+ }
20
41
  super({
21
42
  width,
22
43
  height,
23
44
  resources: ctx.fontResources,
45
+ matrix,
24
46
  });
25
47
  const isUnicode = ctx.isUnicode ?? false;
26
48
  const reverseEncodingMap = ctx.reverseEncodingMap;
@@ -22,5 +22,6 @@ export declare class PdfTextAppearanceStream extends PdfAppearanceStream {
22
22
  markdown?: string;
23
23
  fontVariantNames?: FontVariantNames;
24
24
  quadding?: number;
25
+ rotation?: number;
25
26
  });
26
27
  }
@@ -21,9 +21,24 @@ function calcTextX(quadding, padding, availableWidth, textWidth) {
21
21
  */
22
22
  export class PdfTextAppearanceStream extends PdfAppearanceStream {
23
23
  constructor(ctx) {
24
- const [x1, y1, x2, y2] = ctx.rect;
25
- const width = x2 - x1;
26
- const height = y2 - y1;
24
+ // Normalize rect coordinates: some PDFs have inverted y-axis where y1 > y2
25
+ // This ensures width and height are always positive
26
+ let [x1, y1, x2, y2] = ctx.rect;
27
+ if (x2 < x1)
28
+ [x1, x2] = [x2, x1];
29
+ if (y2 < y1)
30
+ [y1, y2] = [y2, y1];
31
+ const rectWidth = x2 - x1;
32
+ const rectHeight = y2 - y1;
33
+ // For rotated pages, the field rect is in the unrotated coordinate
34
+ // space. A 90° or 270° page rotation swaps how the field appears on
35
+ // screen (tall/narrow rect becomes wide/short when viewed). We swap
36
+ // the layout dimensions so text is laid out for the displayed shape,
37
+ // then apply a rotation matrix to map back into the BBox.
38
+ const rotation = ctx.rotation ?? 0;
39
+ const needsSwap = rotation === 90 || rotation === 270;
40
+ const width = needsSwap ? rectHeight : rectWidth;
41
+ const height = needsSwap ? rectWidth : rectHeight;
27
42
  const value = ctx.value;
28
43
  const isUnicode = ctx.isUnicode ?? false;
29
44
  const reverseEncodingMap = ctx.reverseEncodingMap;
@@ -161,11 +176,24 @@ export class PdfTextAppearanceStream extends PdfAppearanceStream {
161
176
  }
162
177
  g.restore();
163
178
  g.endMarkedContent();
179
+ // For rotated pages, the BBox uses the displayed (swapped)
180
+ // dimensions and a Matrix entry maps back to annotation space.
181
+ let matrix;
182
+ if (rotation === 90) {
183
+ matrix = [0, 1, -1, 0, rectWidth, 0];
184
+ }
185
+ else if (rotation === 270) {
186
+ matrix = [0, -1, 1, 0, 0, rectHeight];
187
+ }
188
+ else if (rotation === 180) {
189
+ matrix = [-1, 0, 0, -1, rectWidth, rectHeight];
190
+ }
164
191
  super({
165
192
  width,
166
193
  height,
167
194
  contentStream: g.build(),
168
195
  resources: ctx.fontResources,
196
+ matrix,
169
197
  });
170
198
  }
171
199
  }
@@ -88,7 +88,12 @@ export class PdfButtonFormField extends PdfFormField {
88
88
  const rect = this.rect;
89
89
  if (!rect || rect.length !== 4)
90
90
  return false;
91
- const [x1, y1, x2, y2] = rect;
91
+ // Normalize rect coordinates: some PDFs have inverted y-axis where y1 > y2
92
+ let [x1, y1, x2, y2] = rect;
93
+ if (x2 < x1)
94
+ [x1, x2] = [x2, x1];
95
+ if (y2 < y1)
96
+ [y1, y2] = [y2, y1];
92
97
  const width = x2 - x1;
93
98
  const height = y2 - y1;
94
99
  // Merge own flags with parent flags so inherited bits (e.g. Radio) are
@@ -97,11 +97,15 @@ export class PdfChoiceFormField extends PdfFormField {
97
97
  da: parsed,
98
98
  flags: this.flags,
99
99
  fontResources,
100
+ resolvedFonts: font
101
+ ? new Map([[parsed.fontName, font]])
102
+ : undefined,
100
103
  isUnicode,
101
104
  reverseEncodingMap,
102
105
  displayOptions: this.options.map((opt) => opt.label),
103
106
  selectedIndex: this.selectedIndex,
104
107
  quadding: this.quadding,
108
+ rotation: this.page?.rotate ?? 0,
105
109
  });
106
110
  if (options?.makeReadOnly) {
107
111
  this.readOnly = true;
@@ -55,6 +55,7 @@ export class PdfTextFormField extends PdfFormField {
55
55
  markdown: this.markdownValue,
56
56
  fontVariantNames: variantNames,
57
57
  quadding: this.quadding,
58
+ rotation: this.page?.rotate ?? 0,
58
59
  });
59
60
  if (options?.makeReadOnly) {
60
61
  this.readOnly = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-lite",
3
- "version": "1.7.4-alpha.4",
3
+ "version": "1.7.4-alpha.6",
4
4
  "main": "dist/index.js",
5
5
  "type": "module",
6
6
  "exports": {