Flying Saucer takes XML or XHTML and applies CSS 2.1-compliant stylesheets to it, in order to render to PDF (via iText), images, and on-screen using Swing or SWT. The library implements (basically) the entirety of CSS 2.1 and aims to be fully compliant with the W3C specification; it includes a small handful of CSS 3 features.[source]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | package com.ebhor; import static com.opensymphony.xwork2.Action.SUCCESS; import com.opensymphony.xwork2.ActionSupport; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.StringBufferInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xhtmlrenderer.pdf.ITextRenderer; public class PdfAction extends ActionSupport { private InputStream inputStream = null; public String DownloadPdf() { Document doc = null; try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = builder.parse(new StringBufferInputStream(new HtmlFormats().htmlContent().toString())); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(doc, null); ByteArrayOutputStream bos = new ByteArrayOutputStream(); renderer.layout(); renderer.createPDF(bos); setInputStream(new ByteArrayInputStream(bos.toByteArray())); } catch (Exception e) { System.out.println("Exception " + e); } return SUCCESS; } public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } } public class HtmlFormats { public StringBuffer htmlContent() { StringBuffer sb = new StringBuffer("<html><body> " + "<h2>Hello Friends</h2>" + "</body></html>"); return sb; } |
1 2 3 4 5 6 7 8 9 10 11 | <action name="DownloadPdf" class="com.ebhor.PdfAction" method="DownloadPdf"> <result name="success" type="stream"> <param name="contentDisposition">attachment;filename=Invoice.pdf</param> <param name="contentType">application/pdf</param> <param name="inputName">inputStream</param> <param name="bufferSize">1024</param> </result> <result name="input">/pages/reports/SingleBill.jsp</result> <result name="login">/pages/login.jsp</result> <result name="UNAUTHORIZED">/pages/Unautho.jsp</result> </action> |
In jsp page
1 | <a href="DownloadPdf">Download</a> |