从 JasperReports 打印 PDF [英] Printing PDF from JasperReports

查看:24
本文介绍了从 JasperReports 打印 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JasperReports 的新手,发现自己对它迷失了方向.我在 JSF 中有一个 webapp,我想用它来打印 PDF.我已经构建了报告,并且能够成功编译并使用我的所有参数填充它.但是,我对实际输出部分迷失了方向.我希望它以 PDF 格式发送到打印机.我不在乎在屏幕上看到它,直接到打印机将是理想的(从服务器将是理想的,但客户端也可以,因为我们可以将客户端设置为根据需要进行打印(这是一个内部应用程序)).

I'm new to JasperReports and find myself getting pretty lost with it. I've got a webapp in JSF that I want to use to print a PDF. I've built the report, and am able to successfully compile and fill it with all my parameters. However, I'm lost on the actual output portion. I'd like it go to a printer as a PDF. I don't care about ever seeing it on screen, straight to printer would be the ideal (from the server would be ideal, but client would also be fine as we could setup the clients to print as necessary (it's an internal app)).

推荐答案

我希望它以 PDF 格式发送到打印机.我不在乎在屏幕上看到它,直接到打印机将是理想的

你不能用普通的 HTML/CSS/JS 来做到这一点.由于 JSF 基本上只是一个 HTML/CSS/JS 代码生成器,它不能为您做任何魔术.最接近的是 JavaScript 的 window.print(),但这仍会向用户显示打印机设置等(基本上,它与 Ctrl+P).

You can't do it with plain HTML/CSS/JS. As JSF is basically just a HTML/CSS/JS code generator, it can't do any magic for you. Closest what you can get is JavaScript's window.print(), but that would still show the user the printer settings and such (basically, it does the same as Ctrl+P).

最好的办法是创建一个 Applet使用 javax.print API,然后通过 HTML 标签将该 Applet 嵌入到您的 JSF 页面中.

Your best bet is to create an Applet which uses the javax.print API and then embed that Applet in your JSF page by HTML <applet> or <object> tag.

如果您可以直接在屏幕上看到它并将打印作业委托给最终用户本身,那么您可以通过 JSF 将 PDF 文件发送到屏幕,如下所示:

If you can live with seeing it straight on screen and delegating the print job to the enduser itself, then you can send a PDF file to screen by JSF as follows:

public void sendPdf() throws IOException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/pdf");
    externalContext.setResponseHeader("Content-Disposition", "inline; filename="filename.pdf"");
    yourJasperReportsClass.writePdfTo(externalContext.getResponseOutputStream());
    facesContext.responseComplete();
}

我从未使用过 JasperReports,所以 yourJasperReportsClass.writePdfTo() 只是一个随机猜测,但提示应该足够清楚.您基本上需要指示它将 PDF 写入响应正文.

I have never worked with JasperReports, so the yourJasperReportsClass.writePdfTo() was just a random guess, but the hint should be clear enough. You basically need to instruct it to write the PDF to the response body.

更新:根据评论,该打印机实际上连接到服务器,而不是连接到客户端,您实际上想让服务器将其打印到其打印机上.在这种情况下,只需使用 javax.print API.在该文档的底部,您可以找到一些代码示例.以下是相关性的摘录:

Update: as per the comments, that printer is actually connected to the server, not to the client and you actually want to let the server print it to its printer. In that case, just use the javax.print API. At the bottom of that document you can find some code examples. Here's an extract of relevance:

使用 Java Print Service API 的典型应用程序执行以下步骤来处理打印请求:

Using the API

A typical application using the Java Print Service API performs these steps to process a print request:

  • 选择 DocFlavor.
  • 创建一组属性.
  • 定位可以处理由 DocFlavor 和属性集指定的打印请求的打印服务.
  • 创建一个封装了 DocFlavor 和实际打印数据的 Doc 对象,它可以采用多种形式,包括:Postscript 文件、JPEG 图像、URL 或纯文本.
  • 从打印服务获取打印作业,由 DocPrintJob 表示.
  • 调用打印作业的打印方法.

以下代码示例演示了 Java Print Service API 的典型用法:定位可以在 A4 纸上打印 Postscript 文档的五份双面副本的打印机,从返回的打印服务之一创建打印作业,以及调用打印.

The following code sample demonstrates a typical use of the Java Print Service API: locating printers that can print five double-sided copies of a Postscript document on size A4 paper, creating a print job from one of the returned print services, and calling print.

FileInputStream psStream; 

try { 
  psStream = new FileInputStream("file.ps");
} catch (FileNotFoundException ffne) {
} 

if (psStream == null) { 
  return;
}

DocFlavor psInFormat = DocFlavor.INPUT_STREAM.POSTSCRIPT; 
Doc myDoc = new SimpleDoc(psStream, psInFormat, null); 
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
aset.add(new Copies(5)); 
aset.add(MediaSize.A4); 
aset.add(Sides.DUPLEX); 

PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset); > 
if (services.length > 0) { 
  DocPrintJob job = services[0].createPrintJob(); 

  try {
    job.print(myDoc, aset); 
  } catch (PrintException pe) {
  }
} 

以上代码是否由 JSF 托管 bean 调用无关紧要.毕竟只是Java.您可能只想修改 DocFlavor 和其他设置.

It's not relevant if the above code called by a JSF managed bean. It's after all just Java. You might only want to modify the DocFlavor and other settings.

这篇关于从 JasperReports 打印 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