-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Am trying to get the data from database and showing up in a pdf format ?
how can we do it ?
Please Have a Look Here. It will help you
Google for iText . It's a library for pdf manipulation. Hope this can help you
best
Stephan
i've created a document using pdf writer. now the problem is coming how to show that pdf data in zul file ?
will this code will do ?
<?page title="Home" contentType="application/pdf; charset=UTF-8"?>
or do we have to bind with some label or any other control ?
is there any simple way like we do in jsp
response.setContentType="application/pdf" ?
A simple way to embed a pdf file into a zul page is to use the iframe component. To set the iframe content property build a org.zkoss.util.media.AMedia class instance. This class has a lot of constructors that can be used depending on which type of media you used to store your pdf information. For instance, if you used iText tool - that in my opinion is a pretty good choice to create and manage pdf files - you probably have a ByteArrayOutputStream. Let's call it "baos":
Media content = new AMedia("filename", "pdf", "application/pdf", baos.getBytes());
However I prefer to display the pdf files in a stand alone browser window. A pdf file is generally a report or a generated form printout and could be huge in size. I would like the user has the full control of all the browser pdf plugin capabilities: copy, save, print, search, etc. To display a pdf file in a stand alone browser window I make use of a simple servlet:
public class ReportServlet extends GenericServlet { private static final long serialVersionUID = 1L; private static final Log log = Log.lookup(ReportServlet.class); public static final String REPORT_STREAM = "report-stream"; public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { HttpServletRequest httpRequest = (HttpServletRequest)request; HttpServletResponse httpResponse = (HttpServletResponse)response; ByteArrayOutputStream baos = (ByteArrayOutputStream)httpRequest.getSession().getAttribute(REPORT_STREAM); if (baos != null) { byte[] data = baos.toByteArray(); httpResponse.setHeader("Content-disposition", "filename=report.pdf"); httpResponse.setContentType("application/pdf"); ServletOutputStream out = httpResponse.getOutputStream(); out.write(data); httpRequest.getSession().setAttribute(REPORT_STREAM, null); } else { log.info("No input found."); } } }
To show the pdf I send a redirect to the servlet url from my application code:
if (baos.size() > 0) { Sessions.getCurrent().setAttribute(ReportServlet.REPORT_STREAM, baos); Executions.getCurrent().sendRedirect("/report", "_blank"); }
This opens a new browser window containing the pdf content. Do not forget to declare the servlet in your web.xml file.
Hope this can help.
/Adriano
@Dua Please post solution so that it will help some other developer.
thanks
Asked: 2012-09-17 13:23:09 +0800
Seen: 303 times
Last updated: Oct 08 '12