use of net.sf.jasperreports.engine.JRExporter in project DynamicJasper by intive-FDV.
the class FormatInfoRegistry method getExporter.
public JRExporter getExporter(final String _format) {
checkFormat(_format);
final JRExporter exporter = FORMAT_INFO.get(_format).getExporterInstance();
// FIXME migrate to Exporter
// exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
return exporter;
}
use of net.sf.jasperreports.engine.JRExporter in project DynamicJasper by intive-FDV.
the class ReportWriterFactory method getReportWriter.
/**
* Returns a ReportWriter that which will use memory or a file depending on the parameter PAGES_THRESHOLD
* @param _jasperPrint
* @param _format
* @param _parameters
* @return
*/
public ReportWriter getReportWriter(final JasperPrint _jasperPrint, final String _format, final Map<JRExporterParameter, Object> _parameters) {
final JRExporter exporter = FormatInfoRegistry.getInstance().getExporter(_format);
exporter.setParameters(_parameters);
if (_jasperPrint.getPages().size() > PAGES_THRESHHOLD) {
return new FileReportWriter(_jasperPrint, exporter);
} else {
return new MemoryReportWriter(_jasperPrint, exporter);
}
}
use of net.sf.jasperreports.engine.JRExporter in project bboss by bbossgroups.
the class AbstractJasperReportsSingleFormatView method renderReport.
/**
* Perform rendering for a single Jasper Reports exporter, that is,
* for a pre-defined output format.
*/
@Override
protected void renderReport(JasperPrint populatedReport, Map<String, Object> model, HttpServletResponse response) throws Exception {
JRExporter exporter = createExporter();
Map mergedExporterParameters = getConvertedExporterParameters();
if (!CollectionUtils.isEmpty(mergedExporterParameters)) {
exporter.setParameters(mergedExporterParameters);
}
if (useWriter()) {
renderReportUsingWriter(exporter, populatedReport, response);
} else {
renderReportUsingOutputStream(exporter, populatedReport, response);
}
}
use of net.sf.jasperreports.engine.JRExporter in project bgerp by Pingvin235.
the class JasperReport method addPrintQueueDocumentToOutputStream.
public void addPrintQueueDocumentToOutputStream(DynActionForm form, List<Object[]> data, Queue queue, PrintType printType, OutputStream ostream) throws Exception {
FastReportBuilder drb = new FastReportBuilder();
drb.setTitle(form.l.l("Очередь") + " : " + queue.getTitle()).setSubtitle(form.l.l("Дата генерации") + " " + TimeUtils.format(new Date(), TimeUtils.FORMAT_TYPE_YMD)).setPrintBackgroundOnOddRows(true).setOddRowBackgroundStyle(oddRowStyle).setColumnsPerPage(1).setUseFullPageWidth(true).setColumnSpace(5).setAllowDetailSplit(false).setMargins(0, 0, 0, 0).setDefaultStyles(titleStyle, titleStyle, headerStyle, defaultStyle);
if (printType != null) {
if (printType.getOrientation().equals(PrintType.ORIENTATION_LANDSCAPE)) {
drb.setPageSizeAndOrientation(Page.Page_A4_Landscape());
} else {
drb.setPageSizeAndOrientation(Page.Page_A4_Portrait());
}
} else {
drb.setPageSizeAndOrientation(Page.Page_A4_Landscape());
}
List<ColumnConf> printColumns = null;
if (printType != null) {
printColumns = queue.getColumnConfList(printType.getColumnIds());
List<Integer> widths = printType.getColumnWidths();
for (int i = 0; i < printColumns.size(); i++) {
ColumnConf col = printColumns.get(i);
Integer width = widths.get(i);
var column = ColumnBuilder.getNew().setColumnProperty(String.valueOf(col.getColumnId()), String.class.getName()).setTitle(col.getColumnConf().get("title")).setWidth(width).setStyle(defaultStyle).setHeaderStyle(headerStyle).build();
drb.addColumn(column);
}
} else {
printColumns = queue.getMediaColumnList("print");
for (ColumnConf col : printColumns) {
int width = 50;
Style style = defaultStyle;
if ("description".equals(col.getColumnConf().get("value"))) {
width = 300;
style = descriptionStyle;
}
AbstractColumn column = ColumnBuilder.getNew().setColumnProperty(String.valueOf(col.getColumnId()), String.class.getName()).setTitle(col.getColumnConf().get("title")).setWidth(width).setStyle(style).setHeaderStyle(headerStyle).build();
drb.addColumn(column);
}
}
final int size = printColumns.size();
List<Map<String, Object>> jdata = new ArrayList<Map<String, Object>>();
for (Object[] objects : data) {
Map<String, Object> map = new HashMap<String, Object>(size);
jdata.add(map);
for (int i = 0; i < size; i++) {
ColumnConf col = printColumns.get(i);
map.put(String.valueOf(col.getColumnId()), objects[i]);
}
}
DynamicReport dr = drb.build();
JRDataSource ds = new JRBeanCollectionDataSource(jdata);
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ostream);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, Font.PDF_ENCODING_CP1251_Cyrillic);
exporter.exportReport();
}
use of net.sf.jasperreports.engine.JRExporter in project struts by apache.
the class JasperReportsResult method doExecute.
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
// Will throw a runtime exception if no "datasource" property. TODO Best place for that is...?
initializeProperties(invocation);
LOG.debug("Creating JasperReport for dataSource = {}, format = {}", dataSource, format);
HttpServletRequest request = invocation.getInvocationContext().getServletRequest();
HttpServletResponse response = invocation.getInvocationContext().getServletResponse();
// TODO Set content type to config settings?
if ("contype".equals(request.getHeader("User-Agent"))) {
try (OutputStream outputStream = response.getOutputStream()) {
response.setContentType("application/pdf");
response.setContentLength(0);
} catch (IOException e) {
LOG.error("Error writing report output", e);
throw new ServletException(e.getMessage(), e);
}
return;
}
// Construct the data source for the report.
ValueStack stack = invocation.getStack();
ValueStackDataSource stackDataSource = null;
Connection conn = (Connection) stack.findValue(connection);
if (conn == null) {
boolean evaluated = parsedDataSource != null && !parsedDataSource.equals(dataSource);
boolean reevaluate = !evaluated || isAcceptableExpression(parsedDataSource);
if (reevaluate) {
stackDataSource = new ValueStackDataSource(stack, parsedDataSource, wrapField);
} else {
throw new ServletException(String.format("Error building dataSource for excluded or not accepted [%s]", parsedDataSource));
}
}
if ("https".equalsIgnoreCase(request.getScheme())) {
// set the the HTTP Header to work around IE SSL weirdness
response.setHeader("CACHE-CONTROL", "PRIVATE");
response.setHeader("Cache-Control", "maxage=3600");
response.setHeader("Pragma", "public");
response.setHeader("Accept-Ranges", "none");
}
// Determine the directory that the report file is in and set the reportDirectory parameter
// For WW 2.1.7:
// ServletContext servletContext = ((ServletConfig) invocation.getInvocationContext().get(ServletActionContext.SERVLET_CONFIG)).getServletContext();
ServletContext servletContext = invocation.getInvocationContext().getServletContext();
String systemId = servletContext.getRealPath(finalLocation);
Map<String, Object> parameters = new ValueStackShadowMap(stack);
File directory = new File(systemId.substring(0, systemId.lastIndexOf(File.separator)));
parameters.put("reportDirectory", directory);
parameters.put(JRParameter.REPORT_LOCALE, invocation.getInvocationContext().getLocale());
// put timezone in jasper report parameter
if (timeZone != null) {
timeZone = conditionalParse(timeZone, invocation);
final TimeZone tz = TimeZone.getTimeZone(timeZone);
if (tz != null) {
// put the report time zone
parameters.put(JRParameter.REPORT_TIME_ZONE, tz);
}
}
// Add any report parameters from action to param map.
boolean evaluated = parsedReportParameters != null && !parsedReportParameters.equals(reportParameters);
boolean reevaluate = !evaluated || isAcceptableExpression(parsedReportParameters);
Map reportParams = reevaluate ? (Map) stack.findValue(parsedReportParameters) : null;
if (reportParams != null) {
LOG.debug("Found report parameters; adding to parameters...");
parameters.putAll(reportParams);
}
ByteArrayOutputStream output;
JasperPrint jasperPrint;
// Fill the report and produce a print object
try {
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(systemId));
if (conn == null) {
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, stackDataSource);
} else {
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);
}
} catch (JRException e) {
LOG.error("Error building report for uri {}", systemId, e);
throw new ServletException(e.getMessage(), e);
}
// Export the print object to the desired output format
try {
if (contentDisposition != null || documentName != null) {
final StringBuilder tmp = new StringBuilder();
tmp.append((contentDisposition == null) ? "inline" : contentDisposition);
if (documentName != null) {
tmp.append("; filename=");
tmp.append(documentName);
tmp.append(".");
tmp.append(format.toLowerCase());
}
response.setHeader("Content-disposition", tmp.toString());
}
// TODO: replace deprecated logic
JRExporter exporter;
switch(format) {
case FORMAT_PDF:
response.setContentType("application/pdf");
exporter = new JRPdfExporter();
break;
case FORMAT_CSV:
response.setContentType("text/csv");
exporter = new JRCsvExporter();
break;
case FORMAT_HTML:
response.setContentType("text/html");
// IMAGES_MAPS seems to be only supported as "backward compatible" from JasperReports 1.1.0
Map imagesMap = new HashMap();
request.getSession(true).setAttribute("IMAGES_MAP", imagesMap);
exporter = new HtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + imageServletUrl);
// Needed to support chart images:
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint);
break;
case FORMAT_XLS:
response.setContentType("application/vnd.ms-excel");
exporter = new JRXlsExporter();
break;
case FORMAT_XML:
response.setContentType("text/xml");
exporter = new JRXmlExporter();
break;
case FORMAT_RTF:
response.setContentType("application/rtf");
exporter = new JRRtfExporter();
break;
default:
throw new ServletException("Unknown report format: " + format);
}
evaluated = parsedExportParameters != null && !parsedExportParameters.equals(exportParameters);
reevaluate = !evaluated || isAcceptableExpression(parsedExportParameters);
Map exportParams = reevaluate ? (Map) stack.findValue(parsedExportParameters) : null;
if (exportParams != null) {
LOG.debug("Found export parameters; adding to exporter parameters...");
exporter.getParameters().putAll(exportParams);
}
output = exportReportToBytes(jasperPrint, exporter);
} catch (JRException e) {
LOG.error("Error producing {} report for uri {}", format, systemId, e);
throw new ServletException(e.getMessage(), e);
} finally {
try {
if (conn != null) {
// avoid NPE if connection was not used for the report
conn.close();
}
} catch (Exception e) {
LOG.warn("Could not close db connection properly", e);
}
}
response.setContentLength(output.size());
// Will throw ServletException on IOException.
writeReport(response, output);
}
Aggregations