Flutter打印服务插件printer_service的使用

发布于 1周前 作者 bupafengyu 来自 Flutter

Flutter打印服务插件printer_service的使用

Note:

这是对项目的分叉:https://pub.dev/packages/thermal_printer

此版本添加了在Windows上通过USB获取默认打印机的功能,函数名为getDefaultPrinter()

Printer_service

这是一个用于发现打印机并发送打印机命令的库。

该库允许向不同平台(如Android、iOS、Windows)的不同接口(蓝牙、蓝牙低功耗、USB和WiFi/以太网)发送ESC命令。

flutter_pos_printer启发。

主要功能

  • 支持Android、iOS和Windows
  • 扫描蓝牙设备
  • 发送原始List<int> bytes数据到设备,可参考flutter_esc_pos_utils生成ESC/POS命令。

功能

Android iOS Windows 描述
USB接口 🟥 允许连接USB设备。
蓝牙经典接口 🟥 🟥 允许连接经典蓝牙设备。
蓝牙低功耗(BLE)接口 🟥 允许连接BLE蓝牙设备。
网络接口(以太网/WiFi) 允许连接网络设备。
扫描 开始扫描仅限于蓝牙设备或网络设备(Android/iOS)。
连接 建立与设备的连接。
断开连接 取消活动或待处理的设备连接。
状态 蓝牙设备状态变化流。
打印 打印字节。

开始使用

完整的示例请查看/example文件夹。这里仅列出最核心的代码部分以说明如何使用该库。

生成用于打印的字节:

import 'package:esc_pos_utils/esc_pos_utils.dart';

final profile = await CapabilityProfile.load();
final generator = Generator(PaperSize.mm58, profile);
List<int> bytes = [];

bytes += generator.text('Test Print', styles: const PosStyles(align: PosAlign.center));
bytes += generator.text('Product 1');
bytes += generator.text('Product 2');

Android

允许连接蓝牙(经典和BLE)、USB和网络设备。

更改Android的minSdkVersion

thermal_printer仅兼容从Android SDK版本21开始的版本,因此需要在android/app/build.gradle中更改:

在build.gradle中设置:

defaultConfig {
    ...
    minSdkVersion 19
    targetSdkVersion 33
    ...
}

选择设备类型PrinterType(蓝牙、USB、网络)

如果选择蓝牙,可以传递以下可选参数:

  • isBle -> 允许连接支持此技术的蓝牙设备
  • autoconnect -> 当设备状态为None时允许重新连接

USB:你可以启用原生广播接收器来通知已连接的USB设备

在AndroidManifest.xml中添加以下代码:

<receiver
    android:name="com.codingdevs.thermal_printer.usb.UsbReceiver"
    android:exported="false">

    <intent-filter>
        <action android:name="android.hardware.usb.action.ACTION_USB_PERMISSION" />
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

    </intent-filter>
</receiver>

iOS

允许连接蓝牙(BLE)和网络设备。

Windows

允许连接USB和网络设备。 对于网络设备,需要设置ipAddress。

如何使用它

初始化一个PrinterManager实例
import 'package:printer_service/thermal_printer.dart';

var printerManager = PrinterManager.instance;
扫描
var devices = [];
_scan(PrinterType type, {bool isBle = false}) {
    // 查找打印机
    PrinterManager.instance.discovery(type: type, isBle: isBle).listen((device) {
        devices.add(device);
    });
}
连接
_connectDevice(PrinterDevice selectedPrinter, PrinterType type, {bool reconnect = false, bool isBle = false, String? ipAddress = null}) async {
    switch (type) {
      // 仅适用于Windows和Android
      case PrinterType.usb:
        await PrinterManager.instance.connect(
            type: type,
            model: UsbPrinterInput(name: selectedPrinter.name, productId: selectedPrinter.productId, vendorId: selectedPrinter.vendorId));
        break;
      // 仅适用于iOS和Android
      case PrinterType.bluetooth:
        await PrinterManager.instance.connect(
            type: type,
            model: BluetoothPrinterInput(
                name: selectedPrinter.name,
                address: selectedPrinter.address!,
                isBle: isBle,
                autoConnect: reconnect));
        break;
      case PrinterType.network:
        await PrinterManager.instance.connect(type: type, model: TcpPrinterInput(ipAddress: ipAddress ?? selectedPrinter.address!));
        break;
      default:
    }
}
断开连接
_disconnectDevice(PrinterType type) async {
    await PrinterManager.instance.disconnect(type: type);
}
监听蓝牙状态
_listenBluetoothState() {
    PrinterManager.instance.stateBluetooth.listen((status) {
      log(' ----------------- status bt $status ------------------ ');
    });
}
发送字节进行打印
_sendBytesToPrint(List<int> bytes, PrinterType type) async {
    PrinterManager.instance.send(type: type, bytes: bytes);
}

