av6-pdf-engine 1.2.0 → 3.0.0

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
@@ -1,5 +1,29 @@
1
1
  import PDFDocument from 'pdfkit';
2
2
 
3
+ type ShapeType = "rect" | "roundedRect" | "circle" | "ellipse" | "line" | "polygon" | "path";
4
+ interface ShapePoint {
5
+ x: number;
6
+ y: number;
7
+ }
8
+ interface ShapeBlock extends BaseBlock {
9
+ type: "shape";
10
+ shape: ShapeType;
11
+ width?: number;
12
+ height?: number;
13
+ radius?: number;
14
+ points?: ShapePoint[];
15
+ path?: string;
16
+ fillColor?: string;
17
+ strokeColor?: string;
18
+ lineWidth?: number;
19
+ opacity?: number;
20
+ align?: Alignment;
21
+ marginTop?: number;
22
+ marginBottom?: number;
23
+ marginLeft?: number;
24
+ marginRight?: number;
25
+ }
26
+
3
27
  interface SignatureBlock extends BaseBlock {
4
28
  type: "signature";
5
29
  height: number;
@@ -11,6 +35,39 @@ interface SignatureBlock extends BaseBlock {
11
35
  marginRight?: number;
12
36
  }
13
37
 
38
+ type ListStyle = "bullet" | "number";
39
+ interface ListItem {
40
+ text: string;
41
+ bold?: boolean;
42
+ italic?: boolean;
43
+ underline?: boolean;
44
+ strike?: boolean;
45
+ link?: string;
46
+ color?: string;
47
+ fontSize?: number;
48
+ font?: FontName;
49
+ lineGap?: number;
50
+ style?: StyleRef;
51
+ }
52
+ interface ListBlock extends BaseBlock {
53
+ type: "list";
54
+ items: (string | ListItem)[];
55
+ listStyle?: ListStyle;
56
+ bullet?: string;
57
+ start?: number;
58
+ markerWidth?: number;
59
+ markerGap?: number;
60
+ itemGap?: number;
61
+ fontSize?: number;
62
+ lineGap?: number;
63
+ color?: string;
64
+ style?: StyleRef;
65
+ marginTop?: number;
66
+ marginBottom?: number;
67
+ marginLeft?: number;
68
+ marginRight?: number;
69
+ }
70
+
14
71
  interface KeyValueItem {
15
72
  key: string;
16
73
  value: string;
@@ -48,8 +105,25 @@ interface PageBreakBlock extends BaseBlock {
48
105
  type: "pageBreak";
49
106
  }
50
107
 
108
+ type TableBorderSide = "top" | "right" | "bottom" | "left";
109
+ interface TableBorderStyle {
110
+ visible?: boolean;
111
+ color?: string;
112
+ width?: number;
113
+ dash?: number[];
114
+ }
115
+ type TableCellBorder = boolean | TableBorderStyle | Partial<Record<TableBorderSide, boolean | TableBorderStyle>>;
116
+ interface TableColumnStyle {
117
+ border?: TableCellBorder;
118
+ fillColor?: string;
119
+ }
120
+ interface TableRowStyle {
121
+ border?: TableCellBorder;
122
+ fillColor?: string;
123
+ }
51
124
  interface TableCell {
52
- text: string;
125
+ text?: string;
126
+ blocks?: Block[];
53
127
  italic?: boolean;
54
128
  underline?: boolean;
55
129
  link?: string;
@@ -67,6 +141,7 @@ interface TableCell {
67
141
  paddingRight?: number;
68
142
  paddingBottom?: number;
69
143
  paddingLeft?: number;
144
+ border?: TableCellBorder;
70
145
  }
71
146
  interface TableBlock extends BaseBlock {
72
147
  type: "table";
@@ -77,6 +152,11 @@ interface TableBlock extends BaseBlock {
77
152
  border?: "all" | "none";
78
153
  hLineColor?: string;
79
154
  vLineColor?: string;
155
+ borderColor?: string;
156
+ borderWidth?: number;
157
+ borderDash?: number[];
158
+ rowStyles?: Record<number, TableRowStyle>;
159
+ columnStyles?: Record<number, TableColumnStyle>;
80
160
  };
81
161
  lineGap?: number;
82
162
  marginTop?: number;
@@ -167,6 +247,8 @@ interface TextBlock extends BaseBlock {
167
247
  color?: string;
168
248
  font?: FontName;
169
249
  style?: StyleRef;
250
+ marginTop?: number;
251
+ marginBottom?: number;
170
252
  marginLeft?: number;
171
253
  marginRight?: number;
172
254
  }
@@ -177,16 +259,7 @@ type Orientation = "horizontal" | "vertical";
177
259
  type Width = number | "*";
178
260
  type InlineTextStyle = Omit<TextBlock, "type" | "text" | "style">;
179
261
  type BuiltInFont = "Helvetica" | "Helvetica-Bold" | "Helvetica-Oblique" | "Helvetica-BoldOblique" | "Times-Roman" | "Times-Bold" | "Times-Italic" | "Times-BoldItalic" | "Courier" | "Courier-Bold" | "Courier-Oblique" | "Courier-BoldOblique";
180
- /**
181
- * FontName:
182
- * - autocomplete for built-ins
183
- * - but still allows any custom string when you registerFont
184
- */
185
262
  type FontName = BuiltInFont | (string & {});
186
- /**
187
- * Style definition similar to pdfmake's style object.
188
- * Can be applied to text blocks and table cells.
189
- */
190
263
  interface StyleDef {
191
264
  italic?: boolean;
192
265
  underline?: boolean;
@@ -200,14 +273,8 @@ interface StyleDef {
200
273
  fillColor?: string;
201
274
  font?: FontName;
202
275
  }
203
- /** Shared style reference field (single or multiple style names). */
204
276
  type StyleRef = string | string[] | undefined;
205
277
  interface PageMargins {
206
- /**
207
- * TOP is treated as header band height.
208
- * BOTTOM is treated as footer band height.
209
- * LEFT/RIGHT are used for content only.
210
- */
211
278
  top: number;
212
279
  right: number;
213
280
  bottom: number;
@@ -223,20 +290,24 @@ interface WatermarkDef {
223
290
  mode?: WatermarkMode;
224
291
  }
225
292
  interface PageBackgroundDef {
226
- /** Path or Buffer for the image to be used as full-page background */
227
293
  src?: string | Buffer;
228
- /** Optional global opacity for background (0–1). Default: 1 */
229
294
  opacity?: number;
230
295
  }
231
296
  interface FontRegistration {
232
297
  name: FontName;
233
298
  src: string | Buffer;
234
299
  }
300
+ interface BoxSpacing {
301
+ top?: number;
302
+ right?: number;
303
+ bottom?: number;
304
+ left?: number;
305
+ }
306
+ type SpacingInput = number | BoxSpacing;
235
307
  interface CustomDocDefinition {
236
- pageSize?: string;
308
+ pageSize?: string | [number, number];
237
309
  margins: PageMargins;
238
310
  pageOrientation?: "portrait" | "landscape";
239
- /** Optional font registrations (custom TTF/OTF) */
240
311
  fonts?: FontRegistration[];
241
312
  header?: HeaderDef;
242
313
  pageBackground?: PageBackgroundDef;
@@ -244,26 +315,41 @@ interface CustomDocDefinition {
244
315
  content: Block[];
245
316
  styles?: Record<string, StyleDef>;
246
317
  footer?: FooterInput;
318
+ defaultImage?: string | Buffer;
247
319
  }
248
320
  interface HeaderDef {
249
- blocks: Block[];
321
+ blocks?: Block[];
322
+ backgroundBlocks?: Block[];
250
323
  visible?: boolean;
251
324
  marginTop?: number;
252
325
  marginBottom?: number;
253
326
  marginLeft?: number;
254
327
  marginRight?: number;
328
+ padding?: SpacingInput;
329
+ paddingTop?: number;
330
+ paddingRight?: number;
331
+ paddingBottom?: number;
332
+ paddingLeft?: number;
255
333
  backgroundColor?: string;
256
334
  backgroundImage?: string | Buffer;
335
+ backgroundOpacity?: number;
257
336
  }
258
337
  interface FooterDef {
259
338
  blocks: Block[];
339
+ backgroundBlocks?: Block[];
260
340
  visible?: boolean;
261
341
  marginTop?: number;
262
342
  marginBottom?: number;
263
343
  marginLeft?: number;
264
344
  marginRight?: number;
345
+ padding?: SpacingInput;
346
+ paddingTop?: number;
347
+ paddingRight?: number;
348
+ paddingBottom?: number;
349
+ paddingLeft?: number;
265
350
  backgroundColor?: string;
266
351
  backgroundImage?: string | Buffer;
352
+ backgroundOpacity?: number;
267
353
  }
268
354
  type FooterInput = FooterDef | Block | Block[] | ((pageNumber: number, pageSize: {
269
355
  width: number;
@@ -271,8 +357,17 @@ type FooterInput = FooterDef | Block | Block[] | ((pageNumber: number, pageSize:
271
357
  }) => FooterDef | Block | Block[] | null | undefined);
272
358
  interface BaseBlock {
273
359
  visible?: boolean;
360
+ backgroundColor?: string;
361
+ backgroundImage?: string | Buffer;
362
+ backgroundOpacity?: number;
363
+ backgroundBlocks?: Block[];
364
+ padding?: SpacingInput;
365
+ paddingTop?: number;
366
+ paddingRight?: number;
367
+ paddingBottom?: number;
368
+ paddingLeft?: number;
274
369
  }
275
- type Block = TextBlock | ImageBlock | QrCodeBlock | BarcodeBlock | LineBlock | ColumnsBlock | TableBlock | PageBreakBlock | KeyValueGridBlock | SignatureBlock;
370
+ type Block = TextBlock | ImageBlock | QrCodeBlock | BarcodeBlock | LineBlock | ColumnsBlock | TableBlock | PageBreakBlock | KeyValueGridBlock | ListBlock | SignatureBlock | ShapeBlock;
276
371
 
277
372
  type BarcodeType = "CODE128" | "EAN13" | "CODE39" | "ITF" | "CODE93";
278
373
  declare const BARCODE_TYPES: Record<BarcodeType, BarcodeType>;
@@ -305,11 +400,18 @@ interface PageContext {
305
400
  signaturePlaced: boolean;
306
401
  afterSignature: boolean;
307
402
  inFooter: boolean;
403
+ /** True while finishPage is executing doc.addPage(), so the pageAdded listener can skip drawing the header (we draw it manually via startNewPageLayout). */
404
+ inManualPageAdd: boolean;
308
405
  }
309
406
  interface RenderEnv {
310
407
  marginLeft: number;
311
408
  innerWidth: number;
312
409
  allowPageBreak: boolean;
410
+ /**
411
+ * Internal flag.
412
+ * Used while rendering backgroundBlocks.
413
+ */
414
+ isBackgroundLayer?: boolean;
313
415
  }
314
416
 
315
417
  declare enum PdfEngineErrorCode {
@@ -355,7 +457,159 @@ declare function toPdfEngineError(cause: unknown, fallback: {
355
457
  retryable?: boolean;
356
458
  }): PdfEngineError;
357
459
 
460
+ declare const images: {
461
+ default: string;
462
+ };
463
+
358
464
  declare function renderCustomPdf(def: CustomDocDefinition, outputPath: string): Promise<void>;
359
465
  declare function renderCustomPdfToBuffer(def: CustomDocDefinition): Promise<Buffer>;
360
466
 
361
- export { type Alignment, BARCODE_TYPES, type BarcodeBlock, type BarcodeType, type BaseBlock, type Block, type BuiltInFont, type ColumnsBlock, type CustomDocDefinition, type FontName, type FontRegistration, type FooterDef, type FooterInput, type HeaderDef, type ImageBlock, type InlineTextStyle, type KeyValueGridBlock, type KeyValueItem, type LineBlock, type Orientation, type PDFDoc, type PageBackgroundDef, type PageBreakBlock, type PageContext, type PageMargins, PdfEngineError, PdfEngineErrorCode, type PlacedCell, type QRErrorLevel, QR_ERROR_LEVEL, type QrCodeBlock, type RenderEnv, type SignatureBlock, type StyleDef, type StyleRef, type TableBlock, type TableBodyIterable, type TableCell, type TextBlock, type WatermarkDef, type WatermarkMode, type Width, isPdfEngineError, renderCustomPdf, renderCustomPdfToBuffer, toPdfEngineError };
467
+ type MeasureBlockHeightFn = (block: Block, env: RenderEnv) => number;
468
+ declare function createMeasureBlockHeight(deps: {
469
+ doc: PDFDoc;
470
+ styles: Record<string, StyleDef>;
471
+ computeColumnPixelWidths: (widths: (number | "*")[], totalWidth: number) => number[];
472
+ }): MeasureBlockHeightFn;
473
+
474
+ interface BlockRendererDeps {
475
+ doc: PDFDoc;
476
+ ctx: PageContext;
477
+ styles: Record<string, StyleDef>;
478
+ computeColumnPixelWidths: (widths: (number | "*")[], totalWidth: number) => number[];
479
+ finishPage: (addNewPage: boolean) => void;
480
+ processSignatureBlock: (block: SignatureBlock) => void;
481
+ defaultImage?: string | Buffer;
482
+ }
483
+ declare function createBlockRenderer(deps: BlockRendererDeps): {
484
+ renderBlock: (block: Block, y: number | null, env: RenderEnv) => number;
485
+ renderBlockArray: (blocks: Block[], startY: number, env: RenderEnv) => number;
486
+ measureBlockHeight: MeasureBlockHeightFn;
487
+ };
488
+
489
+ declare function createInitialContext(doc: PDFDoc): PageContext;
490
+
491
+ type EnsureSpaceForFn = (heightNeeded: number, env: RenderEnv) => void;
492
+ /**
493
+ * Creates the `ensureSpaceFor` helper:
494
+ * - warns when a block is taller than the full page content area (it will overflow regardless)
495
+ * - if current block doesn't fit in remaining space, calls `finishPage(true)`
496
+ */
497
+ declare function createEnsureSpaceFor(ctx: PageContext, doc: PDFDoc, bottomLimitForContent: BottomLimitForContentFn, finishPage: (addNewPage: boolean) => void): EnsureSpaceForFn;
498
+
499
+ declare const contentEnv: (doc: PDFDoc) => RenderEnv;
500
+
501
+ type RenderBlockArrayFn = (blocks: Block[], startY: number, env: RenderEnv) => number;
502
+ interface FinishPageParams {
503
+ addNewPage: boolean;
504
+ doc: PDFDoc;
505
+ def: CustomDocDefinition;
506
+ ctx: PageContext;
507
+ footerBandHeight: number;
508
+ renderBlockArray: RenderBlockArrayFn;
509
+ startNewPageLayout: () => void;
510
+ }
511
+ declare function finishPage({ addNewPage, doc, def, ctx, footerBandHeight, renderBlockArray, startNewPageLayout, }: FinishPageParams): void;
512
+
513
+ declare function drawFooter(doc: PDFDoc, def: CustomDocDefinition, ctx: PageContext, footerBandHeight: number, renderBlockArray: (blocks: Block[], startY: number, env: RenderEnv) => number): void;
514
+
515
+ declare function drawHeader(doc: PDFDoc, def: CustomDocDefinition, headerBandHeight: number, header: HeaderDef | undefined, renderBlockArray: (blocks: Block[], startY: number, env: RenderEnv) => number): void;
516
+
517
+ declare function normalizeImageSrc(src: string | Buffer | undefined, fallback?: string | Buffer): Promise<string | Buffer | undefined>;
518
+ declare function materializeImagesInBlocks(blocks: Block[], fallback?: string | Buffer): Promise<Block[]>;
519
+
520
+ /**
521
+ * Compute pixel widths for a set of column width descriptors.
522
+ * - number: fixed width
523
+ * - "*": flex unit
524
+ */
525
+ declare function computeColumnPixelWidths(widths: Width[], totalWidth: number): number[];
526
+
527
+ /**
528
+ * Draws a full-page background image with optional opacity.
529
+ */
530
+ declare function drawPageBackground(doc: PDFDoc, pageBackground?: PageBackgroundDef): void;
531
+
532
+ declare function getBottomLimitForContent(doc: PDFDoc, ctx: PageContext): number;
533
+
534
+ type BottomLimitForContentFn = () => number;
535
+ /**
536
+ * Creates a function that returns the current "bottom limit"
537
+ * for content on a page (taking signature reservation into account).
538
+ */
539
+ declare function createBottomLimitForContent(doc: PDFDoc, ctx: PageContext): BottomLimitForContentFn;
540
+
541
+ declare function generateQrBuffer(value: string, size: number, version?: number, errorCorrectionLevel?: "L" | "M" | "Q" | "H"): Promise<Buffer>;
542
+ declare function mapBarcodeTypeToBcid(bcType?: BarcodeType | null): string;
543
+ declare function generateBarcodeBuffer(value: string, options?: {
544
+ bcType?: BarcodeType;
545
+ scale?: number;
546
+ barHeight?: number;
547
+ includetext?: boolean;
548
+ textalign?: "left" | "center" | "right";
549
+ }): Promise<Buffer>;
550
+ declare function materializeQrAndBarcodesInBlocks(blocks: Block[]): Promise<Block[]>;
551
+
552
+ declare function drawSignatureBlock(doc: PDFDoc, block: SignatureBlock, y: number, env: RenderEnv, renderBlockArray: (blocks: Block[], startY: number, env: RenderEnv) => number): void;
553
+ /** Dependencies for the signature reservation logic */
554
+ interface SignatureProcessorDeps {
555
+ doc: PDFDoc;
556
+ ctx: PageContext;
557
+ styles: Record<string, StyleDef>;
558
+ /** Called to finish current page and optionally start a new one */
559
+ finishPage: (addNewPage: boolean) => void;
560
+ /** Returns the base RenderEnv for content for the current page */
561
+ contentEnvFn: (doc: PDFDoc) => RenderEnv;
562
+ }
563
+ /**
564
+ * Creates the `processSignatureBlock` function used in the main renderer.
565
+ * It measures inner content, reserves space at the bottom of the page,
566
+ * and updates `ctx.signature*` fields.
567
+ */
568
+ declare function createProcessSignatureBlock({ doc, ctx, styles, finishPage, contentEnvFn }: SignatureProcessorDeps): (block: SignatureBlock) => void;
569
+
570
+ interface ResolvedSpacing {
571
+ top: number;
572
+ right: number;
573
+ bottom: number;
574
+ left: number;
575
+ }
576
+ declare const resolveSpacing: (value?: SpacingInput, overrides?: BoxSpacing) => ResolvedSpacing;
577
+ declare const resolveBlockPadding: (block: BaseBlock) => ResolvedSpacing;
578
+ declare const resolveHeaderPadding: (header: HeaderDef) => ResolvedSpacing;
579
+ declare const resolveFooterPadding: (footer: FooterDef) => ResolvedSpacing;
580
+ declare const hasPadding: (padding: ResolvedSpacing) => boolean;
581
+
582
+ interface StartPageLayoutDeps {
583
+ doc: PDFDoc;
584
+ def: CustomDocDefinition;
585
+ ctx: PageContext;
586
+ headerBandHeight: number;
587
+ renderBlockArray: RenderBlockArrayFn;
588
+ }
589
+ /**
590
+ * Returns the `startNewPageLayout` function used in the main renderer.
591
+ * It:
592
+ * - draws page background
593
+ * - draws header
594
+ * - (if mode is not "last"/"firstLast"/etc) draws watermark
595
+ * - resets `ctx` per new page
596
+ */
597
+ declare function createStartNewPageLayout({ doc, def, ctx, headerBandHeight, renderBlockArray }: StartPageLayoutDeps): () => void;
598
+
599
+ declare function mergeStyleDefs(styles: Record<string, StyleDef> | undefined, names: StyleRef): StyleDef;
600
+ declare function resolveTextBlock(styles: Record<string, StyleDef> | undefined, block: TextBlock): TextBlock;
601
+ declare function resolveTableCell(styles: Record<string, StyleDef> | undefined, cell: TableCell): TableCell;
602
+ declare const getFontNameForText: (tb: TextBlock) => FontName;
603
+ declare const drawStyledText: (doc: PDFDoc, tb: TextBlock, x: number, y: number, width: number) => void;
604
+
605
+ /**
606
+ * Returns true if the watermark mode requires knowledge of the last page.
607
+ */
608
+ declare function watermarkUsesLast(mode?: WatermarkMode): boolean;
609
+ /**
610
+ * Draw watermark text for a given page.
611
+ * This is extracted so the same logic can be reused from multiple places.
612
+ */
613
+ declare function drawWatermarkForPage(doc: PDFDoc, watermark: WatermarkDef, pageNumber: number, isLast: boolean): void;
614
+
615
+ export { type Alignment, BARCODE_TYPES, type BarcodeBlock, type BarcodeType, type BaseBlock, type Block, type BlockRendererDeps, type BottomLimitForContentFn, type BoxSpacing, type BuiltInFont, type ColumnsBlock, type CustomDocDefinition, type EnsureSpaceForFn, type FinishPageParams, type FontName, type FontRegistration, type FooterDef, type FooterInput, type HeaderDef, type ImageBlock, type InlineTextStyle, type KeyValueGridBlock, type KeyValueItem, type LineBlock, type ListBlock, type ListItem, type ListStyle, type MeasureBlockHeightFn, type Orientation, type PDFDoc, type PageBackgroundDef, type PageBreakBlock, type PageContext, type PageMargins, PdfEngineError, PdfEngineErrorCode, type PlacedCell, type QRErrorLevel, QR_ERROR_LEVEL, type QrCodeBlock, type RenderBlockArrayFn, type RenderEnv, type ResolvedSpacing, type ShapeBlock, type ShapePoint, type ShapeType, type SignatureBlock, type SpacingInput, type StyleDef, type StyleRef, type TableBlock, type TableBodyIterable, type TableBorderSide, type TableBorderStyle, type TableCell, type TableCellBorder, type TableColumnStyle, type TableRowStyle, type TextBlock, type WatermarkDef, type WatermarkMode, type Width, computeColumnPixelWidths, contentEnv, createBlockRenderer, createBottomLimitForContent, createEnsureSpaceFor, createInitialContext, createMeasureBlockHeight, createProcessSignatureBlock, createStartNewPageLayout, drawFooter, drawHeader, drawPageBackground, drawSignatureBlock, drawStyledText, drawWatermarkForPage, finishPage, generateBarcodeBuffer, generateQrBuffer, getBottomLimitForContent, getFontNameForText, hasPadding, images, isPdfEngineError, mapBarcodeTypeToBcid, materializeImagesInBlocks, materializeQrAndBarcodesInBlocks, mergeStyleDefs, normalizeImageSrc, renderCustomPdf, renderCustomPdfToBuffer, resolveBlockPadding, resolveFooterPadding, resolveHeaderPadding, resolveSpacing, resolveTableCell, resolveTextBlock, toPdfEngineError, watermarkUsesLast };