@naphatjm/cdh-thermal-printer 1.0.1 → 1.0.3

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.
Files changed (2) hide show
  1. package/README.md +118 -9
  2. package/package.json +4 -2
package/README.md CHANGED
@@ -109,9 +109,66 @@ printer
109
109
  .print(printers[0]);
110
110
  ```
111
111
 
112
+ ### Convert Thai Text to Image (Client-side Helper)
113
+
114
+ For **server-side printing** with Thai text, use the `textToImageData()` utility function to render text on the client, then send the image data to your server API.
115
+
116
+ #### Client-side (React/Next.js):
117
+
118
+ ```javascript
119
+ import { textToImageData } from "@naphatjm/cdh-thermal-printer";
120
+
121
+ // Convert Thai text to image data
122
+ const imageData = textToImageData("สวัสดีครับ", 384, 22);
123
+
124
+ // Send image data to server API
125
+ const response = await fetch("/api/print", {
126
+ method: "POST",
127
+ headers: { "Content-Type": "application/json" },
128
+ body: JSON.stringify({
129
+ imageData: {
130
+ data: Array.from(imageData.data),
131
+ width: imageData.width,
132
+ height: imageData.height,
133
+ },
134
+ printerName: "EPSON TM-58",
135
+ }),
136
+ });
137
+ ```
138
+
139
+ #### Server-side (Next.js API Route):
140
+
141
+ ```javascript
142
+ // pages/api/print.js
143
+ import { ThermalPrinter } from "@naphatjm/cdh-thermal-printer";
144
+
145
+ export default function handler(req, res) {
146
+ if (req.method !== "POST") return res.status(405).end();
147
+
148
+ const { imageData, printerName } = req.body;
149
+
150
+ // Convert JSON data back to ImageDataLike object
151
+ const imageDataLike = {
152
+ data: new Uint8ClampedArray(imageData.data),
153
+ width: imageData.width,
154
+ height: imageData.height,
155
+ };
156
+
157
+ // Print using server-side library
158
+ const printer = new ThermalPrinter();
159
+ try {
160
+ printer.init().image(imageDataLike).feed(3).cut().print(printerName);
161
+
162
+ res.status(200).json({ message: "✅ Print successful" });
163
+ } catch (error) {
164
+ res.status(500).json({ error: error.message });
165
+ }
166
+ }
167
+ ```
168
+
112
169
  ## ⚠️ Important: Font Setup for Thai Text
113
170
 
114
- When using `printThaiText()`, you **must** load the Sarabun font in your HTML file:
171
+ When using `printThaiText()` or `textToImageData()`, you **must** load the Sarabun font in your HTML file:
115
172
 
116
173
  ```html
117
174
  <link
@@ -132,6 +189,28 @@ new ThermalPrinter((driverApiUrl = "http://localhost:9123"), (width = 384));
132
189
  - `driverApiUrl`: URL of the thermal printer driver API
133
190
  - `width`: Printer width in dots (384 = 58mm, 576 = 80mm)
134
191
 
192
+ ### Utility Functions
193
+
194
+ #### `textToImageData(text, width, fontSize)`
195
+
196
+ Converts Thai text to ImageData for use with `image()` method or server-side printing.
197
+
198
+ **Parameters:**
199
+
200
+ - `text` (string) - Thai text to convert
201
+ - `width` (number, default: 384) - Canvas width in pixels
202
+ - `fontSize` (number, default: 22) - Font size in pixels
203
+
204
+ **Returns:** ImageData object
205
+
206
+ **Example:**
207
+
208
+ ```javascript
209
+ const imageData = textToImageData("สวัสดี", 384, 22);
210
+ // Use with image() method
211
+ printer.image(imageData).print();
212
+ ```
213
+
135
214
  ### Instance Methods
136
215
 
137
216
  #### Text & Formatting
@@ -152,8 +231,8 @@ new ThermalPrinter((driverApiUrl = "http://localhost:9123"), (width = 384));
152
231
 
153
232
  #### Image & Printing
154
233
 
155
- - `image(imageData)` - Print image from canvas ImageData
156
- - `printThaiText(text, fontSize)` - Print Thai text with automatic image rendering (default fontSize: 22px)
234
+ - `image(imageData)` - Print image from canvas ImageData or ImageDataLike object
235
+ - `printThaiText(text, fontSize)` - Print Thai text with automatic image rendering (client-side only, default fontSize: 22px)
157
236
  - `raw(bytes)` - Send raw byte array
158
237
 
159
238
  #### Buffer Management
@@ -189,12 +268,42 @@ printer
189
268
  .cut();
190
269
  ```
191
270
 
192
- ## Requirements
271
+ ## Usage Patterns
193
272
 
194
- - Modern browser with Canvas API support
195
- - Active connection to thermal printer driver API (default: http://localhost:9123)
196
- - Sarabun font loaded for Thai text rendering
273
+ ### Pattern 1: Client-side Direct Printing
274
+
275
+ ```javascript
276
+ // Simple, all processing on client
277
+ printer.init().printThaiText("สวัสดี").feed(2).cut().print("Printer Name");
278
+ ```
279
+
280
+ ### Pattern 2: Client Render → Server Print (Recommended for production)
197
281
 
198
- ## License
282
+ ```javascript
283
+ // 1. Client renders
284
+ const imageData = textToImageData("สวัสดี", 384, 22);
285
+
286
+ // 2. Send to server
287
+ await fetch("/api/print", { body: JSON.stringify(imageData) });
199
288
 
200
- MIT
289
+ // 3. Server prints (more secure & flexible)
290
+ printer.image(imageDataLike).print("Printer Name");
291
+ ```
292
+
293
+ ### Pattern 3: Custom Image Printing
294
+
295
+ ```javascript
296
+ // Use with QR codes, logos, or custom images
297
+ const canvas = document.createElement("canvas");
298
+ const ctx = canvas.getContext("2d");
299
+ // ... draw custom content ...
300
+ const imageData = ctx.getImageData(0, 0, 384, 200);
301
+
302
+ printer.image(imageData).print("Printer Name");
303
+ ```
304
+
305
+ ## Requirements
306
+
307
+ - Modern browser with Canvas API support (for `printThaiText()` and `textToImageData()`)
308
+ - Active connection to thermal printer driver API (default: http://localhost:9123)
309
+ - Sarabun font loaded in HTML for Thai text rendering
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naphatjm/cdh-thermal-printer",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Thai Thermal Printer Library via Local Driver",
5
5
  "type": "module",
6
6
  "files": [
@@ -8,10 +8,12 @@
8
8
  ],
9
9
  "main": "./dist/thermal-printer.umd.js",
10
10
  "module": "./dist/thermal-printer.es.js",
11
+ "types": "./dist/index.d.ts",
11
12
  "exports": {
12
13
  ".": {
13
14
  "import": "./dist/thermal-printer.es.js",
14
- "require": "./dist/thermal-printer.umd.js"
15
+ "require": "./dist/thermal-printer.umd.js",
16
+ "types": "./dist/index.d.ts"
15
17
  }
16
18
  },
17
19
  "scripts": {