Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 提供PDF下载后自动打开打印机对话框_Jsf_Pdf_Primefaces_Printing_Jasper Reports - Fatal编程技术网

Jsf 提供PDF下载后自动打开打印机对话框

Jsf 提供PDF下载后自动打开打印机对话框,jsf,pdf,primefaces,printing,jasper-reports,Jsf,Pdf,Primefaces,Printing,Jasper Reports,我目前正在浏览器的新选项卡中打开pdf文件,但我需要知道如何在按下命令按钮后打开打印机对话框以打印pdf jasper报告 这是在新选项卡中打开pdf的方法: public void printJasper() { JasperReport compiledTemplate = null; JRExporter exporter = null; ByteArrayOutputStream out = null; ByteArrayInputStream inpu

我目前正在浏览器的新选项卡中打开pdf文件,但我需要知道如何在按下命令按钮后打开打印机对话框以打印pdf jasper报告

这是在新选项卡中打开pdf的方法:

public void printJasper() {

    JasperReport compiledTemplate = null;
    JRExporter exporter = null;
    ByteArrayOutputStream out = null;
    ByteArrayInputStream input = null;
    BufferedOutputStream output = null;

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

    try {

        List<String> sampleList = new ArrayList<String>();
        sampleList.add("Fist sample string");
        sampleList.add("Second sample string");

        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(sampleList);
        Map<String, Object> reportValues = new HashMap<String, Object>();
        reportValues.put("anyTestValue", "test value");

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

        FileInputStream file = new FileInputStream("/any_dir/sample.jasper");
        compiledTemplate = (JasperReport) JRLoader.loadObject(file);

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

        exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
        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=\"fileName.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) {
        /* ... */
    } finally {
        try {
            if (output != null) {
                output.close();
            }
            if (input != null) {
                input.close();
            }
        } catch (Exception exception) {
            /* ... */
        }
    }
    facesContext.responseComplete();
}
<p:commandButton action="#{sampleBB.printJasper}"
    ajax="false" onclick="this.form.target='_blank'"
    value="#{msg['generate.report']}" />
public void printJasper(){
JasperReport compiledTemplate=null;
jreporter=null;
ByteArrayOutputStream out=null;
ByteArrayInputStream输入=null;
BufferedOutputStream输出=null;
FacesContext FacesContext=FacesContext.getCurrentInstance();
ExternalContext=facesContext.getExternalContext();
HttpServletResponse=(HttpServletResponse)externalContext.getResponse();
试一试{
List sampleList=new ArrayList();
添加(“第一个样本字符串”);
添加(“第二个样本字符串”);
JRBeanCollectionDataSource beanCollectionDataSource=新的JRBeanCollectionDataSource(sampleList);
Map reportValues=newhashmap();
reportValues.put(“anyTestValue”、“testvalue”);
facesContext=facesContext.getCurrentInstance();
externalContext=facesContext.getExternalContext();
response=(HttpServletResponse)externalContext.getResponse();
FileInputStream file=newfileinputstream(“/any_dir/sample.jasper”);
compiledTemplate=(JasperReport)JRLoader.loadObject(文件);
out=新的ByteArrayOutputStream();
JasperPrint JasperPrint=JasperFillManager.fillReport(compiledTemplate、reportValues、beanCollectionDataSource);
exporter=新的JRPdfExporter();
exporter.setParameter(jreporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_流,out);
出口商。出口报告();
input=newbytearrayinputstream(out.toByteArray());
response.reset();
setHeader(“内容类型”、“应用程序/pdf”);
setHeader(“内容长度”,String.valueOf(out.toByteArray().Length));
setHeader(“内容处置”、“内联;文件名=\”filename.pdf\”);
输出=新的BufferedOutputStream(response.getOutputStream(),Constants.DEFAULT\u BUFFER\u SIZE);
byte[]buffer=新字节[Constants.DEFAULT_buffer_SIZE];
整数长度;
而((长度=输入.读取(缓冲区))>0){
输出.写入(缓冲区,0,长度);
}
output.flush();
}捕获(异常){
/* ... */
}最后{
试一试{
if(输出!=null){
output.close();
}
如果(输入!=null){
input.close();
}
}捕获(异常){
/* ... */
}
}
facesContext.responseComplete();
}
这是打开pdf文件的按钮:

public void printJasper() {

    JasperReport compiledTemplate = null;
    JRExporter exporter = null;
    ByteArrayOutputStream out = null;
    ByteArrayInputStream input = null;
    BufferedOutputStream output = null;

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

    try {

        List<String> sampleList = new ArrayList<String>();
        sampleList.add("Fist sample string");
        sampleList.add("Second sample string");

        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(sampleList);
        Map<String, Object> reportValues = new HashMap<String, Object>();
        reportValues.put("anyTestValue", "test value");

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

        FileInputStream file = new FileInputStream("/any_dir/sample.jasper");
        compiledTemplate = (JasperReport) JRLoader.loadObject(file);

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

        exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
        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=\"fileName.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) {
        /* ... */
    } finally {
        try {
            if (output != null) {
                output.close();
            }
            if (input != null) {
                input.close();
            }
        } catch (Exception exception) {
            /* ... */
        }
    }
    facesContext.responseComplete();
}
<p:commandButton action="#{sampleBB.printJasper}"
    ajax="false" onclick="this.form.target='_blank'"
    value="#{msg['generate.report']}" />


我需要做什么?

您不能直接从JavaScript打印URL,只能打开现有页面的打印对话框

PDF在服务器上生成并发送到web浏览器(作为单独的“页面”),该浏览器必须决定如何处理它-通常会询问用户是否要显示或保存PDF

要“自动打印”(即打开打印对话框)一个HTML页面,您只需要以下内容:

window.onload = function() {
  window.print();
};
但这不能用于PDF,因为它不是HTML页面

要“自动打印”HTML页面以外的内容,您需要有一个web浏览器插件来处理来自服务器的PDF

另一个可能性是编写一个GreaseMonkey用户脚本,该脚本将对
*.myserver.com/**.pdf
作出反应并将其打印出来。注意:GreaseMonkey是一个Mozilla Firefox插件

重量级选项

您可以通过向服务器应用程序添加打印支持来完成任务。申请条件:

  • 它必须是一个内部网应用程序(在用户网络内自托管)
  • 管理员用户需要注册可通过JSP页面从服务器访问的网络打印机
  • “打印对话框”页面,您可以在其中选择已注册的打印机,然后单击“打印”按钮发送“打印”请求,例如:

    /打印?打印机=printer1&doc=/reports/report1

我见过一个Java web应用程序支持这一点,但正如您所看到的,这不是一项容易的任务

@Sujan Sivagurunathan

我尝试通过将p:printer演示页上的图像替换为p:media演示页中的PDF文件来组合和:

// Replace this line:
<img id="j_idt18:image" src="/showcase/images/nature1.jpg?pfdrid_c=true" alt="">

// With this one:
<object **id="j_idt18:image"** style="display:none;" type="application/pdf" data="/showcase/resources/other/guide.pdf?pfdrid_c=true">Undefined</object>
//替换此行:
//关于这一点:
未定义
当你点击打印按钮时,你会看到一个空白页面。如果省略
style=“display:none;”
并保留PDF的
height=“300px”width=“100%”
页面打印预览中将显示一个小矩形

Eidt

谢谢你,巴卢斯克和苏扬。我同意,有一个在PDF中嵌入JavaScript的选项,但出于安全原因,通常会禁用该选项

我认为最兼容浏览器和用户友好的方式是有一个专用的打印预览弹出窗口,带有一个通过GET请求显示给定PDF的
iframe
,以及一个打印按钮来调用iframe的
contentWindow.Print()

通常,只打印文档而不让用户选择打印机并进行配置是一个坏主意。

有一个
Primefaces组件用于此目的

类似的东西可能会起作用,但没有经过测试

<h:form>
    <h:commandButton value="Print" type="button" icon="ui-icon-print">
        <p:printer target="pdf" />
    </h:commandButton>

    <p:media style="display:none;" id="pdf" value="/aPDF.pdf" />
</h:form>

注:

确实有一个打印按钮来打印显示的pdf

编辑:

您必须将pdf文件嵌入iframe中,并在其上使用JavaScript print()函数,或者可以在pdf本身中激活自动打印函数。但它是defi
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <h:head> 
         <style>html, body { height: 100%; margin: 0; overflow: hidden; }</style>
    </h:head>
    <h:body>
        <iframe src="#{request.contextPath}/pdf?#{request.queryString}"
            width="100%" height="100%" onload="this.contentWindow.print()" />
    </h:body>
</html>
<h:form target="_blank">
    <h:commandButton value="Generate report" action="#{bean.printPdf}" />
</h:form>