Print PDF from JasperReports

I am new to JasperReports and believe that I lost it. I have a webapp in JSF that I want to use for printing PDF. I have built a report and can successfully compile and populate it with all my parameters. However, I get lost on the actual output part. I would like him to switch to a PDF printer. I don’t care about ever seeing it on the screen directly on the printer would be ideal (from the server it would be perfect, but the client would be fine too, since we could set up clients to print as needed (this is internal attachment)).

+2
source share
1 answer

I would like him to switch to a PDF printer. I don’t care about ever seeing it on the screen, directly to the printer would be ideal

You cannot do this with simple HTML / CSS / JS. Since JSF is just an HTML / CSS / JS code generator, it cannot do any magic for you. The closest you can get is JavaScript window.print() , but it still shows the user the printer settings, etc. (Basically, it does the same as Ctrl+P ).

It is best to create an Applet that uses the javax.print API , and then paste this applet into your JSF page using the <applet> or <object> .

If you can live by seeing it right on the screen and delegating the print job to the end user yourself, you can send the PDF file to the JSF screen 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(); } 

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


Update : according to the comments, this printer is actually connected to the server, not the client, and you really want the server to print it on your printer. In this case, just use the javax.print API . At the bottom of this document you will find code examples. Here's an excerpt of relevance:

API usage

A typical application that uses the Java print service API performs the following steps to process a print request:

  • Chooses DocFlavor.
  • Creates a set of attributes.
  • It has a print service that can handle a print request as specified by DocFlavor and a set of attributes.
  • Creates a Doc object that encapsulates DocFlavor and actual print data, which can take many forms, including a Postscript file, JPEG image, URL, or plain text.
  • Retrieves the print job submitted by DocPrintJob from the print service. A.
  • Invokes the method for printing a print job.

The following code example demonstrates a typical use of the Java print service API: finding printers that can print five two-sided copies of a Postscript document on A4 paper, creating a print job from one of the returned print services and a print call. A.

 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) { } } 

It doesn’t matter if the above code is called by a managed JSF bean. It's just Java. You can only change DocFlavor and other parameters.

+4
source

Source: https://habr.com/ru/post/1310816/


All Articles