av6-pdf-engine 2.0.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;
@@ -190,6 +247,8 @@ interface TextBlock extends BaseBlock {
190
247
  color?: string;
191
248
  font?: FontName;
192
249
  style?: StyleRef;
250
+ marginTop?: number;
251
+ marginBottom?: number;
193
252
  marginLeft?: number;
194
253
  marginRight?: number;
195
254
  }
@@ -200,16 +259,7 @@ type Orientation = "horizontal" | "vertical";
200
259
  type Width = number | "*";
201
260
  type InlineTextStyle = Omit<TextBlock, "type" | "text" | "style">;
202
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";
203
- /**
204
- * FontName:
205
- * - autocomplete for built-ins
206
- * - but still allows any custom string when you registerFont
207
- */
208
262
  type FontName = BuiltInFont | (string & {});
209
- /**
210
- * Style definition similar to pdfmake's style object.
211
- * Can be applied to text blocks and table cells.
212
- */
213
263
  interface StyleDef {
214
264
  italic?: boolean;
215
265
  underline?: boolean;
@@ -223,14 +273,8 @@ interface StyleDef {
223
273
  fillColor?: string;
224
274
  font?: FontName;
225
275
  }
226
- /** Shared style reference field (single or multiple style names). */
227
276
  type StyleRef = string | string[] | undefined;
228
277
  interface PageMargins {
229
- /**
230
- * TOP is treated as header band height.
231
- * BOTTOM is treated as footer band height.
232
- * LEFT/RIGHT are used for content only.
233
- */
234
278
  top: number;
235
279
  right: number;
236
280
  bottom: number;
@@ -246,20 +290,24 @@ interface WatermarkDef {
246
290
  mode?: WatermarkMode;
247
291
  }
248
292
  interface PageBackgroundDef {
249
- /** Path or Buffer for the image to be used as full-page background */
250
293
  src?: string | Buffer;
251
- /** Optional global opacity for background (0–1). Default: 1 */
252
294
  opacity?: number;
253
295
  }
254
296
  interface FontRegistration {
255
297
  name: FontName;
256
298
  src: string | Buffer;
257
299
  }
300
+ interface BoxSpacing {
301
+ top?: number;
302
+ right?: number;
303
+ bottom?: number;
304
+ left?: number;
305
+ }
306
+ type SpacingInput = number | BoxSpacing;
258
307
  interface CustomDocDefinition {
259
- pageSize?: string;
308
+ pageSize?: string | [number, number];
260
309
  margins: PageMargins;
261
310
  pageOrientation?: "portrait" | "landscape";
262
- /** Optional font registrations (custom TTF/OTF) */
263
311
  fonts?: FontRegistration[];
264
312
  header?: HeaderDef;
265
313
  pageBackground?: PageBackgroundDef;
@@ -267,28 +315,41 @@ interface CustomDocDefinition {
267
315
  content: Block[];
268
316
  styles?: Record<string, StyleDef>;
269
317
  footer?: FooterInput;
270
- /** Fallback image used when an image src fails to load or render. */
271
318
  defaultImage?: string | Buffer;
272
319
  }
273
320
  interface HeaderDef {
274
- blocks: Block[];
321
+ blocks?: Block[];
322
+ backgroundBlocks?: Block[];
275
323
  visible?: boolean;
276
324
  marginTop?: number;
277
325
  marginBottom?: number;
278
326
  marginLeft?: number;
279
327
  marginRight?: number;
328
+ padding?: SpacingInput;
329
+ paddingTop?: number;
330
+ paddingRight?: number;
331
+ paddingBottom?: number;
332
+ paddingLeft?: number;
280
333
  backgroundColor?: string;
281
334
  backgroundImage?: string | Buffer;
335
+ backgroundOpacity?: number;
282
336
  }
283
337
  interface FooterDef {
284
338
  blocks: Block[];
339
+ backgroundBlocks?: Block[];
285
340
  visible?: boolean;
286
341
  marginTop?: number;
287
342
  marginBottom?: number;
288
343
  marginLeft?: number;
289
344
  marginRight?: number;
345
+ padding?: SpacingInput;
346
+ paddingTop?: number;
347
+ paddingRight?: number;
348
+ paddingBottom?: number;
349
+ paddingLeft?: number;
290
350
  backgroundColor?: string;
291
351
  backgroundImage?: string | Buffer;
352
+ backgroundOpacity?: number;
292
353
  }
293
354
  type FooterInput = FooterDef | Block | Block[] | ((pageNumber: number, pageSize: {
294
355
  width: number;
@@ -299,8 +360,14 @@ interface BaseBlock {
299
360
  backgroundColor?: string;
300
361
  backgroundImage?: string | Buffer;
301
362
  backgroundOpacity?: number;
363
+ backgroundBlocks?: Block[];
364
+ padding?: SpacingInput;
365
+ paddingTop?: number;
366
+ paddingRight?: number;
367
+ paddingBottom?: number;
368
+ paddingLeft?: number;
302
369
  }
303
- 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;
304
371
 
305
372
  type BarcodeType = "CODE128" | "EAN13" | "CODE39" | "ITF" | "CODE93";
306
373
  declare const BARCODE_TYPES: Record<BarcodeType, BarcodeType>;
@@ -333,11 +400,18 @@ interface PageContext {
333
400
  signaturePlaced: boolean;
334
401
  afterSignature: boolean;
335
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;
336
405
  }
337
406
  interface RenderEnv {
338
407
  marginLeft: number;
339
408
  innerWidth: number;
340
409
  allowPageBreak: boolean;
410
+ /**
411
+ * Internal flag.
412
+ * Used while rendering backgroundBlocks.
413
+ */
414
+ isBackgroundLayer?: boolean;
341
415
  }
342
416
 
343
417
  declare enum PdfEngineErrorCode {
@@ -390,4 +464,152 @@ declare const images: {
390
464
  declare function renderCustomPdf(def: CustomDocDefinition, outputPath: string): Promise<void>;
391
465
  declare function renderCustomPdfToBuffer(def: CustomDocDefinition): Promise<Buffer>;
392
466
 
393
- 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 TableBorderSide, type TableBorderStyle, type TableCell, type TableCellBorder, type TableColumnStyle, type TableRowStyle, type TextBlock, type WatermarkDef, type WatermarkMode, type Width, images, 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 };