@rejaul/react-native-printer-kit 0.3.4 → 0.4.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.
- package/README.md +293 -14
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/rejaul/reactnativeprinterkit/ReactNativePrinterKitModule.kt +267 -31
- package/lib/module/NativeReactNativePrinterKit.js +19 -0
- package/lib/module/NativeReactNativePrinterKit.js.map +1 -1
- package/lib/module/enums.js +45 -0
- package/lib/module/enums.js.map +1 -0
- package/lib/module/index.js +107 -6
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/NativeReactNativePrinterKit.d.ts +241 -5
- package/lib/typescript/src/NativeReactNativePrinterKit.d.ts.map +1 -1
- package/lib/typescript/src/enums.d.ts +44 -0
- package/lib/typescript/src/enums.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +73 -7
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/NativeReactNativePrinterKit.ts +280 -5
- package/src/enums.ts +69 -0
- package/src/index.tsx +155 -9
|
@@ -1,14 +1,122 @@
|
|
|
1
1
|
import { TurboModuleRegistry, type TurboModule, Platform } from 'react-native';
|
|
2
2
|
|
|
3
|
+
/** How sure the library is that a paired Bluetooth device is a printer. */
|
|
4
|
+
export type PrinterConfidence = 'CONFIDENT' | 'LIKELY' | 'UNKNOWN';
|
|
5
|
+
|
|
6
|
+
/** `ALL` lists every paired device, `LIKELY_PRINTERS` hides non-printers, `STRICT` keeps only certain ones. */
|
|
7
|
+
export type PrinterFilter = 'ALL' | 'LIKELY_PRINTERS' | 'STRICT';
|
|
8
|
+
|
|
9
|
+
export type PrinterConnectionType = 'BLUETOOTH' | 'NETWORK' | 'USB';
|
|
10
|
+
|
|
3
11
|
export type BluetoothPrinterDevice = {
|
|
12
|
+
/** `null` for a Wi-Fi/LAN printer. */
|
|
4
13
|
name: string | null;
|
|
14
|
+
/** Bluetooth MAC, the host for a Wi-Fi/LAN printer, or `"vendorId:productId"` for a USB printer. */
|
|
5
15
|
address: string;
|
|
16
|
+
confidence: PrinterConfidence;
|
|
17
|
+
/** Why it got that confidence, e.g. "Printer-like name + SPP service". May be `null`. */
|
|
18
|
+
reason: string | null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type GetBondedPrintersParams = {
|
|
22
|
+
filter?: PrinterFilter;
|
|
23
|
+
/** Extra name fragments to treat as printers, for printers with unusual names. */
|
|
24
|
+
extraNameKeywords?: string[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type UsbPrinterDevice = {
|
|
28
|
+
name: string | null;
|
|
29
|
+
vendorId: number;
|
|
30
|
+
productId: number;
|
|
31
|
+
deviceName: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type UsbDeviceParams = {
|
|
35
|
+
vendorId: number;
|
|
36
|
+
productId: number;
|
|
6
37
|
};
|
|
7
38
|
|
|
8
39
|
export type PrinterImageType = 'PNG' | 'JPEG';
|
|
9
40
|
|
|
10
41
|
export type ConnectPrinterParams = {
|
|
42
|
+
/** Bluetooth MAC address, or the IP/host for `NETWORK`. Ignored (use `""`) for `USB`. */
|
|
11
43
|
address: string;
|
|
44
|
+
/** Default `BLUETOOTH`. */
|
|
45
|
+
type?: PrinterConnectionType;
|
|
46
|
+
/** `NETWORK` only. Default 9100. */
|
|
47
|
+
port?: number;
|
|
48
|
+
/** `NETWORK` connect timeout in ms. Default 5000. */
|
|
49
|
+
timeoutMs?: number;
|
|
50
|
+
/** `USB` only, from `getUsbPrinters()`. */
|
|
51
|
+
vendorId?: number;
|
|
52
|
+
/** `USB` only, from `getUsbPrinters()`. */
|
|
53
|
+
productId?: number;
|
|
54
|
+
/** Title of the ongoing "connected" notification. Default "Printer connected". */
|
|
55
|
+
notificationTitle?: string;
|
|
56
|
+
/** Title of the notification shown if the printer drops later. Default "Printer disconnected". */
|
|
57
|
+
disconnectedNotificationTitle?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type PrinterStatusParams = {
|
|
61
|
+
/** How long to wait for the printer to answer, in ms. Default 800. */
|
|
62
|
+
timeoutMs?: number;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* `null` means "unknown" - the printer did not answer or does not report that condition. It is NOT "ok".
|
|
67
|
+
*/
|
|
68
|
+
export type PrinterStatus = {
|
|
69
|
+
connected: boolean;
|
|
70
|
+
/** `false` means the printer ignored the status request and every field below is `null`. */
|
|
71
|
+
responded: boolean;
|
|
72
|
+
online: boolean | null;
|
|
73
|
+
paperOut: boolean | null;
|
|
74
|
+
paperNearEnd: boolean | null;
|
|
75
|
+
coverOpen: boolean | null;
|
|
76
|
+
hasError: boolean | null;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* `PICTURES` is the public Pictures folder, so the image also shows in the Gallery app; it accepts images
|
|
81
|
+
* only (a `.png`/`.jpg` file name or an `image/...` `mimeType`) and refuses anything else.
|
|
82
|
+
*/
|
|
83
|
+
export type FileLocation =
|
|
84
|
+
| 'CACHE'
|
|
85
|
+
| 'APP_FILES'
|
|
86
|
+
| 'DOWNLOADS'
|
|
87
|
+
| 'DOCUMENTS'
|
|
88
|
+
| 'PICTURES'
|
|
89
|
+
| 'CUSTOM_FOLDER';
|
|
90
|
+
|
|
91
|
+
export type SaveFileParams = {
|
|
92
|
+
/** Path of an existing file to copy. Give exactly one of `sourcePath`, `text` or `base64`. */
|
|
93
|
+
sourcePath?: string;
|
|
94
|
+
/** Content to save as UTF-8 text, instead of `sourcePath`. Needs `fileName`. */
|
|
95
|
+
text?: string;
|
|
96
|
+
/**
|
|
97
|
+
* Content to save as bytes (base64 encoded; a `data:...;base64,` prefix is fine), instead of
|
|
98
|
+
* `sourcePath`. Needs `fileName`. Use it for images, PDFs and other binary data.
|
|
99
|
+
*/
|
|
100
|
+
base64?: string;
|
|
101
|
+
/** Defaults to the source file's name. Required with `text` or `base64`. */
|
|
102
|
+
fileName?: string;
|
|
103
|
+
/** Default `DOWNLOADS`. */
|
|
104
|
+
location?: FileLocation;
|
|
105
|
+
/** Required for `CUSTOM_FOLDER`: a `content://` tree Uri string or a directory path. */
|
|
106
|
+
customFolder?: string;
|
|
107
|
+
subFolder?: string;
|
|
108
|
+
/** Guessed from the file extension when not given. */
|
|
109
|
+
mimeType?: string;
|
|
110
|
+
overwrite?: boolean;
|
|
111
|
+
deleteSource?: boolean;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export type SaveFileResult = {
|
|
115
|
+
success: boolean;
|
|
116
|
+
path: string | null;
|
|
117
|
+
uri: string | null;
|
|
118
|
+
fileName: string | null;
|
|
119
|
+
error: string | null;
|
|
12
120
|
};
|
|
13
121
|
|
|
14
122
|
export type PrintTextParams = {
|
|
@@ -56,6 +164,15 @@ export type PrintPdfParams = {
|
|
|
56
164
|
|
|
57
165
|
export type HtmlToPdfParams = {
|
|
58
166
|
html: string;
|
|
167
|
+
/**
|
|
168
|
+
* Full path of the PDF to write, file name included (e.g.
|
|
169
|
+
* `/data/user/0/<your.app>/files/invoice.pdf`). Leave it out to get a fresh file with a
|
|
170
|
+
* generated name in the app's cache folder. The folder must already exist and be one the
|
|
171
|
+
* app may write to (its own files/cache folders); an existing file is overwritten. To put
|
|
172
|
+
* the PDF in a public folder such as Downloads, convert first and then call `saveFile`.
|
|
173
|
+
* The path that was written is returned.
|
|
174
|
+
*/
|
|
175
|
+
outputPath?: string;
|
|
59
176
|
pageWidthDp?: number;
|
|
60
177
|
heightDp?: number;
|
|
61
178
|
minPageHeightDp?: number;
|
|
@@ -73,6 +190,93 @@ export type PrintHtmlParams = {
|
|
|
73
190
|
bandDelayMs?: number;
|
|
74
191
|
};
|
|
75
192
|
|
|
193
|
+
export type RemoveFileParams = {
|
|
194
|
+
/**
|
|
195
|
+
* What to delete: an absolute file path, or the `content://` Uri that `saveFile` returned (for
|
|
196
|
+
* a file in Downloads/Documents on Android 10+, which only works for files your app created).
|
|
197
|
+
*/
|
|
198
|
+
path: string;
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
export type RemoveFileResult = {
|
|
202
|
+
success: boolean;
|
|
203
|
+
path: string | null;
|
|
204
|
+
uri: string | null;
|
|
205
|
+
fileName: string | null;
|
|
206
|
+
error: string | null;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* The shape `readFile` returns the content in: `base64` (plain), `dataUri` (`data:<type>;base64,...`), `text`
|
|
211
|
+
* (decoded string), `hex` (two lower-case digits per byte) or `bytes` (array of numbers 0..255, max 1 MB).
|
|
212
|
+
*/
|
|
213
|
+
export type ReadFileFormat = 'base64' | 'dataUri' | 'text' | 'hex' | 'bytes';
|
|
214
|
+
|
|
215
|
+
export type ReadFileParams = {
|
|
216
|
+
/** An absolute file path, or the `content://` Uri that `saveFile` returned. */
|
|
217
|
+
path: string;
|
|
218
|
+
/** Default `base64`. */
|
|
219
|
+
format?: ReadFileFormat;
|
|
220
|
+
/** Only for `text`: `UTF-8` (default), `UTF-16`, `ISO-8859-1`, ... */
|
|
221
|
+
encoding?: string;
|
|
222
|
+
/** Files bigger than this are refused instead of loaded. Default 10 MB; `bytes` is always capped at 1 MB. */
|
|
223
|
+
maxBytes?: number;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
export type ReadFileResult = {
|
|
227
|
+
success: boolean;
|
|
228
|
+
format: ReadFileFormat | null;
|
|
229
|
+
/** Set for `base64`, `dataUri`, `text` and `hex`. */
|
|
230
|
+
content: string | null;
|
|
231
|
+
/** Set for `bytes`. */
|
|
232
|
+
bytes: number[] | null;
|
|
233
|
+
sizeBytes: number | null;
|
|
234
|
+
/** From the file extension, or the content provider's answer for a Uri. */
|
|
235
|
+
mimeType: string | null;
|
|
236
|
+
error: string | null;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export type HtmlToImageParams = {
|
|
240
|
+
html: string;
|
|
241
|
+
/** Full path of the image to write, file name included (folder must exist). Default: generated name in the cache folder. */
|
|
242
|
+
outputPath?: string;
|
|
243
|
+
/** Default `PNG`. */
|
|
244
|
+
imageType?: PrinterImageType;
|
|
245
|
+
/** Width of the image in pixels. Default: twice the page width. */
|
|
246
|
+
targetWidthPx?: number;
|
|
247
|
+
pageWidthDp?: number;
|
|
248
|
+
heightDp?: number;
|
|
249
|
+
minPageHeightDp?: number;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
export type ReadDirParams = {
|
|
253
|
+
/**
|
|
254
|
+
* The folder to read: an absolute folder path, or a `content://` Uri of a folder the user picked
|
|
255
|
+
* with the system folder picker (or the `path` of a sub-folder returned by an earlier call).
|
|
256
|
+
*/
|
|
257
|
+
path: string;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
export type DirEntry = {
|
|
261
|
+
name: string;
|
|
262
|
+
/** Pass it to `removeFile` (a file) or to `readDir` again (a folder). */
|
|
263
|
+
path: string;
|
|
264
|
+
isDirectory: boolean;
|
|
265
|
+
/** `null` for a folder. */
|
|
266
|
+
sizeBytes: number | null;
|
|
267
|
+
/** Milliseconds since 1970, `null` if unknown. */
|
|
268
|
+
lastModified: number | null;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
export type ReadDirResult = {
|
|
272
|
+
success: boolean;
|
|
273
|
+
path: string | null;
|
|
274
|
+
/** Just the names, in the same order as `entries`. */
|
|
275
|
+
names: string[];
|
|
276
|
+
entries: DirEntry[];
|
|
277
|
+
error: string | null;
|
|
278
|
+
};
|
|
279
|
+
|
|
76
280
|
export type OpenFileParams = {
|
|
77
281
|
path: string;
|
|
78
282
|
/** Inferred from the file extension if not given. */
|
|
@@ -100,16 +304,35 @@ export interface Spec extends TurboModule {
|
|
|
100
304
|
requestBluetoothPermission(): Promise<boolean>;
|
|
101
305
|
|
|
102
306
|
/**
|
|
103
|
-
* Returns
|
|
307
|
+
* Returns the Bluetooth devices already paired (bonded) with this device, best printer matches first.
|
|
104
308
|
* On Android 12+ this requires the BLUETOOTH_CONNECT permission to be granted.
|
|
309
|
+
* Pass `{}` for every paired device, or `{ filter: 'LIKELY_PRINTERS' }` to hide non-printers.
|
|
105
310
|
*/
|
|
106
|
-
getBondedBluetoothPrinters(
|
|
311
|
+
getBondedBluetoothPrinters(
|
|
312
|
+
params: GetBondedPrintersParams
|
|
313
|
+
): BluetoothPrinterDevice[];
|
|
107
314
|
|
|
108
315
|
/**
|
|
109
|
-
*
|
|
110
|
-
|
|
316
|
+
* USB printers currently plugged in (USB printer class). Needs a phone with USB host (OTG).
|
|
317
|
+
*/
|
|
318
|
+
getUsbPrinters(): UsbPrinterDevice[];
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Whether the user already granted this app access to that USB printer.
|
|
322
|
+
*/
|
|
323
|
+
hasUsbPermission(params: UsbDeviceParams): boolean;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Shows the system USB permission dialog and resolves with the user's answer.
|
|
327
|
+
* Call this before `connectPrinter` with `type: 'USB'`. The grant is lost when the app is reinstalled.
|
|
328
|
+
*/
|
|
329
|
+
requestUsbPermission(params: UsbDeviceParams): Promise<boolean>;
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Connects over Bluetooth (default), Wi-Fi/LAN or USB depending on `params.type`.
|
|
333
|
+
* Closes any existing printer connection first. Remembers the printer so the
|
|
111
334
|
* app reconnects automatically next time it's opened (see the README's
|
|
112
|
-
* "Persistent connection" section).
|
|
335
|
+
* "Persistent connection" section). Resolves `false` (does not reject) if it could not connect.
|
|
113
336
|
*/
|
|
114
337
|
connectPrinter(params: ConnectPrinterParams): Promise<boolean>;
|
|
115
338
|
|
|
@@ -128,6 +351,30 @@ export interface Spec extends TurboModule {
|
|
|
128
351
|
*/
|
|
129
352
|
getConnectedPrinter(): Promise<BluetoothPrinterDevice | null>;
|
|
130
353
|
|
|
354
|
+
/**
|
|
355
|
+
* Whether a printer is connected AND currently accepts data (it may be switched off or out of range).
|
|
356
|
+
* Does not check paper or the cover - use `getPrinterStatus` for that.
|
|
357
|
+
*/
|
|
358
|
+
isPrinterReady(): Promise<boolean>;
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Asks the printer for its state (online, paper, cover, error). Many cheap printers do not answer:
|
|
362
|
+
* then `responded` is `false` and every other field is `null` (unknown, NOT ok).
|
|
363
|
+
*/
|
|
364
|
+
getPrinterStatus(params: PrinterStatusParams): Promise<PrinterStatus>;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Prints a short ASCII test page (connection type, printer, address, time).
|
|
368
|
+
*/
|
|
369
|
+
printTestPage(): Promise<void>;
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Saves a file to cache, app storage, public Downloads/Documents or a chosen folder: either
|
|
373
|
+
* copies an existing file (`sourcePath`, e.g. the PDF from `htmlToPdf`) or writes content you
|
|
374
|
+
* pass directly (`text` or `base64`, with a `fileName`). Never rejects: check `result.success`.
|
|
375
|
+
*/
|
|
376
|
+
saveFile(params: SaveFileParams): Promise<SaveFileResult>;
|
|
377
|
+
|
|
131
378
|
/**
|
|
132
379
|
* Print raw text using the printer's built-in font. ASCII only - not
|
|
133
380
|
* suitable for Bangla or other non-Latin scripts, use printHtml() instead.
|
|
@@ -176,6 +423,34 @@ export interface Spec extends TurboModule {
|
|
|
176
423
|
*/
|
|
177
424
|
printHtml(params: PrintHtmlParams): Promise<boolean>;
|
|
178
425
|
|
|
426
|
+
/**
|
|
427
|
+
* Deletes ONE file (what `saveFile` wrote, or any file your app may delete). Never a folder, never a
|
|
428
|
+
* pattern; cannot be undone. A missing file is reported as a failure ("File not found").
|
|
429
|
+
* Never rejects: check `result.success`.
|
|
430
|
+
*/
|
|
431
|
+
removeFile(params: RemoveFileParams): Promise<RemoveFileResult>;
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Reads ONE file and returns its content in the shape `format` asks for (`base64`, `dataUri`, `text`,
|
|
435
|
+
* `hex` or `bytes`). Read-only. A file over `maxBytes` (default 10 MB) is refused instead of loaded.
|
|
436
|
+
* Never rejects: check `result.success`.
|
|
437
|
+
*/
|
|
438
|
+
readFile(params: ReadFileParams): Promise<ReadFileResult>;
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Renders HTML to ONE image file (PNG or JPEG) and returns its absolute path, or `null` on failure.
|
|
442
|
+
* It is `htmlToPdf` plus one rendered page, so a very tall page makes a very large bitmap and can run
|
|
443
|
+
* out of memory (then the result is `null`). For base64 pass the path to `readFile`.
|
|
444
|
+
*/
|
|
445
|
+
htmlToImage(params: HtmlToImageParams): Promise<string | null>;
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* Reads ONE folder: the files and folders directly inside it (not their contents), folders first and
|
|
449
|
+
* then A-Z. Read-only. A path that is a file, missing or relative is refused. Never rejects: check
|
|
450
|
+
* `result.success`.
|
|
451
|
+
*/
|
|
452
|
+
readDir(params: ReadDirParams): Promise<ReadDirResult>;
|
|
453
|
+
|
|
179
454
|
/**
|
|
180
455
|
* Opens a file with whatever app the user has installed that can handle its
|
|
181
456
|
* type (e.g. a PDF viewer) - shows the system's "Open with" chooser if more
|
package/src/enums.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FileLocation as FileLocationValue,
|
|
3
|
+
PrinterConfidence as PrinterConfidenceValue,
|
|
4
|
+
PrinterConnectionType as PrinterConnectionTypeValue,
|
|
5
|
+
PrinterFilter as PrinterFilterValue,
|
|
6
|
+
PrinterImageType as PrinterImageTypeValue,
|
|
7
|
+
ReadFileFormat as ReadFileFormatValue,
|
|
8
|
+
} from './NativeReactNativePrinterKit';
|
|
9
|
+
|
|
10
|
+
// Runtime values for the string unions the native module uses, so callers can write
|
|
11
|
+
// `PrinterFilter.LIKELY_PRINTERS` instead of a bare string. Each object is checked
|
|
12
|
+
// against its union with `satisfies`, so a typo or a value the native side does not
|
|
13
|
+
// know fails to compile. The plain strings keep working everywhere too.
|
|
14
|
+
|
|
15
|
+
/** How sure the library is that a paired Bluetooth device is a printer. */
|
|
16
|
+
export const PrinterConfidence = {
|
|
17
|
+
CONFIDENT: 'CONFIDENT',
|
|
18
|
+
LIKELY: 'LIKELY',
|
|
19
|
+
UNKNOWN: 'UNKNOWN',
|
|
20
|
+
} as const satisfies Record<PrinterConfidenceValue, PrinterConfidenceValue>;
|
|
21
|
+
export type PrinterConfidence =
|
|
22
|
+
(typeof PrinterConfidence)[keyof typeof PrinterConfidence];
|
|
23
|
+
|
|
24
|
+
/** `ALL` lists every paired device, `LIKELY_PRINTERS` hides non-printers, `STRICT` keeps only certain ones. */
|
|
25
|
+
export const PrinterFilter = {
|
|
26
|
+
ALL: 'ALL',
|
|
27
|
+
LIKELY_PRINTERS: 'LIKELY_PRINTERS',
|
|
28
|
+
STRICT: 'STRICT',
|
|
29
|
+
} as const satisfies Record<PrinterFilterValue, PrinterFilterValue>;
|
|
30
|
+
export type PrinterFilter = (typeof PrinterFilter)[keyof typeof PrinterFilter];
|
|
31
|
+
|
|
32
|
+
export const PrinterConnectionType = {
|
|
33
|
+
BLUETOOTH: 'BLUETOOTH',
|
|
34
|
+
NETWORK: 'NETWORK',
|
|
35
|
+
USB: 'USB',
|
|
36
|
+
} as const satisfies Record<
|
|
37
|
+
PrinterConnectionTypeValue,
|
|
38
|
+
PrinterConnectionTypeValue
|
|
39
|
+
>;
|
|
40
|
+
export type PrinterConnectionType =
|
|
41
|
+
(typeof PrinterConnectionType)[keyof typeof PrinterConnectionType];
|
|
42
|
+
|
|
43
|
+
export const PrinterImageType = {
|
|
44
|
+
PNG: 'PNG',
|
|
45
|
+
JPEG: 'JPEG',
|
|
46
|
+
} as const satisfies Record<PrinterImageTypeValue, PrinterImageTypeValue>;
|
|
47
|
+
export type PrinterImageType =
|
|
48
|
+
(typeof PrinterImageType)[keyof typeof PrinterImageType];
|
|
49
|
+
|
|
50
|
+
export const FileLocation = {
|
|
51
|
+
CACHE: 'CACHE',
|
|
52
|
+
APP_FILES: 'APP_FILES',
|
|
53
|
+
DOWNLOADS: 'DOWNLOADS',
|
|
54
|
+
DOCUMENTS: 'DOCUMENTS',
|
|
55
|
+
PICTURES: 'PICTURES',
|
|
56
|
+
CUSTOM_FOLDER: 'CUSTOM_FOLDER',
|
|
57
|
+
} as const satisfies Record<FileLocationValue, FileLocationValue>;
|
|
58
|
+
export type FileLocation = (typeof FileLocation)[keyof typeof FileLocation];
|
|
59
|
+
|
|
60
|
+
/** The shape `readFile` returns the content in. */
|
|
61
|
+
export const ReadFileFormat = {
|
|
62
|
+
BASE64: 'base64',
|
|
63
|
+
DATA_URI: 'dataUri',
|
|
64
|
+
TEXT: 'text',
|
|
65
|
+
HEX: 'hex',
|
|
66
|
+
BYTES: 'bytes',
|
|
67
|
+
} as const satisfies Record<string, ReadFileFormatValue>;
|
|
68
|
+
export type ReadFileFormat =
|
|
69
|
+
(typeof ReadFileFormat)[keyof typeof ReadFileFormat];
|
package/src/index.tsx
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
import ReactNativePrinterKit, {
|
|
2
2
|
type BluetoothPrinterDevice,
|
|
3
3
|
type ConnectPrinterParams,
|
|
4
|
+
type DirEntry,
|
|
5
|
+
type HtmlToImageParams,
|
|
6
|
+
type GetBondedPrintersParams,
|
|
4
7
|
type HtmlToPdfParams,
|
|
5
8
|
type OpenFileParams,
|
|
6
9
|
type PdfToImageParams,
|
|
7
|
-
type
|
|
10
|
+
type PrinterStatus,
|
|
11
|
+
type PrinterStatusParams,
|
|
8
12
|
type PrintHtmlParams,
|
|
9
13
|
type PrintImageBase64Params,
|
|
10
14
|
type PrintImageParams,
|
|
11
15
|
type PrintPdfParams,
|
|
12
16
|
type PrintTextParams,
|
|
17
|
+
type ReadDirParams,
|
|
18
|
+
type ReadDirResult,
|
|
19
|
+
type ReadFileParams,
|
|
20
|
+
type ReadFileResult,
|
|
21
|
+
type RemoveFileParams,
|
|
22
|
+
type RemoveFileResult,
|
|
23
|
+
type SaveFileParams,
|
|
24
|
+
type SaveFileResult,
|
|
25
|
+
type UsbDeviceParams,
|
|
26
|
+
type UsbPrinterDevice,
|
|
13
27
|
} from './NativeReactNativePrinterKit';
|
|
14
28
|
|
|
15
29
|
/**
|
|
@@ -35,16 +49,47 @@ export function requestBluetoothPermission(): Promise<boolean> {
|
|
|
35
49
|
}
|
|
36
50
|
|
|
37
51
|
/**
|
|
38
|
-
* Get
|
|
52
|
+
* Get the Bluetooth devices already paired (bonded) with this device, best printer
|
|
53
|
+
* matches first. Every device carries a `confidence`. Pass
|
|
54
|
+
* `{ filter: 'LIKELY_PRINTERS' }` to hide devices that do not look like printers
|
|
55
|
+
* (headphones, watches...), or `extraNameKeywords` for a printer with an unusual name.
|
|
39
56
|
*/
|
|
40
|
-
export function getBondedBluetoothPrinters(
|
|
41
|
-
|
|
57
|
+
export function getBondedBluetoothPrinters(
|
|
58
|
+
params: GetBondedPrintersParams = {}
|
|
59
|
+
): BluetoothPrinterDevice[] {
|
|
60
|
+
return ReactNativePrinterKit.getBondedBluetoothPrinters(params);
|
|
42
61
|
}
|
|
43
62
|
|
|
44
63
|
/**
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
64
|
+
* USB printers currently plugged in. Needs a phone with USB host (OTG).
|
|
65
|
+
*/
|
|
66
|
+
export function getUsbPrinters(): UsbPrinterDevice[] {
|
|
67
|
+
return ReactNativePrinterKit.getUsbPrinters();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether the user already granted this app access to that USB printer.
|
|
72
|
+
*/
|
|
73
|
+
export function hasUsbPermission(params: UsbDeviceParams): boolean {
|
|
74
|
+
return ReactNativePrinterKit.hasUsbPermission(params);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Shows the system USB permission dialog. Call before `connectPrinter` with
|
|
79
|
+
* `type: 'USB'`. The grant is lost when the app is reinstalled.
|
|
80
|
+
*/
|
|
81
|
+
export function requestUsbPermission(
|
|
82
|
+
params: UsbDeviceParams
|
|
83
|
+
): Promise<boolean> {
|
|
84
|
+
return ReactNativePrinterKit.requestUsbPermission(params);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Connect to a printer over Bluetooth (default; paired printer's MAC address),
|
|
89
|
+
* Wi-Fi/LAN (`type: 'NETWORK'`, `address` = IP) or USB (`type: 'USB'` with
|
|
90
|
+
* `vendorId`/`productId`). The connection survives the app being swiped from
|
|
91
|
+
* Recents, and is reconnected to automatically the next time the app starts.
|
|
92
|
+
* Resolves `false` if it could not connect.
|
|
48
93
|
*/
|
|
49
94
|
export function connectPrinter(params: ConnectPrinterParams): Promise<boolean> {
|
|
50
95
|
return ReactNativePrinterKit.connectPrinter(params);
|
|
@@ -71,6 +116,81 @@ export function getConnectedPrinter(): Promise<BluetoothPrinterDevice | null> {
|
|
|
71
116
|
return ReactNativePrinterKit.getConnectedPrinter();
|
|
72
117
|
}
|
|
73
118
|
|
|
119
|
+
/**
|
|
120
|
+
* Whether a printer is connected and currently accepts data. Does not check
|
|
121
|
+
* paper or cover - use `getPrinterStatus()`.
|
|
122
|
+
*/
|
|
123
|
+
export function isPrinterReady(): Promise<boolean> {
|
|
124
|
+
return ReactNativePrinterKit.isPrinterReady();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Asks the printer for its state (online / paper out / cover open / error).
|
|
129
|
+
* Many printers do not answer: then `responded` is `false` and the other fields
|
|
130
|
+
* are `null` (unknown, not "ok"). Some printers report an open cover as
|
|
131
|
+
* `paperOut`, so treat `paperOut: true` as "no paper or cover open".
|
|
132
|
+
*/
|
|
133
|
+
export function getPrinterStatus(
|
|
134
|
+
params: PrinterStatusParams = {}
|
|
135
|
+
): Promise<PrinterStatus> {
|
|
136
|
+
return ReactNativePrinterKit.getPrinterStatus(params);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Prints a short ASCII test page.
|
|
141
|
+
*/
|
|
142
|
+
export function printTestPage(): Promise<void> {
|
|
143
|
+
return ReactNativePrinterKit.printTestPage();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Copies a file (e.g. the PDF from `htmlToPdf`) to cache, app storage, public
|
|
148
|
+
* Downloads/Documents or a chosen folder. Never rejects: check `result.success`.
|
|
149
|
+
*/
|
|
150
|
+
export function saveFile(params: SaveFileParams): Promise<SaveFileResult> {
|
|
151
|
+
return ReactNativePrinterKit.saveFile(params);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Reads ONE file and returns its content in the shape `format` asks for: `base64` (default), `dataUri`
|
|
156
|
+
* (`data:<type>;base64,...`), `text` (optionally with an `encoding`), `hex` or `bytes` (array of numbers,
|
|
157
|
+
* max 1 MB). `path` is an absolute path or the `content://` Uri `saveFile` returned. A file over `maxBytes`
|
|
158
|
+
* (default 10 MB) is refused instead of loaded. Never rejects: check `result.success`.
|
|
159
|
+
*/
|
|
160
|
+
export function readFile(params: ReadFileParams): Promise<ReadFileResult> {
|
|
161
|
+
return ReactNativePrinterKit.readFile(params);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Renders HTML to ONE image file (PNG, or JPEG with `imageType: 'JPEG'`) and returns its path, or `null`
|
|
166
|
+
* on failure. Pass `outputPath` to choose the file name. A very tall page can run out of memory (then
|
|
167
|
+
* `null`). For base64 pass the returned path to `readFile`.
|
|
168
|
+
*/
|
|
169
|
+
export function htmlToImage(params: HtmlToImageParams): Promise<string | null> {
|
|
170
|
+
return ReactNativePrinterKit.htmlToImage(params);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Reads ONE folder: the files and folders directly inside it, folders first and then A-Z (it does not
|
|
175
|
+
* go into sub-folders; call it again with an entry's `path` to go deeper). `result.names` is just the
|
|
176
|
+
* names, `result.entries` has `name`, `path`, `isDirectory`, `sizeBytes` and `lastModified`. A path that
|
|
177
|
+
* is a file, missing or relative is refused. Never rejects: check `result.success`.
|
|
178
|
+
*/
|
|
179
|
+
export function readDir(params: ReadDirParams): Promise<ReadDirResult> {
|
|
180
|
+
return ReactNativePrinterKit.readDir(params);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Deletes ONE file: an absolute path, or the `content://` Uri `saveFile` returned. Never a folder or a
|
|
185
|
+
* pattern; cannot be undone. A missing file is a failure ("File not found"). Never rejects: check
|
|
186
|
+
* `result.success`.
|
|
187
|
+
*/
|
|
188
|
+
export function removeFile(
|
|
189
|
+
params: RemoveFileParams
|
|
190
|
+
): Promise<RemoveFileResult> {
|
|
191
|
+
return ReactNativePrinterKit.removeFile(params);
|
|
192
|
+
}
|
|
193
|
+
|
|
74
194
|
/**
|
|
75
195
|
* Print raw text using the printer's built-in font (ASCII only - use
|
|
76
196
|
* printHtml() for Bangla or other non-Latin scripts).
|
|
@@ -90,7 +210,9 @@ export function printImage(params: PrintImageParams): Promise<void> {
|
|
|
90
210
|
/**
|
|
91
211
|
* Print a base64-encoded image as an ESC/POS raster image.
|
|
92
212
|
*/
|
|
93
|
-
export function printImageBase64(
|
|
213
|
+
export function printImageBase64(
|
|
214
|
+
params: PrintImageBase64Params
|
|
215
|
+
): Promise<void> {
|
|
94
216
|
return ReactNativePrinterKit.printImageBase64(params);
|
|
95
217
|
}
|
|
96
218
|
|
|
@@ -134,19 +256,43 @@ export function openFile(params: OpenFileParams): boolean {
|
|
|
134
256
|
return ReactNativePrinterKit.openFile(params);
|
|
135
257
|
}
|
|
136
258
|
|
|
259
|
+
// Export enum-like constants (usable at runtime, and as types of the same name)
|
|
260
|
+
export {
|
|
261
|
+
FileLocation,
|
|
262
|
+
PrinterConfidence,
|
|
263
|
+
PrinterConnectionType,
|
|
264
|
+
PrinterFilter,
|
|
265
|
+
PrinterImageType,
|
|
266
|
+
ReadFileFormat,
|
|
267
|
+
} from './enums';
|
|
268
|
+
|
|
137
269
|
// Export types
|
|
138
270
|
export type {
|
|
139
271
|
BluetoothPrinterDevice,
|
|
140
272
|
ConnectPrinterParams,
|
|
273
|
+
DirEntry,
|
|
274
|
+
GetBondedPrintersParams,
|
|
275
|
+
HtmlToImageParams,
|
|
141
276
|
HtmlToPdfParams,
|
|
142
277
|
OpenFileParams,
|
|
143
278
|
PdfToImageParams,
|
|
144
|
-
|
|
279
|
+
PrinterStatus,
|
|
280
|
+
PrinterStatusParams,
|
|
145
281
|
PrintHtmlParams,
|
|
146
282
|
PrintImageBase64Params,
|
|
147
283
|
PrintImageParams,
|
|
148
284
|
PrintPdfParams,
|
|
149
285
|
PrintTextParams,
|
|
286
|
+
ReadDirParams,
|
|
287
|
+
ReadDirResult,
|
|
288
|
+
ReadFileParams,
|
|
289
|
+
ReadFileResult,
|
|
290
|
+
RemoveFileParams,
|
|
291
|
+
RemoveFileResult,
|
|
292
|
+
SaveFileParams,
|
|
293
|
+
SaveFileResult,
|
|
294
|
+
UsbDeviceParams,
|
|
295
|
+
UsbPrinterDevice,
|
|
150
296
|
};
|
|
151
297
|
|
|
152
298
|
// Export native module
|