如何将已加载的 PDF 连接到蓝牙热敏打印机。
如何将已加载的 PDF 连接到蓝牙热敏打印机。
在
pubspec.yaml
中添加以下插件:
依赖关系:
path_provider: ^1.6.27
将版本号更新为当前版本。
并将其导入您的代码中。
import 'package:path_provider/path_provider.dart';
您还必须
import dart:io
才能使用 File 类。
import 'dart:io';
Future <List<int>> _readPDFFile() async {
try {
final Directory directory = await getApplicationDocumentsDirectory();
final File file = File('${directory.path}/my_file.pdf');
final bytes = File(file.path).readAsBytesSync();
} catch (e) {
print("Couldn't read file");
}
return bytes;
}
使用热敏打印机包链接 = bluetooth_Thermal_printer: ^0.0.6
最后将这些字节数据发送到热敏打印机中
Future<void> printRecipientPaper() async {
String isConnected = await BluetoothThermalPrinter.connectionStatus;
if (isConnected == "true") {
List<int> bytes = await _readPDFFile();
final result = await BluetoothThermalPrinter.writeBytes(bytes);
print("Print $result");
} else {
//Hadnle Not Connected Senario
}
}
如果您有 pdf 文件中的图像,也可以点击以下链接 如何使用Flutter将PDF文件转换为图像?
大家早上好,
使用 ziad-h 的答案 https://stackoverflow.com/users/saves/7632367
您可以执行以下操作:
依赖关系:
#Printing dependencies
esc_pos_utils_plus: ^2.0.3
print_bluetooth_thermal: ^1.1.0
pdfx: ^2.6.0
import 'package:pdfx/pdfx.dart';
import 'package:print_bluetooth_thermal/print_bluetooth_thermal.dart';
import 'package:image/image.dart' as img;
First, you should get the image from your pdf file:
Future<PdfPageImage?> getImage() async {
final document = await PdfDocument.openAsset('assets/cv.pdf');
final page = await document.getPage(1);
final image = await page.render(
width: page.width * 2, //decrement for less quality
height: page.height * 2,
format: PdfPageImageFormat.jpeg,
backgroundColor: '#ffffff',
// Crop rect in image for render
//cropRect: Rect.fromLTRB(left, top, right, bottom),
);
return image;
}
在打印/票据生成函数中声明一个新变量
final newImage = await getImage();
final imgs = img.decodeImage(newImage!.bytes);
//Add it to your byte/generator
bytes += generator.imageRaster(imgs, align: PosAlign.center);