Use IPP(Internet Printing Protocol) or LPR(Line printer Remote) to print file in android

Exception picture Exception · May 8, 2015 · Viewed 12k times · Source

My requirement is to print a file from an android device without using any cloud based service.

I have been able to achieve it using "Raw" print protocol i.e by simply sending the file to printer's IP address at Port 9100. Here is the code snippet for that:

 client = new Socket(ip,port); //Port is 9100
 byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
 fileInputStream = new FileInputStream(file);
 bufferedInputStream = new BufferedInputStream(fileInputStream);
 bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
 outputStream = client.getOutputStream();
 outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
 outputStream.flush();
 bufferedInputStream.close();
 outputStream.close();

The problem with "Raw" printing protocol is that there is no way to get the status back from the printer.

So, I recently read about IPP and LDR using which we can get the status back from printer.

I have tried to find a way to implement them using android but had no success. I have already went through this answer but had no success in finding my solution.

It will be really helpful if someone can guide me on how to implement IPP or LDR in android.

Thanks in advance!

Answer

IPP Nerd picture IPP Nerd · May 11, 2015

General usage of IPP:

  1. Once a print job has been submitted the printer returns a job-id
  2. Use the Get-Job-Attributes-Operation in order to get the current job-state
  3. Wait until the attribute job-state equals to 9 (means 'completed')

There are other final job-states you should check for: aborted or canceled

For prototyping you could use the ipptool (native for desktop usage):

# ipptool -t -d job=482 ipp://192.168.2.113/ipp job.ipp
{
OPERATION Get-Job-Attributes
GROUP operation-attributes-tag
  ATTR charset attributes-charset utf-8
  ATTR language attributes-natural-language en
  ATTR uri printer-uri $uri
  ATTR integer job-id $job
}

Update 5/2020

I have published a kotlin implementation of the ipp protocol.

https://github.com/gmuth/ipp-client-kotlin

Once submitted you can wait for the print job to terminate: job.waitForTermination()