JasperReports: how to add font not to application class path

I am trying to use a font that is not installed on my local OS with JasperReports . The jasper report is used as follows:

 <textField> <reportElement x="0" y="0" width="137" height="20"/> <textElement> <font fontName="Corbel" size="12"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$F{something}]]></textFieldExpression> </textField> 

A font named Corbel was exported as a font extension (using iReport) and is contained in a file (Corbel.jar) in a folder on my system. I am adding this extension to the application class path using the following code:

 ClassLoader cl = new URLClassLoader(new URL[] {new URL("file:///D:/path/to/Corbel_jar_folder/")}); param = new HashMap(); param.put(JRParameter.REPORT_CLASS_LOADER, cl); jasperReport = JasperCompileManager.compileReport("d:/path/to/Report_with_Corbel_font.jrxml"); jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource()); 

After filling out the report, I export it using JRPdfExporter . However, in the result pdf file, the element does not have the Corbel font applied. I did not include pdf export because I think the problem is somewhere with filling. I searched and read numerous posts and questions related to using / including fonts (i.eont extensions) in JasperReports ; yet I don’t know where the error or problem is. Any help would be greatly appreciated.

Thank you in advance! (sorry for the indentation in the bad code, and I hope I have included enough details)

+6
source share
3 answers

The problem was that font extension extensions were loaded from the stream context class loader and from JRParameter.REPORT_CLASS_LOADER or JRExporterParameter.CLASS_LOADER .

Therefore, in my case, the current (initial) thread class loader had to be saved, we needed to do something like Thread.currentThread().setContextClassLoader(cl) , where cl was the context of the application based on JasperReports , and then the thread context classloader was returned to the original.

The answer to this question and details are available here .

I hope this answer helps other people encounter similar problems (fonts).

+7
source

Jasper Reports: adding custom fonts

  1. Get the font files you want to use (TTF, EOT, SVG or WOFF (OTF converted to TTF also works))
  2. Add fonts to Jaspersoft Studio (or iReport) using the user interface. (Window-> Settings-> Jaspersoft Studio-> Fonts)

    Add font

    You must provide the details as in the picture. Specifically, the PDF font name field is misleading. We need to specify the path to this font in the font-extention.jar file, which we will create in the next step.

    Now you can view the font in your report.

    Font added

  3. Export the fonts to the fonts-extention.jar file for use in your application. Select and export the required fonts. (Window-> Settings-> Jaspersoft Studio-> Fonts-> Select and Export)

  4. Take care of integrating banks into your build. If you use maven for your project, you can integrate it as follows.

  <dependency> <groupId>local.jasperFont</groupId> <artifactId>local.jasperFont</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${basedir}/src/main/resources/reports/fonts-extension.jar</systemPath> </dependency> 

And add fonts-extension.jar to the project resources directory / reports.

0
source

We could use Google Noto fonts with Jasper with these steps.

  1. Packing the NotoSans family into a jar file, as indicated in the Jasper documentation.

  2. Uploaded this jar to our Maven repository

  3. Define a dependency in
 <dependency> <groupId>com.crunchtime.shared.fonts</groupId> <artifactId>notosans</artifactId> <version>1.0</version> </dependency> 
  1. And then use in ours. I think this is the hardest part, but that’s how we did it

private static final String DEFAULT_PDF_STYLE = "defaultPdf";

private static end line DEFAULT_FONT_FAMILY = "NotoSans";

private static final String CJK_FONT = "fonts / NotoSansCJKtc-Regular.otf";

  if (language-other-than-English) { defaultStyle.setPdfFontName(CJK_FONT); } else { defaultStyle.setFontName(DEFAULT_FONT_FAMILY); } 

We saw this null pointer exception, but the key was to explicitly set the font for the PDF

0
source

Source: https://habr.com/ru/post/1314351/


All Articles