Reputation: 21
We have a web app that displays a PDF in an iframe. When using the Chrome PDF viewer's built-in download it seems to be downloading a cached PDF, not the currently displayed PDF. If we use the print option however, and then choose print to PDF, it downloads the proper PDF file. After searching around I've come across a few odd solutions involving HTTP headers, but nothing so far has fixed the issue. Additionally, if I open the url for the iframe content as its own tab and then download, it downloads the correct PDF file.
Upvotes: 2
Views: 3223
Reputation: 3610
Indeed, this happens at least in Chrome and Edge browsers.
It happens because the pdf file that you pass to the viewer (iframe or PDFObject) is always cached internally with the same name. You can solve it add a random number or date and time as the name of the pdf.
I give you an example of how I solved it:
xhtml file:
<iframe type="application/pdf" id="idReport" src="#{yourController.serverAndContextPath}/PDFServlet" width="100%" height="500"></iframe>
or
<object type="application/pdf" id="idReport" data="#{yourController.serverAndContextPath}/PDFServlet" width="100%" height="500px"/>
Servlet class:
public class PDFServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Date date = new Date();
DateFormat hourdateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-Disposition", "inline; filename=\"" + "pdfFileName" + hourdateFormat.format(date) + ".pdf" + "\"");
}
}
Upvotes: 1