2016-11-01 115 views
-1

对不起,我的英文很差,但我真的想在浏览器的不同选项卡上同时显示来自jasper报告的两个pdf报告。我正在使用java jsf,primefaces。主要想法是点击按钮时,在不同的选项卡中显示此报告。我试着这样做:如何在java的不同选项卡中显示两个pdf

我有这个在Managed Bean:

 public void showReports() { 
        RequestContext.getCurrentInstance().execute("document.getElementById('fromGeneral:rep2').click();"); 
      RequestContext.getCurrentInstance().execute("document.getElementById('fromGeneral:rep3').click();"); 
      } 

    public void printReport(String name) { 
      try { 
       Map<String, Object> mapParametros = new HashMap<>(); 
       mapParametros.put("CORR", corr); 
       printJasper(mapParametros, new File("/Jasper/Reports/" + name)); 
      } catch (Exception e) { 
       System.out.println(e.getMessage()); 
      } 
     } 

    public void printJasper(Map<String, Object> reportValues, File fileJ) { 

     ByteArrayInputStream input = null; 
     BufferedOutputStream output = null; 
     FacesContext facesContext = FacesContext.getCurrentInstance(); 
     ExternalContext externalContext = facesContext.getExternalContext(); 
     HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); 

     try { 
      facesContext = FacesContext.getCurrentInstance(); 
      externalContext = facesContext.getExternalContext(); 
      response = (HttpServletResponse) externalContext.getResponse(); 

      FileInputStream file = new FileInputStream(fileJ); 
      JasperReport compiledTemplate = (JasperReport) JRLoader.loadObject(file); 

      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      JasperPrint jasperPrint = JasperFillManager.fillReport(compiledTemplate, reportValues, dataSourceP.getConnection()); 

      JRExporter exporter = new JRPdfExporter(); 
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out); 
      exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();"); 
      exporter.exportReport(); 

      input = new ByteArrayInputStream(out.toByteArray()); 

      response.reset(); 
      response.setHeader("Content-Type", "application/pdf"); 
      response.setHeader("Content-Length", String.valueOf(out.toByteArray().length)); 
      response.setHeader("Content-Disposition", "inline; filename=\"ticket.pdf\""); 
      output = new BufferedOutputStream(response.getOutputStream(), Constants.DEFAULT_BUFFER_SIZE); 

      byte[] buffer = new byte[Constants.DEFAULT_BUFFER_SIZE]; 
      int length; 
      while ((length = input.read(buffer)) > 0) { 
       output.write(buffer, 0, length); 
      } 
      output.flush(); 

     } catch (Exception exception) { 
      System.out.println(exception.getMessage()); 
     } finally { 
      try { 
       if (output != null) { 
        output.close(); 
       } 
       if (input != null) { 
        input.close(); 
       } 
      } catch (Exception exception) { 
       /* ... */ 
      } 
     } 
     facesContext.responseComplete(); 
    } 

的这在我的观点:

<h:form>   
    <p:commandButton value="Show them" action="#{reportBean.showReports()}"/> 
    <p:commandButton value="REPORT 1" id="rep1" style="font-size: 25px; float:right;visibility: hidden;" action="#{reportBean.printReport("Report1")}" ajax="false" onclick="this.form.target = '_blank';"/> 
    <p:commandButton value="REPORT 2" id="rep2" style="font-size: 25px; float:right;visibility: hidden;" action="#{reportBean.printReport("Report2")}" ajax="false" onclick="this.form.target = '_blank';"/> 
    </h:form> 

但doesn't的工作,它只是显示的第二次报告。 帮助! 谢谢!

回答

0

最后我找到了解决它的其他方法。

方法showReports()改为点击两个按钮,这个打开两个xhtml,每个里面都有一个带自动运行功能的<h.form>远程命令,显示报告。我不知道这是否是最好的方法,但它的工作原理。

感谢所有的意见

0

尝试点击使用p:commandLink就像我正在使用。

<p:commandLink id="PreviewR1" value="Print Preview" ajax="false" action="#{reportBean.printReport("Report1")}" target="_blank" /> 
<p:commandLink id="PreviewR2" value="Print Preview" ajax="false" action="#{reportBean.printReport("Report2")}" target="_blank" /> 

这将在单独的浏览器选项卡中打开报表1 & 2,而原来的网页将保持不变。

+0

你好Sarz!感谢您的评论。但它不起作用,它仍然显示一个报告。 :/ –

+0

尝试使用不同的方法,即printReport1 printReport2,而不是采用参数。 – Sarz

+0

我也是这样做的,但它仍然不起作用,但我发布了我解决的问题。感谢您的时间回答。 –

0

你只是不点击正确的按钮:

​​

你点击REP2和REP3。 rep3不存在,您需要点击rep1代替。这应该是为什么只显示第二份报告的原因。

+0

谢谢lastresort回答,我的错!但我已经修好了,仍然没有工作。 –

相关问题