UNPKG

invoice-craft

Version:

Customizable, browser-first invoice PDF generator library with modern TypeScript API

145 lines (144 loc) 3.92 kB
import type { LabelsMap } from "../utils/localization"; export type KeyValue = { [key: string]: any; }; export interface Party { name: string; address?: string; email?: string; phone?: string; logoUrl?: string; brandColor?: string; [key: string]: any; } export interface InvoiceItem extends KeyValue { description: string; quantity: number; unitPrice: number; taxRate?: number; } export interface InvoiceData extends KeyValue { from: Party; to: Party; items: InvoiceItem[]; invoiceNumber: string; invoiceDate: string; dueDate?: string; currency?: string; discount?: number; notes?: string; terms?: string; [key: string]: any; } export interface ExtraSection { title: string; content: string; } export interface ExportOptions { format?: 'pdf' | 'html' | 'json' | 'csv'; compression?: boolean; quality?: 'low' | 'medium' | 'high'; includeStyles?: boolean; brandColor?: string; logoUrl?: string; layoutStyle?: 'default' | 'modern' | 'minimal' | 'creative'; } export interface ValidationError { field: string; message: string; code: string; severity: 'error' | 'warning'; } export interface ValidationResult { isValid: boolean; errors: ValidationError[]; warnings: ValidationError[]; } export interface TemplateSection { content: string | ((data: any) => string); styles?: TemplateStyles; visible?: boolean | ((data: any) => boolean); } export interface TemplateStyles { fontSize?: number; fontFamily?: string; color?: string; backgroundColor?: string; padding?: number | [number, number, number, number]; margin?: number | [number, number, number, number]; alignment?: 'left' | 'center' | 'right' | 'justify'; bold?: boolean; italic?: boolean; } export interface CustomTemplate { id: string; name: string; description?: string; header: TemplateSection; body: TemplateSection; footer: TemplateSection; styles: TemplateStyles; supportedFeatures?: { logo?: boolean; brandColor?: boolean; rtl?: boolean; extraSections?: boolean; }; } export interface InvoicePlugin { name: string; version?: string; beforeRender?: (invoice: InvoiceData) => InvoiceData | Promise<InvoiceData>; afterRender?: (pdf: any) => any | Promise<any>; beforeValidation?: (invoice: InvoiceData) => InvoiceData | Promise<InvoiceData>; afterValidation?: (result: ValidationResult) => ValidationResult | Promise<ValidationResult>; } export interface BatchOptions { concurrency?: number; onProgress?: (completed: number, total: number) => void; onError?: (error: Error, invoice: InvoiceData, index: number) => void; continueOnError?: boolean; } export interface BatchResult { success: Array<{ blob: Blob; filename: string; index: number; }>; errors: Array<{ error: Error; invoice: InvoiceData; index: number; }>; summary: { total: number; successful: number; failed: number; }; } export interface PreviewOptions { includeStyles?: boolean; responsive?: boolean; theme?: 'light' | 'dark'; showGrid?: boolean; } export interface GeneratePdfOptions { layoutStyle?: 'default' | 'modern' | 'minimal' | 'creative'; logoUrl?: string; brandColor?: string; labels?: LabelsMap; rtl?: boolean; notes?: string; filenameTemplate?: (args: { invoice: InvoiceData; }) => string; validate?: (invoice: InvoiceData) => void | ValidationResult; extraSections?: ExtraSection[]; hooks?: { beforeRender?: (invoice: InvoiceData) => void; afterRender?: (pdfBlob: Blob) => void; }; plugins?: InvoicePlugin[]; customTemplate?: CustomTemplate; exportOptions?: ExportOptions; }