故障排除

错误:‘CBCentralManager的状态恢复只允许应用指定了"bluetooth-central"后台模式的应用’

info.plist添加:

<key>NSBluetoothAlwaysUsageDescription</key>
<string>Allow App use bluetooth?</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Allow App use bluetooth?</string>
<key>UIBackgroundModes</key>
<array>
    <string>bluetooth-central</string>
    <string>bluetooth-peripheral</string>
</array>

信用

完整示例代码

import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:printer_service/esc_pos_utils_platform/esc_pos_utils_platform.dart';
import 'package:printer_service/thermal_printer.dart';
import 'package:image/image.dart' as img;
import 'package:dart_ping_ios/dart_ping_ios.dart';
import 'image_utils.dart';

void main() {
  // 注册DartPingIOS
  if (Platform.isIOS) {
    DartPingIOS.register();
  }
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // 打印机类型 [bluetooth, usb, network]
  var defaultPrinterType = PrinterType.bluetooth;
  var _isBle = false;
  var _reconnect = false;
  var _isConnected = false;
  var printerManager = PrinterManager.instance;
  var devices = <BluetoothPrinter>[];
  StreamSubscription<PrinterDevice>? _subscription;
  StreamSubscription<BTStatus>? _subscriptionBtStatus;
  StreamSubscription<USBStatus>? _subscriptionUsbStatus;
  StreamSubscription<TCPStatus>? _subscriptionTCPStatus;
  BTStatus _currentStatus = BTStatus.none;
  // _currentUsbStatus仅在Android上支持
  // _currentUsbStatus仅在Android上支持
  USBStatus _currentUsbStatus = USBStatus.none;
  List<int>? pendingTask;
  String _ipAddress = '';
  String _port = '9100';
  final _ipController = TextEditingController();
  final _portController = TextEditingController();
  BluetoothPrinter? selectedPrinter;

  [@override](/user/override)
  void initState() {
    if (Platform.isWindows) defaultPrinterType = PrinterType.usb;
    super.initState();
    _portController.text = _port;
    _scan();

    // 订阅以监听蓝牙连接状态的变化
    _subscriptionBtStatus = PrinterManager.instance.stateBluetooth.listen((status) {
      log(' ----------------- status bt $status ------------------ ');
      _currentStatus = status;
      if (status == BTStatus.connected) {
        setState(() {
          _isConnected = true;
        });
      }
      if (status == BTStatus.none) {
        setState(() {
          _isConnected = false;
        });
      }
      if (status == BTStatus.connected && pendingTask != null) {
        if (Platform.isAndroid) {
          Future.delayed(const Duration(milliseconds: 1000), () {
            PrinterManager.instance.send(type: PrinterType.bluetooth, bytes: pendingTask!);
            pendingTask = null;
          });
        } else if (Platform.isIOS) {
          PrinterManager.instance.send(type: PrinterType.bluetooth, bytes: pendingTask!);
          pendingTask = null;
        }
      }
    });
    // PrinterManager.instance.stateUSB仅在Android上支持
    _subscriptionUsbStatus = PrinterManager.instance.stateUSB.listen((status) {
      log(' ----------------- status usb $status ------------------ ');
      _currentUsbStatus = status;
      if (Platform.isAndroid) {
        if (status == USBStatus.connected && pendingTask != null) {
          Future.delayed(const Duration(milliseconds: 1000), () {
            PrinterManager.instance.send(type: PrinterType.usb, bytes: pendingTask!);
            pendingTask = null;
          });
        }
      }
    });

    // PrinterManager.instance.stateUSB仅在Android上支持
    _subscriptionTCPStatus = PrinterManager.instance.stateTCP.listen((status) {
      log(' ----------------- status tcp $status ------------------ ');
      _currentTCPStatus = status;
    });
  }

  [@override](/user/override)
  void dispose() {
    _subscription?.cancel();
    _subscriptionBtStatus?.cancel();
    _subscriptionUsbStatus?.cancel();
    _subscriptionTCPStatus?.cancel();
    _portController.dispose();
    _ipController.dispose();
    super.dispose();
  }

  // 按照PrinterType扫描设备
  void _scan() {
    devices.clear();
    _subscription = printerManager.discovery(type: defaultPrinterType, isBle: _isBle).listen((device) {
      devices.add(BluetoothPrinter(
        deviceName: device.name,
        address: device.address,
        isBle: _isBle,
        vendorId: device.vendorId,
        productId: device.productId,
        typePrinter: defaultPrinterType,
      ));
      setState(() {});
    });
  }

  void setPort(String value) {
    if (value.isEmpty) value = '9100';
    _port = value;
    var device = BluetoothPrinter(
      deviceName: value,
      address: _ipAddress,
      port: _port,
      typePrinter: PrinterType.network,
      state: false,
    );
    selectDevice(device);
  }

  void setIpAddress(String value) {
    _ipAddress = value;
    var device = BluetoothPrinter(
      deviceName: value,
      address: _ipAddress,
      port: _port,
      typePrinter: PrinterType.network,
      state: false,
    );
    selectDevice(device);
  }

  void selectDevice(BluetoothPrinter device) async {
    if (selectedPrinter != null) {
      if ((device.address != selectedPrinter!.address) || (device.typePrinter == PrinterType.usb && selectedPrinter!.vendorId != device.vendorId)) {
        await PrinterManager.instance.disconnect(type: selectedPrinter!.typePrinter);
      }
    }

    selectedPrinter = device;
    setState(() {});
  }

  Future _printReceiveTest() async {
    List<int> bytes = [];

    // Xprinter XP-N160I
    final profile = await CapabilityProfile.load(name: 'XP-N160I');

    // PaperSize.mm80 or PaperSize.mm58
    final generator = Generator(PaperSize.mm58, profile);
    bytes += generator.setGlobalCodeTable('CP1252');
    bytes += generator.text('Test Print', styles: const PosStyles(align: PosAlign.left));
    bytes += generator.text('Product 1');
    bytes += generator.text('Product 2');

    // bytes += generator.text('¥1,990', containsChinese: true, styles: const PosStyles(align: PosAlign.left));
    // bytes += generator.emptyLines(1);

    // sum width total column must be 12
    bytes += generator.row([
      PosColumn(width: 8, text: 'Lemon lime export quality per pound x 5 units', styles: const PosStyles(align: PosAlign.left, codeTable: 'CP1252')),
      PosColumn(width: 4, text: 'USD 2.00', styles: const PosStyles(align: PosAlign.right, codeTable: 'CP1252')),
    ]);

    final ByteData data = await rootBundle.load('assets/ic_launcher.png');
    if (data.lengthInBytes > 0) {
      final Uint8List imageBytes = data.buffer.asUint8List();
      // 解码字节为图像
      final decodedImage = img.decodeImage(imageBytes)!;
      // 创建黑色底图
      // 缩放图像为130x?缩略图(保持宽高比)。
      img.Image thumbnail = img.copyResize(decodedImage, height: 130);
      // 创建具有指定尺寸的原始图像副本
      img.Image originalImg = img.copyResize(decodedImage, width: 380, height: 130);
      // 用白色背景填充原始图像
      img.fill(originalImg, color: img.ColorRgb8(255, 255, 255));
      var padding = (originalImg.width - thumbnail.width) / 2;

      // 将图像插入到框架内并居中
      drawImage(originalImg, thumbnail, dstX: padding.toInt());

      // 将图像转换为灰度
      var grayscaleImage = img.grayscale(originalImg);

      bytes += generator.feed(1);
      // bytes += generator.imageRaster(img.decodeImage(imageBytes)!, align: PosAlign.center);
      bytes += generator.imageRaster(grayscaleImage, align: PosAlign.center);
      bytes += generator.feed(1);
    }

    // // 中文字符
    bytes += generator.row([
      PosColumn(width: 8, text: '豚肉・木耳と玉子炒め弁当', styles: const PosStyles(align: PosAlign.left), containsChinese: true),
      PosColumn(width: 4, text: '¥1,990', styles: const PosStyles(align: PosAlign.right), containsChinese: true),
    ]);
    _printEscPos(bytes, generator);
  }

  /// 打印票据
  void _printEscPos(List<int> bytes, Generator generator) async {
    var connectedTCP = false;
    if (selectedPrinter == null) return;
    var bluetoothPrinter = selectedPrinter!;

    switch (bluetoothPrinter.typePrinter) {
      case PrinterType.usb:
        bytes += generator.feed(2);
        bytes += generator.cut();
        await printerManager.connect(
            type: bluetoothPrinter.typePrinter,
            model: UsbPrinterInput(name: bluetoothPrinter.deviceName, productId: bluetoothPrinter.productId, vendorId: bluetoothPrinter.vendorId));
        pendingTask = null;
        break;
      case PrinterType.bluetooth:
        bytes += generator.cut();
        await printerManager.connect(
            type: bluetoothPrinter.typePrinter,
            model: BluetoothPrinterInput(
                name: bluetoothPrinter.deviceName,
                address: bluetoothPrinter.address!,
                isBle: bluetoothPrinter.isBle ?? false,
                autoConnect: _reconnect));
        pendingTask = null;
        if (Platform.isAndroid) pendingTask = bytes;
        break;
      case PrinterType.network:
        bytes += generator.feed(2);
        bytes += generator.cut();
        connectedTCP = await printerManager.connect(type: bluetoothPrinter.typePrinter, model: TcpPrinterInput(ipAddress: bluetoothPrinter.address!));
        if (!connectedTCP) print(' --- 请检查您的连接 --- ');
        break;
      default:
    }
    if (bluetoothPrinter.typePrinter == PrinterType.bluetooth && Platform.isAndroid) {
      if (_currentStatus == BTStatus.connected) {
        printerManager.send(type: bluetoothPrinter.typePrinter, bytes: bytes);
        pendingTask = null;
      }
    } else {
      printerManager.send(type: bluetoothPrinter.typePrinter, bytes: bytes);
    }
  }

  // 连接设备
  _connectDevice() async {
    _isConnected = false;
    if (selectedPrinter == null) return;
    switch (selectedPrinter!.typePrinter) {
      case PrinterType.usb:
        await printerManager.connect(
            type: selectedPrinter!.typePrinter,
            model: UsbPrinterInput(name: selectedPrinter!.deviceName, productId: selectedPrinter!.productId, vendorId: selectedPrinter!.vendorId));
        _isConnected = true;
        break;
      case PrinterType.bluetooth:
        await printerManager.connect(
            type: selectedPrinter!.typePrinter,
            model: BluetoothPrinterInput(
                name: selectedPrinter!.deviceName,
                address: selectedPrinter!.address!,
                isBle: selectedPrinter!.isBle ?? false,
                autoConnect: _reconnect));
        break;
      case PrinterType.network:
        await printerManager.connect(type: selectedPrinter!.typePrinter, model: TcpPrinterInput(ipAddress: selectedPrinter!.address!));
        _isConnected = true;
        break;
      default:
    }

    setState(() {});
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter Pos Plugin Platform example app'),
        ),
        body: Center(
          child: Container(
            height: double.infinity,
            constraints: const BoxConstraints(maxWidth: 400),
            child: SingleChildScrollView(
              padding: EdgeInsets.zero,
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Row(
                      children: [
                        Expanded(
                          child: ElevatedButton(
                            onPressed: selectedPrinter == null || _isConnected
                                ? null
                                : () {
                                    _connectDevice();
                                  },
                            child: const Text("Connect", textAlign: TextAlign.center),
                          ),
                        ),
                        const SizedBox(width: 8),
                        Expanded(
                          child: ElevatedButton(
                            onPressed: selectedPrinter == null || !_isConnected
                                ? null
                                : () {
                                    if (selectedPrinter != null) printerManager.disconnect(type: selectedPrinter!.typePrinter);
                                    setState(() {
                                      _isConnected = false;
                                    });
                                  },
                            child: const Text("Disconnect", textAlign: TextAlign.center),
                          ),
                        ),
                      ],
                    ),
                  ),
                  DropdownButtonFormField<PrinterType>(
                    value: defaultPrinterType,
                    decoration: const InputDecoration(
                      prefixIcon: Icon(
                        Icons.print,
                        size: 24,
                      ),
                      labelText: "Type Printer Device",
                      labelStyle: TextStyle(fontSize: 18.0),
                      focusedBorder: InputBorder.none,
                      enabledBorder: InputBorder.none,
                    ),
                    items: <DropdownMenuItem<PrinterType>>[
                      if (Platform.isAndroid || Platform.isIOS)
                        const DropdownMenuItem(
                          value: PrinterType.bluetooth,
                          child: Text("bluetooth"),
                        ),
                      if (Platform.isAndroid || Platform.isWindows)
                        const DropdownMenuItem(
                          value: PrinterType.usb,
                          child: Text("usb"),
                        ),
                      const DropdownMenuItem(
                        value: PrinterType.network,
                        child: Text("Wifi"),
                      ),
                    ],
                    onChanged: (PrinterType? value) {
                      setState(() {
                        if (value != null) {
                          setState(() {
                            defaultPrinterType = value;
                            selectedPrinter = null;
                            _isBle = false;
                            _isConnected = false;
                            _scan();
                          });
                        }
                      });
                    },
                  ),
                  Visibility(
                    visible: defaultPrinterType == PrinterType.bluetooth && Platform.isAndroid,
                    child: SwitchListTile.adaptive(
                      contentPadding: const EdgeInsets.only(bottom: 20.0, left: 20),
                      title: const Text(
                        "This device supports ble (low energy)",
                        textAlign: TextAlign.start,
                        style: TextStyle(fontSize: 19.0),
                      ),
                      value: _isBle,
                      onChanged: (bool? value) {
                        setState(() {
                          _isBle = value ?? false;
                          _isConnected = false;
                          selectedPrinter = null;
                          _scan();
                        });
                      },
                    ),
                  ),
                  Visibility(
                    visible: defaultPrinterType == PrinterType.bluetooth && Platform.isAndroid,
                    child: SwitchListTile.adaptive(
                      contentPadding: const EdgeInsets.only(bottom: 20.0, left: 20),
                      title: const Text(
                        "reconnect",
                        textAlign: TextAlign.start,
                        style: TextStyle(fontSize: 19.0),
                      ),
                      value: _reconnect,
                      onChanged: (bool? value) {
                        setState(() {
                          _reconnect = value ?? false;
                        });
                      },
                    ),
                  ),
                  Column(
                      children: devices
                          .map(
                            (device) =>
                                ListTile(
                                  title: Text('${device.deviceName}'),
                                  subtitle: Platform.isAndroid && defaultPrinterType == PrinterType.usb
                                      ? null
                                      : Visibility(visible: !Platform.isWindows, child: Text("${device.address}")),
                                  onTap: () {
                                    // do something
                                    selectDevice(device);
                                  },
                                  leading: selectedPrinter != null &&
                                          ((device.typePrinter == PrinterType.usb && Platform.isWindows
                                                  ? device.deviceName == selectedPrinter!.deviceName
                                                  : device.vendorId != null && selectedPrinter!.vendorId == device.vendorId) ||
                                              (device.address != null && selectedPrinter!.address == device.address))
                                      ? const Icon(
                                          Icons.check,
                                          color: Colors.green,
                                        )
                                      : null,
                                  trailing: OutlinedButton(
                                    onPressed: selectedPrinter == null || device.deviceName != selectedPrinter?.deviceName
                                        ? null
                                        : () async {
                                            _printReceiveTest();
                                          },
                                    child: const Padding(
                                      padding: EdgeInsets.symmetric(vertical: 2, horizontal: 20),
                                      child: Text("Print test ticket", textAlign: TextAlign.center),
                                    ),
                                  ),
                                ),
                          )
                          .toList()),
                  Visibility(
                    visible: defaultPrinterType == PrinterType.network && Platform.isWindows,
                    child: Padding(
                      padding: const EdgeInsets.only(top: 10.0),
                      child: TextFormField(
                        controller: _ipController,
                        keyboardType: const TextInputType.numberWithOptions(signed: true),
                        decoration: const InputDecoration(
                          label: Text("Ip Address"),
                          prefixIcon: Icon(Icons.wifi, size: 24),
                        ),
                        onChanged: setIpAddress,
                      ),
                    ),
                  ),
                  Visibility(
                    visible: defaultPrinterType == PrinterType.network && Platform.isWindows,
                    child: Padding(
                      padding: const EdgeInsets.only(top: 10.0),
                      child: TextFormField(
                        controller: _portController,
                        keyboardType: const TextInputType.numberWithOptions(signed: true),
                        decoration: const InputDecoration(
                          label: Text("Port"),
                          prefixIcon: Icon(Icons.numbers_outlined, size: 24),
                        ),
                        onChanged: setPort,
                      ),
                    ),
                  ),
                  Visibility(
                    visible: defaultPrinterType == PrinterType.network && Platform.isWindows,
                    child: Padding(
                      padding: const EdgeInsets.only(top: 10.0),
                      child: OutlinedButton(
                        onPressed: () async {
                          if (_ipController.text.isNotEmpty) setIpAddress(_ipController.text);
                          _printReceiveTest();
                        },
                        child: const Padding(
                          padding: EdgeInsets.symmetric(vertical: 4, horizontal: 50),
                          child: Text("Print test ticket", textAlign: TextAlign.center),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class BluetoothPrinter {
  int? id;
  String? deviceName;
  String? address;
  String? port;
  String? vendorId;
  String? productId;
  bool? isBle;

  PrinterType typePrinter;
  bool? state;

  BluetoothPrinter(
      {this.deviceName,
      this.address,
      this.port,
      this.state,
      this.vendorId,
      this.productId,
      this.typePrinter = PrinterType.bluetooth,
      this.isBle = false});
}

更多关于Flutter打印服务插件printer_service的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter打印服务插件printer_service的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


printer_service 是一个 Flutter 插件,用于在 Flutter 应用中实现打印功能。它支持多种打印任务,包括文本、图像、HTML、PDF 等。以下是如何在 Flutter 项目中使用 printer_service 插件的基本步骤。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 printer_service 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  printer_service: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来获取依赖。

2. 导入插件

在你的 Dart 文件中导入 printer_service 插件。

import 'package:printer_service/printer_service.dart';

3. 初始化打印服务

在使用打印功能之前,通常需要初始化打印服务。

void initPrinterService() async {
  await PrinterService.init();
}

4. 打印文本

你可以使用 PrinterService 来打印纯文本。

void printText() async {
  await PrinterService.printText('Hello, World!');
}

5. 打印图像

你也可以打印图像。首先,你需要将图像转换为字节数据。

void printImage() async {
  ByteData imageData = await rootBundle.load('assets/image.png');
  Uint8List imageBytes = imageData.buffer.asUint8List();
  await PrinterService.printImage(imageBytes);
}

6. 打印HTML

printer_service 还支持打印 HTML 内容。

void printHtml() async {
  await PrinterService.printHtml('<h1>Hello, World!</h1>');
}

7. 打印PDF

如果你有 PDF 文件,可以直接打印它。

void printPdf() async {
  ByteData pdfData = await rootBundle.load('assets/document.pdf');
  Uint8List pdfBytes = pdfData.buffer.asUint8List();
  await PrinterService.printPdf(pdfBytes);
}

8. 打印自定义内容

printer_service 还支持打印自定义内容,比如使用 PrintJob 类来定义打印任务。

void printCustomContent() async {
  PrintJob printJob = PrintJob(
    content: 'This is custom content',
    type: PrintJobType.text,
  );
  await PrinterService.printJob(printJob);
}

9. 处理打印结果

你可以监听打印任务的结果。

void printWithCallback() async {
  await PrinterService.printText('Hello, World!').then((result) {
    if (result) {
      print('Printing succeeded');
    } else {
      print('Printing failed');
    }
  });
}

10. 释放资源

在应用退出或不再需要打印服务时,释放资源。

void disposePrinterService() async {
  await PrinterService.dispose();
}

完整示例

以下是一个完整的示例,展示了如何使用 printer_service 插件打印文本、图像、HTML 和 PDF。

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:printer_service/printer_service.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: PrinterExample(),
    );
  }
}

class PrinterExample extends StatefulWidget {
  [@override](/user/override)
  _PrinterExampleState createState() => _PrinterExampleState();
}

class _PrinterExampleState extends State<PrinterExample> {
  [@override](/user/override)
  void initState() {
    super.initState();
    initPrinterService();
  }

  void initPrinterService() async {
    await PrinterService.init();
  }

  void printText() async {
    await PrinterService.printText('Hello, World!');
  }

  void printImage() async {
    ByteData imageData = await rootBundle.load('assets/image.png');
    Uint8List imageBytes = imageData.buffer.asUint8List();
    await PrinterService.printImage(imageBytes);
  }

  void printHtml() async {
    await PrinterService.printHtml('<h1>Hello, World!</h1>');
  }

  void printPdf() async {
    ByteData pdfData = await rootBundle.load('assets/document.pdf');
    Uint8List pdfBytes = pdfData.buffer.asUint8List();
    await PrinterService.printPdf(pdfBytes);
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Printer Service Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: printText,
              child: Text('Print Text'),
            ),
            ElevatedButton(
              onPressed: printImage,
              child: Text('Print Image'),
            ),
            ElevatedButton(
              onPressed: printHtml,
              child: Text('Print HTML'),
            ),
            ElevatedButton(
              onPressed: printPdf,
              child: Text('Print PDF'),
            ),
          ],
        ),
      ),
    );
  }
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!