开发者

JasperReports: How to add font not in the application classpath

开发者 https://www.devze.com 2023-01-05 11:36 出处:网络
I am trying to use a font, that is not installed on my local OS, with a JasperReports. The jasper report uses in this way:

I am trying to use a font, that is not installed on my local OS, with a JasperReports. The jasper report uses in this way:

<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>

The font named Corbel was exported as a font extension (using iReport) and is contained in file (Corbel.jar), in folder, on my system. I add this extension to the classpath of the application, using a code as follows:

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

After the report is filled, I export it using a JRPdfExporter. However, in the result pdf file, the element does not have the Corbel font applied. I have not included the pdf exporting, because I think that the problem is somewhere with the filling. I have searched and read numerous posts and questions related to using/including fonts (i.e.font extensions) in JasperReports; still I am not aware of where the mistake or the problem resides. Any help would be greatly appreciated.

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


The problem was that the loading of font extensions jars is done from the thread context classloader and from the JRParameter.REPORT_CLASS_LOADER, nor the JRExporterParameter.CLASS_LOADER.

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

The question has been answered and details are available here.

I hope this answer will help others facing similar (font) issues.


Jasper Reports: Adding Custom Fonts

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

    JasperReports: How to add font not in the application classpath

    You need to give details as in the image. Specially PDF Font Name field is misleading. We need to give the path here for this font in font-extention.jar which we create in next step.

    Now you can preview the font in your report.

    JasperReports: How to add font not in the application classpath

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

  4. Take care of integrating the jar into your build. If you are using maven for your project you can integrate 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 the fonts-extension.jar in project resources/reports directory.


We could use Google Noto fonts with Jasper with these steps

  1. Packaging the NotoSans family into a jar file as instructed in Jasper documentation.

  2. Uploaded this jar to our maven repo

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

private static final String DEFAULT_PDF_STYLE = "defaultPdf";

private static final String 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 had been seeing this null pointer exception but the key was to set explicitly set the font for the pdf

0

精彩评论

暂无评论...
验证码 换一张
取 消