I have researched several options for printing a pdf inside an iframe and none seem to be working.
Simple Details:
Advanced Details:
Here is part of the first jsp page (the run that calls the print function):
<body onload='printPDF()'>
<table>
<tr>
<td class="content">
<%
// get myfilename from the myfile parameter on the URL
String myfile = request.getParameter("myfile");
out.print("<iframe src='fc_view_letter.jsp?myfile="+ myfile + "' id='pdfFrame'></iframe>");
%>
</td>
</tr>
</table>
<script>
function printPDF()
{
var id = 'pdfFrame';
var iframe = document.frames ? document.frames[0] : document.getElementById(id);
var ifWin = iframe.contentWindow || iframe;
ifWin.focus();
ifWin.printPage();
//ifWin.print();
}
</script>
</body>
Here is most of the second JSP Page (the one that shows the pdf):
<%@ page session="false" %>
<%@ page import="java.io.*" %>
<%@ page import="java.net.URLDecoder" %>
<html>
<head>
</head>
<body>
<%
String myfile = request.getParameter("myfile");
String myfiledecoded = "";
myfiledecoded = URLDecoder.decode(myfile, "UTF8");
String myfilename = myfiledecoded;
String extension;
int dotPos = myfilename.lastIndexOf(".")+1;
extension = myfilename.substring(dotPos);
int slashPos = myfilename.lastIndexOf("/")+1;
String secondparam = "filename=" + myfiledecoded.substring(slashPos);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", secondparam);
try {
ServletOutputStream sout = response.getOutputStream();
response.setHeader("Content-Disposition", secondparam);
File file = new File(myfilename);
FileInputStream fstream = new FileInputStream(file);
BufferedInputStream bis = null;
bis = new BufferedInputStream(fstream);
BufferedOutputStream bos = null;
bos = new BufferedOutputStream(sout);
byte[] buff = new byte[1024];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
sout.flush();
sout.close();
//file.delete();
}
catch (Exception e) {
System.out.println("Exception Occured...................." );
}
out.clear();
out = pageContext.pushBody();
%>
</body>
</html>
What I think is the issue: I'm thinking that the buffer eliminates all of the html and only displays the PDF. Or at least it does that in IE. When I looked in Firefox it embedded the PDF file. Maybe I cannot grab the contents of the iframe because it is no longer HTML.
Here are my sources so far:
Javascript Print iframe contents only
How to open print dialog after pdf generated?
http://www.ehow.com/how_7352227_use-javascript-print-pdf.html
http://www.webmasterworld.com/forum91/4086.htm
how to print pdf inside embed/iframe using javascript
Printing contents of a dynamically created iframe from parent window
What I ended up doing was generating the PDF then using iText I added some print JavaScript that ran when the PDF loaded