Getting the response as PDF in browser
I want to setup response for PDF output. How do I achieve this?
I can setup for Excel output and successfully get the desired excel sheet from the browser, but for PDF I could not get the desired output.
For Excel the following code works fine
String excelContent = "an html table..";
getServletResponse().setContentType("“application/vnd.ms-excel");
getServletResponse().setHeader("Content-Disposition","inline; filename=" + pageTitle + ".xls");
PrintWriter ps = getServletResponse().getWriter();
ps.println(excelContent);
But for PDF I tried setting the content type to PDF, but could not get it properly (no content gets displayed even though a PDF file is opened in the browser)
String excelContent = "an html table..";
getServletResponse().setContentType("“application/pdf");
getServletResponse().setHeader("Content-Disposition","inline; filename=" + pageTitle + ".pdf");
PrintWriter ps = getServletResponse().getWriter();
ps.println(excelContent);
Do html tables cannot be displayed as such in PDF?
It's very simple with the Flying Saucer renderer. It takes HTML input and returns PDF. What you have to do is to declare content type as PDF, generate HTML to a string, and call a method from Flying Saucer.
Here is an example xhtml to pdf servlet with flyingsaucer
You are writing html context to the response stream. You need to write pdf format to the response.
You can use libraries like iText or Apache FOP to create PDF format.
链接地址: http://www.djcxy.com/p/46876.html上一篇: 在上传对象时键入很重要
下一篇: 在浏览器中以PDF形式获取响应