'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 1:[1]

The issue is very clear. As we can see that the printed receipt has formatting syntaxes with it. Which is used to format text and images in a PDF file. So, the printer through which you are trying to print doesn't support printing a PDF file. So, if possible you should provide the file in a compatible format such as a text file.

To know more about formatting text in a Bluetooth printer, you can have a look at this post here. Let me know whether this solves your problem or not.

Solution 2:[2]

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()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vishal Roy
Solution 2 Arnab Kar