Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display PDF file in browser

Tags:

java

servlets

In my servlet I am using the code below to open a PDF file in a browser, but instead, it shows a download dialog box.

What I am doing wrong?

response.setContentType("application/pdf");
out = response.getWriter();
String filepath = "D:/MyFolder/PDF/MyFile.pdf";

response.setHeader("Content-Disposition", "inline; filename=" + filepath + ";");
FileOutputStream fileOut = new FileOutputStream("D:/MyFolder/PDF/MyFile.pdf");

fileOut.close();
out.close();
like image 586
vineeth Avatar asked Sep 09 '15 10:09

vineeth


People also ask

How do I get a PDF to open in browser instead of downloading?

At the top right, click More Settings. At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” (Uncheck this box if you want PDFs to open automatically when you click them.)


1 Answers

As you have to set the response type with the following configuration:-

File outPutFile=new File(generatedFile);
stream = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
response.setContentLength((int) outPutFile.length());
like image 169
SaviNuclear Avatar answered Oct 12 '22 10:10

SaviNuclear