⬆️ ⬇️

JasperReport + ZK integration without a single cent spent

Good all the time of day. When I started to learn this excellent framework zk , but it was two years ago, then of course I didn’t meet any Russian mans, then I climbed on all of us with an adorable habr and finally found one introductory post. But he was so introductory (no offense to the distinguished one who bothered to write it) that he had to turn to one source, their own forum. Therefore, my goal is to fill this huge gap in the presence of Russian-language tutorials.



Well, enough preludes, let's begin. Based on the title of the post, it is obvious that the story will be about the integration of JasperReport with zk, and that this integration should be smooth for the budget. Since, if you look here , you can find such an impartial phrase - This feature requires ZK PE or EE.

About CE, PE and EE licenses can be read on their website, and if briefly, only CE is free. But what to do if, in almost any application, it is necessary to implement the functions of generating and outputting reports to the user. There are several ways out of this situation, the first - to pay for PE or EE, the second will be described here.



Let's start from far away, that is, with the jasper compiled class. For these purposes, I advise everyone to use the iReport product, an excellent editor for designing, compiling and previewing reports. I will not describe how to generate reports in JasperReport, since there are even a lot of Russian help for this. As a result, we will have a compiled report with the extension .jasper. Great, but for now let's forget about it and write a couple of lines of integration code.

')

For simplicity, we will create a zk project and in the WebContent we will create, of course, if it has not been generated automatically, index.zul the file and enter the following lines:

<window title="ZK+JasperReport" border="normal" width="100%" use="ui.component.Main" id="wndMainForm"> <label value="view report" /> <toolbarbutton label="show->" onClick="wndMainForm.onShowMessage()" /> <iframe id="iframe" width="100%" height="90%"/> </window> 




Next in the inherited class we will write the procedure for generating the report in pdf, and if everything went well, we’ll display it on the form.

 package ui.component; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import org.zkoss.util.media.AMedia; import org.zkoss.zul.Iframe; import org.zkoss.zul.Messagebox; import org.zkoss.zul.Window; public class Main extends Window { private static final long serialVersionUID = 1L; public void onShowMessage() throws InterruptedException { try { JasperPrint print = JasperFillManager.fillReport("  /HelloWorld.jasper", null); JasperExportManager.exportReportToPdfFile(print, "  /HelloWorld.pdf"); Iframe iframe = (Iframe) this.getFellow("iframe"); File f = new File("  /HelloWorld.pdf"); byte[] buffer = new byte[(int) f.length()]; FileInputStream fs = new FileInputStream(f); fs.read(buffer); fs.close(); ByteArrayInputStream is = new ByteArrayInputStream(buffer); AMedia amedia = new AMedia("HelloWorld.pdf", "pdf", "application/pdf", is); iframe.setContent(amedia); } catch (IOException e) { Messagebox.show("   "); e.printStackTrace(); } catch (JRException e) { Messagebox.show("   pdf-"); e.printStackTrace(); } } } 


A small note: if you want to pass parameters and a connection string to a compiled class, you can use the following code as an example:

 HashMap<String, String> hm = new HashMap<String, String>(); hm.put("dateStart", new SimpleDateFormat("HH:mm:ss").format(dbStart.getValue())); hm.put("pCommand", "\" \""); Connection conn = ;// ,     ,      JasperPrint print = JasperFillManager.fillReport("  /HelloWorld.jasper",hm,conn ); 


Well, that's all, very simple and easy.

PS If you have questions on zul markup, on code, or even on a framework, then ask - in the comments I will try to answer all the questions

Oh, I almost forgot. Here is a list of all the necessary libraries to make it work:



The files breeze.jar, sapphire.jar, silvertail.jar are skins, so their presence on your server is not necessary

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



All Articles