Reputation: 29
How can I connect already loaded PDF into bluetooth thermal printer.
How can I connect already loaded PDF into bluetooth thermal printer.
Upvotes: 0
Views: 2218
Reputation: 51
Good morning guys,
Using ziad-h's answer from https://stackoverflow.com/users/saves/7632367
You can do as follow:
Dependencies:
#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;
}
inside your print/ticket generation function declare a new variable
final newImage = await getImage();
final imgs = img.decodeImage(newImage!.bytes);
//Add it to your byte/generator
bytes += generator.imageRaster(imgs, align: PosAlign.center);
Upvotes: 0
Reputation: 1224
Add the following plugin in pubspec.yaml
:
dependencies:
path_provider: ^1.6.27
Update the version number to whatever is current.
And import it in your code.
import 'package:path_provider/path_provider.dart';
You also have to import dart:io
to use the File class.
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;
}
use thermal printer package link = bluetooth_thermal_printer: ^0.0.6
finally send those bytes data into thermal printer
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
}
}
if you have image in pdf file you can follow below link as well How to convert a PDF file to an image with Flutter?
Upvotes: 0