2012-10-12 71 views
2

我已經研究了在iframe中打印pdf的幾種選擇,但似乎沒有任何工作。在jsp中打印iframe中的PDF內容

簡單的細節:

  1. 我從一個用戶的一些搜索參數。
  2. 我做一個數據庫搜索,然後使用Apachi FOP生成結果的PDF。
  3. 它們被引導到一個具有打印和取消按鈕的網頁。
  4. 當用戶點擊打印按鈕時,它將打開一個顯示PDF的窗口。
  5. 打印對話框將打開供用戶打印PDF。
  6. 將PDF文件從服務器中刪除。
  7. 的Windows關閉

高級詳細信息:

  • 這隻需要在IE8工作。
  • FOP集成不使用任何XSLT轉換。它只使用輸入的FOP XML的StringReader格式化爲字符串
  • 顯示PDF的窗口實際上是兩個JSP頁面。
  • 第一頁:
    • 具有與第二JSP頁面作爲源
    • 的iframe運行在負載的printPDF()函數,打印PDF在iframe
  • 第二頁:
  • 使用Java的BufferedOutputStream和ServletOutputStream的
    • 將輸出
    • 後刪除文件
    • 使用out = pageContent.pushBody();

這是第一個JSP頁面(即調用打印功能運行)的一部分:

<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

我願意一個onload事件打印事件,如果有人知道添加到FOP XML跑一些打印的JavaScript如何將JavaScript事件添加到FOP XML。 – milesacul

回答

0

最後我做什麼是生成PDF,然後利用iText我說當PDF加載