Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

Send Data Stream to Printer

Servlet generates PDF data stream and returns the stream to applet. How to send the stream to printer so that it can be directly printed? Thanks..
0
kasimchen
Asked:
kasimchen
  • 3
  • 3
  • 3
  • +1
1 Solution
 
objectsCommented:
Doubt you can. What is done with the data is handled by the browser, the servlet really has nothing to do with it.
0
 
kasimchenAuthor Commented:
sorry.. i didn't mention it clearly.
I mean to send the stream to printer from applet side.
What is the Java API to do this action?
0
 
Tommy BraasCommented:
As far as I know, all printing in a browser (where the applet is running) is handled by the browser. In other words, I don't believe that you can print the stream content to a printer using the printing support in Java from inside  applet.

Also, I haven't heard of a printer that is directly PDF compatible, which means that even if you could open a network stream directly to a printer, you wouldn't be able to print it without transforming the PDF to a printer specific language. Of course, translating PDF to PostScript is rather simple, but not to e.g. PCL.

Good luck,

\t
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
Theoretically, you could encode it as PostScript in the applet and send it to a printer, using new FileOutputStream("lpt1");
The applet would have to be signed or the security policy altered.
0
 
objectsCommented:
Far easier to use Java's Print API.
0
 
kasimchenAuthor Commented:
I tried the method offered by CEHJ, but the applet hangs (not responding) when I execute printing. It stucks at the line fos.write(data); and does not throw any exception. I had the applet signed already.
        try {
            FileInputStream fis =  new FileInputStream("c:/out.ps");
            FileOutputStream fos = new FileOutputStream("LPT1");
            int data;
            while ((data=fis.read()) != -1)
                fos.write(data);
            fos.close();
        }
        catch (Throwable ex) {
            System.err.println("*** exception ***");
        }
0
 
Tommy BraasCommented:
Just making sure, is there a printer connected to LPT1: ? Also, I am not sure that LPT1: behaves like a file on Windows. It does (if supported) on *nix platforms. You could try (if supported by the printer) to access the printer over TCP/IP and switching the implementation to use an outputstream acquired from a Socket.
0
 
CEHJCommented:
>>Also, I am not sure that LPT1: behaves like a file on Windows.

Yes, it does. But the printer will have to be set up to print like this, as it's a glimpse back into the sepia-tinted world of DOS printing.
0
 
kasimchenAuthor Commented:
No, I use network printer.
It seems that Java's Print API doesn't help much. Maybe it's better and more efficient to show the PDF in browser and let user prints from Acrobat Reader.
0
 
CEHJCommented:
Yes, if you set the contentType to pdf, if there's a browser plugin, it should activate in-place
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now