My requirement is to generate a PDF file using iText, I use the code below to create a PDF sample
Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); document.open(); document.add(new Paragraph("success PDF FROM STRUTS")); document.close(); ServletOutputStream outputStream = response.getOutputStream() ; baos.writeTo(outputStream); response.setHeader("Content-Disposition", "attachment; filename=\"stuReport.pdf\""); response.setContentType("application/pdf"); outputStream.flush(); outputStream.close();
If you see in the above code, iText does not use any inputStream parameter, rather, it writes directly to the response output stream. While struts-2 gives us the ability to use the InputStream parameter (see Configuration below)
<action name="exportReport" class="com.export.ExportReportAction"> <result name="pdf" type="stream"> <param name="inputName">inputStream</param> <param name="contentType">application/pdf</param> <param name="contentDisposition">attachment;filename="sample.pdf"</param> <param name="bufferSize">1024</param> </result> </action>
I know that my class must have getters and seters for inputStream, and I have this too in the class specified in struts-configuration
private InputStream inputStream; public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; }
But since iText really does not need an input stream, but writes directly to the response output stream, I get exceptions because I do not set anything for the inputStream parameter.
Please let me know how to use iText code in struts-2 having resultType as stream
thanks
source share