-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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.
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
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
Asked: 2015-12-21 11:05:48 +0800
Seen: 34 times
Last updated: Dec 22 '15
Create tab dynamically, multitab application
Customize ZK's JavaScript Code
zk Calendar October infinite loop
Problem calling window.addEventListener inside a script
How to choose avatar image from images directory?
Global DecimalBox onChanging currency format
EL in a forEach @command not working?
adding a menuitem with image on Menubar
can somebody give the use of imagecontent in menuitem ? [closed]
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 )editHi 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 )edithow did you sign? self signed? or using a certificate of a known certifications authority?
cor3000 ( 2015-12-23 09:16:57 +0800 )editI 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 )editthen it's "self signed": https://www.java.com/en/download/help/java_blocked.xml
cor3000 ( 2015-12-24 09:33:50 +0800 )edit