Warm tip: This article is reproduced from serverfault.com, please click

How to open pdf file in browser

发布于 2020-12-03 15:16:29

I'm trying to open a pdf file in which has been exported from a repository. Here is the code that I'm using:

ConnectionManager con = new ConnectionManager();
String id = request.getParameter("uname"); 
String objname = request.getParameter("pass");
Properties prop = new Properties();
//ResourceBundle resource = ResourceBundle.getBundle("query");
//prop.load(getClass().getResourceAsStream("query.properties"));
String uname = "DmAdmin";
String pass = "<pass>";
String docbase = "QDocs";
String ext = new String();
IDfSession ssn = con.getSession(uname, pass, docbase);
sysObj = (IDfSysObject)ssn.getObject((IDfId)new DfId(id));
//ByteArrayInputStream buf = sysObj.getContent();
//sysObj.getFile("C:\\Users\\rsaha04\\Downloads\\"+objname+".pdf");
String path = "C:\\Users\\rsaha04\\Downloads\\";
String filename = path + sysObj.getObjectName().toString();

IDfCollection coll = sysObj.getRenditions(null);
if (coll != null)
{
    while (coll.next())
    {
        String format = coll.getString("full_format");
        {
            if (format.equalsIgnoreCase("pdf"))
            {
                ext = "pdf";
                System.out.println("extension set: "+ext);
            }
        }
    }
    
    filename = filename+"."+ext;
    sysObj.getFileEx(filename, ext, 0, false);
}
con.closeConnection(ssn);
//Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+filename);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename='"+filename+"'");

I'm able to open the pdf file in adobe acrobat reader but it is failing for browser with this error.enter image description here

Please help me understand where I'm going wrong here.

Questioner
Zeus07
Viewed
0
f1sh 2020-12-04 01:24:42

You need your server to respond with a pdf file. You set the response headers, but your code never writes the pdf data into the response.

Do that using

response.write(bytesFromPdfFile)