pols-pdf 1.0.2 → 1.1.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.
package/dist/index.d.ts CHANGED
@@ -74,6 +74,10 @@ export type PFont = {
74
74
  size: number;
75
75
  color: string;
76
76
  };
77
+ export type PFontSize = number | `${number}%`;
78
+ export type PFontParams = Omit<PFont, 'size'> & {
79
+ size: PFontSize;
80
+ };
77
81
  export type PImage = {
78
82
  filePath: string | Buffer;
79
83
  left?: number;
@@ -113,7 +117,7 @@ export type PRectangle = {
113
117
  };
114
118
  export type PTextParams = {
115
119
  chars: string;
116
- font?: Partial<PFont>;
120
+ font?: Partial<PFontParams>;
117
121
  };
118
122
  type PLetter = {
119
123
  char: string;
@@ -145,7 +149,7 @@ export type PCellParams = {
145
149
  left?: number;
146
150
  top?: number;
147
151
  width?: 'auto' | number;
148
- font?: Partial<PFont>;
152
+ font?: Partial<PFontParams>;
149
153
  fillColor?: string;
150
154
  padding?: PBoundParams;
151
155
  border?: PBorderParams;
@@ -161,7 +165,7 @@ export type PCellParams = {
161
165
  } & ({
162
166
  height: number;
163
167
  } | {
164
- autojump: true | undefined;
168
+ autojump?: boolean;
165
169
  });
166
170
  export type PRow = {
167
171
  cells: Omit<PCell, 'autojump'>[];
@@ -191,7 +195,7 @@ export type PRowParams = {
191
195
  padding?: PBoundParams;
192
196
  border?: PBorderParams;
193
197
  radius?: PRadiusParams;
194
- font?: Partial<PFont>;
198
+ font?: Partial<PFontParams>;
195
199
  stroke?: {
196
200
  width?: number;
197
201
  color?: PBorderColorParams;
@@ -209,7 +213,7 @@ export type PTableParams = {
209
213
  padding?: PBoundParams;
210
214
  border?: PBorderParams;
211
215
  radius?: PRadiusParams;
212
- font?: Partial<PFont>;
216
+ font?: Partial<PFontParams>;
213
217
  stroke?: {
214
218
  width?: number;
215
219
  color?: PBorderColorParams;
@@ -251,7 +255,8 @@ export declare class PPdf {
251
255
  private capturedFont?;
252
256
  private captureFont;
253
257
  private restoreFont;
254
- setFont(font: Partial<PFont>): void;
258
+ private resolveFontSize;
259
+ setFont(font: Partial<PFontParams>): void;
255
260
  constructor(params?: {
256
261
  title?: string;
257
262
  page?: Partial<PPage>;
package/dist/index.js CHANGED
@@ -47,13 +47,25 @@ class PPdf {
47
47
  if (this.capturedFont)
48
48
  this.currentFont = this.capturedFont;
49
49
  }
50
+ /* Resuelve un PFontSize a un número absoluto. Un porcentaje ('x%') se calcula sobre `base`,
51
+ que es siempre el tamaño de fuente activo (this.currentFont.size) antes de aplicar este tamaño. */
52
+ resolveFontSize(size, base) {
53
+ if (size == null)
54
+ return undefined;
55
+ if (typeof size == 'number')
56
+ return size;
57
+ const match = /^(\d+(?:\.\d+)?)%$/.exec(size);
58
+ if (!match)
59
+ throw new Error(`Formato de tamaño de fuente inválido: "${size}"`);
60
+ return base * (Number(match[1]) / 100);
61
+ }
50
62
  /* Establece en el engine la fuente especificada en currentFont */
51
63
  setFont(font) {
52
64
  const finalFont = {
53
65
  name: font.name ?? this.currentFont.name,
54
66
  bold: font.bold ?? this.currentFont.bold,
55
67
  oblique: font.oblique ?? this.currentFont.oblique,
56
- size: font.size ?? this.currentFont.size,
68
+ size: this.resolveFontSize(font.size, this.currentFont.size) ?? this.currentFont.size,
57
69
  color: font.color ?? this.currentFont.color,
58
70
  };
59
71
  let fontName = this.currentFont.name;
@@ -193,7 +205,7 @@ class PPdf {
193
205
  top: cursorTop,
194
206
  width: usefulWidth,
195
207
  font: params.font,
196
- autojump: (params.autojump ?? true) ? true : undefined
208
+ autojump: params.autojump ?? true
197
209
  });
198
210
  if (preparedRow.currentPage.cells.some(cell => cell.letters?.length)) {
199
211
  buffer.rows.push(preparedRow.currentPage);
@@ -293,7 +305,7 @@ class PPdf {
293
305
  top: top + padding.top,
294
306
  width: cellWidth,
295
307
  font: cell.font ?? params.font,
296
- autojump: (params.autojump ?? true) ? true : undefined,
308
+ autojump: params.autojump ?? true,
297
309
  complete: params.complete
298
310
  });
299
311
  response.currentPage.cells.push(preparedCell.currentPage);
@@ -451,7 +463,7 @@ class PPdf {
451
463
  name: t.font?.name ?? params.font?.name ?? this.currentFont.name,
452
464
  bold: t.font?.bold ?? params.font?.bold ?? this.currentFont.bold,
453
465
  oblique: t.font?.oblique ?? params.font?.oblique ?? this.currentFont.oblique,
454
- size: t.font?.size ?? params.font?.size ?? this.currentFont.size,
466
+ size: this.resolveFontSize(t.font?.size, this.currentFont.size) ?? this.resolveFontSize(params.font?.size, this.currentFont.size) ?? this.currentFont.size,
455
467
  color: t.font?.color ?? params.font?.color ?? this.currentFont.color,
456
468
  };
457
469
  this.setFont(font);
@@ -566,7 +578,7 @@ class PPdf {
566
578
  if (!firstLetterOfLine)
567
579
  continue;
568
580
  /* Si la letra se desborda, se crea un nuevo párrafo */
569
- if ('autojump' in params && currentParagraph != 'nextPage' && firstLetterOfLine.top + firstLetterOfLine.height + padding.bottom > (this.engine.page.height - this.engine.page.margins.bottom)) {
581
+ if (!('height' in params) && (params.autojump ?? true) && currentParagraph != 'nextPage' && firstLetterOfLine.top + firstLetterOfLine.height + padding.bottom > (this.engine.page.height - this.engine.page.margins.bottom)) {
570
582
  if (params.complete) {
571
583
  paragraphs.nextPage = paragraphs.currentPage;
572
584
  paragraphs.currentPage = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pols-pdf",
3
- "version": "1.0.2",
3
+ "version": "1.1.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {