androidbluetoothbytethermal-printer

Printing a pdf file on a thermal printer


enter image description here

I getting issue, printing through bluetooth on thermal printer from pdf file become text view.

Print Pdf file via Bluetooth Printer Android I was tried these example but didn't what I expected.

this is my current code

code file source:

       String checkout     = "checkout";
       String fpath        = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) +"/"+ checkout + ".pdf";

code to printing

       FileInputStream fis = new FileInputStream(file);
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       byte[] buf = new byte[1024];

       try {
           for (int readNum; (readNum = fis.read(buf)) != -1;) {
               bos.write(buf, 0, readNum);
               System.out.println("read " + readNum + " bytes,");
           }
       } catch (IOException ex) {
           System.out.println("ERROR!");
       }
       byte[] bytesPDF = bos.toByteArray();
       byte[] printformat = { 27, 33, 0 }; //try adding this print format
       mService.write(printformat);
       mService.write(bytesPDF);

I hope able to print pdf file by thermal bluetooth printer. Please help me. Thankyou.


Solution

  • The way how Thermal printer works is

    1. Open socket connection to printer
    2. Send the encoded data that the printer understands
    3. Close the connection

    So, the question here boils down to what's the format of the data to be sent so that the printer is able to understand it and print accordingly. It depends on the manufacturer of the Printer. The encodings are either well documented, packed into an SDK/driver for use or are open source standard encoding for ESC/POS generic printers.

    At the end, what you need to do to print a PDF file is -

    1. Convert PDF file to Bitmap[] of pages.
    2. Encode the pages one by one by the command for printing bitmap as provided by the manufacturer.
    3. Pass this encoded string data to the printer.

    For Example, do look at the generic ESC/POS implementation in the following GitHub Repo https://github.com/DantSu/ESCPOS-ThermalPrinter-Android

    PrinterTextParserImg.bitmapToHexadecimalString()