raikumardipak
raikumardipak

Reputation: 1605

Jasper Report PDF does not open

I am trying to build a sample web application which loads a pdf report generated through jasper reports (.jasper file). However the report does not open as a pdf with the message "This PDF document might not be displayed correctly". The code snippets are as below. There is no exception log coming in the server log. Any help would be great. Thanks.

Servlet Controller Class

@WebServlet("/generateReport")
public class JasperReportsExample extends HttpServlet{

    final String deviceJasper = "SampleReport.jasper";
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
    response.setContentType("application/pdf");
    response.setCharacterEncoding("UTF-8");
    try{
    List<SampleReportBean> sampleReportBeanList = new ArrayList<SampleReportBean>();
    SampleReportBean bean = new SampleReportBean();
    bean.setName("Hello");
    sampleReportBeanList.add(bean);
    InputStream is = this.getClass().getClassLoader().getResourceAsStream(deviceJasper);
    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);
    JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(sampleReportBeanList);
    Map<String, Object> parameters = new HashMap<String, Object>();
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, beanCollectionDataSource);
    JasperExportManager.exportReportToPdfFile(jasperPrint, deviceJasper);
    }catch(Exception e) {
        System.out.println("Error:while creating jasper report....");
        e.printStackTrace();
    }
    }

}

html page

<form action="/samplejasperreport/generateReport" method="get">
<div>
<input type="submit" name="Generate Report" value="Generate Report">
</div>
</form>

SampleReport.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="SampleReport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="038b20ef-43f5-4f05-8318-627e403a7110">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JavaBean Adapter"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="name" class="java.lang.String">
        <fieldDescription><![CDATA[name]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band splitType="Stretch"/>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="31" splitType="Stretch">
            <textField>
                <reportElement x="150" y="0" width="140" height="30" isPrintWhenDetailOverflows="true" uuid="f6eec303-affe-4804-bb6a-e5c07516a46f"/>
                <textElement textAlignment="Center">
                    <font size="22"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="1" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

Directory structure image

enter image description here

Error screen:

enter image description here

Upvotes: 0

Views: 3096

Answers (1)

raikumardipak
raikumardipak

Reputation: 1605

Thanks to AlexK:

JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());//send the pdfstream to the browser

Upvotes: 2

Related Questions