README.md 4.56 KiB
Tauri Plugin Printer
一个用于 Tauri 2.0 应用的打印机插件,支持网络打印机、串口打印机和蓝牙打印机。
功能特性
- 支持网络打印机(TCP)
- 支持串口打印机(Serial Port)
- 支持 USB 串口打印机(USB Serial)
- 支持蓝牙打印机(Bluetooth)
- 支持多平台(Windows/macOS/Linux/Android/iOS)
- 异步数据传输
- 自动重连机制
- 错误处理和重试机制
安装
npm安装
npm i @package/tauri-plugin-printer
[dependencies]
+ tauri-plugin-printer = { git = "https://git.leadwaycloud.com/package/tauri-plugin-printer" }
使用方法
在 Tauri 应用中注册插件
修改src-tauri/src/lib.rs 的初始化中
fn fun() {
tauri::Builder::default()
+ .plugin(tauri_plugin_printer::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
修改src-tauri/capabilities/mobile.json 允许前端允许指定方法
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "enables the default permissions",
"windows": [
"main"
],
"permissions": [
"core:default",
+ "printer:default"
]
}
前端 API 使用
import { connect, disconnect, send } from '@package/tauri-plugin-printer';
// 连接 TCP 打印机
await connect('printer1', {
type: 'tcp',
params: {
host: '192.168.1.100',
port: 9100
}
});
// 连接串口打印机
await connect('printer2', {
type: 'serial',
params: {
port: '/dev/ttyS0',
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: 'none'
}
});
// 连接 USB 串口打印机
await connect('printer3', {
type: 'usbserial',
params: {
port: '/dev/ttyUSB0',
baudRate: 9600,
dataBits: 8,
parity: 'none',
stopBits: 1,
flowControl: 'none'
}
});
// 连接USB 打印机
await connect('printer3', {
type: 'usb',
params: {
deviceName: 'RP80'
}
});
或者使用默认的打印机
await connect('printer3', {
type: 'usb',
params: {}
});
// 连接蓝牙打印机 (Android/iOS)
await connect('printer4', {
type: 'bluetooth',
params: {
deviceName: 'BT-Printer'
}
});
// 连接 MFi 打印机 (仅 iOS)
await connect('printer5', {
type: 'mfi',
params: {
deviceName: 'Star Printer',
protocol: 'com.starmicronics.line'
}
});
// 连接 AirPrint 打印机 (仅 iOS)
await connect('printer6', {
type: 'airprint',
params: {
printerName: 'HP Office',
format: 'raw' // 'pdf' | 'image' | 'raw'
}
});
// 发送数据
await send('printer1', new Uint8Array([...]));
// 断开连接
await disconnect('printer1');
API 参考
connect(id: string, endpoint: PrinterEndpoint): Promise
连接打印机。
PrinterEndpoint 类型:
- TCP 打印机
{
type: 'tcp',
params: {
host: string,
port: number
}
}
- 串口打印机
{
type: 'serial',
params: {
port: string,
baudRate: number,
dataBits: 5 | 6 | 7 | 8,
parity: 'none' | 'odd' | 'even',
stopBits: 1 | 2,
flowControl: 'none' | 'hardware' | 'software'
}
}
- USB 串口打印机
{
type: 'usbserial',
params: {
// 同串口打印机参数
}
}
- 蓝牙打印机 (Android/iOS)
{
type: 'bluetooth',
params: {
deviceName: string
}
}
- MFi 打印机 (仅 iOS)
{
type: 'mfi',
params: {
deviceName: string,
protocol: string
}
}
- AirPrint 打印机 (仅 iOS)
{
type: 'airprint',
params: {
printerName: string,
format: 'pdf' | 'image' | 'raw'
}
}
disconnect(id: string): Promise
断开打印机连接。
send(id: string, data: Uint8Array): Promise
发送数据到打印机。
平台支持
功能 | Windows | macOS | Linux | Android | iOS |
---|---|---|---|---|---|
TCP | ✓ | ✓ | ✓ | ✓ | ✓ |
Serial | ✓ | ✓ | ✓ | - | - |
USB Serial | ✓ | ✓ | ✓ | ✓ | - |
Bluetooth | - | - | - | ✓ | ✓ |
MFi | - | - | - | - | ✓ |
AirPrint | - | - | - | - | ✓ |
注意事项
- iOS 平台的串口打印机支持仅限于 MFi 认证的设备
- Android 平台的串口打印机支持需要 ROOT 权限
- 蓝牙打印机连接需要相应的系统权限
许可证
MIT License
贡献指南
欢迎提交 Issue 和 Pull Request。在提交 PR 之前,请确保:
- 代码已经通过
cargo fmt
格式化 - 所有测试用例通过
- 文档已经更新
- 符合代码规范