Search code examples
javajasper-reports

Jasper creates empty PDF


I am starting to work with the jasper library. If I try to compile my project, jasper generates an empty PDF. Normally, this main function should generate a pdf file with the text 'hello'. Can anyone help me?

Main.java

public class Main {

public static void main(String[] args) throws Throwable {

    JasperReport jasperReport;
    JasperPrint jasperPrint;
    Map<String, Object> parameter = new HashMap<String, Object>();

    parameter.put("aParameter", "hello");

    try {
        jasperReport = JasperCompileManager
                .compileReport("C:/Users/Sam/Desktop/helloworld.jrxml");

        jasperPrint = JasperFillManager.fillReport(jasperReport, parameter);
        JasperExportManager.exportReportToPdfFile(jasperPrint,
                "C:/Users/Sam/Desktop/helloworld.pdf");

    } catch (JRException e) {
        e.printStackTrace();
    }
}

helloworld.jrxml

<?xml version="1.0" encoding="UTF-8"  ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="Example1">

 <parameter name="aParameter" class="java.lang.String"/>

 <detail>
  <band height="150">
   <textField >
    <reportElement x="100" y="60" width="100" height="50" />      
    <textElement><font size="36"/></textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$P{aParameter}]]></textFieldExpression>
   </textField>
  </band>
 </detail>

</jasperReport> 

Solution

  • The detail band of your report will be repeated for every row in the datasource. However your filling the report with an empty data source, this is why the band is not showing. Try moving your textfield in the title band to see it.