Print Pdf file via Bluetooth Printer Android
You could try getting bytes from a pdf file & send them to printer as follows:
/* * This will send data to be printed by the bluetooth printer*/void sendData() throws IOException {try { // the text typed by the user //String msg = myTextbox.getText().toString(); //msg += "\n";InputStream is = this.openFileInput("filename.pdf"); // Where this is ActivityByteArrayOutputStream bos = new ByteArrayOutputStream();byte[] b = new byte[1024];while ((int bytesRead = is.read(b)) != -1) { bos.write(b, 0, bytesRead);}byte[] bytes = bos.toByteArray(); byte[] printformat = { 27, 33, 0 }; //try adding this print format mmOutputStream.write(printformat); mmOutputStream.write(bytes); // tell the user data were sent myLabel.setText("Data Sent"); closeBT();} catch (NullPointerException e) { e.printStackTrace();} catch (Exception e) { e.printStackTrace();}}
You will need to add following permissions in your Android Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You could try to open the pdf using an Android intent if it's possible to print it from another app (maybe the manufacturer's own?)
Intent intent = new Intent();intent.setAction(android.content.Intent.ACTION_VIEW);intent.setDataAndType(Uri.fromFile(file), "application/pdf");c.startActivity(intent);
Any application print a pdf has some library to decode pdf.
Of course you can send only a text to your printer.
If you want to print directly pdf you need your own library to do that.
For android i always use mupdf, very powerfuf lib and open source.
look at stackoverflow how to install step-by-step Integrate MuPDF Reader in an app
after you done run a full demo project from https://github.com/derek-watson/mupdf
list on directory (mupdf/platform/android/)
It will run main activity ChoosePDFActivity.java after you picked up pdf from your device it will showing, then main top bar have print button (PrintDialogActivity.java to print on google cloud print) change code here to send stream to your printer
Hope this help!!!