pols-pdf 1.1.4 → 1.2.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
@@ -31,10 +31,17 @@ export type PBorderColorParams = string | {
31
31
  left?: string;
32
32
  leftTop?: string;
33
33
  };
34
+ export type PPageMarginsParams = number | {
35
+ top?: number;
36
+ bottom?: number;
37
+ left?: number;
38
+ right?: number;
39
+ show?: boolean;
40
+ };
34
41
  export type PPage = {
35
42
  size: 'A4' | 'LETTER';
36
43
  layout: 'portrait' | 'landscape';
37
- margins: PBoundParams;
44
+ margins: PPageMarginsParams;
38
45
  };
39
46
  export type PFont = {
40
47
  name: string;
@@ -157,6 +164,7 @@ export declare class PPdf {
157
164
  private configPage;
158
165
  engine: typeof pdfkit;
159
166
  template?: () => void;
167
+ private templateIsRunning;
160
168
  private currentFont;
161
169
  get title(): string;
162
170
  set title(value: string);
@@ -176,6 +184,7 @@ export declare class PPdf {
176
184
  addFirstPage?: boolean;
177
185
  });
178
186
  addPage(config?: Partial<PPage>): void;
187
+ private drawMarginLines;
179
188
  rectangleTo(params: Omit<PRectangle, 'width' | 'height'>): void;
180
189
  rectangle(params: PRectangle): void;
181
190
  build(): PDFKit.PDFDocument;
@@ -198,6 +207,7 @@ export declare class PPdf {
198
207
  cell(params: PCellParams): void;
199
208
  image(params: PImage): void;
200
209
  private prepareBoundValues;
210
+ private prepareMarginsValues;
201
211
  private prepareRadiusValues;
202
212
  private prepareBorderValues;
203
213
  private prepareBorderColorValues;
package/dist/index.js CHANGED
@@ -31,6 +31,7 @@ class PPdf {
31
31
  configPage;
32
32
  engine;
33
33
  template;
34
+ templateIsRunning = false;
34
35
  currentFont = {
35
36
  name: 'Helvetica',
36
37
  bold: false,
@@ -95,31 +96,47 @@ class PPdf {
95
96
  size: 'A4',
96
97
  layout: 'portrait',
97
98
  ...(params?.page ?? {}),
98
- margins: this.prepareBoundValues(params?.page?.margins, {
99
+ margins: this.prepareMarginsValues(params?.page?.margins, {
99
100
  left: 20,
100
101
  top: 20,
101
102
  right: 20,
102
- bottom: 20
103
+ bottom: 20,
104
+ show: false
103
105
  })
104
106
  };
105
107
  if (params?.addFirstPage == null || params.addFirstPage)
106
108
  this.addPage();
107
109
  }
108
110
  addPage(config) {
111
+ if (this.templateIsRunning)
112
+ throw new Error(`Intento de inserción de página dentro de una llamada a la función 'template'`);
109
113
  const finalConfig = {
110
114
  ...this.configPage,
111
115
  ...(config ?? {}),
112
- margins: this.prepareBoundValues(config?.margins ?? this.configPage.margins, {
113
- left: 20,
114
- top: 20,
115
- right: 20,
116
- bottom: 20
117
- })
116
+ margins: this.prepareMarginsValues(config?.margins ?? this.configPage.margins, this.configPage.margins)
118
117
  };
119
118
  this.engine.addPage(finalConfig);
120
119
  this.configPage = finalConfig;
121
120
  this.cursor.top = finalConfig.margins.top;
122
- this.template?.();
121
+ this.drawMarginLines();
122
+ this.templateIsRunning = true;
123
+ try {
124
+ this.template?.();
125
+ }
126
+ finally {
127
+ this.templateIsRunning = false;
128
+ }
129
+ }
130
+ /* Dibuja líneas grises en los márgenes de la hoja actual, sin alterar el cursor */
131
+ drawMarginLines() {
132
+ if (!this.configPage.margins.show)
133
+ return;
134
+ const left = this.configPage.margins.left;
135
+ const top = this.configPage.margins.top;
136
+ const right = this.engine.page.width - this.configPage.margins.right;
137
+ const bottom = this.engine.page.height - this.configPage.margins.bottom;
138
+ this.engine.lineWidth(0.5);
139
+ this.engine.path(`M${left},${top} L${right},${top} L${right},${bottom} L${left},${bottom} Z`).stroke('#999999');
123
140
  }
124
141
  rectangleTo(params) {
125
142
  this.rectangle({
@@ -374,6 +391,9 @@ class PPdf {
374
391
  if (value == 'left') {
375
392
  n = this.engine.page.margins.left;
376
393
  }
394
+ else if (value < 0) {
395
+ n = this.engine.page.width + value;
396
+ }
377
397
  else {
378
398
  n = value;
379
399
  }
@@ -389,6 +409,9 @@ class PPdf {
389
409
  if (value == 'top') {
390
410
  n = this.engine.page.margins.top;
391
411
  }
412
+ else if (value < 0) {
413
+ n = this.engine.page.height + value;
414
+ }
392
415
  else {
393
416
  n = value;
394
417
  }
@@ -757,6 +780,12 @@ class PPdf {
757
780
  return defa;
758
781
  }
759
782
  }
783
+ prepareMarginsValues(value, defa) {
784
+ return {
785
+ ...this.prepareBoundValues(value, defa),
786
+ show: (typeof value == 'object' ? value.show : undefined) ?? defa.show,
787
+ };
788
+ }
760
789
  prepareRadiusValues(value, defa = {
761
790
  leftTop: 0,
762
791
  leftBottom: 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pols-pdf",
3
- "version": "1.1.4",
3
+ "version": "1.2.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {