0

I'm facing an issue writing PDF file to BluetoothSocket OutputStream in my android project. Text file write(print) is working fine but when I'm writing PDF file, it prints out some gibberish text not the data inside the PDF file. Here is my code snippet:

            File file = new File("/storage/emulated/0/Download/sample.pdf");

            inputStream = new FileInputStream(file);
            byte[] bytes = new byte[(int) file.length()];

            int nextByte;
            while ((nextByte = inputStream.read(bytes, 0, bytes.length)) != -1) {
                outputStream.write(bytes, 0, nextByte);
                outputStream.flush();
            }

            inputStream.close();
            outputStream.close();

Here outputStream is BluetoothSocket OutputStream.(ex: bluetoothSocket.getOutputStream())

Tohedul
  • 29
  • 1
  • 7
  • @KJ it's a thermal printer attached with an android device [device](https://www.ocominc.com/products/POS-Q1-Q2-Android-Portable-POS-Terminal-with-58mm-thermal-Printer.html#.YbMqRb1BxD8), where my app is running. From other apps, pdf printing is quite okay but when I'm sending pdf data through outpustream it's not working. Is this important to keep open the pdf file to get its actual inside data? – Tohedul Dec 26 '21 at 09:24
  • `%PDF-1.4 与獒 10 obj <> endobj 20 obj < – Tohedul Dec 26 '21 at 09:36
  • Even if I convert pdf to text line by line it only supports English and Mandarin but My text font is Bengali which won't support it. Another way I can convert this to an image file and print that image file. will this work? – Tohedul Dec 26 '21 at 14:17
  • That is not gibberish. It is a PDF perhaps being treated as text and not a binary document which it is. That is why it works for text. You need some PDF app or code written yourself to print PDFs. Why not open PDF with intent and send to print, using the app that displays the PDF to handle this? – Kevin Brown Jan 01 '22 at 22:10

0 Answers0