2

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 ?

hudi
  • 15,555
  • 47
  • 142
  • 246
  • 3
    I 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 Answers1

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