Domanda

I am building a report with JasperReportBuilder from a JDBC data source and afterward I export it to HTML by calling the toHtml method with a JasperHtmlExporterBuilder object parameter. I have been trying to get rid of extra space that exists above the text in each of my rows, column header row, and even my title. I have tried everything I can think of and have been searching online to no avail.

It seems most people have the opposite problem: they need to grow their rows to fit their text. I would like to force my rows to be the height of their contents. If that's not possible, I would be happy with restricting the height of my rows.

The HTML exporter appears to be controlling the height of my rows by inserting an image and setting its height.

Any help would be appreciated. Thanks!

Edit: Here is my code.

String imageServletUrl = "images?image=";

StringWriter writer = new StringWriter();

JasperHtmlExporterBuilder htmlExporter = Exporters.htmlExporter(writer);
htmlExporter.setImagesURI(imageServletUrl);

SqlRowSet rowSet = getData(databaseIpAddr, portNumber, databaseName, databaseUser, databasePassword);

JRDataSource ds = createDataSource(rowSet);

JasperReportBuilder builder = DynamicReports.report();

IntegerType intType = DynamicReports.type.integerType();
DateType dateType = DynamicReports.type.dateType();

int rowHeightPx = 20;

TextColumnBuilder<Integer> col1 = col
   .column(COL_TITLE_RESULT_NUM, RESULTS_COL_TITLE_RESULT_NUM, intType)
   .setWidth(35)
   .setFixedHeight(rowHeightPx)
   .setStretchWithOverflow(true);   

... create other eight columns ...

StyleBuilder titleStyle = stl.style()
   .bold()
   .setFontSize(22)
   .setHorizontalAlignment(HorizontalAlignment.CENTER);

StyleBuilder columnTitleStyle = stl.style()
   .bold()
   .setAlignment(HorizontalAlignment.CENTER, VerticalAlignment.MIDDLE)
   .setBorder(stl.pen1Point());

StyleBuilder columnStyle = stl.style()
   .setAlignment(HorizontalAlignment.CENTER, VerticalAlignment.MIDDLE)
   .setLineSpacing(LineSpacing.SINGLE)
   .setBorder(stl.pen1Point());

TextFieldBuilder<String> titleBuilder = DynamicReports.cmp
   .text(selectedAgency)
   .setStyle(titleStyle)
   .setFixedHeight(20);

TextFieldBuilder<String> subtitleBuilder = DynamicReports.cmp
   .text("Start Time: " + startDate + " " + getStartTime() + ", End Time: " + endDate + " " + getEndTime())
   .setFixedHeight(20);

TextFieldBuilder<String> noDataMsgBuilder = DynamicReports.cmp
   .text(NO_DATA_MSG)
   .setStyle(columnStyle);

builder
   .title(titleBuilder)
   .addPageHeader(subtitleBuilder)
   .setColumnTitleStyle(columnTitleStyle)
   .columns(col1, col2, col3, col4, col5, col6, col7, col8, col9)
   .setColumnStyle(columnStyle)
   .highlightDetailOddRows()
   .noData(titleBuilder, subtitleBuilder, noDataMsgBuilder)
   .setWhenNoDataType(WhenNoDataType.NO_DATA_SECTION);

String reportHtml;
try {
   // this seems to be required to get to the DynamicReports images
   getContext().getRequest().getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, builder.toJasperPrint());

   builder.toHtml(htmlExporter);

   reportHtml = writer.toString();
}
catch (DRException e) 
{
   e.printStackTrace();
   reportHtml = "There was an error generating the report";
}
È stato utile?

Soluzione

Edit: Below solution works fine only with DynamicJasper library rather than DynamicReports. There must be similar operations in DR API as well i believe (not sure though - my exposure with DR is only very limitted :().

A combination of DynamicReportBuilder::setHeaderHeight(..), setDetailHeight(..) and JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN parameter setting must do it?

// build dynamicreport instance
DynamicReportBuilder dynamicReportBuilder = new DynamicReportBuilder();

dynamicReportBuilder.setHeaderHeight(50); //sets the column header height
dynamicReportBuilder.setDetailHeight(50); //sets the detail band rows height

DynamicReport dynamicReport = dynamicReportBuilder.build();

// build jasperprint..
JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(
    dynamicReport, new ClassicLayoutManager(), dataSource, new HashMap<String, Object>());

// export as html
JRExporter exporter = new JRHtmlExporter();

// tell jasper not to use images for aligning
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, targetFile);

exporter.exportReport();

Altri suggerimenti

adapt reports band heigts to heigth of your row for each band, make sure that you dont have any emty spaces between your rows. If you want to have "emty row" create an emty text field with width of the corresponding band. Think about the report as about grid.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top