0

Unable to get default printer name while using applet

asked 2015-12-21 11:05:48 +0800

anupganatra gravatar image anupganatra
1 1

updated 2015-12-21 13:05:55 +0800

Hi,

I want to print my file directly from webpage. For that I am using following reference and trying to implement same with ZUL and Composer.

tonny-bruckers.blogspot.in - 2012/11/printing-files-directly-from-web-page.html

ZUL File :-

<zk>
<applet code = "PrintApplet.class" codebase = "applet/" id="printApplet" width="400px" style="border: 1px" />
<button id="btnClickMe" label="Click Me" sclass="light-btn"/>
</zk>

PrintApplet.class is present inside "WebContent/applet".

public class AppletComposer extends GenericForwardComposer<Window> {
    private Applet printApplet;
    public void doOverrideAfterComposer(Window comp) throws Exception {

    }
    public void onClick$btnClickMe(Event event) throws Exception {
        String Originalstr = "ByteArrayInputStream Example!";
        byte[] Originalbytes = Originalstr.getBytes();
            ByteArrayInputStream bis=new ByteArrayInputStream(Originalbytes);
        printApplet.invoke("print", bis);
    }
}

PrintApplet Class :-

public class PrintApplet extends Applet {
    public void init() 
    {

    }
    public void print(ByteArrayInputStream bis) throws PrintException 
    {
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        if (service != null) {
            DocFlavor psFormat = DocFlavor.INPUT_STREAM.PDF;
            PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();    
            DocPrintJob job = service.createPrintJob();
            Doc pdfDoc = new SimpleDoc(bis,psFormat, null);
            job.print(pdfDoc, attributes);         
        }
    }
}

I am able to invoke PrintApplet with this approach but getting Null as service. PrintApplet is working fine with AppletViewer and with normal Java Application but unable to get default printer service while using the above approach.

delete flag offensive retag edit

Comments

Hi Darksu,

With this example one popup will open and we are using the browser's printing functionality. I want to bypass browser's default printing functionality and want to print directly when user click on "Click Me" button.

Do not want to show any pop up or any printer selection box.

anupganatra ( 2015-12-21 11:42:57 +0800 )edit

Hi Robert, Tried Signing the JAR file with JAR Signer but getting same issue.PrintService object returns null value as its not able to identify the default printer.It working fine when I run Java file with option Run As ->Java Applet.But its not workning when I run it with any browser.

anupganatra ( 2015-12-23 07:53:46 +0800 )edit

how did you sign? self signed? or using a certificate of a known certifications authority?

cor3000 ( 2015-12-23 09:16:57 +0800 )edit

I have signed it with Jar Signer present at jdk/bin directory. Generated key using keytool and then used Jarsigner to sign the Jar.

anupganatra ( 2015-12-24 08:07:03 +0800 )edit

then it's "self signed": https://www.java.com/en/download/help/java_blocked.xml

cor3000 ( 2015-12-24 09:33:50 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2015-12-21 11:30:55 +0800

Darksu gravatar image Darksu
1991 1 4

Hello anupganatra,

Have you checked out the approach that is described in the following small talk?

http://books.zkoss.org/wiki/SmallTalks/2014/December/PrintingIn_ZK

Best Regards,

Darksu

link publish delete flag offensive edit
0

answered 2015-12-22 02:06:26 +0800

cor3000 gravatar image cor3000
4366 2 7
ZK Team

updated 2015-12-22 02:07:11 +0800

To me this sounds like an expected security restriction inside the Applet's Sandbox. In contrast to AppletViewer and Java Applications, Applets run in a very restricted environment, many system resources (Printers, Network, File System ...) are not easily available.

The related java docs state something similar:

Downloaded applets are the most common example of untrusted code.

The solution seems to sign the jar containing the Applet ... besides that the the browser security policies might still prevent this function discussed here: http://stackoverflow.com/questions/1016372/printservicelookup-lookupdefaultprintservice-returns-null

Robert

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2015-12-21 11:05:48 +0800

Seen: 34 times

Last updated: Dec 22 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More