如何在java中的不同选项卡中显示两个pdf

时间:2016-11-01 23:22:46

标签: jsf primefaces

很抱歉我的英语很差,但我真的想在浏览器的不同标签中同时显示来自jasper报告的两份pdf报告。我正在使用java jsf,primefaces。主要思想是单击按钮时显示此报告在不同的选项卡中。我试着这样做:

我在Managed Bean中有这个:

<ul class="pagination">
  {% if resources.has_previous %}
    <li><a href="?page={{ resources.previous_page_number }}"><i class="fa fa-angle-left"></i></a></li>
  {% endif %}
  {% for page_num in resources.paginator.page_range %}
    <li {% if page_num == resources.number %}class="active"{% endif %}><a href="?page={{ page_num }}">{{ page_num }}</a></li>
  {% endfor %}
  {% if resources.has_next %}
    <li><a href="?page={{ resources.next_page_number }}"><i class="fa fa-angle-right"></i></a></li>
  {% endif %}
</ul>

我认为这是:

        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();
    }

但不起作用,它只显示第二份报告。 救命!。 谢谢!

3 个答案:

答案 0 :(得分: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&amp; 2在单独的浏览器选项卡中,而原始网页将保持不变。

答案 1 :(得分:0)

您只是没有点击正确的按钮:

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

您点击了rep2和rep3。 rep3不存在,您需要单击rep1。这应该是仅显示第二份报告的原因。

答案 2 :(得分:0)

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

方法showReports()代替点击两个按钮,这两个按钮打开两个xhtml,每个按钮都在<h.form>内,一个带有autorun true的远程命令,显示报告。我不知道这是否是最好的方法,但它确实有效。

感谢您的所有意见