消息关闭
    暂无新消息!
用户需要实现页面表单打印,但是不想用浏览器打印,只有后台实现传入数据调用打印机打印,
但是打印机有权限设置,有部门管理认证。
从网上找的打印例子,查了 API自己写了认证,java后台不报错,但是打印机作业信息里报错管理部门错误,还请大神帮忙看看什么问题。谢谢!
代码如下:
public void printer(HttpServletRequest request,String stringHtml,
            HttpServletResponse response, HttpSession session) throws Exception {
      File file2 = new File("D:\\test.html");
      file2.createNewFile();
      PrintStream printStream = new PrintStream(new FileOutputStream(file2));  
       printStream.println(stringHtml.toString());
       System.out.println("=============="+stringHtml.toString());
    
         String  path = "D:/test.html";
         File f = new File(path);
            //File[] fies=file.listFiles();
               //for(File f:fies){
               System.out.println("file "+f.getName());
                    String fileExt=f.getName().substring(f.getName().indexOf(".")+1,f.getName().length());
                     if("html".equalsIgnoreCase(fileExt)){
                       String filepath=path;
                       File pdfFile=new File(filepath);
                       //构建打印请求属性集
                         PrintRequestAttribute pb = new RequestingUserName("1024", null);
                         PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(pb);             
                       //设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
                       DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
                       //查找所有的可用打印服务
//                       PrintService printService[] = PrintServiceLookup.llookupPrintServices(flavor, pras);
                       //定位默认的打印服务
                       PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
                       PrintJobAttribute pjb = new JobOriginatingUserName("1024",null);
                       PrintJobAttributeSet ss = new HashPrintJobAttributeSet(pjb);
                        //显示打印对话框  
//                     PrintService service = ServiceUI.printDialog(null, 200, 200, printService,   defaultService, flavor, pras);
                       if(defaultService!=null){
                        try {    
                           DocPrintJob job = defaultService.createPrintJob(); //doc模式创建打印作业                          
                           job.addPrintJobAttributeListener(null, ss);
                           FileInputStream fis = new FileInputStream(pdfFile); //构造待打印的文件流
                           DocAttributeSet das = new HashDocAttributeSet();
//                           das.add(pjt);
                           Doc doc = new SimpleDoc(fis, flavor, das); //建立打印文件格式
                           job.print(doc, pras); //进行文件的打印
                           Thread.sleep(10000);
                           System.out.println("==============已执行完毕");
                        } catch (FileNotFoundException fe) {
                        fe.printStackTrace();
                        } catch (PrintException e) {
                          e.printStackTrace();
                       }
                       f.delete();
               }
           }

1个回答