Support > Repository > Form > PDF output using JasperReports (1) Detailed display
ja | en

I will explain how to output PDF files using JasperReports.

JasperReportsIs an open source form output library that runs in Java.

Wagby can output PDF and RTF format forms using JasperReports. This document explains how to pass data from Wagby to JasperReports.

Reference materials of JasperReports are as follows.

How to use JasperReports and how to design a form is not subject to Wagby support.Please consult Wagby sales partner who has JasperReports engineer.Some partners are carrying out workshops such as training.

JasperReports Library

It is bundled with Wagby.Jasperreports.jar is 6.2.0.It also includes iText.jar and iTextAsian.jar.You can also replace it with the jar file included in your Jaspersoft Studio.[More ...]

Jaspersoft Studio

Please download and install by yourself.The version used at the time of writing this document is 5.6.0.final.

Sample data in XML format

Wagby converts the data that forms the form into an XML format file and passes it to JasperReports. In this case, we will obtain the XML file to be passed to JasperReports in advance and use it for form design.Please follow the steps below to obtain it.

  1. Launch the built Wagby application.
  2. Open wagbyapp \ webapps \ wagby \ $ (model name) \ show $ (model name) _body.jsp with a text editor.Notepad attached to Windows can not be used.Please use a text editor such as TeraPad.
  3. Add a line of <c: out value = "$ {(model name) _ p}" /> to the same file.Here is an example of the customer model.
    <%@ page pageEncoding="UTF-8" %>
    <fmt:setBundle basename="jfcapp"/>
    <c:out value="${customer_p}"/>
    <table width="100%">
    ...
    
  4. Log on to the application and open the details display screen of this model.Data in XML format is displayed at the top of the screen.This is the test data used for form layout design in Jaspersoft Studio.
  5. Use the mouse to select and copy the displayed XML data.Save the data as a text file.(The file name is a combination of half-size alphanumeric characters and the extension is ".xml".) The character code is utf-8.
  6. In addition, add the following two lines to the beginning of this text file.
    <?xml version="1.0" encoding="utf-8"?>
    <root>
  7. Add the following line to the last line.
    </root>

In the following description, suppose you save test data for detailed forms as "c: \ customer_show.xml".

Figure 1 Customer_show.xml prepared
<?xml version="1.0" encoding="utf-8"?>
<root>
  <customer_p>
    <customerid errorcode="">1000</customerid>
    <name errorcode="">琉球太郎</name>
    <kananame errorcode="">リュウキュウタロウ</kananame>
  </customer_p>
</root>

Design the form layout using Jaspersoft Studio.Specifically, we will place "Text Field" on the screen and we will decide which element of TextField (which is XML data) it corresponds to.In this document, detailed usage of Jaspersoft Studio will be omitted.

Figure 2 Jaspersoft Studio

The setting points are as follows.

Data Adapter Wizard

Prepare the data source "XML document".In the example, name it "showCustomer" and link it with customer_show.xml.

Fig. 3 Data Adapter Wizard setting screen

Dataset and Query

Open the Dataset and Query Dialog.

Figure 4 Opening the Dataset and Query Dialog

Select showCustomer as the data source.Language is "XPath".Set the character "/" to the first "root" element.We will associate the text field displayed on the screen with which node of the data.

Figure 5 Linking XML elements and Fields

Expression Editor

Make detailed settings for the fields prepared for the form layout.

Figure 6 Expression Editor

PDF Encoding, PDF Font Name

In the property of the text field, specify PDF Encoding and PDF Font Name.Here, we set Encoding as "UniJIS - UCS 2 - H" and Font Name as "HeiseiKakuGo - W 5".(If you do not do this setting, Japanese will not be displayed when PDF is output from the application.)

Figure 7 Property

Preview

When you select the Preview tab, the form image is displayed.

Figure 8 Preview

Source

All settings can be confirmed on the Source tab.Here is an example of detailed form setting.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version last-->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="showCustomer1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ee5f7461-8b85-4830-9157-4365647f44c4">
  <property name="com.jaspersoft.studio.data.defaultdataadapter" value="showCustomer"/>
  <queryString language="xPath">
    <![CDATA[/]]>
  </queryString>
  <field name="Field_1" class="java.lang.String">
    <fieldDescription><![CDATA[/root/customer_p/name]]></fieldDescription>
  </field>
  <background>
    <band splitType="Stretch"/>
  </background>
  <title>
    <band height="79" splitType="Stretch"/>
  </title>
  <pageHeader>
    <band height="35" splitType="Stretch"/>
  </pageHeader>
  <columnHeader>
    <band height="61" splitType="Stretch"/>
  </columnHeader>
  <detail>
    <band height="125" splitType="Stretch">
      <textField>
        <reportElement stretchType="RelativeToBandHeight" x="56" y="36" width="200" height="20" uuid="d9d975f0-1a46-4f50-be3f-73f83f9dd1b4">
          <property name="local_mesure_unitwidth" value="pixel"/>
          <property name="com.jaspersoft.studio.unit.width" value="px"/>
        </reportElement>
        <textElement>
          <font pdfFontName="HeiseiKakuGo-W5" pdfEncoding="UniJIS-UCS2-H"/>
        </textElement>
        <textFieldExpression><![CDATA[$F{Field_1}]]></textFieldExpression>
      </textField>
    </band>
  </detail>
  <columnFooter>
    <band height="45" splitType="Stretch"/>
  </columnFooter>
  <pageFooter>
    <band height="54" splitType="Stretch"/>
  </pageFooter>
  <summary>
    <band height="42" splitType="Stretch"/>
  </summary>
