Sami Sami - 1 year ago 314
Java Question

How to print pdf-file from stream in vaadin 7?

I am trying to print a pdf straight from the stream in vaadin 7 using Apache FOP + xsl. At the moment I can successfully create new xml document and regenerate pdf-file from it using xsl fo. I save the file to the server and everything is looking good.

My problem is that it is not possible to save file to the server, so I need to read it to some kind of bytearray and then open it when user is clicking "Print"-button.

out = new

try {
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

// XSL
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer =
factory.newTransformer(new StreamSource("printTemplate.xsl"));

Result res = new SAXResult(fop.getDefaultHandler());

transformer.transform(xml, res);
} finally {

When user clicks the print button, the generated pdf-document looks good after that when I check it from the server.

print.addClickListener(new ClickListener() {

public void buttonClick(ClickEvent event)
PdfComponent pdf = new PdfComponent(lang, bookingDto);

try {
DOMSource xml = pdf.getXMLSource(bookingDto, lang);
} catch (Exception e) {



How can I get the PDF document to a stream and how can I get it open automatically in a browser when user clicks the print button in Vaadin 7 application without saving the file to a server?

Answer Source

Downloading files in Vaadin works as described here. Summary:

Button downloadButton = new Button("Download image");
StreamResource myResource = createResource();
FileDownloader fileDownloader = new FileDownloader(myResource);

Then your StreamResource provides an InputStream that contains the file data. So, you don't need to save the file on the server. Regarding the printing, I don't know if it is even possible to instruct the browser open the print dialog directly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download