0

How can I connect already loaded PDF into bluetooth thermal printer.

How can I connect already loaded PDF into bluetooth thermal printer.

Micro
  • 29
  • 1
  • 5
  • AS far I know There is no simple way to send a pdf to a POS/thermal printer. You would need to render the PDF and rasterize it into a bitmap that you can send line by line. It will be easier to get the transaction details and generate your own receipt by text/design by sending commands to the printer – Babul Feb 28 '23 at 09:50
  • Kindly show me how to do it – Micro Feb 28 '23 at 10:50
  • " render the PDF and rasterize it into a bitmap"--- Kindly guide how to solve this, I haven't found any solution for this. – Micro Feb 28 '23 at 10:58

1 Answers1

0

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?

Babul
  • 1,076
  • 7
  • 9
  • // Sir can u check this program , here on clicking print I am reading DocumnetPDF() and then calling printTicket() to read the PDF and print. Please do check if this is correct. – Micro Mar 01 '23 at 06:51
  • I have emailed you the answer , kindly do check and reply. – Micro Mar 01 '23 at 06:57
  • To this Email ID: babul.sust.cse@gmail.com – Micro Mar 01 '23 at 06:57
  • My Email-Id : eby.hanson@microcentergmail.com – Micro Mar 01 '23 at 06:58
  • The way u discuss is fine. It should works. See thermal package implementation. – Babul Mar 01 '23 at 08:02
  • I have checked mail. your code seems to me fine. try to give a print if the result print as you desired mark answer as correct. happy coding. – Babul Mar 01 '23 at 08:11
  • But Sir In order to Print the answer contained in Bytes: – Micro Mar 01 '23 at 08:22
  • result = await BluetoothThermalPrinter.writeText(result!); My doubt doubt is how to pass the result to get printed either the above way or easing generator.text. Kinldly guide to code the answer. – Micro Mar 01 '23 at 08:24
  • result = await BluetoothThermalPrinter.writeText(result!) is not working /// Now going to try like this: //( I ahve passed result as parameter and taken as answer variable )///////////////////////////////// printPDF(answer) async { // List n_bytes = []; CapabilityProfile profile = await CapabilityProfile.load(); final generator = Generator(PaperSize.mm80, profile); bytes += generator.text( answer, // styles: PosStyles(align: PosAlign.center), //linesAfter: 1 ); } – Micro Mar 01 '23 at 08:48
  • That also didnt worked – Micro Mar 01 '23 at 09:07
  • It would have been helpful if u could edit my previous code attached in E-mail. – Micro Mar 01 '23 at 09:08