</jasperReport>

"Description" in Figure 5 is XPath notation.

Item type Inscription Description Specification example
String, number, date / Root/model name _ p/item name The value of the item is posted. /root/customer_p/customer_name
Model reference (except check box) / Root/model name _ p/item name/@ content The content part is posted. /root/customer_p/companyname/@content
/ Root/model name _ p/item name/@ id The ID part of the model is posted. /root/customer_p/companyname/@id
Model reference (check box) / Root/model name _ p/item name [subscript]/@ content The content part of the model corresponding to the subscript is posted.Subscript starts from "1". /root/customer_p/customertype[@choose= true()][2]/@content
/ Root/model name _ p/item name [subscript]/@ id The ID part of the model corresponding to the subscript is posted.Subscript starts from "1". /root/customer_p/ customertype[@choose= true()][2]/@id
Repeat item / Root/model name _ p/item name [subscript] The value of the repetition item corresponding to the subscript is posted.Subscript starts from "1". /root/customer_p/email[2]/@content
Items in the repeating container / Root/model name _ p/item name of container [subscript]/item name The value of the item in the repeating container corresponding to the subscript is posted.Subscript starts from "1". /root/customer_p/report[2]/rdate

In case of model reference (check box), repeating item, item in repeating container, omitting [subscript] operates assuming that [1] is specified.

Handling variable length data

In the method using "subscript" in the above model introduction (check box), repeating item, item in repeating container, etc., only the defined report field can be displayed in the form.For example, if there are ten data in the repetition item email, if you have defined only two report fields (email [1], email [2]), you can not output more than two data.

If you want to dynamically change the number of displayed items depending on the number of data in the input data, you can use "subreport function" of JaspreReports.However, since the setting method of subreport is complicated, explanation is omitted in this document.For details please contact Wagby sales partner with JasperReports engineer.

Definition

Prepare a form template with "Screen> Form output".Determine the name of the template file and choose JasperReports as the library to use in the detail display - PDF.

Fig. 9 Form setting

Form template setting

Log on to the application you have built and register the form template.

Figure 10 Selecting a form template from the menu

For the ID, specify the template file name set in Figure 9.For the template file, specify the jrxml file created by Jaspersoft Studio.

Fig. 11 Form template new registration screen
Register the jrxml file as a template.

Execution

Register a new customer model.The "PDF print" button is prepared on the detail display screen.(Button names are those set in Figure 9.)

Figure 12 Customer Details Screen

When you press the PDF print button, a new tab is prepared and PDF is output.If you specify "Download" as the output method in Figure 10, the PDF file will be downloaded.

Figure 13 Example of executing PDF printing

JasperReports has a database connection function by JDBC.By using SQL, you can also operate the table generated by Wagby directly to collect data and output the report.

In the form setting of Designer, please select "JasperReports - JDBC".

Figure 14 Selection of usage library (JDBC)

We do not set up JDBC in Japsersoft Studio.You can specify SQL for the queryString element of the configuration file jrxml as follows.

<queryString><![CDATA[select * from "juser" where "userid" = 'admin']]></queryString>

Enclose the table name and item name in double quotes.Fixed values ​​are enclosed in single quotes.

When using the JDBC setting, XML can not be used for the data set.(Even if you specify it, it will be ignored.) Specifically, even if you write it as / root / model name _ p / item name etc., "No XML source was provided." Will be output to the log at run time, nothing will be displayed in the PDF.

The library (jar file) is not included in Wagby, but you can also use the library that is included in your Jaspersoft Studio.Here is a specific procedure.

  1. Look for the jar file of jasperreports and iText that is included in Jaspersoft Studio.In 5.6.2.final, it was stored in the following folder.
    • configuration/org.eclipse.osgi/bundles/38/1/.cp/lib/jasperreports-5.6.1.jar
    • configuration/org.eclipse.osgi/bundles/6/1/.cp/lib/iText-2.1.7.js2.jar
    The necessary files are the above two files.The version number may be different from the example above.
  2. Change each file name.Jasperreports - 5.6.1.jar to jasperreports.jar, iText - 2.1.7.js2.jar is iText.jar.
  3. Copy (overwrite) the two renamed files to the wagbyapp/webapps/wagby/WEB-INF/lib folder.
  4. Restart Wagby application and output PDF.

If you can confirm the output with this procedure, copy the jar file prepared in 2 to the customize/webapp/WEB-INF/lib folder.In the subsequent build process, these files will be used.

Q. What is the location of the subreport?

A. The registration function of "Form template" does not correspond to sub report.Please place the subreport in the form where the relative path is embedded in the main form template.

The standard origin folder is wagbyapp/bin.

Q. When embedding images, please tell me where to place the image files.

A. Please save the image to be used fixedly in the form below WEB - INF and specify the path (to the image file) with JasperReport setting.

The path when wagbyapp is executed is wagbyapp/bin.(Startup.bat will start up in this folder.) Therefore, if JasperReport recognizes a relative path notation starting from here or a folder under WEB-INF, it is better to have the image file read by relative path notation starting from there.

When placing an image file in wagbyapp/bin, please place the file in customize/tomcat/bin. After that it will be copied to wagbyapp/bin at the timing of the build. If it is less than WEB-INF, it will be copied at build time by placing it under customize/webapp/WEB-INF as well.