android network printing printing sockets

Simple Printing in Android to a Network Printer

So for a few days I have been researching how to send a document to a network printer without using any third party libraries in android. This sounds like a simple task but by default Android doesn’t offer this service, sure there are loads of apps that can do it but this took me a while to find, and I think someone else may find it useful one day.

I made use of an AsyncTask so as not to tie up the UI thread. In the doInBackground method, the following simple code helped me achieve printing to a network printer.

DataOutputStream outToServer;
Socket clientSocket;
try {
   FileInputStream fileInputStream = new FileInputStream(android.os.Environment.getExternalStorageDirectory()  + "test.pdf");
   InputStream is =fileInputStream;
   clientSocket = new Socket(ipAddress, portNumber);
   outToServer = new DataOutputStream(clientSocket.getOutputStream());
   byte[] buffer = new byte[3000];
   while ( !=-1){
   return 1;
}catch (ConnectException connectException){
   Log.e(TAG, connectException.toString(), connectException);
   return -1;
catch (UnknownHostException e1) {
   Log.e(TAG, e1.toString(), e1);
   return 0;
} catch (IOException e1) {
   Log.e(TAG, e1.toString(), e1);
   return 0;
}finally {


This code will allow for sending PDF files, PCL files and PS files directly to the printer by using Sockets. The default port for most printers is 9100. You will also need to know the IP Address of the printer.

This code might not work with all printers but it worked with 2 different brands that I tried it with.

One reply on “Simple Printing in Android to a Network Printer”

Leave a Reply