我已经研究过在iframe中打印pdf的几个选项,但似乎都没有。
简单细节:
高级详情:
这是第一个jsp页面(调用print函数的运行)的一部分:
<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>
以下是第二个JSP页面(显示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>
我认为是什么问题: 我认为缓冲区消除了所有的HTML,只显示PDF。或者至少在IE中这样做。当我查看Firefox时,它嵌入了PDF文件。也许我无法获取iframe的内容,因为它不再是HTML。
到目前为止,这是我的资料来源:
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
答案 0 :(得分:0)
我最终做的是生成PDF然后使用iText我添加了一些在加载PDF时运行的打印JavaScript