Applet and Java Printing API

Discussion in 'Java' started by java_helloworld, May 6, 2004.

  1. I am trying to print from an applet using the Java Print API.

    If I run the applet on appletviewer then things work fine i.e. I am
    able to get the print out. If I run the applet on a browser then the
    print icon is loaded on the system tray and there is no print outs. I
    am trying to print on HP laser jet printer.

    Please help me to sort the err in the code or my understaning.

    Thanks a ton!!!

    JAVA_HelloWorld
    java_helloworld, May 6, 2004
    #1
    1. Advertising

  2. On 5 May 2004 21:57:44 -0700, java_helloworld wrote:

    > I am trying to print from an applet


    URL?

    >..using the Java Print API.
    >
    > If I run the applet on appletviewer then things work fine i.e. I am
    > able to get the print out. If I run the applet on a browser then the
    > print icon is loaded on the system tray and there is no print outs.


    You need to find the Java console in the browser..
    For IE [ Tools | Sun Java Console ]
    It will show the exceptions your applet is throwing
    because it is not signed.

    You can install it using JWS
    <http://www.physci.org/codes/javafaq.jsp#jws>
    But first you will need to Jar and Sign it.
    <http://www.physci.org/codes/javafaq.jsp#jar>

    BTW, please do not x-post so widely..
    comp.lang.java.programmer,
    comp.lang.java.help,
    comp.lang.java
    <http://www.physci.org/codes/javafaq.jsp#xpost>

    Invalid group c.l.j dropped from X-Post,
    F'Ups set to c.l.j.help.

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 6, 2004
    #2
    1. Advertising

  3. java_helloworld

    Murray Guest

    "java_helloworld" <> wrote in message
    news:...
    > I am trying to print from an applet using the Java Print API.
    >
    > If I run the applet on appletviewer then things work fine i.e. I am
    > able to get the print out. If I run the applet on a browser then the
    > print icon is loaded on the system tray and there is no print outs. I
    > am trying to print on HP laser jet printer.
    >
    > Please help me to sort the err in the code or my understaning.
    >
    > Thanks a ton!!!
    >
    > JAVA_HelloWorld


    Is the applet signed? If it's not, you won't be able to print due to
    security constraints.
    Murray, May 7, 2004
    #3
  4. > Is the applet signed? If it's not, you won't be able to print due to
    > security constraints.


    Yes the applet is not a signed Applet. I think with the Java printng
    API we can print from an unsigned applet also. It just that before
    printing it asks for a confirmation something like "Do you want to
    allow the applet wants to print?".

    I am able to instantiate the print icon on the system tray but not
    able to spool to the printer.

    If I run the code as a applet application i.e. using appletviewer then
    things are fine but as I run on an HTML page the print icon loads on
    the system tray but the print outs are not there on the printer.

    Please excuse the coding standards.

    Thanks,
    Java Hello World

    Here is the snapshot of the code.

    package Applet.unsigned;

    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    import java.net.*;

    import javax.print.*;
    import javax.print.attribute.*;
    import javax.print.attribute.standard.*;
    import javax.swing.*;

    public class PrinterApplet extends JApplet {
    //~ Instance fields
    ========================================================

    private PrintRequestAttributeSet aset;
    DocFlavor DocFlavor;
    public Doc doc;
    DocPrintJob printerJob;
    JTextPane pane;

    //~ Methods ================================================================

    /**
    * DOCUMENT ME!
    */
    public void init() {
    getContentPane().setLayout(new BorderLayout());

    pane = new JTextPane();
    pane.setSize(150, 100);
    pane.setContentType("text/html");
    pane.setText(
    "<html><center><b><big>Applet Test</big></b><br>"
    + "</center></html>");
    getContentPane().add(pane, "Center");

    JPanel buttons = new JPanel();
    buttons.setBackground(Color.white);

    JButton print = new JButton("Print");

    buttons.add(print);

    getContentPane().add(buttons, "South");

    print.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    print();
    }
    });
    }

    /**
    * DOCUMENT ME!
    */
    void prep() {

    URL url = null;
    aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.ISO_A4);
    aset.add(new Copies(1));

    try {
    url =
    new URL("http://sohowww.nascom.nasa.gov//data//realtime//eit_171//512//latest.gif");

    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    PrintService pservices =
    PrintServiceLookup.lookupDefaultPrintService();
    System.out.println(pservices.getName());
    //DocFlavor flavor = javax.print.DocFlavor.INPUT_STREAM.GIF;
    DocFlavor flavor = javax.print.DocFlavor.URL.GIF;
    //doc = new InputStreamDoc("a.gif", flavor);
    //doc = new InputStreamDoc(pane.getText(), flavor);
    doc = new SimpleDoc(url, javax.print.DocFlavor.URL.GIF, null);
    try {
    System.out.println("DOC : \n " + doc.getPrintData());
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    /* Create a Print Job */
    printerJob = pservices.createPrintJob();

    }

    /**
    * DOCUMENT ME!
    */
    void print() {
    prep();
    System.out.println("Printer Name : " +
    printerJob.getPrintService());
    try {
    printerJob.print(doc, aset);

    } catch (PrintException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println("Done Printing.");
    }

    }
    java_helloworld, May 8, 2004
    #4
  5. java_helloworld

    Murray Guest

    > url =
    > new URL("http://sohowww.nascom.nasa.gov//data//realtime//eit_171//512//latest.gif");


    Here's your problem. An unsigned applet is not allowed to make a
    connection to a host other than the one it was loaded from. I would
    have thought DocPrintJob would throw an exception, but it appears to
    be swallowing it silently.

    url.openConnection();
    url.openStream();

    This will throw an AccessControlException as expected. Either sign the
    applet, or host the image locally.
    Murray, May 9, 2004
    #5
  6. Thanks Murray, for the clarification. But I am able to print the
    picture from the URL, if I run the Unsigned Applet as a Application
    i.e. not on a web -browser.

    BTW do you know any website/article/book, which talks about uses and
    limitations of Java Printing and Unsigned/Signed Applet.
    java_helloworld, May 12, 2004
    #6
  7. On 12 May 2004 03:15:16 -0700, java_helloworld wrote:

    [ F'Ups set to c.l.j.help ]

    > Thanks Murray, for the clarification. But I am able to print the
    > picture from the URL, if I run the Unsigned Applet as a Application
    > i.e. not on a web -browser.


    Yes, that supports what Murray was saying,
    applications can do anything, whereas applets
    are limited by the applet 'security sandbox'..

    To 'break out' of the applet sandbox, you
    need to jar and sign your classes..
    <http://www.physci.org/codes/javafaq.jsp#jar>

    [ Unfortunatley that entry focuses on 'double
    click lauch' applications, but the 'jar/sign'
    bit relates equally to applet security. ]

    The only other option is to change the policy
    files settings in the user's browser, which
    seems only good for testing purposes..

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, May 13, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. webster
    Replies:
    0
    Views:
    835
    webster
    Jul 20, 2003
  2. Krista
    Replies:
    3
    Views:
    2,296
    Andrew Thompson
    Sep 15, 2004
  3. Archana
    Replies:
    1
    Views:
    1,363
    Andrew Thompson
    Oct 25, 2004
  4. yawnmoth
    Replies:
    1
    Views:
    869
    Andrew Thompson
    Aug 15, 2006
  5. Replies:
    4
    Views:
    5,818
    Laurent D.A.M. MENTEN
    Jan 11, 2007
Loading...

Share This Page