2012-07-23 55 views
0

我想下載並打印PDF。我想完全靜靜地做到這一點,沒有對話框顯示給用戶。我目前正在使用一個自簽名的applet(在我們部署時將使用權限進行簽名)。它在Linux和OS X上按照需要運行,但不是Windows。在Windows上,它向用戶顯示一個對話框來保存文件(類型爲.xps)。文件保存後,它會悄悄地發送到打印機,所以我的問題是當前如何讓文檔在沒有用戶看到對話的情況下自動保存。下面的代碼。我正在使用Apache PDFBox。我讀過我可能需要使用PrivilegedAction,但我不明白爲什麼,因爲問題不在於我無法下載文件,但我無法默默地這樣做。在Windows上悄悄下載並從Java Applet打印PDF文檔

/** 
* Print Applet 
* Author: kareem 
* Date: 12-02-20 
* Time: 10:57 AM 
*/ 

import java.applet.Applet; 
import java.awt.print.PrinterException; 
import java.io.*; 
import java.net.*; 
import org.apache.pdfbox.pdmodel.PDDocument; 
import netscape.javascript.*; 

public class PrintApplet extends Applet { 
    public void init() { 
     String callbackURL = this.getParameter("callbackurl"); 
     String fileToPrint = this.getParameter("file"); 
     String successCallback = this.getParameter("successcallback"); 
     String failureCallback = this.getParameter("failurecallback"); 
     String published = this.getParameter("published"); 
     JSObject window = JSObject.getWindow(this); 
     if (fileToPrint == null || callbackURL == null) { 
      window.call(failureCallback, new String[] { "Missing required parameters." }); 
     } else { 
      try { 
       URL printURL; 
       printURL = new URL(fileToPrint); 
       PDDocument doc; 
       doc = PDDocument.load(printURL); 
       doc.silentPrint(); 
       try { 
        URL updateOrderUrl = new URL(callbackURL); 
        HttpURLConnection updateOrderHTTP = (HttpURLConnection) updateOrderUrl.openConnection(); 
        updateOrderHTTP.setDoOutput(true); 
        updateOrderHTTP.setDoInput(true); 
        updateOrderHTTP.setRequestMethod("PUT"); 
        String updateOrderData = URLEncoder.encode("printed", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(System.currentTimeMillis()/1000) + "&" + URLEncoder.encode("published", "UTF-8") + "=" + URLEncoder.encode(published, "UTF-8"), "UTF-8"); 
        OutputStreamWriter out = new OutputStreamWriter(updateOrderHTTP.getOutputStream()); 
        out.write(updateOrderData); 
        updateOrderHTTP.getInputStream(); 
        out.close(); 
        window.call(successCallback, null); 
       } catch(Exception e) { 
        window.call(failureCallback, new String[] { "Server callback failed." }); 
       } 
      } catch (MalformedURLException e) { 
       System.out.println("Malformed URL Exception: " + e.getMessage()); 
       window.call(failureCallback, new String[] { "Invalid print file URL." }); 
      } catch (IOException e) { 
       System.out.println("IO Exception: " + e.getMessage()); 
       window.call(failureCallback, new String[] { "Print file could not be loaded." }); 
      } catch(PrinterException e) { 
       System.out.println("Printer exception: " + e.getMessage()); 
       window.call(failureCallback, new String[] { e.getMessage() }); 
      } 
     } 
    } 
} 
+0

聽起來很粗略... – javajavajava 2012-07-23 15:22:02

+0

爲什麼?我們正在打印容易受到欺詐的文件,而且我們的客戶的條件是隻允許打印一次。所以我們不能向用戶顯示文件或給他們一個允許他們改變份數的對話框。 – Kareem 2012-07-23 15:24:08

+2

@Kareem即使您沒有顯示對話框,我也可以始終打印到PDF文件,或配置打印機驅動程序將輸出發送到文件。沒有辦法確保用戶只打印一次文檔,並且我個人會避免使用這樣的系統,因爲即使在最可靠的打印機中,卡紙總是會發生。 – yms 2012-07-23 15:55:59

回答

0

我有同樣的問題,並在評論中找到上述答案:我們的默認打印機設置爲Microsoft XPS文檔編寫器。

我在這裏提到它會引起任何人對評論的掩蓋,並認爲沒有答案。

請1up的yms的評論,而不是此答案。