开发者

Blank PDF even with the simplest Jasperreport jrxml

开发者 https://www.devze.com 2023-02-15 19:36 出处:网络
i have a EJB site with glassfish 3.1 + JSF for jasperreport 4.0.1.the site has no problem on streaming pdf, but it products blank PDF while printing PDF with runReportToPdfStream, below is the code sn

i have a EJB site with glassfish 3.1 + JSF for jasperreport 4.0.1. the site has no problem on streaming pdf, but it products blank PDF while printing PDF with runReportToPdfStream, below is the code snippet:

EJB

public class BookEJB {  
    public void printReport() throws ClassNotFoundException, IOException, JRException {
        Map parameterMap = new HashMap();

        FacesContext ctx = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
        InputStream reportStream = ctx.getExternalContext().getResourceAsStream("/reports/test.jasper");

        ServletOutputStream servletOutputStream = response.getOutputStream();
        servletOutputStream.flush();

        response.setContentType("application/pdf");
        JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parameterMap);

        servletOutputStream.flush();
        servlet开发者_Python百科OutputStream.close();
        ctx.responseComplete();
}}

test.jrxml - a simple report without SQL connection

<?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="test" pageWidth="800" pageHeight="1200" columnWidth="555" leftMargin="25" rightMargin="25" topMargin="30" bottomMargin="30">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <pageHeader>
        <band height="100">
            <staticText>
                <reportElement x="0" y="0" width="285" height="36"/>
                <textElement>
                    <font size="24" isBold="true"/>
                </textElement>
                <text><![CDATA[Report of Testing]]></text>
            </staticText>   
        </band>
    </pageHeader>
    <detail>
        <band height="200">
            <staticText>
                <reportElement x="0" y="0" width="374" height="48"/>
                <textElement>
                    <font size="18"/>
                </textElement>
                <text><![CDATA[If you don't see this, it didn't work blah blah blah.... ]]></text>
            </staticText>
        </band>
    </detail>
    <pageFooter>
        <band height="100"/>
    </pageFooter>
</jasperReport>

no error log in glassfish when generating this report on JSF, but only blank PDF has been shown. Please help, let me know if you need further info for the analysis.

Steven


After all, JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parameterMap, new JREmptyDataSource()); solved the problem.

Quote from Sanda of Jasperreport:

By default, when no datasource info is present in a report, JR generates no pages. Another option (which can be set in the report's whenNoDataType attribute) would be to print all report sections, excepting the <detail>.

This report contains a detail section, but only with some static data. To ensure this section will be printed too, the simplest way is to provide an empty data source, containing a single empty record.

Source: https://community.jaspersoft.com/questions/537650/blank-pdf-even-simplest-jrxml


When you are not using the details band, just static values, you can do the following:

Right click in the iReport project, then select 'Properties', search for the property 'When no Data', and select 'All Sections, No Detail'

It works for me, using iReport 4.0


In addition to what Mythox said, I'll show the best way to fake a data source in jasper and if you need also on JasperServer.

1) Define an Empty Data Adapter (a simple .xml file), and deploy it in Server or put it in reports folder:

<?xml version="1.0" encoding="UTF-8" ?><emptyDataAdapter class="net.sf.jasperreports.data.empty.EmptyDataAdapterImpl"><name>Nuovo Data Adapter 1</name><recordCount>1</recordCount></emptyDataAdapter>

2) Link it into the main report:

<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="repor" language="javascript" pageWidth="612" pageHeight="792" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isFloatColumnFooter="true" uuid="c0eee90e-1b1a-4f34-ad99-1112847752de">
<property name="net.sf.jasperreports.data.adapter" value="EmptyDataAdapter.xml"/>

prefix "repo:" to the value of the property for the data adapter, if the xml is deployed on jasper server.

The attribute "whenNoDataType" will be ignored.

Other details here.

0

精彩评论

暂无评论...
验证码 换一张
取 消