This discussion is archived
6 Replies Latest reply: Feb 13, 2013 8:14 AM by Timo Hahn RSS

Getting error while opening pdf on server

Kanika Newbie
Currently Being Moderated
Dear All,

I am working on jdeveloper 11.1.1.4.0.
I have a use case where on click of link , generating a dynamic pdf. The pdf i am arranging with the help of itext through backing bean. It is generating as well downloading the pdf.I have used filedownloadlistner to call the generate and download pdf methods.
On integrated weblogic server the pdf is working fine, but when i deploy on server while opening pdf getting error :

*"Adobe Reader could not open 'test.pdf' beacause it is either not a supported file type*
*or because the file has been damaged (for example, it was sent as an email attachment and*

*wasn't correctly decoded)."*

I am not able to get the root cause for the problem. The sample code to generate and download pdf is :

// Generate PDF
private void generatePDFFile(FacesContext facesContext,
java.io.OutputStream outputStream) {
try {
System.out.println("In Generate PDF................");
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
addMetaData(document);
addTitlePage(document);
document.close();
System.out.println("End of PDF......................");
facesContext = facesContext.getCurrentInstance();
ServletContext context = (ServletContext)facesContext.getExternalContext().getContext();

System.out.println(context.getRealPath("/"));
String FILE = "test.pdf";
File file = new File(FILE);

FileInputStream fdownload;
//BufferedInputStream bis;
byte[] b;
System.out.println(file.getCanonicalPath());
System.out.println(file.getAbsolutePath());
fdownload = new FileInputStream(file);

int n;
while ((n = fdownload.available()) > 0) {

b = new byte[n];

int result = fdownload.read(b);

outputStream.write(b, 0, b.length);

if (result == -1)
break;
}
outputStream.flush();

} catch (Exception e) {

e.printStackTrace();

}


Download PDF

private void downloadPDF(FacesContext facesContext, java.io.OutputStream outputStream) {
try {
facesContext = facesContext.getCurrentInstance();
ServletContext context = (ServletContext)facesContext.getExternalContext().getContext();
ExternalContext ctx = facesContext.getExternalContext();
HttpServletResponse res = (HttpServletResponse)ctx.getResponse();
res.setContentType("application/pdf");
outputStream = res.getOutputStream();

System.out.println(context.getRealPath("/"));
File file = new File(FILE); // FILE = 'test.pdf'
FileInputStream fdownload;
// BufferedInputStream bis;
byte[] b;
fdownload = new FileInputStream(file);
//bis = new BufferedInputStream (new FileInputStream(file));
int n;

while ((n = fdownload.available()) > 0) {
b = new byte[n];
int result = fdownload.read(b);
//outputStream.write(b, 0, b.length);
outputStream.write(b, 0, b.length);
if (result == -1)
break;
}
outputStream.flush();
outputStream.close();
fdownload.close();
} catch (Exception e) {
e.printStackTrace();
}
}


}

Any help will be appreciated.

Thanks
Kanika

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points