Hot questions for Using JasperReports in html

Question:

The code below gets a byte[] result, which works for PDF and XLSX. For HTML, an exception is raised.

    JasperPrint jasperPrint = JasperFillManager.fillReport(report,
            params, dataSource != null ? new JRMapArrayDataSource(
                    dataSource) : new JREmptyDataSource());

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    @SuppressWarnings("rawtypes")
    Exporter exporter;
    switch (format) {
    case PDF:
        exporter = new JRPdfExporter();
        break;
    case XLSX:
        exporter = new JRXlsxExporter();
        break;
    case HTML:
        exporter = new HtmlExporter();
        break;
    default:
        throw new ReportException("Unknown export format");
    }
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.exportReport();
    return out.toByteArray();

The exception for HTML is at exporter.exportReport(); line which says

java.lang.ClassCastException: 

net.sf.jasperreports.export.SimpleOutputStreamExporterOutput cannot be cast to net.sf.jasperreports.export.HtmlExporterOutput
at net.sf.jasperreports.engine.export.HtmlExporter.exportReport(HtmlExporter.java:232)

The error is the same for v6.0 and v5.6. This used to work in v5.0 (some of the classes were deprecated in v5.6).

How do you export a report in various formats, including HTML?


Answer:

For HTML and other formats:

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXmlExporter;
import net.sf.jasperreports.export.Exporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;

public byte[] export(final JasperPrint print) throws JRException {
    final Exporter exporter;
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    boolean html = false;

    switch (getReportFormat()) {
        case HTML:
            exporter = new HtmlExporter();
            exporter.setExporterOutput(new SimpleHtmlExporterOutput(out));
            html = true;
            break;

        case CSV:
            exporter = new JRCsvExporter();
            break;

        case XML:
            exporter = new JRXmlExporter();
            break;

        case XLSX:
            exporter = new JRXlsxExporter();
            break;

        case PDF:
            exporter = new JRPdfExporter();
            break;

        default:
            throw new JRException("Unknown report format: " + getReportFormat().toString());
    }

    if (!html) {
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(out));
    }

    exporter.setExporterInput(new SimpleExporterInput(print));
    exporter.exportReport();

    return out.toByteArray();
}

Call it using:

JasperPrint print = JasperFillManager.fillReport(report, parameters, dataSource);
byte report[] = export(print);

Question:

We have HTML from database, and we would like to create a Word report with this HTML. This HTML comes from users, and may contain only formatted text (bold, italic...), or text + embedded image encoded in base64 way.

Example:

<b>My photo :</b>
<img src="data:image/png;base64,iVBORw0K... " />

We know that with textField, markup="html", it does not work with img html tags. With Jasper Studio 6.3.1, we tried with <hc:html/>, it works with image on the hard drive, but it won't work with embedded image, saying there are syntax errors on the base64 encryption.

jrxml file :

<?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="StylesReport" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="53f914b8-f951-4433-971d-6b1819430c56">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <property name="net.sf.jasperreports.export.html.embed.image" value="true"/>
    <import value="org.apache.commons.codec.binar.*"/>
    <title>
        <band height="742">
            <textField hyperlinkType="Reference" hyperlinkTarget="Blank">
                <reportElement x="0" y="30" width="515" height="30" uuid="c2015c9b-9130-4f39-a09e-c341c91d3794"/>
                <textElement textAlignment="Center">
                    <font size="18"/>
                </textElement>
                <textFieldExpression><![CDATA["HTML  Element  Report"]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="0" y="100" width="230" height="110" backcolor="#ADD8E6" uuid="332dd551-e8cd-4cb0-a11f-7325f481017b"/>
                <hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RetainShape" horizontalAlign="Left" verticalAlign="Middle">
                    <hc:htmlContentExpression><![CDATA["<p style='background-color:yellow;font-family:verdana;font-size:50px;'>Hi, I am through HTML CODE using HTML Component</p><br><br><img src='data:image/gif;base64,/9j/dkjdkdkdkddk(INSERTIMAGEHEREITSTOOLONG!!!)' alt='Japser Architecture' height='1000' width='1800'>"]]></hc:htmlContentExpression>
                </hc:html>
            </componentElement>
        </band>
    </title>
</jasperReport>

Is it possible to do this with Jasper ? We wouldn't like to parse the HTML from database to do tons of things with, we really would like the HTML written as-is on the report.


Answer:

This is a nice challenge, the problem is that it's not supported but it does not mean that it can not be done.

The html component uses the JEditorPane to render the html as an image and there is no default protocol handler for "data:"

However this excellent answer by Joop Eggen shows how you can add one.

Example

jrxml (htmlComponentBase64.jrxml)

<?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="StylesReport" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="53f914b8-f951-4433-971d-6b1819430c56">
    <title>
        <band height="742">
            <textField hyperlinkType="Reference" hyperlinkTarget="Blank">
                <reportElement x="0" y="30" width="515" height="30" uuid="c2015c9b-9130-4f39-a09e-c341c91d3794"/>
                <textElement textAlignment="Center">
                    <font size="18"/>
                </textElement>
                <textFieldExpression><![CDATA["HTML  Element  Report"]]></textFieldExpression>
            </textField>
            <componentElement>
                <reportElement x="0" y="100" width="230" height="110" backcolor="#ADD8E6" uuid="332dd551-e8cd-4cb0-a11f-7325f481017b"/>
                <hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RetainShape" horizontalAlign="Left" verticalAlign="Middle">
                    <hc:htmlContentExpression><![CDATA["<p style='background-color:yellow;font-family:verdana;font-size:50px;'>Hi, I am through HTML CODE using HTML Component</p><br><br><img src='data:image/gif;base64,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' alt='Japser Architecture' height='1000' width='1800'>"]]></hc:htmlContentExpression>
                </hc:html>
            </componentElement>
        </band>
    </title>
</jasperReport>

java

add Joop Eggen's protocol handler (remember to attribute him and maybe a nice upvote also) and execute this java code, in this case to export to pdf

public class TestReport {
    public static void main(String[] args) throws JRException {

        Handler.install(); //Install Joop's protocol handler

         //Compile report and fill, no datasource needed
        JasperReport report = JasperCompileManager.compileReport("htmlComponentBase64.jrxml");
        JasperPrint jasperPrint = JasperFillManager.fillReport(report, new HashMap<String, Object>());

        //Export to pdf
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("pdf/htmlcomponentbase64.pdf"));
        SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
        exporter.setConfiguration(configuration);
        exporter.exportReport();

    }
}

