Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay PDF in browser with a servlet
    primarykey
    data
    text
    <p>I want to display a PDF file in browser. I have the path to the pdf in JS and I am making a call to grab the PDF as a servlet from java. Here's what I have so far:</p> <p>JavaScript:</p> <pre><code>RequestManager.getJSON(Config.server + "getPDF.json?pdfPath=" + this.pathToPdfFile, (function(data){ $("#" + this.divId).append('&lt;object id="' + this.pdfObjectId + '" data="' + data + '" type="application/pdf" width="600" height="800"&gt;&lt;/object&gt;'); ResizeManager.addResizeHandler(this.pdfObjectId, this.divId, -10, -10); }).bind(this)); </code></pre> <p>Java:</p> <pre><code>@RequestMapping("/getPDF") public void pdfPathToServlet(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pdfPath = request.getParameter("pdfPath"); if (pdfPath == null || pdfPath.equals("")) throw new ServletException("Invalid or non-existent file parameter in UrlServlet servlet."); if (pdfPath.indexOf(".pdf") == -1) pdfPath += ".pdf"; File pdf = new File(pdfPath); String pdfName = pdfPath.substring(pdfPath.lastIndexOf("/") + 1, pdfPath.length()); logger.debug(pdfName); ServletOutputStream stream = null; BufferedInputStream buf = null; try { stream = response.getOutputStream(); response.setContentType("application/pdf"); response.setHeader("Content-Disposition", "inline; filename='" + pdfName + "'"); FileInputStream input = new FileInputStream(pdf); response.setContentLength((int) pdf.length()); buf = new BufferedInputStream(input); int readBytes = 0; while ((readBytes = buf.read()) != -1) stream.write(readBytes); } catch (IOException ioe) { throw new ServletException(ioe.getMessage()); } finally { if (stream != null) stream.close(); if (buf != null) buf.close(); } } </code></pre> <p>My problem is that this is showing the <em>binary output</em> in my browser as text.</p> <p>I'm not sure what I am doing incorrectly. I have tried changing the header to be attachment instead of inline, but that showed the same thing. I believe I want inline though, as I wish to show it in browser and not download it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload