Reputation: 643
I want to display pdf file in webpage using iframe with spring framework. Instead of download options i have to show pdf file in my webpage. Pls anyone can help me.
Thanks in advance..
My java code:
Employee employee = employeeManager.getObjectOrNull(
Employee .class, id);
File file = new File(dir,
employee .getFileName());
org.apache.commons.io.FileUtils.writeByteArrayToFile(file,
employee .getfile());
if (employee != null) {
ControllerUtils.openFile(dir,
response, employee.getFileName());
String contentType = Utils.getContentType(employee
.getFileName());
response.setContentType(contentType);
response.setHeader("Cache-Control", "private, max-age=5");
Upvotes: 0
Views: 4559
Reputation: 643
I've got the solution. I used embed
tag. My HTML code:
<embed width="900px" height="500px" name="plugin" src="/content/downloadPdf" type="application/pdf">
My java code
response.setContentType("application/pdf");
response.setHeader("Cache-Control", "private, max-age=5");
response.setHeader("Pragma", "");
if (file.length>0) {
response.setContentLength(file.length);
}
ServletOutputStream ouputStream = response.getOutputStream();
InputStream is = new ByteArrayInputStream(file);
try {
int b;
while ((b = is.read()) != -1) {
ouputStream.write(b);
}
} finally {
ouputStream.flush();
ouputStream.close();
is.close();
}
Upvotes: 2