When export a report to PDF change the font
Asked Answered
F

6

10

I'm using Jaspersoft Studio 5.2, and I did a report with Courier New font, and when I exports this to PDF, it's change the font to Arial.

I'm using just the Studio tool. When I preview the report is all right, but when I export it's happens.

What can I do with my report to export with Courier New font?

My textField model that I'm using in report:

<textField pattern="dd/MM/yyyy  HH:mm:ss">
    <reportElement uuid="f50b279a-b480-498f-8af7-be7e23c4b80b" x="415" y="11" width="105" height="10"/>
    <textElement>
        <font fontName="Courier New" size="8"/>
    </textElement>
    <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
</textField>
Furbish answered 7/10, 2013 at 19:20 Comment(2)
I just change the value on fontName to Courier New and nothing anymore.. the tool show to me that´s all right.. but when i print or export to pdf, is always Arial..Furbish
There is no easy way to do this automatically, like before call JasperFillManager.fillReport(report, parameters, connection); and embed all used fonts ?Uzzia
E
28

You should use the Font Extensions mechanism.

Steps

Creating font in Jaspersoft Studio

You should create a new font with Window -> Preferences dialog:

enter image description here

With Font Family dialog you can set ttf file for your font:

enter image description here

Creating report's style

You should create a new style with help of context menu:

enter image description here

After that you can set font for this style:

enter image description here

And after that you can use this new style:

enter image description here

The sample

The jrxml file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version last-->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test_courier" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2010b76c-0fe5-4a27-9c86-6846a6f9cc6a">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Sample DB"/>
    <style name="CustomFont" fontName="Courier New"/>
    <queryString>
        <![CDATA[select task from tasks]]>
    </queryString>
    <field name="TASK" class="java.lang.String"/>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement uuid="9205f787-f3b4-4b21-b2d9-19f52824e187" style="CustomFont" x="280" y="36" width="70" height="20"/>
                <textElement/>
                <text><![CDATA[Title]]></text>
            </staticText>
        </band>
    </title>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="f23e8674-c45d-4dc7-92f3-93e5d0b41c16" style="CustomFont" x="0" y="0" width="70" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{TASK}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The report's design in Studio:

enter image description here

And the result will be (pdf file generated by Jaspersoft Studion and opened in Adobe Reader):

enter image description here


Note:

You can find more info about Font Extensions here

Edi answered 7/10, 2013 at 20:24 Comment(2)
i face that arabic letters appear as ????, so i add the Arial font and exported as jar (same Alex K explain without create style) and used in the glass path then the arabic letters are appeared.Beerbohm
I have similar problem. I use Arial but Jasper changes it to Helvetica and only Helvetica is embedded in PDF. Arial is installed in my OS so I do not understand why I have to export Arial font from Jasper Studio and add it to classpath to make it work. As I understand PDF has two types of fonts: embedded and non-embedded. Why Arial is not used as non-embedded font?Endlong
G
4

The same problem happened to me. I tried the following way to find a solution -

If you want a font to be embedded in your Jasper Report PDF file viewer and that font is not available in your font list, all you have to do is:

  • First download the .ttf file of your font you have to embed.
  • After that, Go to Window -> Preferences -> Jaspersoft Studio -> Fonts. You will see a box to add your .ttf file of your font.
  • Add the .ttf file of your font and tick Embed this font to PDF. After that you will see your font in the list of fonts with the name as you gave earlier.
  • Select the font and export it to .jar file. And add that jar file of your font to the class path of your application if you want to view the pdf from your application.

That's it. Thank You.

Giesser answered 29/12, 2016 at 8:40 Comment(0)
H
3

If your report is not being correctly rendered for any fonts, you may have some compatibility issue with iReport / JasperStudio and jasper libs in your system. I had this problem when trying to export a report to pdf using a jasper file. The solution was to compile the report inside my code like below:

    InputStream reportStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("/reports.jrxml");
    JasperReport report = JasperCompileManager.compileReport(reportStream);
    jasperPrint = JasperFillManager.fillReport(report, parameters, dataSource);
Hake answered 13/10, 2014 at 14:33 Comment(0)
C
1

Similar issue i faced and it got resolved by adding required font ttf file to jar or existing jasperreport-fonts.jar and adding its dependency to application.

Chuckle answered 18/11, 2020 at 16:17 Comment(0)
C
0

The same thing happened to me, I did all the configuration as indicated here, and it did not work, it still did not take into account the configuration file of the jasperreports sources jasperreports_extension.properties.

In my project I am using Maven to manage the java libraries and in the assembly process, it threw the message:

jasperreports_extension.properties already added, skipping

So, I put the sources extension file, in the first dependencies, even before jasperreports.

Then I generated the Maven project and the fonts worked, because the jasperreports_extension.properties was prioritized.

Courses answered 26/4, 2021 at 3:43 Comment(0)
I
0

The PDF format has its own set of fonts. And, if a document needs to use other fonts, they must be embedded inside the PDF. In the first versions of JasperReports this problem was solved by introducing some properties on the textual elements to allow the specification of the PDF font name and encoding, and, if the font will be embedded or not inside the final PDF.

This approach can still be used for backward compatibility, but it is now deprecated and discouraged. In fact, in the latest version of Jaspersoft Studio, a textual element that uses this property shows a warning icon and message. This is because the Font Extension handles the problem better. So, if a user wants to use a custom font in a PDF export, then it should done using a Font Extension.

Here is the way to do it: Custom Font with the Font Extension.

Inventive answered 9/9, 2021 at 19:56 Comment(1)
I have similar problem. I use Arial but Jasper changes it to Helvetica and only Helvetica is embedded in PDF. Arial is installed in my OS so I do not understand why I have to export Arial font from Jasper Studio and add it to classpath to make it work. As I understand PDF has two types of fonts: embedded and non-embedded. Why Arial is not used as non-embedded font?Endlong

© 2022 - 2024 — McMap. All rights reserved.