Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to send data in printable format to USB POS printer from flutter web #1

Closed
techttiru opened this issue Aug 7, 2022 · 3 comments

Comments

@techttiru
Copy link

techttiru commented Aug 7, 2022

Hi,

First of all, thanks for your package and this help for me to connect my local mac machine POS USB printer from flutter web. Everything good and i able to connect and send the data. but the problem is, the data that is being printed in printer is not readable character. so i need to convert the data into correct format and send to transferOut api.

Code is below.

static Future<void> connectUsbPrinter() async {
   bool canUse = canUseUsb();
   if (canUse) {
    //* connect the specific USB printer device using the device's vendor id and product id.
     UsbDevice requestDevice = await usb.requestDevice(
       RequestOptions(
         filters: [
           RequestOptionsFilter(
             vendorId: 0x04B8,
             productId: 0x0E02,
           ),
         ],
       ),
     );
     
        await requestDevice.open();
        await requestDevice.reset();
        int firstInterfaceNumber =
        requestDevice.configuration?.interfaces[0].interfaceNumber ?? 0;
        await requestDevice.claimInterface(firstInterfaceNumber);


//* i am using dart-pdf package to generate the pdf document with 80 mm size and if i print manually 
//*the generated pdf from flutter web, then all is good. but my expectation is send the pdf data to printer directly but 
based on the current limitation in dart-pdf, printing package, i was unable to send data to local printer directly from flutter web. 
//*but later based on some research, i ended up your package to connect the local printer from flutter web using webusb package. 

          //* The below function actually generates the pdf and respond the data in Uint8List type. now i want to send this data to 
          //* your transferOut api to printer in printable format.

          final pdfDataInUint8ListType = await generatePdf(PdfPageFormat.roll80);

//* here is the link for dart-pdf pacakge how to connect the printer and how to convert Uint8List type printable format.
[https://github.com/DavBfr/dart_pdf/wiki/Document-Output#using-another-plugin]

         //* this is the api from web_usb to send data to device.  This is how i am sending today but its not printing in readable 
         //* format.

          var usbOutTransferResult =   await requestDevice.transferOut(1, pdfDataInUint8ListType);
          
         //* all of them are working and i am able to connect the local machine device from flutter web and able to send the data 
         //*but the problem is, data is being printed as undisplayable character in paper.  as per dart-pdf page example,  i see below code to convert from Uint8List to some printable format. I dont know whether i can use this to trasferOut api from this code returns List<int> type but transferOut api expected TypedData. your makeBlock function returns array length error.
         
  Future<void> printTicket() async {
  final printer = PrinterNetworkManager('192.168.0.123');
  final res = await printer.connect();

  if (res != PosPrintResult.success) {
    throw Exception('Unable to connect to the printer');
  }

  final profile = await CapabilityProfile.load();
  final generator = Generator(PaperSize.mm80, profile);
  var ticket = <int>[];

  await for (var page
      in Printing.raster(await _generateTicket(), dpi: dpi)) {
    final image = page.asImage();
    ticket += generator.image(image);
    ticket += generator.feed(2);
    ticket += generator.cut();
  }

  printer.printTicket(ticket);
  printer.disconnect();
}
     
     //* ideally the question is, how to convert the Uint8List to printable character type and send to transferOut api. transferOut api accepts TypedData type to send data.
        
   }

I looked at your example in the below link. but it seems to be ledger nano device. i want to do the same for USB printer.
https://github.com/woodemi/web_usb.dart/blob/master/example/lib/ledger_nano_s_page.dart

Can you please share example of converting Uint8List type TypedData so that it can print in readable character?

Appreciate your response.

@Sunbreak
Copy link
Collaborator

Sunbreak commented Aug 8, 2022

Sorry, I'm not sure what your readable character is. Mine is Chinese

And it is has nothing to do with web_usb.dart itself

@Sunbreak Sunbreak closed this as completed Aug 8, 2022
@techttiru
Copy link
Author

@Sunbreak hi, Thanks for responding.

Attached is the impage & pdf that i am expecting to print using POS USB device using web_usb package.
Dp80_print_demo.pdf
Screen Shot 2022-08-07 at 9 05 08 PM

But it prints as below. The reason could be i am not doing the correct conversation when i call transferOut method. So, i am asking how to adapt "Unit8List" type to "TypedData" type to send the data to printer. As i mentioned, i followed your example with the function that you wrote "makeBlock" function but its not working for printer as your example is for some other device. so can you give example of how to do it for USB printer? It would be really helpful. Thank you!.

image

@Sunbreak
Copy link
Collaborator

Sunbreak commented Aug 8, 2022

Sorry I cant help. This repo only focus on problem of web_usb.dart itself

Your problem is about the protocol of your printer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants