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

1

6

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!

Fairchild answered 8/5, 2015 at 4:52 Comment(4)
What kind of status do you want to read? IPP offers a generic response-status that is related to the IPP communication itself. You can read attributes from printer-objects or job-objects. You could check whether the job has been submitted okay or even wait until the job-state is 'completed' (=printed).Assamese
Basically I just want a confirmation that job submitted has finished or not i.e whether the printer printed the file or not. In my current scenario mentioned above, I couldn't get any status like thatFairchild
@Fairchild i was trying your code to print a pdf file but it is only printing byte code and not the actual file data, could you please help me hereCognation
Could you tell me what printer this works with? I am using an inexpensive HP model, and it will only print ANSI characters.Prostration
A
1

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

Assamese answered 11/5, 2015 at 7:0 Comment(4)
I still don't understand how do I implement it on android and I don't want to use Android-PrintService because I don't think it uses IPP or LPRFairchild
There are a number of android print service apps available that claim to support IPP - with no cloud-processing involved. Unfortunately they don't seem to work very well. You can very well bypass the official android print api and connect straight to the printer. The downside is that you have to implement IPP yourself.Assamese
@IPPNerd How to use ipp in Android application? Do you have sample for demo?Couldst
I've provided examples how to use the library here: github.com/gmuth/ipp-samplesAssamese

© 2022 - 2024 — McMap. All rights reserved.