@plevands/epson-thermal-printer 0.1.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.
@@ -0,0 +1,140 @@
1
+ import { epson } from './epson-sdk';
2
+ import { loadEpsonSDK, isEpsonSDKLoaded, getLoaderState, initializeEpsonSDK } from './epson-sdk-loader';
3
+ import { EpsonPrinterConfig, PrintResult, PrintOptions } from '../types';
4
+ export type { EpsonPrinterConfig, PrintResult, PrintOptions } from '../types';
5
+ export { loadEpsonSDK, isEpsonSDKLoaded, initializeEpsonSDK, getLoaderState, };
6
+ /**
7
+ * Check SDK status - useful for debugging
8
+ */
9
+ export declare function checkEpsonSDKStatus(): {
10
+ loaded: boolean;
11
+ loading: boolean;
12
+ error: Error | null;
13
+ classes: string[];
14
+ };
15
+ /**
16
+ * ePOS Print Service using official SDK
17
+ */
18
+ export declare class EposPrintService {
19
+ private config;
20
+ private printOptions;
21
+ private initPromise;
22
+ constructor(config: EpsonPrinterConfig, options?: PrintOptions);
23
+ /**
24
+ * Ensure SDK is loaded before any print operation (lazy loading)
25
+ */
26
+ private ensureSDKLoaded;
27
+ /**
28
+ * Get the printer URL for ePOS Print
29
+ */
30
+ private getPrinterUrl;
31
+ /**
32
+ * Print a canvas element using ePOSBuilder
33
+ *
34
+ * IMPORTANT: We don't use CanvasPrint.print() directly due to an SDK bug.
35
+ * The SDK's prototypal inheritance causes CanvasPrint.print() to call
36
+ * this.send(printjobid) which internally creates a new empty ePOSBuilder,
37
+ * ignoring all the commands built in 'this'.
38
+ *
39
+ * Instead, we use ePOSBuilder to construct the print commands manually,
40
+ * get the XML, and send it via ePOSPrint.send(xml).
41
+ */
42
+ printCanvas(canvas: HTMLCanvasElement): Promise<PrintResult>;
43
+ /**
44
+ * Get alignment value for SDK (instance property values are strings)
45
+ */
46
+ private getAlignValue;
47
+ /**
48
+ * Print using ePOSPrint with builder pattern
49
+ */
50
+ printWithBuilder(buildFn: (builder: epson.ePOSBuilder) => void): Promise<PrintResult>;
51
+ /**
52
+ * Print multiple canvases (pages) with optional header/footer
53
+ */
54
+ printPages(canvases: HTMLCanvasElement[], options?: {
55
+ header?: string;
56
+ footer?: string;
57
+ pageSeparator?: boolean;
58
+ }): Promise<PrintResult>;
59
+ /**
60
+ * Check printer connection without printing anything.
61
+ * Sends a status request to verify the printer is online and responding.
62
+ *
63
+ * @returns Promise with connection result
64
+ *
65
+ * @example
66
+ * ```typescript
67
+ * const service = new EposPrintService(config);
68
+ * const result = await service.checkConnection();
69
+ *
70
+ * if (result.success) {
71
+ * console.log('Printer is online!');
72
+ * } else {
73
+ * console.log('Printer offline:', result.message);
74
+ * }
75
+ * ```
76
+ */
77
+ checkConnection(): Promise<PrintResult>;
78
+ /**
79
+ * Get human-readable message for printer status code
80
+ */
81
+ private getStatusMessage;
82
+ /**
83
+ * Test printer connection by printing a small test receipt.
84
+ * Use `checkConnection()` if you want to test without printing.
85
+ */
86
+ testConnection(): Promise<PrintResult>;
87
+ /**
88
+ * Print a test page
89
+ */
90
+ printTestPage(): Promise<PrintResult>;
91
+ }
92
+ /**
93
+ * PDF to Canvas conversion utilities
94
+ */
95
+ export declare function pdfToCanvases(pdfFile: File, options?: {
96
+ scale?: number;
97
+ maxWidth?: number;
98
+ }): Promise<{
99
+ canvas: HTMLCanvasElement;
100
+ width: number;
101
+ height: number;
102
+ }[]>;
103
+ /**
104
+ * Convert image file to canvas
105
+ */
106
+ export declare function imageToCanvas(file: File, maxWidth?: number): Promise<{
107
+ canvas: HTMLCanvasElement;
108
+ width: number;
109
+ height: number;
110
+ }>;
111
+ export declare class EposPrintBuilder {
112
+ private builder;
113
+ constructor();
114
+ reset(): this;
115
+ addText(text: string): this;
116
+ addTextLine(text: string): this;
117
+ addFeedLine(lines?: number): this;
118
+ addCut(type?: 'no_feed' | 'feed' | 'reserve'): this;
119
+ addTextAlign(align: 'left' | 'center' | 'right'): this;
120
+ addTextStyle(reverse?: boolean, underline?: boolean, bold?: boolean, color?: 'color_1' | 'color_2' | 'color_3' | 'color_4'): this;
121
+ addTextSize(width?: number, height?: number): this;
122
+ addImage(canvas: HTMLCanvasElement, mode?: 'mono' | 'gray16'): this;
123
+ addBarcode(data: string, type?: string, hri?: 'none' | 'above' | 'below' | 'both', width?: number, height?: number): this;
124
+ addQRCode(data: string, type?: 'model_1' | 'model_2' | 'micro', level?: 'level_l' | 'level_m' | 'level_q' | 'level_h', width?: number): this;
125
+ build(): string;
126
+ getBuilder(): epson.ePOSBuilder;
127
+ }
128
+ export declare function pdfToImages(pdfFile: File, options?: {
129
+ scale?: number;
130
+ maxWidth?: number;
131
+ }): Promise<{
132
+ images: string[];
133
+ width: number;
134
+ height: number;
135
+ }[]>;
136
+ export declare function imageToBase64(file: File): Promise<{
137
+ base64: string;
138
+ width: number;
139
+ height: number;
140
+ }>;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Dynamic loader for Epson ePOS SDK
3
+ * Implements singleton pattern to avoid multiple loads
4
+ */
5
+ interface LoaderState {
6
+ loading: boolean;
7
+ loaded: boolean;
8
+ error: Error | null;
9
+ promise: Promise<boolean> | null;
10
+ }
11
+ /**
12
+ * Check if SDK is already available in window
13
+ */
14
+ export declare function isEpsonSDKLoaded(): boolean;
15
+ /**
16
+ * Load Epson SDK dynamically (singleton pattern)
17
+ * Returns true if loaded successfully, false otherwise
18
+ */
19
+ export declare function loadEpsonSDK(options?: {
20
+ timeout?: number;
21
+ }): Promise<boolean>;
22
+ /**
23
+ * Get current loader state
24
+ */
25
+ export declare function getLoaderState(): Readonly<LoaderState>;
26
+ /**
27
+ * Reset loader state (useful for testing)
28
+ */
29
+ export declare function resetLoaderState(): void;
30
+ /**
31
+ * Get Epson SDK (throws if not loaded)
32
+ */
33
+ export declare function getEpsonSDK(): typeof window.epson;
34
+ /**
35
+ * Initialize SDK (optional - for eager loading)
36
+ */
37
+ export declare function initializeEpsonSDK(options?: {
38
+ sdkPath?: string;
39
+ timeout?: number;
40
+ }): Promise<{
41
+ success: boolean;
42
+ error?: string;
43
+ }>;
44
+ export {};
@@ -0,0 +1,29 @@
1
+ import { LoggerConfig } from '../types';
2
+ /**
3
+ * Configure the logger
4
+ * @param newConfig - Logger configuration options
5
+ */
6
+ export declare function configureLogger(newConfig: Partial<LoggerConfig>): void;
7
+ /**
8
+ * Get current logger configuration
9
+ */
10
+ export declare function getLoggerConfig(): Readonly<LoggerConfig>;
11
+ /**
12
+ * Log debug message (only when enabled)
13
+ */
14
+ export declare function debug(message: string, ...args: unknown[]): void;
15
+ /**
16
+ * Log warning message (only when enabled)
17
+ */
18
+ export declare function warn(message: string, ...args: unknown[]): void;
19
+ /**
20
+ * Log error message (always shown)
21
+ */
22
+ export declare function error(message: string, ...args: unknown[]): void;
23
+ export declare const logger: {
24
+ debug: typeof debug;
25
+ warn: typeof warn;
26
+ error: typeof error;
27
+ configure: typeof configureLogger;
28
+ getConfig: typeof getLoggerConfig;
29
+ };
@@ -0,0 +1,101 @@
1
+ import { PDFPageProxy } from 'pdfjs-dist';
2
+ /** Default CDN URL for PDF.js worker (version appended after loading) */
3
+ export declare const PDFJS_CDN_WORKER_BASE = "https://unpkg.com/pdfjs-dist@";
4
+ /** Full CDN URL (populated after loading pdfjs-dist) */
5
+ export declare let PDFJS_CDN_WORKER_URL: string;
6
+ /**
7
+ * Error thrown when pdfjs-dist is not installed
8
+ */
9
+ export declare class PdfJsNotInstalledError extends Error {
10
+ constructor();
11
+ }
12
+ /**
13
+ * Check if pdfjs-dist is available (installed).
14
+ * Useful to check before calling PDF functions.
15
+ *
16
+ * @returns Promise that resolves to true if pdfjs-dist is available
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * if (await isPdfJsAvailable()) {
21
+ * const pages = await processPdfFile(file);
22
+ * } else {
23
+ * console.log('PDF processing not available. Install pdfjs-dist to enable.');
24
+ * }
25
+ * ```
26
+ */
27
+ export declare function isPdfJsAvailable(): Promise<boolean>;
28
+ /**
29
+ * Configure PDF.js worker source.
30
+ * Call this BEFORE using any PDF processing functions.
31
+ * If not called, the worker will be loaded from CDN automatically.
32
+ *
33
+ * @param workerSrc - URL to the PDF.js worker.
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * // Use your own worker instead of CDN
38
+ * configurePdfWorker('/assets/pdf.worker.min.mjs');
39
+ * ```
40
+ */
41
+ export declare function configurePdfWorker(workerSrc: string): void;
42
+ /**
43
+ * Check if PDF.js worker is configured
44
+ */
45
+ export declare function isPdfWorkerConfigured(): boolean;
46
+ export interface PdfProcessingConfig {
47
+ /** Enable PDF processing (trimming, scaling) */
48
+ enabled: boolean;
49
+ /** Margin settings for trimming white space */
50
+ trimMargins?: {
51
+ top?: number;
52
+ bottom?: number;
53
+ left?: number;
54
+ right?: number;
55
+ };
56
+ /** Target width for printer paper (576 for 80mm, 384 for 58mm) */
57
+ targetWidth?: number;
58
+ /** Rendering scale for quality (higher = better quality) */
59
+ scale?: number;
60
+ /** Threshold for monochrome conversion (0-255, lower = darker) */
61
+ monochromeThreshold?: number;
62
+ }
63
+ export interface ProcessedPage {
64
+ base64: string;
65
+ width: number;
66
+ height: number;
67
+ rasterBase64: string;
68
+ canvas: HTMLCanvasElement;
69
+ }
70
+ /**
71
+ * Default configuration for PDF processing
72
+ */
73
+ export declare const DEFAULT_PDF_CONFIG: Required<PdfProcessingConfig>;
74
+ /**
75
+ * Process a PDF page to canvas with optional trimming and scaling
76
+ */
77
+ export declare function processPdfPage(page: PDFPageProxy, config?: PdfProcessingConfig): Promise<ProcessedPage>;
78
+ /**
79
+ * Process all pages of a PDF file.
80
+ *
81
+ * @remarks
82
+ * Requires `pdfjs-dist` to be installed:
83
+ * ```bash
84
+ * npm install pdfjs-dist
85
+ * ```
86
+ *
87
+ * @param file - PDF file to process
88
+ * @param config - Processing configuration
89
+ * @returns Promise with array of processed pages
90
+ * @throws {PdfJsNotInstalledError} If pdfjs-dist is not installed
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * const pages = await processPdfFile(file, {
95
+ * enabled: true,
96
+ * targetWidth: 576, // 80mm paper
97
+ * trimMargins: { top: 10, bottom: 10, left: 5, right: 5 },
98
+ * });
99
+ * ```
100
+ */
101
+ export declare function processPdfFile(file: File, config?: PdfProcessingConfig): Promise<ProcessedPage[]>;
@@ -0,0 +1,86 @@
1
+ import { ProcessedPage } from '../lib/pdf-processor';
2
+ /**
3
+ * Type definitions for @plevands/epson-thermal-printer library
4
+ */
5
+ export type { PdfProcessingConfig, ProcessedPage } from '../lib/pdf-processor';
6
+ export interface EpsonPrinterConfig {
7
+ printerIP: string;
8
+ printerPort?: number;
9
+ deviceId?: string;
10
+ timeout?: number;
11
+ }
12
+ export interface PrintResult {
13
+ success: boolean;
14
+ code?: string;
15
+ status?: number;
16
+ message?: string;
17
+ printjobid?: string;
18
+ }
19
+ export interface PrintOptions {
20
+ halftone?: 0 | 1 | 2;
21
+ brightness?: number;
22
+ mode?: 'mono' | 'gray16';
23
+ cut?: boolean;
24
+ align?: 'left' | 'center' | 'right';
25
+ }
26
+ export interface LoaderState {
27
+ loading: boolean;
28
+ loaded: boolean;
29
+ error: Error | null;
30
+ }
31
+ export interface SDKLoadOptions {
32
+ timeout?: number;
33
+ }
34
+ export interface InitializeSDKResult {
35
+ success: boolean;
36
+ error?: string;
37
+ }
38
+ export interface PrinterStatus {
39
+ loaded: boolean;
40
+ loading: boolean;
41
+ error: Error | null;
42
+ classes: string[];
43
+ }
44
+ export interface UseEpsonPrinterReturn {
45
+ /** Print a single canvas to the thermal printer */
46
+ print: (canvas: HTMLCanvasElement) => Promise<PrintResult>;
47
+ /** Print multiple canvases (pages) with optional page selection */
48
+ printPages: (canvases: HTMLCanvasElement[], options?: {
49
+ pageSelection?: 'all' | number[];
50
+ headerText?: string;
51
+ footerText?: string;
52
+ }) => Promise<PrintResult>;
53
+ /** Check printer connection without printing anything */
54
+ checkConnection: () => Promise<PrintResult>;
55
+ /** Test printer connection by printing a small test receipt */
56
+ testConnection: () => Promise<PrintResult>;
57
+ /** Whether a print operation is in progress */
58
+ isLoading: boolean;
59
+ /** Error message from the last operation, if any */
60
+ error: string | null;
61
+ /** Current SDK loading status */
62
+ sdkStatus: PrinterStatus;
63
+ }
64
+ export interface UsePrinterConfigReturn {
65
+ config: EpsonPrinterConfig;
66
+ updateConfig: (newConfig: Partial<EpsonPrinterConfig>) => void;
67
+ resetConfig: () => void;
68
+ isConfigured: boolean;
69
+ }
70
+ export interface UsePdfProcessorReturn {
71
+ processFile: (file: File) => Promise<ProcessedPage[]>;
72
+ isProcessing: boolean;
73
+ error: string | null;
74
+ }
75
+ export type LogLevel = 'debug' | 'warn' | 'error';
76
+ export interface LogEntry {
77
+ level: LogLevel;
78
+ message: string;
79
+ args?: unknown[];
80
+ }
81
+ export interface LoggerConfig {
82
+ /** Enable debug/warn logs in console (errors always shown). Default: false */
83
+ enabled: boolean;
84
+ /** Callback to intercept all logs (including errors) */
85
+ onLog?: (entry: LogEntry) => void;
86
+ }
package/package.json ADDED
@@ -0,0 +1,88 @@
1
+ {
2
+ "name": "@plevands/epson-thermal-printer",
3
+ "version": "0.1.1",
4
+ "description": "Library for Epson thermal printer integration with PDF support and React hooks",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "sideEffects": false,
17
+ "files": [
18
+ "dist/*.js",
19
+ "dist/*.cjs",
20
+ "dist/*.d.ts",
21
+ "dist/hooks",
22
+ "dist/lib",
23
+ "dist/types",
24
+ "README.md",
25
+ "LICENSE"
26
+ ],
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/plevands/epson-thermal-printer.git"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/plevands/epson-thermal-printer/issues"
33
+ },
34
+ "homepage": "https://github.com/plevands/epson-thermal-printer#readme",
35
+ "scripts": {
36
+ "dev": "vite --host --port 5123",
37
+ "build": "tsc -b && vite build",
38
+ "dev:lib": "vite build --watch",
39
+ "lint": "eslint .",
40
+ "preview": "vite preview"
41
+ },
42
+ "peerDependencies": {
43
+ "react": ">=18.0.0",
44
+ "react-dom": ">=18.0.0",
45
+ "pdfjs-dist": ">=4.0.0"
46
+ },
47
+ "peerDependenciesMeta": {
48
+ "react": {
49
+ "optional": true
50
+ },
51
+ "react-dom": {
52
+ "optional": true
53
+ },
54
+ "pdfjs-dist": {
55
+ "optional": true
56
+ }
57
+ },
58
+ "devDependencies": {
59
+ "@eslint/js": "^9.39.1",
60
+ "@types/node": "^24.10.1",
61
+ "@types/react": "^19.2.5",
62
+ "@types/react-dom": "^19.2.3",
63
+ "@vitejs/plugin-react": "^5.1.1",
64
+ "babel-plugin-react-compiler": "^1.0.0",
65
+ "eslint": "^9.39.1",
66
+ "eslint-plugin-react-hooks": "^7.0.1",
67
+ "eslint-plugin-react-refresh": "^0.4.24",
68
+ "globals": "^16.5.0",
69
+ "typescript": "~5.9.3",
70
+ "typescript-eslint": "^8.46.4",
71
+ "vite": "npm:rolldown-vite@7.2.5",
72
+ "vite-plugin-dts": "^4.5.4"
73
+ },
74
+ "overrides": {
75
+ "vite": "npm:rolldown-vite@7.2.5"
76
+ },
77
+ "keywords": [
78
+ "epson",
79
+ "printer",
80
+ "thermal-printer",
81
+ "epos",
82
+ "pdf",
83
+ "react",
84
+ "typescript"
85
+ ],
86
+ "author": "Colegio Plevand's",
87
+ "license": "MIT"
88
+ }