@oxog/printkit 1.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.
@@ -0,0 +1,408 @@
1
+ type PageSizeName = 'A3' | 'A4' | 'A5' | 'Letter' | 'Legal' | 'Tabloid';
2
+ interface CustomSize {
3
+ width: number | string;
4
+ height: number | string;
5
+ }
6
+ type PageSize = PageSizeName | CustomSize;
7
+ type Orientation = 'portrait' | 'landscape';
8
+ interface MarginOptions {
9
+ top?: number | string;
10
+ right?: number | string;
11
+ bottom?: number | string;
12
+ left?: number | string;
13
+ }
14
+ interface PageInfo {
15
+ pageNumber: number;
16
+ totalPages: number;
17
+ title: string;
18
+ date: Date;
19
+ }
20
+ type HeaderFooterFn = (info: PageInfo) => string;
21
+ interface PrintOptions {
22
+ title?: string;
23
+ pageSize?: PageSize;
24
+ orientation?: Orientation;
25
+ margin?: number | MarginOptions;
26
+ header?: string | HeaderFooterFn;
27
+ footer?: string | HeaderFooterFn;
28
+ styles?: string | string[];
29
+ styleSheets?: string[];
30
+ copyStyles?: boolean;
31
+ pageBreakBefore?: string;
32
+ pageBreakAfter?: string;
33
+ avoidBreakInside?: string;
34
+ removeAfterPrint?: boolean;
35
+ printBackground?: boolean;
36
+ onBeforePrint?: () => void | Promise<void>;
37
+ onAfterPrint?: () => void;
38
+ onError?: (error: PrintError$1) => void;
39
+ }
40
+ type PrintTarget = HTMLElement | string | HTMLElement[] | string[];
41
+ interface Printer {
42
+ print(target: PrintTarget, options?: PrintOptions): Promise<void>;
43
+ printHTML(html: string, options?: PrintOptions): Promise<void>;
44
+ printURL(url: string, options?: PrintOptions): Promise<void>;
45
+ printImage(src: string | HTMLImageElement, options?: PrintOptions): Promise<void>;
46
+ printImages(sources: (string | HTMLImageElement)[], options?: PrintOptions): Promise<void>;
47
+ printJSON(data: unknown, options?: PrintJSONOptions): Promise<void>;
48
+ printTable(data: TableData, options?: PrintTableOptions): Promise<void>;
49
+ setDefaultOptions(options: PrintOptions): void;
50
+ getDefaultOptions(): PrintOptions;
51
+ destroy(): void;
52
+ }
53
+ interface PreviewOptions extends PrintOptions {
54
+ modal?: ModalOptions;
55
+ toolbar?: ToolbarOptions;
56
+ onPrint?: () => void;
57
+ onDownload?: () => void;
58
+ onClose?: () => void;
59
+ }
60
+ interface ModalOptions {
61
+ width?: string | number;
62
+ height?: string | number;
63
+ showToolbar?: boolean;
64
+ showPageNumbers?: boolean;
65
+ zoom?: number;
66
+ className?: string;
67
+ }
68
+ interface ToolbarOptions {
69
+ print?: boolean;
70
+ download?: boolean;
71
+ zoom?: boolean;
72
+ navigation?: boolean;
73
+ close?: boolean;
74
+ }
75
+ interface PrintPreview {
76
+ open(): void;
77
+ close(): void;
78
+ print(): Promise<void>;
79
+ download(filename?: string): Promise<void>;
80
+ setZoom(level: number): void;
81
+ getZoom(): number;
82
+ zoomIn(): void;
83
+ zoomOut(): void;
84
+ resetZoom(): void;
85
+ nextPage(): void;
86
+ prevPage(): void;
87
+ goToPage(page: number): void;
88
+ getCurrentPage(): number;
89
+ getTotalPages(): number;
90
+ isOpen(): boolean;
91
+ destroy(): void;
92
+ }
93
+ interface PDFOptions extends PrintOptions {
94
+ quality?: number;
95
+ scale?: number;
96
+ metadata?: PDFMetadata;
97
+ onProgress?: (progress: number) => void;
98
+ }
99
+ interface PDFMetadata {
100
+ title?: string;
101
+ author?: string;
102
+ subject?: string;
103
+ keywords?: string[];
104
+ creator?: string;
105
+ creationDate?: Date;
106
+ }
107
+ interface PDFExporter {
108
+ toBlob(target: PrintTarget, options?: PDFOptions): Promise<Blob>;
109
+ toBase64(target: PrintTarget, options?: PDFOptions): Promise<string>;
110
+ toDataURL(target: PrintTarget, options?: PDFOptions): Promise<string>;
111
+ download(target: PrintTarget, filename: string, options?: PDFOptions): Promise<void>;
112
+ downloadHTML(html: string, filename: string, options?: PDFOptions): Promise<void>;
113
+ }
114
+ type JobPriority = 'low' | 'normal' | 'high';
115
+ type JobStatus = 'pending' | 'printing' | 'completed' | 'failed';
116
+ interface PrintJob {
117
+ id: string;
118
+ content: PrintTarget;
119
+ options?: PrintOptions;
120
+ priority?: JobPriority;
121
+ status: JobStatus;
122
+ error?: Error;
123
+ createdAt: Date;
124
+ completedAt?: Date;
125
+ }
126
+ type NewPrintJob = Omit<PrintJob, 'id' | 'status' | 'createdAt'>;
127
+ type QueueEvent = 'jobStart' | 'jobComplete' | 'jobError' | 'queueComplete' | 'queuePause' | 'queueResume';
128
+ type QueueEventHandler<E extends QueueEvent> = E extends 'jobError' ? (job: PrintJob, error: Error) => void : E extends 'jobStart' | 'jobComplete' ? (job: PrintJob) => void : () => void;
129
+ interface PrintQueue {
130
+ add(job: NewPrintJob): string;
131
+ remove(jobId: string): boolean;
132
+ clear(): void;
133
+ getJob(jobId: string): PrintJob | undefined;
134
+ getJobs(): PrintJob[];
135
+ getPendingJobs(): PrintJob[];
136
+ printNext(): Promise<void>;
137
+ printAll(): Promise<void>;
138
+ pause(): void;
139
+ resume(): void;
140
+ on<E extends QueueEvent>(event: E, handler: QueueEventHandler<E>): () => void;
141
+ off<E extends QueueEvent>(event: E, handler: QueueEventHandler<E>): void;
142
+ destroy(): void;
143
+ }
144
+ interface TableData {
145
+ headers: string[];
146
+ rows: (string | number)[][];
147
+ caption?: string;
148
+ }
149
+ interface PrintTableOptions extends PrintOptions {
150
+ tableStyles?: string;
151
+ headerStyles?: string;
152
+ cellStyles?: string;
153
+ stripedRows?: boolean;
154
+ bordered?: boolean;
155
+ }
156
+ interface PrintJSONOptions extends PrintOptions {
157
+ indent?: number;
158
+ syntaxHighlight?: boolean;
159
+ collapsible?: boolean;
160
+ }
161
+ interface FormatOptions {
162
+ removeScripts?: boolean;
163
+ removeInputs?: boolean;
164
+ inlineStyles?: boolean;
165
+ removeHidden?: boolean;
166
+ }
167
+ interface PageOptions {
168
+ pageSize?: PageSize;
169
+ margin?: number | MarginOptions;
170
+ orientation?: Orientation;
171
+ }
172
+ type PrintErrorCode = 'PRINT_NOT_SUPPORTED' | 'ELEMENT_NOT_FOUND' | 'IFRAME_LOAD_FAILED' | 'PDF_EXPORT_FAILED' | 'STYLE_LOAD_FAILED' | 'CANCELLED' | 'UNKNOWN';
173
+ interface PrintError$1 extends Error {
174
+ code: PrintErrorCode;
175
+ target?: PrintTarget | undefined;
176
+ }
177
+ interface Dimensions {
178
+ width: string;
179
+ height: string;
180
+ }
181
+ interface PrintDocument {
182
+ content: string;
183
+ styles: string;
184
+ header?: string;
185
+ footer?: string;
186
+ pageSize: Dimensions;
187
+ margins: Required<MarginOptions>;
188
+ title: string;
189
+ }
190
+
191
+ /**
192
+ * Creates a new Printer instance
193
+ */
194
+ declare function createPrinter(options?: PrintOptions): Printer;
195
+ /**
196
+ * Gets or creates the default printer instance
197
+ */
198
+ declare function getDefaultPrinter(): Printer;
199
+
200
+ /**
201
+ * Creates a new PrintPreview instance
202
+ */
203
+ declare function createPrintPreview(target: PrintTarget, options?: PreviewOptions): PrintPreview;
204
+
205
+ /**
206
+ * Creates a new PDFExporter instance
207
+ */
208
+ declare function createPDFExporter(): PDFExporter;
209
+
210
+ /**
211
+ * Creates a new PrintQueue instance
212
+ */
213
+ declare function createPrintQueue(options?: PrintOptions): PrintQueue;
214
+
215
+ /**
216
+ * Checks if the print API is supported in the current environment
217
+ */
218
+ declare function isPrintSupported(): boolean;
219
+ /**
220
+ * Checks if PDF export is supported (requires Canvas API)
221
+ */
222
+ declare function isPDFSupported(): boolean;
223
+ /**
224
+ * Checks if the environment is a browser
225
+ */
226
+ declare function isBrowser(): boolean;
227
+
228
+ /**
229
+ * Resolves a single element from a target (selector string or HTMLElement)
230
+ */
231
+ declare function resolveElement(target: string | HTMLElement): HTMLElement | null;
232
+ /**
233
+ * Resolves all elements from a PrintTarget
234
+ */
235
+ declare function resolveElements(target: PrintTarget): HTMLElement[];
236
+ /**
237
+ * Deep clones an element with special handling for canvas and form elements
238
+ */
239
+ declare function cloneElement(element: HTMLElement): HTMLElement;
240
+ /**
241
+ * Waits for all images in an element to load
242
+ */
243
+ declare function waitForImages(element: HTMLElement): Promise<void>;
244
+ /**
245
+ * Sanitizes HTML to prevent XSS
246
+ */
247
+ declare function sanitizeHTML(html: string): string;
248
+
249
+ /**
250
+ * Injects print styles into the document head
251
+ * Returns the style element ID for later removal
252
+ */
253
+ declare function injectPrintStyles(styles: string): string;
254
+ /**
255
+ * Removes previously injected print styles
256
+ */
257
+ declare function removePrintStyles(styleId: string): void;
258
+ /**
259
+ * Removes all PrintKit styles from the document
260
+ */
261
+ declare function removeAllPrintStyles(): void;
262
+ /**
263
+ * Builds a style tag HTML string
264
+ */
265
+ declare function buildStyleTag(styles: string | string[]): string;
266
+ /**
267
+ * Combines multiple style sources into a single CSS string
268
+ */
269
+ declare function combineStyles(sources: Array<string | string[] | undefined>): string;
270
+
271
+ /**
272
+ * Copies all styles (linked and inline) from the current page
273
+ */
274
+ declare function copyPageStyles(): string[];
275
+ /**
276
+ * Gets computed styles for an element as inline CSS
277
+ */
278
+ declare function getComputedStylesCSS(element: HTMLElement): string;
279
+
280
+ /**
281
+ * CSS class to hide elements when printing
282
+ */
283
+ declare const PRINT_HIDE_CLASS = "print-hide";
284
+ /**
285
+ * CSS class to show elements only when printing
286
+ */
287
+ declare const PRINT_ONLY_CLASS = "print-only";
288
+ /**
289
+ * Default print styles
290
+ */
291
+ declare function getDefaultPrintStyles(): string;
292
+ /**
293
+ * Builds base document styles
294
+ */
295
+ declare function getBaseDocumentStyles(): string;
296
+ /**
297
+ * Combines all default styles
298
+ */
299
+ declare function getAllDefaultStyles(): string;
300
+
301
+ /**
302
+ * Standard page sizes in millimeters
303
+ */
304
+ declare const PAGE_SIZES: Record<PageSizeName, Dimensions>;
305
+ /**
306
+ * Gets page dimensions for a given size and orientation
307
+ */
308
+ declare function getPageDimensions(size: PageSize, orientation?: Orientation): Dimensions;
309
+ /**
310
+ * Gets pixel dimensions for a page size (for preview)
311
+ * Uses 96 DPI as standard screen resolution
312
+ */
313
+ declare function getPagePixelDimensions(size: PageSize, orientation?: Orientation, dpi?: number): {
314
+ width: number;
315
+ height: number;
316
+ };
317
+
318
+ /**
319
+ * Normalized margin options with string values only
320
+ */
321
+ interface NormalizedMargins {
322
+ top: string;
323
+ right: string;
324
+ bottom: string;
325
+ left: string;
326
+ }
327
+ /**
328
+ * Normalizes margin options to a complete MarginOptions object
329
+ */
330
+ declare function normalizeMargins(margin?: number | MarginOptions): NormalizedMargins;
331
+ /**
332
+ * Gets margin values as pixels (for preview)
333
+ */
334
+ declare function getMarginsInPixels(margin?: number | MarginOptions, dpi?: number): {
335
+ top: number;
336
+ right: number;
337
+ bottom: number;
338
+ left: number;
339
+ };
340
+
341
+ /**
342
+ * Creates a PageInfo object
343
+ */
344
+ declare function createPageInfo(pageNumber: number, totalPages: number, title: string, date?: Date): PageInfo;
345
+ /**
346
+ * Replaces placeholders in a template string
347
+ */
348
+ declare function replacePlaceholders(template: string, info: PageInfo): string;
349
+ /**
350
+ * Renders a header or footer template
351
+ */
352
+ declare function renderHeaderFooter(template: string | HeaderFooterFn | undefined, info: PageInfo): string;
353
+
354
+ /**
355
+ * CSS class for page break before
356
+ */
357
+ declare const BREAK_BEFORE_CLASS = "print-break-before";
358
+ /**
359
+ * CSS class for page break after
360
+ */
361
+ declare const BREAK_AFTER_CLASS = "print-break-after";
362
+ /**
363
+ * CSS class to avoid break inside
364
+ */
365
+ declare const AVOID_BREAK_CLASS = "print-avoid-break";
366
+ /**
367
+ * Creates a page break element
368
+ */
369
+ declare function pageBreak(): HTMLDivElement;
370
+ /**
371
+ * Marks an element to avoid page breaks inside
372
+ */
373
+ declare function avoidBreak(element: HTMLElement): void;
374
+ /**
375
+ * Marks an element to break before
376
+ */
377
+ declare function breakBefore(element: HTMLElement): void;
378
+ /**
379
+ * Marks an element to break after
380
+ */
381
+ declare function breakAfter(element: HTMLElement): void;
382
+ /**
383
+ * Calculates estimated page count based on content height
384
+ */
385
+ declare function calculatePages(contentHeight: number, pageHeight: number, marginTop?: number, marginBottom?: number): number;
386
+
387
+ /**
388
+ * Custom error class for print operations
389
+ */
390
+ declare class PrintError extends Error implements PrintError$1 {
391
+ readonly code: PrintErrorCode;
392
+ readonly target: PrintTarget | undefined;
393
+ constructor(code: PrintErrorCode, message: string, target?: PrintTarget | undefined);
394
+ }
395
+ /**
396
+ * Creates a PrintError with the appropriate message
397
+ */
398
+ declare function createPrintError(code: PrintErrorCode, details?: string, target?: PrintTarget): PrintError;
399
+ /**
400
+ * Type guard to check if an error is a PrintError
401
+ */
402
+ declare function isPrintError(error: unknown): error is PrintError;
403
+ /**
404
+ * Wraps an unknown error as a PrintError
405
+ */
406
+ declare function wrapError(error: unknown, target?: PrintTarget): PrintError;
407
+
408
+ export { AVOID_BREAK_CLASS, BREAK_AFTER_CLASS, BREAK_BEFORE_CLASS, type CustomSize, type Dimensions, type FormatOptions, type HeaderFooterFn, type JobPriority, type JobStatus, type MarginOptions, type ModalOptions, type NewPrintJob, type Orientation, PAGE_SIZES, type PDFExporter, type PDFMetadata, type PDFOptions, PRINT_HIDE_CLASS, PRINT_ONLY_CLASS, type PageInfo, type PageOptions, type PageSize, type PageSizeName, type PreviewOptions, type PrintDocument, PrintError, type PrintErrorCode, type PrintError$1 as PrintErrorType, type PrintJSONOptions, type PrintJob, type PrintOptions, type PrintPreview, type PrintQueue, type PrintTableOptions, type PrintTarget, type Printer, type QueueEvent, type QueueEventHandler, type TableData, type ToolbarOptions, avoidBreak, breakAfter, breakBefore, buildStyleTag, calculatePages, cloneElement, combineStyles, copyPageStyles, createPDFExporter, createPageInfo, createPrintError, createPrintPreview, createPrintQueue, createPrinter, getAllDefaultStyles, getBaseDocumentStyles, getComputedStylesCSS, getDefaultPrintStyles, getDefaultPrinter, getMarginsInPixels, getPageDimensions, getPagePixelDimensions, injectPrintStyles, isBrowser, isPDFSupported, isPrintError, isPrintSupported, normalizeMargins, pageBreak, removeAllPrintStyles, removePrintStyles, renderHeaderFooter, replacePlaceholders, resolveElement, resolveElements, sanitizeHTML, waitForImages, wrapError };