Last active
June 27, 2018 10:39
-
-
Save johannest/e1bc383afde0fd14111e7d332dc9f50e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example view code | |
final Button makeReport = new Button("Generate report", new ClickListener() { | |
@Override | |
public void buttonClick(ClickEvent event) { | |
try { | |
MyPdfSource pdfSource = new MyPdfSource("test.pdf", new ByteArrayOutputStream()); | |
testServiceBean.doReport(pdfSource.getOutputStream()); | |
resource = new StreamResource(pdfSource, "test.pdf"); | |
resource.setMIMEType("application/pdf"); | |
resource.getStream().setParameter("Content-Disposition", "attachment; filename=test.pdf"); | |
final Button print = new Button("print"); | |
BrowserWindowOpener opener = new BrowserWindowOpener(resource); | |
opener.extend(print); | |
compositionRoot.add(print); | |
} catch (SQLException e) { | |
setMessage(e.getMessage()); | |
} | |
} | |
}); | |
// .... | |
// Example service code (with WildFly datasource) | |
@Resource(lookup = "java:jboss/YourDS") | |
private DataSource dataSource; | |
public ResultDTO doReport(OutputStream outputStream) throws SQLException { | |
final Connection connection = dataSource.getConnection(); | |
final String reportName = "test" + counter + ".pdf"; | |
try { | |
Map<String, Object> hm = new HashMap<String, Object>(); | |
JasperReport jasperReport = JasperCompileManager.compileReport("/path/to/test.jrxml"); | |
JasperPrint print = JasperFillManager.fillReport(jasperReport, hm, connection); | |
JasperExportManager.exportReportToPdfStream(print, outputStream); | |
connection.close(); | |
} catch (JRException e) { | |
e.printStackTrace(); | |
} | |
// ..... | |
} | |
// Example report jrxml-file (for customer entity) | |
<?xml version="1.0" encoding="UTF-8"?> | |
<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="My first report" pageWidth="595" pageHeight="842" columnWidth="535" | |
leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> | |
<queryString language="SQL"> | |
<![CDATA[SELECT * FROM customer;]]> | |
</queryString> | |
<field name="ID" class="java.lang.Integer"> | |
<fieldDescription><![CDATA[]]></fieldDescription> | |
</field> | |
<field name="FIRSTNAME" class="java.lang.String"> | |
<fieldDescription><![CDATA[]]></fieldDescription> | |
</field> | |
<field name="LASTNAME" class="java.lang.String"> | |
<fieldDescription><![CDATA[]]></fieldDescription> | |
</field> | |
<background> | |
<band/> | |
</background> | |
<title> | |
<band height="58"> | |
<line> | |
<reportElement x="0" y="8" width="555" height="1"/> | |
</line> | |
<line> | |
<reportElement positionType="FixRelativeToBottom" x="0" y="51" width="555" | |
height="1"/> | |
</line> | |
</band> | |
</title> | |
<pageHeader> | |
<band/> | |
</pageHeader> | |
<columnHeader> | |
<band height="18"> | |
<staticText> | |
<reportElement mode="Opaque" x="0" y="0" width="138" height="18" | |
forecolor="#FFFFFF" backcolor="#999999"/> | |
<textElement> | |
<font size="12"/> | |
</textElement> | |
<text><![CDATA[ID]]></text> | |
</staticText> | |
<staticText> | |
<reportElement mode="Opaque" x="138" y="0" width="138" height="18" | |
forecolor="#FFFFFF" backcolor="#999999"/> | |
<textElement> | |
<font size="12"/> | |
</textElement> | |
<text><![CDATA[FIRSTNAME]]></text> | |
</staticText> | |
<staticText> | |
<reportElement mode="Opaque" x="276" y="0" width="138" height="18" | |
forecolor="#FFFFFF" backcolor="#999999"/> | |
<textElement> | |
<font size="12"/> | |
</textElement> | |
<text><![CDATA[LASTNAME]]></text> | |
</staticText> | |
</band> | |
</columnHeader> | |
<detail> | |
<band height="20"> | |
<textField hyperlinkType="None"> | |
<reportElement x="0" y="0" width="138" height="20"/> | |
<textElement> | |
<font size="12"/> | |
</textElement> | |
<textFieldExpression class="java.lang.Integer"><![CDATA[$F{ID}]]> | |
</textFieldExpression> | |
</textField> | |
<textField hyperlinkType="None"> | |
<reportElement x="138" y="0" width="138" height="20"/> | |
<textElement> | |
<font size="12"/> | |
</textElement> | |
<textFieldExpression class="java.lang.String"><![CDATA[$F{FIRSTNAME}]]> | |
</textFieldExpression> | |
</textField> | |
<textField hyperlinkType="None"> | |
<reportElement x="276" y="0" width="138" height="20"/> | |
<textElement> | |
<font size="12"/> | |
</textElement> | |
<textFieldExpression class="java.lang.String"><![CDATA[$F{LASTNAME}]]> | |
</textFieldExpression> | |
</textField> | |
</band> | |
</detail> | |
<columnFooter> | |
<band/> | |
</columnFooter> | |
<summary> | |
<band/> | |
</summary> | |
</jasperReport> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment