2

I am trying to use my android phone as a printer. I am using ServerSocket to receive the document to be printed. If I add my phone as IP printer by providing IP address and port and select Generic Postscript Printer, I am able to receive the file in ps format correctly. I don't want to add my phone as printer as IP printer. So Now I am using NsdManager to register my device as printer. It gets recognized as Bonjour printer automatically and I can successfully add as printer. But now every time I print a document from by computer I get this data in the socket's input stream.

POST / HTTP/1.1 Content-Length: 673 Content-Type: application/ipp Host:   
Android-2.local:9200 User-Agent: CUPS/2.1.0 (Darwin 15.2.0; x86_64)   
IPP/2.0 Expect: 100-continue Gattributes-charsetutf-8Hattributes-
natural-languageen-usEprinter-uriipp://Android- 
2.local.:9200/Drequested-attributescompression-supportedDcopies-
supportedDcups-versionDdocument-format-supportedD marker-colorsDmarker-
high-levelsD marker-levelsDmarker-low-levelsDmarker-messageDmarker-
namesDmarker-typesDmedia-col-supportedD$multiple-document-handling-
supportedDoperations-supportedDprint-color-mode-supportedD printer-
alertDprinter-alert-descriptionDprinter-is-accepting-jobsD printer-
mandatory-job-attributesD printer-stateDprinter-state-messageDprinter-  
state-reasons

I read the IPP documentation and I am sending 100 Continue in the response and all the required params like this

clientSocket.setTcpNoDelay(true);
BufferedOutputStream out = new BufferedOutputStream(clientSocket.getOutputStream());
out.write("HTTP/1.1 100\r\n".getBytes("UTF-8"));
out.write("\r\n".getBytes("UTF-8"));
out.write("compression-supported: \"none\"\r\n".getBytes("UTF-8"));
out.write("printer-is-accepting-jobs: \"true\"\r\n".getBytes("UTF-8"));
.....
....
out.flush();

After that if i try to read the input stream for document, it gives null and on my computer i receive message "Printing: Connected to Printer" but if do out.close(); in to close the outputstream for the socket I get the message "unable to get printer status" on my computer. Please help me. Is there any way I just receive the document and not this post request or way to send a correct response and get the document? I am stuck with this for quite a long time now. Any help is highly appreciated.

Zaartha
  • 1,106
  • 9
  • 25
  • What is your usecase? What are you trying to achieve? What's going to happen with the postscript once your app received it? – IPP Nerd Jun 07 '16 at 17:30
  • @PeterKandinsky I am trying to use my android to keep all the documents i print during the day. I am announcing my device using NSDManager using service type "_ipp._tcp." . I am looking for IPP understanding. I looked at the official documentation, it did not help much. If you could help me understand the communication between printer and my computer that would be great. – Zaartha Jun 08 '16 at 04:39
  • suggestion for your usecase "keep documents on phone": just copy the documents (e.g. as pdf or any format your mobile apps support) to your phone. You can either use a cloud drive like dropbox or an app that supports filesharing via FTP, SMB or WEBDAV if you don't like to copy via USB. – IPP Nerd Jun 10 '16 at 08:14

2 Answers2

1

Your computer/CUPS (Mac with El Capitain I guess) is trying to print via IPP, but your phone-print-device does not implement IPP. Obviously, that's not going to work.

Solution 1:

add the printer in CUPS with the correct setup. for network printers CUPS offers:

  • IPP - http
  • IPP - https
  • IPP - ipp
  • IPP - ipps
  • LPD/LPR-Host
  • Windows printer via spoolss
  • AppSocket/HP JetDirect

the selection depends on the procotol you have implemented or plan to support in your app. IPP is not an option for you, except...

Solution 2:

Implement IPP in your "Print-Server-App". That's going to be tough!

There's a lot to implement... see https://www.pwg.org/ipp/

Solution 3:

Properly announce your service via Bonjour Printing 1.2

_pdl-datastream._tcp should be the correct service type. (see also chapter 7.6, Flagship Naming)

IPP Nerd
  • 992
  • 9
  • 25
1

I was finally able to implement it. Here is an excellent implementation for network printer using Node.js. It explains the details of IPP https://github.com/watson/ipp-printer

Also this video is good demonstration https://www.youtube.com/watch?v=58Ti8w1yX2w

And I am using https://github.com/NanoHttpd/nanohttpd to handle the printing requests on my android phone.

Zaartha
  • 1,106
  • 9
  • 25
  • 1
    ***/Part1:*** I upvoted your (own) answer to the question -- even though IMHO you're using "the wrong tool for the job". IPP provides bi-directional communication between print client and print service (starting from querying the service's specific capabilities regarding color or duplex printing; or stapling/punching finishings over job progress monitoring while printing; or job history queries; or printer administration tasks like starting/stopping/pausing/resuming/creating/deleting specific queues). – Kurt Pfeifle Jul 02 '16 at 14:00
  • ***/Part2:*** But your use-case doesn't even remotely require such capabilities. For what you want, a service type of `_pdl-datastream._tcp` would be good enough: *"shoot (data to port 9100) and forget"*. Much simpler, much more easy to develop and maintain.... – Kurt Pfeifle Jul 02 '16 at 14:00
  • @KurtPfeifle Thank you – Zaartha Jul 04 '16 at 08:52
  • I am happy you found a solution for your use case! Why don't you share your findings or implementation on github? This could be a good starting point for some fresh development in the field of IPP. IPP seems to become more popular since smart printers are able to render pdf and developers would love to bypass the operating systems printer management software. So far there's just CUPS available as open source ipp printer server... – IPP Nerd Sep 12 '16 at 09:31