Struts2 Display pdf file in jsp
itext, java, struts2
Solution
This is how I am doing it. You can call this action inside an iframe or in a regular jsp
public class GeneratePdf extends ActionSupport{
private InputStream inputStream;
public String execute(){
HttpServletResponse response = ServletActionContext.getResponse();
Document document = new Document();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
PdfWriter.getInstance(document, buffer);
document.open();
// do your thing
document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
byte[] bytes = null;
bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
if(bytes!=null){
inputStream = new ByteArrayInputStream ( bytes );
}
return SUCCESS;
}
public InputStream getInputStream() {
return inputStream;
}
}
In your struts.xml
<action name="GeneratePdf" class="com.xxx.action.GeneratePdf">
<result name="success" type="stream">
<param name="contentType">application/pdf</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">filename="test.pdf"</param>
<param name="bufferSize">1024</param>
</result>
</action>
Problem
My requirement is to create a dynamic report pdf file with some data from database which I'm doing it using iText. Now, I want to display this pdf file inline in the webpage alongwith menu,header, footer, etc. So, If the user has some pdf viewer then this pdf should be displayed in user machine with print option to print that pdf.