jasperreports在linux環境和Windows環境下字體的設置

1.maven依賴

        <!--版本爲6.0.3-->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.0.3 </version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

特別之處版本爲6.0.3的原因:這個版本包含itextpdf, itext-pdfa這兩個;雖然也可以額外添加這兩個依賴,但其它版本包含 itext包,版本爲2.1.*.js系列,這個maven不會自動下載,容易出錯。

2.jasperReport中字體的設置

fontName
PDF字體設置

對應的jrxml代碼爲:

<staticText>
    <reportElement x="0" y="0" width="60" height="43" backcolor="#E32522" uuid="572b4181-a6ab-41fc-88a3-759d44274eda"/>
    <box padding="5">
        <pen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
            <font fontName="SansSerif" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
    </textElement>
    <text><![CDATA[序號]]></text>
</staticText>

注意:雖然fontName可以選擇宋體,華文中宋等,但強烈建議SansSerif,因爲在Linux上運行容易報錯。

3.jasperReport在Linux上運行的找不到字體問題

問題:net.sf.jasperreports.engine.util.JRFontNotFoundException: Font ‘宋體’ is not available to the JVM.
原因:因爲你的fontName選擇的是宋體,而不是SansSerif。我也試過選擇宋體,或者華文中宋,並將字體文件放到classpath目錄下,同時添加jasperreports.properties 文件,結果不行。最後只能是fontName=”SansSerif”————記錄下這個問題,今天試了一天才解決~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章