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

查看:157
本文介绍了从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(),但这仍然会向用户显示打印机设置等(基本上,它与<$ c $相同) c> 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页面中c $ c>< applet> 或< object> 代码。

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:


使用API​​



使用的典型应用程序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代表。

  • 调用打印作业的打印方法。

  • Chooses a DocFlavor.
  • Creates a set of attributes.
  • Locates a print service that can handle the print request as specified by the DocFlavor and the attribute set.
  • Creates a Doc object encapsulating the DocFlavor and the actual print data, which can take many forms including: a Postscript file, a JPEG image, a URL, or plain text.
  • Gets a print job, represented by DocPrintJob, from the print service.
  • Calls the print method of the print job.

以下代码示例演示了Java Print Service API的典型用法:找到可以在A4纸上打印Postscript文档的五个双面副本的打印机,创建一个从其中一个返回的打印服务打印作业,并调用print。

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天全站免登陆