Can not load pdf in Chrome [message #1746699] |
Thu, 03 November 2016 11:59 |
David Song Messages: 217 Registered: April 2011 |
Senior Member |
|
|
hi,
In my RAP project, pdf file is created by iText dynamically as inputstream and displayed in browser. it can work well in IE with Adobe Reader, but Chrome gives errors as below:
rap-client.js:106 Resource interpreted as Document but transferred with MIME type application/pdf
the code snippet is:
InputStream pdfData = (InputStream) RWT.getUISession().getHttpSession().getAttribute(PrintDialog.PDF_KEY);
//HttpServletResponse resp = RWT.getResponse();
response.setContentType("application/pdf");
response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
//response.setHeader("Cache-Control", "max-age=0");
response.setHeader("Content-Disposition", "inline");
//response.setHeader("content-length", pdfData.);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int next = pdfData.read();
while (next > -1) {
bos.write(next);
next = pdfData.read();
}
bos.flush();
byte[] result = bos.toByteArray();
try {
ServletOutputStream out = response.getOutputStream();
out.write(result);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
ServiceManager manager = RWT.getServiceManager();
manager.registerServiceHandler(SERVICE_HANDLER, new PDFServiceHandler());
browser.setUrl(manager.getServiceHandlerUrl(SERVICE_HANDLER)+"&random="+RandomUtils.nextLong()+"#toolbar=1&view=FitH,top");
Thanks
|
|
|
|
|
Re: Can not load pdf in Chrome [message #1755640 is a reply to message #1755197] |
Mon, 06 March 2017 13:24 |
|
Hi David,
I am using RAP (3.0) for displaying PDFs in the browser widget as you do, but in my case it works both in IE and in Chrome.
Here is my code:
(I keep a reference to the file absolute path to be shown = completeFilePath)
File file = new File(completeFilePath);
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream fileOutputStream = response.getOutputStream();
int bufferSize = 512 * 1024;
byte[] buf = new byte[bufferSize];
int len;
while ((len = fileInputStream.read(buf)) > 0) {
fileOutputStream.write(buf, 0, len);
}
fileInputStream.close();
fileOutputStream.close();
as you can see I do not use neither
response.setContentType()
nor
but it works, at least with IE 11 and Chrome 56, on Windows 10.
Can you do a test with exactly this approach and see what happens?
|
|
|
Re: Can not load pdf in Chrome [message #1756094 is a reply to message #1755640] |
Sat, 11 March 2017 03:54 |
David Song Messages: 217 Registered: April 2011 |
Senior Member |
|
|
Hi Vincenzo,
Thanks for your reply.
When application reads the server-side fixed entity file as pdf , it can be displayed normally in Chrome. But Chrome visit will be reported org.eclipse.jetty.io.EofException exception in eclipse rap console window, Firefox works well and no exception. But changed to read the dynamically generated stream will be wrong, the pdf can not shown, while Google Chrome console error: rap-client.js: 106 copy of the file but transferred with MIME type application / pdf, but Firefox is still working properly.
Regards,
David
Vincenzo Caselli wrote on Mon, 06 March 2017 08:24Hi David,
I am using RAP (3.0) for displaying PDFs in the browser widget as you do, but in my case it works both in IE and in Chrome.
Here is my code:
(I keep a reference to the file absolute path to be shown = completeFilePath)
File file = new File(completeFilePath);
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream fileOutputStream = response.getOutputStream();
int bufferSize = 512 * 1024;
byte[] buf = new byte[bufferSize];
int len;
while ((len = fileInputStream.read(buf)) > 0) {
fileOutputStream.write(buf, 0, len);
}
fileInputStream.close();
fileOutputStream.close();
as you can see I do not use neither
response.setContentType()
nor
but it works, at least with IE 11 and Chrome 56, on Windows 10.
Can you do a test with exactly this approach and see what happens?
|
|
|
|
Powered by
FUDForum. Page generated in 0.03961 seconds