My requirement is to generate PDF file using iText, I use below code to create a sample PDF
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 is not using any inputStream parameter, rather it is writing directly to response’s outputstream. Whereas struts-2 is mandating us to use InputStream parameter (see the 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 should have getters and setters for inputStream and i have that too in the class mentioned in struts-configuration
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
But since iText doesn’t really need inputstream rather it is writing directly to response’s outputstream, i get exceptions since am not setting anything for the inputStream parameter.
Please let me know how to use iText code in struts-2 having the resultType as stream
Thanks
Found solution to this.
The method in the action which performs this PDF export can be void. The result type configuration is not needed while we are writing directly to response’s outputstream
for example, have your action class this way
and have your struts-configuration this way
this works cool !!!
Thanks for all who attempted to answer this question