0

PDFをダウンロードして印刷したい。ユーザーにダイアログ ボックスを表示せずに、これを完全にサイレント モードで実行したいと考えています。現在、自己署名アプレットを使用しています (デプロイ時に認証局を使用して署名します)。Linux と OS X では希望どおりに動作しますが、Windows では動作しません。Windows では、ファイル (タイプ .xps) を保存するためのダイアログ ボックスがユーザーに表示されます。ファイルが保存された後、プリンターにサイレントに送信されるため、現在、ユーザーにダイアログを表示せずにドキュメントをサイレントに保存する方法が問題です。以下のコード。私はApache PDFBoxを使用しています。PrivilegedAction を使用する必要があるかもしれないと読みましたが、問題はファイルをダウンロードできないことではなく、黙ってダウンロードできないことであるため、理由がわかりません。

/**
 * 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() });
            }
        }
    }
}
4

1 に答える 1

0

私は同じ問題を抱えていて、コメントで上記の答えを見つけました: 私たちの既定のプリンターは Microsoft XPS ドキュメント ライターに設定されていました。

誰かがコメントにざっと目を通し、答えがないと思う場合に備えて、ここで言及しています。

この回答の代わりに yms のコメントを 1up してください。

于 2013-07-18T00:50:39.860 に答える