2015-11-05 34 views
0

我需要將pdf碧玉直接發送到打印機,當前代碼PDF委託給瀏覽器,因此用戶可以根據需要打印任意數量的副本。必須允許只打印一份,所以我想我會直接發送到打印。 我搜索了論壇,但不明白什麼是最好的解決方案。將pdf碧玉直接發送到網絡應用程序的打印機

我的代碼看看:

public class UtilRelatorios { 

public static void imprimeRelatorio(String relatorioNome, 
     HashMap parametros) throws IOException, JRException { 
     FacesContext fc = FacesContext.getCurrentInstance(); 
     ServletContext context = (ServletContext) fc.getExternalContext().getContext(); 
     HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); 
     JasperPrint jasperPrint = 
       JasperFillManager.fillReport(
         context.getRealPath("/relatorios")+ File.separator+relatorioNome+".jasper", 
         parametros);  
     //int finalPag = jasperPrint.getPages().size(); 
     //System.out.println("page: "+finalPag); 
     //JasperPrintManager.printPage(jasperPrint,finalPag,false); 
     byte[] b = null; 
     //JasperPrintManager.printPage(jasperPrint, 0, false); 

     try { 
      b = JasperExportManager.exportReportToPdf(jasperPrint); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
     }  

      if (b != null && b.length > 0) { 
       // Envia o relatório em formato PDF para o browser 
       response.setContentType("application/pdf"); 
       int codigo = (int) (Math.random()*1000); 
       response.setHeader("Content-disposition","inline);filename=relatorio_"+codigo+".pdf"); 
       response.setContentLength(b.length); 
       ServletOutputStream ouputStream = response.getOutputStream(); 
       ouputStream.write(b, 0, b.length); 
       ouputStream.flush(); 
       ouputStream.close(); 
      } 
} 

}

回答

0

如果在問題似乎你喜歡通過Web應用程序browser發送報告直接到用戶的打印機。

做!你不能控制網絡用戶從瀏覽器直接打印(不包括使用ActiveX或其他家庭做插件)

也許這是因爲否則運氣而互聯網上瀏覽你就必須打印的人很多打印機上的廣告....

相反,如果你想將它傳送給連接到服務器打印機,這是可以做到!

如果它的服務器打印機請讓我知道,我可以傳給你一些代碼。

+0

我想直接發送到客戶機上的默認打印機。 既然我無法通過瀏覽器控制它,不以爲然: *用戶點擊打印 *除非PDF到一個臨時文件夾 *發送到默認打印機,然後排除目錄文件 你能不能幫我這個? – csf

+0

只有這樣才能確保用戶只打印一次會是你開發某種activeX或其他插件... java web start ... –

+0

我開發了一個打印報告的小程序,但我無法在瀏覽器中運行給出不支持的錯誤插件,你可以幫我解決這個問題http://stackoverflow.com/questions/33735509/applet-running-in-chrome – csf

0

如果客戶端&服務器PC位於同一網絡(即LAN)上,則可以在服務器上共享客戶端的打印機,然後發送報告給它,就像發送到本地安裝的打印機一樣。

+0

但是這根本無助於OP –

相關問題