Enjoy the result

not the nicest report, but the image is displayed

I have added a feature request in jasper-reports community, to include a data protocol handler in future release

Question:

The JRHtmlExporter class is deprecated now (JasperReports 6.x).

I replaced the usage of this class with HtmlExporter. But I can not find equivalent function to replace exporter.setParameter (JRHtmlExporterParameter.IMAGES_URI, imageURI);. I need to set path for storing images for generated report (html file).

My old code:

JRHtmlExporter exporter = new JRHtmlExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT, filedReport);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outputFileName);
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);            
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE);

String imageURI = "q?srvAction=ReportImage&img="+returnFileName.substring(3).replace("/", "%2F")+"_files"+"%2F";
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,imageURI);

What will be the actual code for JasperReports 6.x for defining path to images?


Answer:

As we can see from the javadoc the JRHtmlExporterParameter.IMAGES_URI parameter is really deprecated and the HtmlExporterOutput.getImageHandler() method should be used instead of it.

Define the path where images are storing

We can use the implementation of HtmlResourceHandler interface, for example WebHtmlResourceHandler.

The example of using:

JRExporter exporter = new HtmlExporter();

exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(out);
output.setImageHandler(new WebHtmlResourceHandler("/reports/image?image={0}"));
exporter.setExporterOutput(output);

exporter.exportReport();
Define where to save images during export

With help of FileHtmlResourceHandler handler we can set the path for images for generated html

The example of using:

JRExporter exporter = new HtmlExporter();
// output file for generated html report
File file = new File(String.format("./out/%1$s_%2$s.html", report.getTemplateName(), new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())));

ExporterConfiguration configuration = new SimpleHtmlExporterConfiguration();
exporter.setConfiguration(configuration);

exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(file);
// the folder for storing images. It will be subfolder with name starting like generated html and ended with postfix "_images"
File resourcesDir = new File(file.getParent(), file.getName() + "_images");
// argument ({0}) will be replaced with the real image name
String pathPattern = resourcesDir.getName() + "/{0}";

exporterOutput.setImageHandler(new FileHtmlResourceHandler(resourcesDir, pathPattern));
exporter.setExporterOutput(exporterOutput);
exporter.exportReport();

The generated files and folders will be like this:

 ..                                         [Folder]
    image-test_20170504232649.html          [File]
    image-test_20170504232649.html_images   [Folder]
        img_0_0_0.png                       [File]

Notes:

The sample of using HtmlResourceHandler can be found here

Question:

I managed to add the HtmlComponent to my project through various searches.

My next problem is that I don't have a clue how to add it to a JasperDesign. I simply can't find any way to add my HtmlComponent to a JasperDesign (not even through a group etc).

I use Jasper 5.6.1 and managed to get the HtmlComponent jar (5.0.1) out of the iReport tool (5.6.0).

My end purpose is to be able to have an HTML-table inside my report. I know it's not possible in any way, but perhaps, an HTML-component would be the most interesting thing. This way, an image of the table would be in my report, which is ok.

This is my current code

private JasperDesign getTable(String html, JasperDesign jasperDesign) {
        HtmlComponent table = new HtmlComponent();
        JRDesignExpression expression = new JRDesignExpression();
        expression.setText(html.replace("\n", ""));
        table.setHtmlContentExpression(expression);
        JRDesignGroup group = new JRDesignGroup();
        group.setName(TABLE_GROUP_PREFIX);
        JRDesignBand groupHeader = new JRDesignBand();
        groupHeader.setHeight(200);
        groupHeader.setSplitType(SplitTypeEnum.IMMEDIATE);
        groupHeader.addElement(table);
        ((JRDesignSection)  group.getGroupHeaderSection()).addBand(groupHeader);

       return jasperDesign;
}

The line groupHeader.addElement(table) fails because HtmlComponent is not an Element, but a Component.


Answer:

To add a component to the JRDesign you need to wrap it in the JRDesignComponentElement

JRDesignComponentElement ce = new JRDesignComponentElement(design);
ce.setComponentKey(new ComponentKey("http://jasperreports.sourceforge.net/htmlcomponent", "hc", "html"));
ce.setComponent(table);
Full example
//Create the design
JasperDesign design = new JasperDesign();
design.setWhenNoDataType(WhenNoDataTypeEnum.ALL_SECTIONS_NO_DETAIL); //We will run with no data
design.setName("Html test");
//Add a title band
JRDesignBand title = new JRDesignBand();
title.setHeight(100);
design.setTitle(title);

//Create our component
HtmlComponent hc = new HtmlComponent();
JRDesignExpression expression = new JRDesignExpression();
expression.setText("\"<b>Hello</b> world\"");
hc.setHtmlContentExpression(expression);

//Wrap it in a design componenent
JRDesignComponentElement ce = new JRDesignComponentElement(design);
ce.setComponentKey(new ComponentKey("http://jasperreports.sourceforge.net/htmlcomponent", "hc", "html"));
ce.setComponent(hc);
ce.setHeight(100);
ce.setWidth(100);
title.addElement(ce);

//Compile the design
JasperReport report = JasperCompileManager.compileReport(design);

//Generate the print (passing no datasource, empty)
JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String,Object>());

