Arquivo da tag: JasperReports

Style a text field in JasperReports

Yes, you can apply style for textField elements.

iReport using

The sample of report’s template:
[code type=xml]





















:” + $F{TASKS_SUBTASK} + “ \n “]]>



The quote from iReport Ultimate Guide about markup attribute:

This Markup attribute allows you to format the text using a specific markup language. This is extremely useful when you have to print some text that is pre-formatted, that is, in HTML or RTF. Simple HTML style tags (like for bold and for Italic) can be used in example to highlight a particular chunk of the text. The possible values are as follows:

  • None
    No processing on the text is performed, and the text is printed exactly like it is provided.
  • Styled
    This markup is capable to format the text using a set of HTML-like tags and it is pretty popular in the Java environments. It allows to set a specific font for chunks of text, color, background, style and so on. It’s often good enough to format the text programmatically.
  • HTML
    If you want to print some HTML text into your report, this is what you need, but it’s primary use is to format text, so don’t expect to be able to print tables or add images.
  • RTF
    Setting the markup to this value, the content will be interpreted as RTF code. RTF is a popular document format stored in pure text. The little piece of text saying “this is a text formatted in RTF” in Illustration 19 has been generated using the string:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fnil\fprq2\fcharset0 Swift;}} {*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs20 This is a text \f1\fs52 formatted \f0\fs20 in RTF\par }
    The string is actually an RTF file created using a simple word processor.
  • Report font
    This is the name of a preset font, from which will be taken all the character properties. This attribute is deprecated and it is there only for compatibility reason (that’s why it the label is strukethrough. In order to define a particular style of text to use all over your document, use a style.

The sample of using markup is here.

You can use style for setting:

 

  • Common properties
  • Graphics properties
  • Border and padding properties
  • Text propertiesThe another sample is here.

    DynamicJasper API using

    In case using DynamicJasper API you can set style with help of ar.com.fdvs.dj.domain.builders.ColumnBuilder class:
    [code type=java]
    AbstractColumn columnState = ColumnBuilder.getNew()
    .addColumnProperty(“state”, String.class.getName())
    .addTitle(“State”).addWidth(new Integer(85))
    .addStyle(detailStyle).addHeaderStyle(headerStyle).build();

    The sample is here.

    JasperReports API using

    In case using JasperReports API you can set style, for example, with help of net.sf.jasperreports.engine.base .JRBasePrintText class:
    [code type=java]
    JRPrintText text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
    text.setStyle(boldStyle);

    The sample is here.

 

Java – Font ‘Arial’ is not available to the JVM

sudo apt-get update
sudo apt-get install ttf-mscorefonts-installer

http://stackoverflow.com/questions/3811908/font-is-not-available-to-the-jvm-with-jasper-reports

https://developer.jboss.org/wiki/JBossAS713ProblemasComFontesJasperReportsNoLinux

Passando imagem como parametro no JasperReports

Passando a Imagem em vez do InputStream. Primeiro é preciso carregar a imagem e defini-lo no mapa de parâmetros:

BufferedImage image = ImageIO.read(getClass().getResource("/images/IMAGE.png"));
parameters.put("logo", image );

Agora mude no xml do relatório, adicionando a “imageExpression”:

<image>
  <reportElement x="324" y="16" width="154" height="38"/>
  <imageExpression><![CDATA[$P{logo}]]></imageExpression>
</image>

Referência: http://stackoverflow.com/questions/13598655/how-to-add-image-as-parameter-from-projects-classpath-in-jasper-reports

Geração de relatório com JasperReports em Api Rest

Basta colocar no seu método esse bloco de código.

Map<String, Object> params = new HashMap<>();
JasperReport jasperReport = JasperCompileManager.compileReport(getClass().getResourceAsStream(getPathReport()));
JRBeanCollectionDataSource colecaoDeDados = new JRBeanCollectionDataSource(dao.findeAll());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, colecaoDeDados);
 
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "inline; filename=consulta.xls");
 
final OutputStream outStream = response.getOutputStream();
JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outStream));
exporter.exportReport();