使用java打印html文件而不向用户显示打印对话框 [英] Printing an html file using java without showing print dialog to the user

查看:87
本文介绍了使用java打印html文件而不向用户显示打印对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 html 文件直接打印到默认打印机,而不向用户显示打印对话框.

I'm trying to print an html file directly to the default printer without showing print dialog to the user.

我刚刚从一些在线教程中获得了以下代码,它适用于 PNG 文件.

I just got the below code from some online tutorials and it worked well for the PNG file.

import javax.print.*;
import javax.print.attribute.*;
import java.io.*;

public class Printing {
  public static void main(String args[]) throws Exception {
    String filename = args[0];
    PrintRequestAttributeSet pras = 
      new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
    PrintService printService[] = 
      PrintServiceLookup.lookupPrintServices(flavor, pras);
    PrintService defaultService = 
      PrintServiceLookup.lookupDefaultPrintService();
    PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
    if (service != null) {
      DocPrintJob job = service.createPrintJob();
      FileInputStream fis = new FileInputStream(filename);
      DocAttributeSet das = new HashDocAttributeSet();
      Doc doc = new SimpleDoc(fis, flavor, das);
      job.print(doc, pras);
      Thread.sleep(10000);
    }
    System.exit(0);
  }
}

我想将 DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG; 更改为 DocFlavor flavor = DocFlavor.INPUT_STREAM.<一些 html 格式>;

请建议在此处使用哪种格式合适?

Please suggest which format would be appropriate to use here ?

并请建议如何避免在运行此代码时弹出打印对话框.?

and please suggest how to avoid print dialog pop up while running this code. ?

提前致谢桑迪

推荐答案

PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//    PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null) {
     ... the rest code

您可以只使用默认服务而不是从对话框中显示

You can use just default service rather than showing from the dialog

这篇关于使用java打印html文件而不向用户显示打印对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