//Export to pdf
JRPdfExporter exporter = new JRPdfExporter(); 
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("pdf/htmlComponent.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
exporter.setConfiguration(configuration);
exporter.exportReport();
Output

Tons of code for a Hello world, it's often quicker to do the report in jrxml

Question:

We are building a report feature on our application, in which we pass through a html into a jasper to export it to pdf.

We have ran into a problem where the font-size specified in the HTML code doesn't get "read" by the jasper exporter, and the more content we have, the smaller the report gets.

Here is the HTML:

<html>
<head>
    <style>
        body {
            font-family: "Trebuchet MS",Arial;
            color: #222222;
            background: #F4F0E8;
            font-size:9.0pt;
        }
        .Izquierda{
            float:left;
        }
        .Derecha{
            float:right;
        }
        .CabeceraInicio{
            margin-bottom:10px;
        }

        .filaN1{
            margin-top: 2px;
            text-align: left;
            line-height: 14px;
            font-size:9.0pt;
            font-weight: bold;
            margin-bottom:2px;
        }
        div.CabeceraInicio,a {
            color: #000033;
        }
        .infoTarea{
            font-size:9.0pt;
            font-weight: bold;
            color:#000033;
            text-align: left;
        }
        .TextoDescripcion{
            width: 90px;
            color: #777777;
        }
        table {
            border-collapse: collapse;
            font-size:9.0pt;
            line-height: 17px;
        }
        div.pie {
            font-size:7.0pt;
            color: #777777;
            text-align: center;
        }
        .solucion {
            margin-left:30px;
            /*padding-left:5px;*/
            /*border-left: thin solid grey;*/
        }
        .nuevasComunicaciones{
            color: brown;
            margin-left:8px;
        }

/*          } */
/*      @media print{ */
/*          a:after{content:" (" attr(href) ") ";font-size:0.8em;font-weight:normal;} */
/*      } */

    </style>
</head>
<body>
<div class="CabeceraInicio">
    <table style="width:100%;">
        <tr>
            <td>
                        GT <a rel="nofollow" href='http://*IPIntranet*:8080/StsPyme/Pyme/IncidenciasyTareas/SeguimientoIncidencias/MaestroTareasMail.zul?start=yes&emp=*Empresa*&idtarea=*Codigo*&ip=*ip*&cuenta=*cuenta*'>_Acceso *TipoTarea* _Intranet</a>
            <!-- http://*IPIntranet*:8080/StsWeb/Pyme/IncidenciasyTareas/SeguimientoIncidencias/MaestroTareas.zul?start=yes&emp=*Empresa*&idtarea=*Codigo* -->
<!--                GT <a href='http://*IPIntranet*:8080/StsWeb/Pyme/IncidenciasyTareas/SeguimientoIncidencias/SeguimientoTareas.zul'>_Acceso *TipoTarea* _Intranet</a> -->
            </td>
            <td style="text-align:right;">
                        <a href='http://*IPInternet*:8080/StsPyme/Pyme/IncidenciasyTareas/SeguimientoIncidencias/MaestroTareasMail.zul?start=yes&emp=*Empresa*&idtarea=*Codigo*&ip=*ip*&cuenta=*cuenta*'>_Acceso *TipoTarea* _Internet</a>
<!--                <a href='http://*IPInternet*:8080/StsWeb/Pyme/IncidenciasyTareas/SeguimientoIncidencias/SeguimientoTareas.zul'>_Acceso *TipoTarea* _Internet</a> -->
            </td>
        </tr>
    </table>
</div>
<br/>
<table style="width:100%;">
    <tr>
        <td>
            <span class="filaN1 infoTarea">*Accion* *TipoTarea* *FechaAccion* *DiaSemana* *HoraAccion* *NombreUsuarioAccion*</span>
        </td>
        <td style="text-align:right;">
            <span class="filaN1 infoTarea ">*NombreEmpresa*</span>
        </td>
    </tr>
</table>
<hr/>
<br/>
<div class="Izquierda" style="font-weight: bold;">
    *EstadoTarea*
</div>
<table style="width:100%;">
    <tr>
        <td class="infoTarea">
            _Informacion *TipoTarea*
        </td>
        <td style="text-align:right;">
            <span class="TextoDescripcion">_CentroExplotacion</span><span class="TextoValor"> *CentroExplotacion*</span>
        </td>
    </tr>
</table>
<hr/>
<br/>
<div class="InfoTarea" style="width:100%">
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" valign="top">
                  _Asunto
            </td>
            <td colspan="3" valign="top">
                 *Asunto*
            </td>
        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Codigo
            </td>
            <td valign="top" style="width:35%">
                 *Codigo*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Solicitante
            </td>
            <td valign="top" style="width:35%">
                 *NombreSolicitante*
            </td>
        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Ambito
            </td>
            <td valign="top" style="width:35%">
                 *Ambito*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Responsable
            </td>
            <td valign="top" style="width:35%">
                  *NombreResponsable*
            </td>
        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                  _Tipo
            </td>
            <td valign="top" style="width:35%">
                 *Tipo*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Grado
            </td>
            <td valign="top" style="width:35%">
                  *Grado*
            </td>
        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                  _Sub Tipo
            </td>
            <td valign="top" style="width:35%">
                 *SubTipo*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Prioridad
            </td>
            <td valign="top" style="width:35%">
                  *Prioridad*
            </td>

        </tr>
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                  _Relacion
            </td>
            <td valign="top" style="width:35%">
                 *Relacion*
            </td>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                 _Estado
            </td>
            <td valign="top" style="width:35%">
                 *EstadoTarea*
            </td>
        </tr>
    </table>
    <table style="width:100%;margin-top:15px;">
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%;">
                  _NumReferencia
            </td>
            <td valign="top">
                 *NumReferencia*
            </td>
        </tr>
    </table>
</div>
<hr/>
<br/>
    <div class="InfoTarea" style="width:100%">
        <table style="width:100%">
            <tr>
                <td class="TextoDescripcion" valign="top">
                    <span class="infoTarea">
                        _Solicitud
                    </span>
                </td>
            </tr>
            <tr>
                <td valign="top" style="padding-bottom:10px;">          
                    *Solicitud*
                </td>
            </tr>
            <tr>
                <td class="TextoDescripcion" valign="top">
                    <span class="infoTarea">
                        _Descripcion
                    </span>
                </td>
            </tr>
            <tr>
                <td valign="top" style="padding-bottom:10px;">          
                    *Descripcion*
                </td>
            </tr>
            <tr>
                <td class="TextoDescripcion" valign="top">
                    <span class="infoTarea">
                        _Informe
                    </span>
                </td>
            </tr>
            <tr>
                <td valign="top" style="padding-bottom:10px;">
                    *Informe*
                </td>
            </tr>
            <tr>
                <td class="TextoDescripcion" valign="top" style="width:auto">
                    <span class="infoTarea">
                        _ArchivosAdjuntos
                    </span>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    *TablaArchivosAdjuntos*
                </td>
            </tr>
        </table>
    </div>
<hr/>
<br/>
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" valign="top">
                <span class="infoTarea">
                    _Comunicaciones
                </span>
            </td>
        </tr>
        <tr>
            <td valign="top">
            *Comunicaciones*
            </td>
        </tr>
    </table><hr id="HRcomunicaciones"/><br/>
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" style="width:99%" valign="top">
                <span class="infoTarea">
                    _OtrasComunicaciones
                </span>
            </td>
        </tr>
        <tr>
            <td valign="top">
                *OtrasComunicaciones*
            </td>
        </tr>
    </table>
        <!--  </table> --><hr/>
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" style="width:99%" valign="top">
                <span class="infoTarea">
                    _textoCambioValidacion
                </span>
            </td>
        </tr>
        <tr>
            <td valign="top">
                *textoCambioValidacion*
            </td>
        </tr>
    </table>
<!-- <hr/> -->
<!-- <div class="InfoTarea">
    <table style="width:100%">
        <tr>
            <td class="TextoDescripcion" valign="top" style="width:15%">
                <span class="infoTarea">
                    _Solucion
                </span>
            </td>
        </tr>
        <tr>
            <td valign="top">
                 *Solucion*
            </td>
        </tr>
    </table>
</div> -->
<br>
</body>
</html>

And here the JRXML:

<?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="StylesReport" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="53f914b8-f951-4433-971d-6b1819430c56">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="htmlCode" class="java.lang.String"/>
    <title>
        <band height="742">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="742" uuid="8544346f-4c98-4069-a041-f6080a75e906"/>
                <hc:html xmlns:hc="http://jasperreports.sourceforge.net/htmlcomponent" xsi:schemaLocation="http://jasperreports.sourceforge.net/htmlcomponent http://jasperreports.sourceforge.net/xsd/htmlcomponent.xsd" scaleType="RealSize" horizontalAlign="Left" verticalAlign="Top">
                    <hc:htmlContentExpression><![CDATA[$P{htmlCode}]]></hc:htmlContentExpression>
                </hc:html>
            </componentElement>
        </band>
    </title>
</jasperReport>

And here the Java method for the exporting:

public String generarReportePdfAdjunto(String html, ArrayList<String> pathsImagenes){
        HashMap hm = null;
        String jrxmlFileName = "";
        String jasperFileName = "";
        String rutaPrincipal = "";
        InputStream reportStream = null;
        jasperFileName = "/frameHtmlv2.jasper";
        reportStream = funciones.class.getResourceAsStream(jasperFileName);             
        String pdfFileName ="*pathToPdf*";
        hm = new HashMap<>();
        hm.put("htmlCode",html);
        hm.put("scaleType", "RealSize");
        // Generate jasper print, llena el report y renderiza el pdf
          JasperPrint jprint = JasperFillManager.fillReport(reportStream, hm, new JREmptyDataSource());
          JasperExportManager.exportReportToPdfFile(jprint, pdfFileName);
}

Answer:

The html component (hc:html) uses the JEditorPane to render the html as an image

As you can read in the documentation:

HTML text. The kit used in this case is the class javax.swing.text.html.HTMLEditorKit which provides HTML 3.2 support.

also seen in these posts Which HTML tags are supported in Swing components? and Which HTML tags are supported in Swing components? you need to use HTML 3.2

So whats wrong in my html?

Using https://validator.w3.org selecting DOCTYPE 3.2, passing your HTML I get 31 Errors, 19 warning(s), for example these

Solution:

Pass HTML that conforms to HTML 3.2

Question:

I have designed a simple report by putting HTML palette using iReport. When I run the report i get this exception:

Caused by: java.lang.ClassNotFoundException: net.sf.jasperreports.components.html.HtmlComponent from [Module "deployment.myProject.war:main" from Service Module Loader]

I am using this code.

InputStream is;
JasperReport jReport = null;
JasperPrint jPrint = null;

if (Utils.isEmpty(dataList)) {
    throw new Exception("No data to fill");
}

try {
    is = Thread.currentThread().getContextClassLoader().getResourceAsStream("/templates/jr/myfile.jasper");
    if (is != null) {
        jReport = (JasperReport) JRLoader.loadObject(is);
    }
    if (jReport != null) {
        if ("JDBC".equalsIgnoreCase(dataSrc)) {
            Connection con = ((DataSource) (new InitialContext().lookup(""))).getConnection();
            jPrint = JasperFillManager.fillReport(jReport, params, con);
        } else if ("JAVABEAN".equalsIgnoreCase(dataSrc)) {
            JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(dataList);
            jPrint = JasperFillManager.fillReport(jReport, params, jrDataSource);
        }
    }
} catch (Exception e) {
    System.out.println(e.getMessage());
    throw new Exception("Error generating JR Template:" + templateName, e);
}

I got exception in this line :

jReport = (JasperReport) JRLoader.loadObject(is);

I have checked the jasperReports.jar and found that there is no htmlComponent class. I also use latest version of jasperReport library(i.e 6.1.0) and there is no net.sf.jasperreports.components. html.HtmlComponent class in it. Anyone please help me how to include them into my project with netbeans ???

EDIT 1 : I have included the htmlComponent jar into my maven web project as dependency in pom.xml file using this code:

     <dependency>
     <groupId>net.sf.jasperreports</groupId>
     <artifactId>htmlcomponent</artifactId>
     <scope>system</scope>
     <version>1.0</version>
     <systemPath>${basedir}/src/lib/htmlcomponent.jar</systemPath>
     </dependency>

Now my dependency structure looks like this:

You can see the htmlComponent dependency contains the net.sf.jasperreports.components.html.HtmlComponent class. But i am still getting the same error. Please help.


Answer:

I have added following dependency into pom.xml file.

    <dependency>
        <groupId>htmlComponent</groupId>
        <artifactId>htmlComponent</artifactId>
        <version>1.0</version>
    </dependency>

and then install 3rd party jar in project by following command as describe in this link Guide to installing 3rd party JARs

i.e

mvn install:install-file -Dfile=htmlcomponent.jar -DgroupId=htmlComponent -DartifactId=htmlComponent -Dversion=1.0 -Dpackaging=jar.

and then build the project and the problem is resolved.

Question:

Is it possible to have sharp or at least not very blurry charts in JasperReports when exported in HTML format?

I use SVG chart renderer type, which is fine for PDF exports. HTML exports remain blurry.

I tried to create SVG programaticaly with JFreeChart and BatikRenderer (using a scriptlet). I get SVG XML which i set as String variable in jrxml file and use it in image element:

<image scaleImage="FillFrame">
    <reportElement positionType="Float" x="401" y="566" width="159" height="124" uuid="1baa80f7-3151-4fb8-be85-140059e9e28e"/>
    <imageExpression>
        <![CDATA[net.sf.jasperreports.renderers.BatikRenderer.getInstanceFromText($V{Chart})]]>
    </imageExpression>
</image>

With this approach image is still blurry (later i noticed all image resources in HTML export are in .png format).

Using code below still generates PNG image in HTML export

net.sf.jasperreports.renderers.BatikRenderer.getInstanceFromText("<svg xmlns:svg=\"http://www.w3.org/2000/svg\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.0\" width=\"100\" height=\"100\"><circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"green\" stroke-width=\"4\" fill=\"yellow\" /></svg>")

Is there a way to use generated SVG XML inside of Jasper report or any other solution to get sharp images of charts in HTML file? I was also considering using generated SVG XML inside of TextField component with HTML markup but i don't think that's possible with all HTML tags, just basic ones like <b> and <br>.


Answer:

Starting with JasperReports 6.2.2 the HTML exporter will output SVG for chart elements if net.sf.jasperreports.chart.render.type=svg is set, and for BatikRenderer images.

BatikRenderer has actually been deprecated, you can use net.sf.jasperreports.renderers.SimpleDataRenderer.getInstance(byte[]) instead (if you have the SVG as a String you'll need to pass something like $V{Chart}.getBytes("UTF-8")).

For older versions you can improve the quality of the rasterized SVG by setting the net.sf.jasperreports.image.dpi property (in jasperreports.properties) to something like 300. But that would still not be the same as having the actual SVG, so consider upgrading to the latest JasperReports.

Question:

Is there a way to specify the size of the HTML report as percentages, like width = "90%"? Or to ensure in some other way, that the report's width, when exported to HTML, is a certain percentage of the user's screen?

I don't mind creating an extra template, or just export only a chart as an image.

I'm using Jasper Reports Version 6.9.0.


Answer:

Jasper report export aims for pixel perfect (printable) output, that's why by default the size will correspond in pixel to the actual width of your report no matter what size of the browser window is.

Jasper reports HTMLExporter to achieve this creates a table with 3 columns, first empty column width="50%", second column width in px as width of your report and third empty column again with width="50%". This will center the result and the width of the report will equal the width indicated in jrxml.

If you export from java you can override this behaviour and set your own html header and footer.

Example

HtmlExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleHtmlExporterOutput("html/my.html"));
SimpleHtmlExporterConfiguration configuration = new SimpleHtmlExporterConfiguration();
configuration.setHtmlHeader(
        "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n" + 
        "<html>\r\n" + 
        "<head>\r\n" + 
        "  <title></title>\r\n" + 
        "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\r\n" + 
        "  <style type=\"text/css\">\r\n" + 
        "    a {text-decoration: none}\r\n" + 
        "   .jrPage {width:90% !important}\r\n" +
        "  </style>\r\n" + 
        "</head>\r\n" + 
        "<body text=\"#000000\" link=\"#000000\" alink=\"#000000\" vlink=\"#000000\">\r\n" + 
        "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\r\n" + 
        "<tr><td width=\"5%\">&nbsp;</td><td align=\"center\">\r\n");

configuration.setHtmlFooter(
        "</td><td width=\"5%\">&nbsp;</td></tr>\r\n" + 
        "</table>\r\n" + 
        "</body>\r\n" + 
        "</html>\r\n");
exporter.setConfiguration(configuration);
exporter.exportReport();

Basically the change from default html header and footer that I made is

  1. Added .jrPage style to override the pixel width and instead change to 90% width.

  2. Changed % of the two side columns from 50% to 5%

This will create a dynamically sized html that is centered and takes 90% of available window size.


Example how to automatically scale image in this case generated by a chart

Considering the jrxml in this question How can I export report to PDF/A-1a, PDF/A-1b?

  1. Add net.sf.jasperreports.export.html.class to the chart element

    <pieChart>
      <chart isShowLegend="false">
        <reportElement x="225" y="-670" width="320" height="140" uuid="23bd26a6-04a4-406f-8a1a-5e1b260cb75d">
          <property name="net.sf.jasperreports.export.html.class" value="pieChart"/>
        </reportElement>
    ....
    
  2. Add some CSS in HTML header

     @media only screen and (min-width : 768px) {
         td.pieChart img {height:300px !important;}
     }
     @media only screen and (min-width :  1224px) {
         td.pieChart img {height:400px !important;}
     }
    

Do note however that the image is only "re-scaled" hence it will keep it's original resolution.

Question:

I'm creating PDF reports with JasperReports as follows: JasperExportManager.exportReportToPdf(jasperPrint)

Now I'd also like to generate a HTML text stream. How could I achieve this without having to write that generated file to local disc?

I want to send the generated html as email body.


Answer:

Instead of using JasperExportManager, you could use JRHtmlExporter. Try something like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();

JRHtmlExporter exporter = new JRHtmlExporter();

exporter.setExporterInput(new SimpleExporterInput(yourJasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(baos)); 

exporter.exportReport();

After exportReport is called, the ByteArrayOutputStream will contain your HTML.

Question:

I have a method in Java using the JasperReport to export a report in HTML like this:

public static byte[] exportToHtmlWithConn(String urlReport, Connection conn) throws JRException {

    Map<String, Object> parameters = new HashMap<>();

    String urlLogo = "/reports/image.PNG";

    String urlLogo2 = JasperUtilities.class.getResource(urlLogo).toString();

    parameters.put("CONTEXT", urlLogo2);

    JasperReport report = JasperCompileManager.compileReport(JasperUtilities.class.getResourceAsStream(urlReport));
    jasperPrint = JasperFillManager.fillReport(report, parameters, conn);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    HtmlExporter exporter = new HtmlExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleHtmlExporterOutput(baos));
    exporter.exportReport();

    return baos.toByteArray();
}

In the report I have one parameter like this:

<parameter name="CONTEXT" class="java.lang.String">
    <parameterDescription><![CDATA[CONTEXT]]></parameterDescription>
    <defaultValueExpression><![CDATA[0]]></defaultValueExpression>
</parameter>

And the idea is to show one image inside the report

<image>
    <reportElement x="180" y="-5" width="185" height="80" uuid="d8978cb8-9c4e-4d1b-83fb-a83356803128">
    <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
    <property name="com.jaspersoft.studio.unit.height" value="pixel"/>
    </reportElement>
    <imageExpression><![CDATA[$P{CONTEXT}.toString()]]></imageExpression>
</image>

The problem is when I use this method to export a HTML the image is missing but when I use the same structure for example to export a PDF or xlsx the image works Ok.

Why not work in HTML? When I check the code in html the src is empty

<img src="" style="width: 185px" alt=""/>

Can someone help with any idea about this? I have seen many ways to do the same but this has been impossible to work. I also have used one parameters like java.io.InputStream and the image works in PDF and others formats. But in HTML doesn't work.

Also I have used others images but the same problem.


Answer:

Images in HTML exports, usually require a handler that deals with them. The handler is an implementation of net.sf.jasperreports.engine.export.HtmlResourceHandler.

When using SimpleHtmlExporterOutput(java.io.OutputStream) no handlers are registred by default, hence your output.

When using file-based SimpleHtmlExporterOutput like SimpleHtmlExporterOutput(java.io.File) or SimpleHtmlExporterOutput(java.lang.String) and their flavours, an image handler is registered by default. Besides the HTML file that it writes to disk it will also write a folder(ending in _files) containing your images.

In your case, depending on how you want to deal with images, you could register:

  1. A net.sf.jasperreports.engine.export.FileHtmlResourceHandler to mimic the existing behaviour as described above:

    HtmlExporter exporter = new HtmlExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    
    SimpleHtmlExporterOutput htmlExporterOutput = new SimpleHtmlExporterOutput(baos);
    htmlExporterOutput.setImageHandler(new FileHtmlResourceHandler(new File("html_images"), "html_images/{0}"));
    
    exporter.setExporterOutput(htmlExporterOutput);
    exporter.exportReport();
    

    This will create the html_images folder with the images from your report and point the HTML <img>s to them. You will see that they have different names than the original ones do. This is because some internal mappings are done, and each image name contains the actual location in the jasperPrint.

  2. Or a net.sf.jasperreports.web.util.WebHtmlResourceHandler to provide a custom handling, like serving the images through a servlet.

    HtmlExporter exporter = new HtmlExporter();
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    
    SimpleHtmlExporterOutput htmlOutput = new SimpleHtmlExporterOutput(baos);
    htmlOutput.setImageHandler(new WebHtmlResourceHandler("http://myserver/images?image={0}"));
    
    exporter.setExporterOutput(htmlOutput);
    exporter.exportReport();
    

    In this case the image handling is a bit more complex and involves retrieving the images from the jasperPrint, something similar to what the ImageWebResourceHandler does.

Question:

The goal: represent a table with some cells merged, so that it will be rendered correctly both in PDF and HTML, using JasperReports (Java code, not JasperStudio).

The problem: when we open the created HTML file in Internet Explorer, the positions of elements are incorrect.

More information: each row is placed inside a band (JRDesignBand) in the detail section of a Jasper design. Within those two bands we have text fields (JRDesignTextField) with different widths. Here is how it looks in the PDF (items in the first band are prepended with "R.", those in the second band with "N."): Everything is rendered as desired. However, the following happens in HTML (only in Internet Explorer): As we can see, text fields no longer appear in correct positions, they seem to be stretched outside the table.

I'm using JasperReports 6.1.0. Edit: Same happens in JasperReports 6.2.2.

I'm grateful in advance for your suggestions!

Edit

The code: (If the error is not being reproduced, I suggest making the default font bigger.)

    /* Row 1 */
    JRDesignBand band1 = new JRDesignBand();

    JRDesignTextField textField1 = new JRDesignTextField();

    textField1.setX(80);
    textField1.setWidth(45);
    textField1.setStretchWithOverflow(true);

    JRDesignExpression jrExpression1 = new JRDesignExpression();
    jrExpression1.setText("\"A.1.\"");
    textField1.setExpression(jrExpression1);

    JRDesignTextField textField2 = new JRDesignTextField();

    textField2.setX(160);
    textField2.setWidth(45);
    textField2.setStretchWithOverflow(true);

    JRDesignExpression jrExpression2 = new JRDesignExpression();
    jrExpression2.setText("\"A.2. Lorem ipsum dolor sit\"");
    textField2.setExpression(jrExpression2);

    band1.addElement(textField1);
    band1.addElement(textField2);
    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band1);

    /* Row 2 */
    JRDesignBand band2 = new JRDesignBand();

    JRDesignTextField textField3 = new JRDesignTextField();

    textField3.setX(89);
    textField3.setWidth(331);
    textField3.setStretchWithOverflow(true);

    JRDesignExpression jrExpression3 = new JRDesignExpression();
    jrExpression3
            .setText("\"B.1. Lorem ipsum dolor sit amet consectetur adipiscing elit, sed do eiusmod tempor \"");
    textField3.setExpression(jrExpression3);

    band2.addElement(textField3);
    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band2);

Answer:

In this thread I've found out that it is a widely-known issue in IE to display tables wrong - and I based my fix on the solution shown there (the most up-voted one - not the accepted one).

In short, the easiest way is to add table-layout:fixed; to style attribute of each table tag (in HTML). This can be done by using an extended version of the HtmlExporter class, overriding its exportTable() method and adding the previously mentioned modification in two lines of that method:

writer.write("<table class=\"jrPage\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"table-layout:fixed; empty-cells: show; width: ");

and

writer.write("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"table-layout:fixed; empty-cells: show; width: 100%;");

Question:

I'm using Jasper Server. Ip: http://app2:8080/jasperserver/ Application - Ip: http://app1:4545/gui/

When I get a picture of my Jasper reports using Application Server, If the image is displayed pdf, html unable to view the images.

How can I make the display of images in html reports?


Answer:

On report right click and Properties > +More under the Properties > Add this parameters; net.sf.jasperreports.export.xls.ignore.graphics = false and on Jasper Server :

jasperreports-server-cp-5.0.0/apache-tomcat/webapps/jasperserver/WEB-INF/classes/jasperreports.properties add this parameter; net.sf.jasperreports.export.xls.ignore.graphics = false

Question:

I am using JasperReports API to print reports on the browser. I have many formats to export the report to but mainly I am concern with my HTML. I am using the following code to export my report to HTML:

JRExporter exporter = null;
exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
exporter.exportReport();

This code is printing the output as following('images' even when there are none in my jrxml file):

As Eclipse showed me that JRExporter is deprecated, I did some research and found that now we use 'Exporter' to export. I am trying to use Exporter in my code to remove this error but can't find anyway I can write my jasperPrint file to the output Stream.

New code:

Exporter exporter = null;
exporter = new HtmlExporter();

http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/export/HtmlExporter.html#exportReportToWriter()

some of my code in jrxml is as follows for reference:

<title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="219" y="28" width="194" height="40" uuid="44028360-543f-4352-a028-9e262bb24347"/>
                <textElement>
                    <font size="24"/>
                </textElement>
                <text><![CDATA[Project Report]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="119" y="41" width="100" height="20" uuid="1f6ca9e9-92d5-41e5-9e8d-cd6ede8bfa25"/>
                <text><![CDATA[Project Status]]></text>
            </staticText>
            <staticText>
                <reportElement x="0" y="41" width="100" height="20" uuid="6059b496-7ff9-4156-836b-f91436b8e79c"/>
                <text><![CDATA[Aldon Number]]></text>
            </staticText>
        </band>
    </columnHeader>

Answer:

First of all dont use JRHtmlExporter(). It's deprecated. I got images when I was using HtmlExporter(). Then I started using the following statements:

import net.sf.jasperreports.engine.export.HtmlExporter;
// ...
HtmlExporter exporter = new HtmlExporter();

My coding for HTML is

HtmlExporter exporter = new HtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); 
exporter.exportReport(); 

Question:

So, I already have the report made, but the problem is that I'm using GetMapping to display, and it opens on another page alone, but I need it to open on a div, a table, a card, or even a modal. It just can't change from the previous page.

I have no idea how to open it with out redirecting the page. Any suggestion is appreciated

Method on the controller

@GetMapping("/seguro")
    public void export(HttpServletResponse response) throws IOException, JRException, SQLException {
        response.setContentType("text/html");
        JasperPrint jasperPrint = null;
        jasperPrint = seguroReportService.generatePromissoria(1L);
        HtmlExporter htmlExporter = new HtmlExporter(DefaultJasperReportsContext.getInstance());
        htmlExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
        htmlExporter.setExporterOutput(new SimpleHtmlExporterOutput(response.getWriter()));
        htmlExporter.exportReport();
    }

Method to get the report file

public JasperPrint generatePromissoria(Long id) throws SQLException, JRException, IOException {
        Connection conn = jdbcTemplate.getDataSource().getConnection();

        String path = resourceLoader.getResource("classpath:/reports/SeguroReport.jrxml").getURI().getPath();

        JasperReport jasperReport = JasperCompileManager.compileReport(path);
        // Parameters for report
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("titulo", "Relatório de Seguros");
        parameters.put("ID", id);

        JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, conn);

        return print;
    }

page where is supposed to open the report

Report opening in another page


Answer:

I resolve the problem using the Iframe, but instead of passing the HTML on the src="", I made a onClick function on the button from my form targeting the name of the iframe.

<iframe name="my_iframe" src=""></iframe>

<button onclick="this.form.target='my_iframe'">My Button</button>

Question:

I've created a report with JasperReports 6.4.3 which is normally exported to PDF. Now, I'm trying to export this report to HTML as well. I don't want to create a HTML file via JasperExportManager, so I'm using JasperReport's HtmlExporter to wirte the report directly into an outputstream.

Her is my code:

public void exportToHtml(OutputStream outputStream, JRDataSource jrDataSource, Map<String, Object> parameter, File reportFile) 
      throws IOException {
    try {
      JasperPrint jasperprint = JasperFillManager.fillReport(reportFile.getAbsolutePath(), parameter, jrDataSource);
      HtmlExporter exporter = new HtmlExporter();

      SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(outputStream);
      Map<String, String> images = Maps.newHashMap();
      exporterOutput.setImageHandler(new HtmlResourceHandler() {
        @Override
        public void handleResource(String id, byte[] data) {
          System.err.println("id" + id);
          images.put(id, "data:image/jpg;base64," + Base64.encodeBytes(data));
        }

        @Override
        public String getResourcePath(String id) {
          return images.get(id);
        }
      });
      exporter.setExporterOutput(exporterOutput);
      exporter.setExporterInput(new SimpleExporterInput(jasperprint));

      SimpleHtmlExporterConfiguration exporterConfiguration = new SimpleHtmlExporterConfiguration();
      exporterConfiguration.setBetweenPagesHtml("<div style='page-break-after:always'></div>");
      exporter.setConfiguration(exporterConfiguration);
      exporter.exportReport();
    } catch (JRException jrException) {
      throw new IOException(jrException);
    }
}

The output looks good, but I need to add some styles to the HTML output. So I wonder if it is possible to add a reference to a css file to the exported html report.


Answer:

Similarly to how you are setting the between pages HTML, you could do the same for the header/footer with:

exporterConfiguration.setHtmlHeader("...");
exporterConfiguration.setHtmlFooter("...");

The default HTML code for header is set here and the one for footer is set here.

You need to match the opening/closing tags when modifying any of them.

Question:

I have a file jrxml (create using Jasper Report) and I want show it in an HTML page without using for example pdf file. I want do this conversion by server side (spring).

I call the java method from the page HTML using:

<FORM NAME="formTicket" ACTION='http://localhost:8080/movies/ticket' METHOD="GET"> 

and in java I have:

@RequestMapping(value = "/movies/ticket", method = RequestMethod.GET)//stampa ticket
public Document add(@RequestParam(value="id")int id, int numb) {//numb=numeroBigliettiDaStampare,Id=specificoMovie
    String xmlFile = serv.getPDF(id, numb);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try{
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(new StringReader(xmlFile)));
        return doc;
    }catch(Exception e){
        e.printStackTrace();
    }
    return null;
}

and the getPDF method, with JasperReports:

public String getPDF(int id, int number){
    String fileJrxml = "/home/salvador/workspace/serverMovies/src/main/webapp/resources/Jasper/ticket.jrxml";
    File jasperFileSource = new File(fileJrxml);
    //Log.debug("Crando il PDF");
      try
      {
          ArrayList<Movie> film=new ArrayList<Movie>();
         // film.add(moviedao.getMovie(id));
          Movie f; 
          int posto=1;
          int fila=1;
          for(int i=0; i<number; i++){
              f = new Movie(moviedao.getMovie(id).getTitle(),moviedao.getMovie(id).getActor(),moviedao.getMovie(id).getGenre(),moviedao.getMovie(id).getYear(),moviedao.getMovie(id).getLanguage());
              f.setDurata("80m");
              posto = posto +1;
              fila = fila +1;
              f.setPosto(posto+"");
              f.setFila(fila+"");
              f.setPrezzo("8.50");
              film.add(f);
          }


        JasperDesign jasperDesign = JRXmlLoader.load(jasperFileSource);

        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

        JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(film);

        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jrDataSource);

        String xmlStr = JasperExportManager.exportReportToXml(jasperPrint);

        //JasperExportManager.exportReportToPdfFile(jasperPrint, "/home/salvador/workspace/serverMovies/src/main/webapp/resources/ticket.pdf");

        //Log.debug("PDF creato");
        //System.out.println("Pdf file successfully generated.");

        return xmlStr;

      }//try
      catch (JRException e)
      {
        System.out.println("Error during the generation of PDF file.\n");
        e.printStackTrace();
      } //catch
      return "errore";
}//getPDF

I find the solution for this problem, this is my controller:

@RequestMapping(value = "/movies/ticket", method = RequestMethod.GET)//stampa ticket
public void getTicket(@RequestParam(value="id")int id, int numb, HttpServletResponse response) throws Exception {//numb=numeroBigliettiDaStampare,Id=specificoMovie
    byte[] xmlFile = serv.getPDF(id, numb);
    serv.streamReport(response, xmlFile, "report.pdf");
}//add

that call two methods, the first that create from JasperReport a byteStream:

public byte[] getPDF(int id, int number){
    String fileJrxml = "/home/salvador/workspace/serverMovies/src/main/webapp/resources/Jasper/ticket.jrxml";
    File jasperFileSource = new File(fileJrxml);
    //Log.debug("Crando il PDF");
      try
      {
          ArrayList<Movie> film=new ArrayList<Movie>();
         // film.add(moviedao.getMovie(id));
          Movie f; 
          int posto=1;
          int fila=1;
          for(int i=0; i<number; i++){
              f = new Movie(moviedao.getMovie(id).getTitle(),moviedao.getMovie(id).getActor(),moviedao.getMovie(id).getGenre(),moviedao.getMovie(id).getYear(),moviedao.getMovie(id).getLanguage());
              f.setDurata("80m");
              posto = posto +1;
              fila = fila +1;
              f.setPosto(posto+"");
              f.setFila(fila+"");
              f.setPrezzo("8.50");
              film.add(f);
          }//for i


        JasperDesign jasperDesign = JRXmlLoader.load(jasperFileSource);

        JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

        JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(film);

        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jrDataSource);

        byte[] data= JasperExportManager.exportReportToPdf(jasperPrint);

        return data;

      }//try
      catch (JRException e)
      {
        System.out.println("Error during the generation of PDF file.\n");
        e.printStackTrace();
      } //catch
      return null;
}//getPDF

and the second where I use the stream to show my JasperReport in the browser:

public void streamReport(HttpServletResponse response, byte[] data, String name) throws IOException
{
    response.setContentType("application/pdf");
    response.setHeader("Content-disposition", "attachment; filename=" + name);
    response.setContentLength(data.length);
    response.getOutputStream().write(data);
    response.getOutputStream().flush();
}// streamReport

Answer:

The easy way:

JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);

The most configurable:

JRPdfExporter exporter = new JRPdfExporter();             
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));

SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
//configuration.setConfigurations....;
exporter.setConfiguration(configuration);
exporter.exportReport();

For how to stream directly to browser in Spring context see

Stream directly to response output stream in handler method of Spring MVC 3.1 controller

The writer is what you pass as outputStream

Don't forget to add the header in your response

"Content-disposition", "filename=report.pdf" , display in page

"Content-disposition", "attachment; filename=report.pdf", download it

EDIT: I seen that you answered by yourself in question, to improve your code consider that you do not need to compile jrxml everytime.

@RequestMapping(value = "/movies/ticket", method = RequestMethod.GET)//stampa ticket
public void getTicket(@RequestParam(value="id")int id, int numb, HttpServletResponse response) throws Exception {
    //Use the complied file,you do not need to compile the jrxml everytime
    //Furthermore since its in src (I would have use class loader)
    String fileJasper = "/home/salvador/workspace/serverMovies/src/main/webapp/resources/Jasper/ticket.jasper";
    JasperReport report = (JasperReport)JRLoader.loadObject(fileJasper);
    JasperPrint jasperPrint = JasperFillManager.fillReport(report, null, getDataSource(id));
    response.setContentType("application/pdf");
    response.setHeader("Content-disposition", "attachment; filename=report.pdf");
    JasperExportManager.exportReportToPdfStream(jasperPrint, response);
}

Question:

I'm generating reports in java using Jasper Reports. I have things set up so that when a link to an item is clicked, a report based on that item is generated complete with chart image. The problem is, after generating the first report, any subsequent reports generated will use the same image as the first report. What I want is for each generated report to use it's own chart image. Not sure what I'm doing wrong.

NOTE: I know these methods are deprecated. I need to get things working on the images before I try to upgrade to new methods (which I could also use help with but will make a separate question when the time comes).

  else if (export_format != null && export_format.equalsIgnoreCase("HTML")) {
  jasperprint = JasperFillManager.fillReport(inFileStream, map, conn);
  JRHtmlExporter exporter = new JRHtmlExporter();
  HashMap imagesMap = new HashMap();
  request.getSession().setAttribute("IMAGES_MAP", imagesMap);
  request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperprint);

  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperprint);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
  exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
  exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
  exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
  exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
  exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "jasper/images?image=");
  exporter.exportReport();
}

Answer:

The likely reason for which you see the first chart in subsequent reports is that the browser caches the image at jasper/images?image=...

One easy way to avoid this is to add a random parameter to the images URLs:

  exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "jasper/images?random=" + java.util.UUID.randomUUID() + "&image=");