my wicket apliaction created some pdf file. now I want to add button to print it somethink like this: http://javascript.about.com/library/blprint.htm how I can do it ?
Asked
Active
Viewed 1,770 times
2
-
3I think this is related more to JavaScript than Wicket. Have a look at this: http://stackoverflow.com/questions/205180/how-to-print-a-pdf-from-the-browser – Andrea Del Bene Sep 12 '12 at 15:03
-
Another relevant question if you're generating the PDF: [Can a PDF file's print dialog be opened with Javascript?](http://stackoverflow.com/q/687675/851811) – Xavi López Sep 12 '12 at 18:19
1 Answers
3
it looks you mix two things together. Your example is a javascript. It is not a PDF, it is just printing your document. It is equal as browser menu File -> Print, but the event is invoked from a javascript that handles button action. You can use the same button as in that example and add @print CSS to your web page to make your document nicely printable.
Also there is another way. If you want to print a PDF document from your application and you generate the PDF from Java code, look the following example for Wicket 1.6:
add(new Link<Void>("myPdfLink") {
private static final long serialVersionUID = 1L;
@Override
public void onClick() {
byte[] data = ... // TODO your data
final ByteArrayInputStream stream = new ByteArrayInputStream(data);
IResourceStream resourceStream = new AbstractResourceStream() {
private static final long serialVersionUID = 1L;
@Override
public InputStream getInputStream() throws ResourceStreamNotFoundException {
return stream;
}
@Override
public void close() throws IOException {
stream.close();
}
@Override
public String getContentType() {
return "application/pdf";
}
};
getRequestCycle().scheduleRequestHandlerAfterCurrent(
new ResourceStreamRequestHandler(resourceStream)
.setFileName("my-pdf-to-download.pdf")
.setContentDisposition(ContentDisposition.ATTACHMENT)
.setCacheDuration(Duration.ONE_SECOND)
);
}
});

Martin Strejc
- 4,307
- 2
- 23
- 38
-
+1 for `setCacheDuration()`. nice example, the most complete I have ever seen on one place – 1ac0 Mar 14 '14 at 09:17
-
Thanks, man. To open new tab in browser just change ContentDisposition.INLINE – Roman Haivaronski Jul 22 '20 at 07:46