|
| I have developed a printing function in Struts Portlet that once 'Print' button is clicked by the user, an PDF file will be exported with Jasper Report, and the user can save or open it to print out.
Here is my code snippet:
/***********************/
HttpServletResponse httpServletResponse = (HttpServletResponse) portletResponse;
httpServletResponse.setContentType("application/pdf");
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
InputStream inputStream = this.getServlet().getServletConfig().getServletContext().getResourceAsStream("/rpt/Flights.jasper");
JRBeanCollectionDataSource beanDataSource = new JRBeanCollectionDataSource(filghts); //'flights' is a pre-defined arraylist contains all records to be printed
//Export PDF file
JasperRunManager.runReportToPdfStream(inputStream, outputStream, new HashMap(), beanDataSource);
outputStream.flush();
outputStream.close();
/***********************/
A PDF file can be exported when run that code, but when I try to open it, there is an error message 'The file is damaged'.
Therefore, I tried another method -Export the report to a FileOutputStream,
Replace the outputStream in the code with:
FileOutputStream outputStream = new FileOutputStream(new File("C:\\report.pdf"));
It works very well. I opened the PDFs exported with two different method with UltraEdit, and found out the difference: when using ServletOutputStream, HTML code of portlet was appended after 'EOF' of the PDF!
How can I privent that useless HTML to be appended? Can I configure the ServletOutputStream of portletresponse? All suggestions are appreciated.
|
|