views:

815

answers:

2

I recently got a new printer (specifically a HP Photosmart C6380 if that helps) that allows me to send text to port 9100 to print. Telnetting into it and typing text to print works fine, but I'm wondering if I could make a webpage using HTML and Javascript that can send text directly to it.

I'm currently using the code from the article Cross Site Printing (pdf), but the downside is that when it prints out it shows the entire POST request and not just the text.

So my question for you is am I able to send just the content of the POST request or strip the unneeded characters using only HTML and Javascript?

Note: The reason why it's limited to HTML and Javascript is because I'm hoping that I can use this on an iPhone or iPod Touch.

Edit: It appears that there's no way to get rid of the HTTP headers, so now my question is can I craft a custom POST request that will minimize on what is sent in the header? For example, can I not send the user agent since the printer doesn't care what the user agent is?

+2  A: 

You can HTTP-Request from within JS, so you always have an HTTP-Header, regardless of the port to which you are sending. So the answer is: no.

Martin
+4  A: 

No, there's no way to get rid of the HTTP headers in client-side JavaScript. For that, you need to set up a server-side script which will then make the actual connection to the printer port.

Edit: The server-side script won't send a HTTP request to your printer: It should open a new socket and thus can fully controle what gets sent!

Christoph
Am I able to create a custom POST request in client side Javascript though to minimize on the amount of unnecessary information printed out?
Sean
Yes, you can send custom requests via XMLHttpRequest (also called AJAX). But there's no need for that: Use a plain old HTML form (with method="POST") and a server-side language of your choice (PHP, ASP.NET, Perl, Python, ...). Afaik all of these can read POST variables and create sockets...
Christoph