简体   繁体   English

在Windows上从Java Applet静默下载和打印PDF文档

[英]Silently download and print PDF document from Java Applet on Windows

I want to download a PDF and print it. 我想下载PDF并打印。 I want to do this completely silently with no dialogue boxes displayed to the user. 我想完全无声地执行此操作,而不会向用户显示任何对话框。 I am currently using a self-signed applet (will sign using an authority when we deploy). 我当前使用的是自签名小程序(部署时将使用授权进行签名)。 It works as desired on Linux and OS X, but not Windows. 它可以在Linux和OS X上运行,但不能在Windows上运行。 On Windows it shows the user a dialogue box to save the file (of type .xps). 在Windows上,它向用户显示一个对话框来保存文件(.xps类型)。 After the file saves it is sent silently to the printer, so my issue is currently how to get the document to save silently without the user seeing the dialogue. 文件保存后,它会以静默方式发送到打印机,因此,我的问题是当前如何使文档以静默方式保存而无需用户看到对话框。 Code below. 下面的代码。 I'm using Apache PDFBox. 我正在使用Apache PDFBox。 I've read I may need to use PrivilegedAction, but I don't see why as the issue isn't that I can't download a file, but that I can't do so silently. 我已经读过我可能需要使用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() });
            }
        }
    }
}

I had the same problem, and found the answer above in the comments: Our default printer was set to Microsoft XPS document writer. 我遇到了同样的问题,并在注释中找到了上面的答案:我们的默认打印机设置为Microsoft XPS文档编写器。

I am mentioning it here incase anyone glosses over the comments and thinks there is no answer. 我在这里提到它是为了防止有人掩饰评论并认为没有答案。

Please 1up yms's comment instead of this answer. 请1up yms的评论代替这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM