Download personalized HTML design

Situation

When exporting to HTML, the resulting code appears as follows:

<html>
    <head>
        <title></title>
        <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
        <style type="text/css">
            a {text-decoration: none}
        </style>
    </head>
    <body vlink="#000000" text="#000000" link="#000000" alink="#000000">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <td width="50%">&nbsp;</td>
                    <td align="center">
                        <!-- The report -->
                    </td>
                    <td width="50%">&nbsp;</td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

Challenge

The report is currently centered on the page, but it should actually be aligned to the left.

Past Attempt

Initially, trying JRHtmlExporter's HTML_HEADER parameter seemed like a viable solution. However, due to deprecation of certain classes, an alternative approach was required:

JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, 
    "<html>"+
    "<head>"+
    "  <title></title>"+
    "  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>"+
    "  <link rel=\"stylesheet\" type=\"text/css\" href=\"css/jasper.css\" />"+
    "  <style type="text/css">"+
    "    a {text-decoration: none}"+
    "  </style>"+
    "</head>"+
    "<body text="#000000" link="#000000" alink="#000000" vlink="#000000">"+
    "<table width="100%" cellpadding="0" cellspacing="0" border="0">"+
    "<tr><td width="50%">&nbsp;</td><td align="center">");
exporter.exportReport();

To rectify this issue, utilizing the

net.sf.jasperreports.export.HtmlExporter
and
net.sf.jasperreports.export.SimpleHtmlReportConfiguration
classes became necessary, in the following manner:

HtmlExporter exporterHTML = new HtmlExporter();
SimpleExporterInput exporterInput = new SimpleExporterInput(report.getJasperPrint());
exporterHTML.setExporterInput(exporterInput);
HtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(out);
exporterHTML.setExporterOutput(exporterOutput );

SimpleHtmlReportConfiguration reportExportConfiguration = new SimpleHtmlReportConfiguration();
reportExportConfiguration.setWhitePageBackground(false);
reportExportConfiguration.setRemoveEmptySpaceBetweenRows(true);
exporterHTML.setConfiguration(reportExportConfiguration);

exporterHTML.exportReport(); 

How would you go about resolving this issue?

Context

  • JasperReports version: 5.5.2
  • Java version: 1.6.0_38

Answer №1

The parameter HTML_HEADER in JRHtmlExporterParameter has been updated to:

HtmlExporterConfiguration.getHtmlHeader()

Here is an example:

public byte[] exportHtml(final JasperPrint print) {
    final Exporter exporter = new HtmlExporter();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    exporter.setConfiguration(createHtmlConfiguration());
    exporter.setExporterOutput(new SimpleHtmlExporterOutput(out));
    exporter.setExporterInput(new SimpleExporterInput(print));
    exporter.exportReport();

    return out.toByteArray();
}

private HtmlExporterConfiguration createHtmlConfiguration()
        throws IOException {
    SimpleHtmlExporterConfiguration shec
            = new SimpleHtmlExporterConfiguration();

    shec.setHtmlHeader(getHtmlHeader());
    shec.setHtmlFooter(getHtmlFooter());

    return shec;
}

private String getHtmlHeader() {
    StringBuffer sb = new StringBuffer();
    sb.append("<html>");
    sb.append("<head>");
    sb.append("  <title></title>");
    sb.append("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>");
    sb.append("  <style type=\"text/css\">");
    sb.append("    a {text-decoration: none}");
    sb.append("  </style>");
    sb.append("</head>");
    sb.append("<body text=\"#000000\" link=\"#000000\" alink=\"#000000\" vlink=\"#000000\">");
    sb.append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
    sb.append("<tr><td align=\"left\">");

    return sb.toString();
}

private String getHtmlFooter() {
  // Ensure closing tags from getHtmlHeader() are included...
}

An improved approach would involve utilizing external resources (such as databases, files, or web pages) for the HTML content instead of hardcoded strings.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Design elements using CSS within the <th> tags

When working with HTML tables, the use of a <th> element allows for the implementation of a scope attribute. This attribute serves to indicate whether the header cell is associated with its subsequent column, row, or group. Is it feasible to leverage ...

I created a customized version of jQuery tabs, but I am looking for an external link to display the tab content and to style the original navigation

I've been experimenting with modifying a tabs script in jQuery that I came across. It seems like my attempt to enhance it has made things more complex than necessary. While I know creating tabs with jQuery is simple, I wanted to create my own version ...

The end of the /if in the small template system fails to correctly detect the desired condition

If I write the following line on its own, he cuts it without any reason. So...: Greetings, I'm in need of assistance regarding an issue that is currently beyond my understanding. I'm facing a challenge with a small TPL-System which requires so ...

Showing no background color until the user lifts their finger

I am currently in the process of developing a website. The navigation on the website starts off as transparent (background: transparent) when opened, but as soon as you start scrolling, it should transition to a colorful state with shades like grey, midnig ...

Extracting the URL of the @font-face declaration in CSS with the use of JavaScript

Currently, my CSS file contains numerous @font-face tags. Each tag specifies a unique font-family and src URL. @font-face{ font-family: Garamond; src: url('ePrintFonts/pcl_91545.ttf'); } @font-face{ font-family: C ...

Using a twig loop to display just a single table cell

The following twig loop is used to generate data: {% for reservationDate, rooms in data.pricingTable %} <tr> <td> {% for room in rooms %} <tr> <td ...

Can a radio button set be designed in two dimensions?

There are three sets of data (dataX, dataY, dataZ) that need to be assigned unique values out of a selection of three options (valueA, valueB, valueC). Each piece of data should be linked to one value and each value should have only one data associated wit ...

Refresh the Content of a Page Using AJAX by Forcing a Full Reload

I have a webpage that automatically updates a section using jQuery AJAX every 10 seconds. Whenever I modify the CSS or JavaScript of that page, I would like to include a script in the content fetched via AJAX to trigger a full page reload. The page is ac ...

Several features - Second function malfunctioning

The initial inquiry is effective. However, the subsequent one is encountering issues as it is failing to confirm if an email contains the "@" symbol. My attempted solution involved reordering the functions related to email validation. <body onload="ch ...

Show ng-message when email is invalid in Angular Material without using ng-pattern

I need to show an error message that says Please enter valid email. when an invalid email is entered, but I cannot use the ng-pattern attribute with this specific regex pattern. <md-input-container class="md-block" flex-gt-xs> <label>Ema ...

Converting forward slashes (/) to %2f in an AJAX request

I keep encountering this question, but no one seems to have a satisfactory answer. My problem centers around an ajax request that utilizes a value from a text field in the format: 00000000/relay_1A. Whenever I attempt to submit, I receive a 404 error. St ...

Utilize jQuery and JavaScript dynamically in an HTML document on iOS devices

Having some trouble with this code snippet and getting it to work as intended. - (void)viewDidLoad { [super viewDidLoad]; _webview.delegate = self; [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBun ...

Slanted lines HTML CSS

I am encountering an issue with my HTML and CSS coding. The design requires a zig-zag "border" at the top and bottom of the page, creating a white paper space in between. The paper needs to be positioned underneath the zig-zag borders. Currently, this is ...

What occurs when Click events are triggered on an <object> element?

I have set up a div, and inside that container, I embedded an SVG image using object (which I plan to manipulate later...). <div id="click-me"> some random Text <object data="some.svg" /> </div> Next, I added event listeners for t ...

How to position two divs in a row using Bootstrap

Looking at the output below: https://i.stack.imgur.com/pQM8F.png The blue circle is placed in one div, while the text is in another. I'm perplexed by why the first div appears on top and the second doesn't seem to align properly. Here's th ...

Discovering a deeply nested div or class using Beautiful Soup

Recently, I came across this URL: All I want to do is extract the zestimate and include it in a list. The specific class where it's located is: class="Text-c11n-8-65-2__sc-aiai24-0 eUxMDw". I attempted to target it at a higher level in th ...

Removing background color and opacity with JavaScript

Is there a way to eliminate the background-color and opacity attributes using JavaScript exclusively (without relying on jQuery)? I attempted the following: document.getElementById('darkOverlay').style.removeProperty("background-color"); docume ...

Tab knockout binding

I have a section in my HTML with 2 tabs. The default tab is working properly, but when I attempt to switch to the other tab, I encounter an error. Can anyone provide assistance in determining why this error occurs? Here is the HTML code: <ul class="na ...

When a div is modified through an ajax function, I aim to activate a JavaScript function

Is there a way to automatically execute a javascript function once the status updates to "Upload successful"? I am uncertain about how to accomplish this task. Following a multi file upload, the status will switch based on whether it was successful or en ...

Implement AngularJS to ensure that scripts are only loaded after the page has finished rendering

I am having trouble implementing the TripAdvisor widget on my website. It functions correctly when the page is refreshed, but it does not appear when navigating through links. Additionally, an error message is displayed stating that the document could not ...