Asked  5 Months ago    Answers:  5   Viewed   3.9k times

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)).

 Answers

62

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

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).

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.

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

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.


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:

Using the API

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

  • 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.

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

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.

Friday, July 30, 2021
 
noir
answered 5 Months ago
40

I finally worked it out.

I can't post my code here, but here's what I did:

I rendered the PDF onto 2 canvases, one small for the thumbnail and one huge for printing (hidden). I then had a print button that opened a new window containing an img tag containing the contents of the huge canvas using toImageURL(). The print() function was called on the new window, followed by close() to close it automatically once printed.

This resulted in an almost-full-size print of the PDf, albeit with the usual page no and datestamp from the browser.

Friday, July 30, 2021
 
Troncoso
answered 5 Months ago
78

To answer my own question ...

After 3 days of trial and error I dont think there is any chance to configure the printer via .NET and print PDFs afterwards. You cant use the System.Drawing.Printing.PrintDocument class in combination with PDFs, but using PrintDocument seems to be a prerequisit to change the printer settings.

My solution to configure the printer was inspired by this post on codeplex: http://www.codeproject.com/KB/dotnet/NET_Printer_Library.aspx

To finally print the pdf I send it via commandline parameters to Adobe Reader.

Friday, August 6, 2021
 
Alexander Trauzzi
answered 4 Months ago
70

Thanks to the suggestion by @Al-Mothafar, I have finally solved my issue in the following way:

index.xhtml

...
<h:body>
    <script type="text/javascript" src="resources/Javascript/jquery/jquery-1.7.2.js" />
    <script type="text/javascript" src="resources/Javascript/MyJS.js" />
    <script type="text/javascript" >
        var myBeanProperty = '#{myBean.myProperty}';
    </script>        
</h:body>

MyJS.js

$(document).ready(function() {
    alert(myBeanProperty);
});
Friday, August 20, 2021
 
xilec
answered 4 Months ago
26

put index.jsp

and in it put

<% response.sendRedirect("desired URL"); %>

remove

<welcome-file-list>
    <welcome-file>CentralFeed.jsf</welcome-file>
</welcome-file-list>    

from web.xml

Tuesday, November 23, 2021
 
karolsojko
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share