编程语言
首页 > 编程语言> > java – 如果缺少PDF的字体,为什么jasper报告不会抛出JRFontNotFoundException?

java – 如果缺少PDF的字体,为什么jasper报告不会抛出JRFontNotFoundException?

作者:互联网

我读

> https://stackoverflow.com/a/4028080/5277820
> https://stackoverflow.com/a/35549391/5277820
> http://community.jaspersoft.com/questions/543275/fonts-problem-while-using-jasper-reports-402-version
> http://refermycode.com/remove-font-dependency-in-jasper-report/

如果机器上没有安装字体,我预计Jasper Reports会抛出JRFontNotFoundException.

但我的应用程序没有抛出JRFontNotFoundException,虽然我没有在任何(Jaspersoft Studios,JasperReports,Adobe Acrobat Reader)机器上安装使用过的字体“Helvetica”.

JRXML:

<parameter name="timestamp" class="java.util.Date"/>
[...]
<textField>
    <reportElement x="0" y="0" width="50" height="16" uuid="0007846a-26f1-457a-a198-67a2f7c8417c">
        <property name="local_mesure_unitwidth" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.width" value="px"/>
        <property name="local_mesure_unitx" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.x" value="px"/>
        <property name="local_mesure_unity" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.y" value="px"/>
        <property name="local_mesure_unitheight" value="pixel"/>
        <property name="com.jaspersoft.studio.unit.height" value="px"/>
    </reportElement>
    <box padding="2"/>
    <textElement textAlignment="Left" verticalAlignment="Top">
        <font size="8" pdfFontName="Helvetica" pdfEncoding="Cp1250" isPdfEmbedded="true"/>
    </textElement>
    <textFieldExpression><![CDATA[DATEFORMAT($P{timestamp},"dd.MM HH:mm")]]></textFieldExpression>
</textField>

Maven依赖:

<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>5.6.0</version>
</dependency>
<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports-functions</artifactId>
    <version>5.6.0</version>
</dependency>

Java的:

private byte[] createPdf() {

    try {
        InputStream is = getClass().getResourceAsStream("MyReport.jasper");
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(is);
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("timestamp", new Date());
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(new Vector(), false);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrDataSource);
        byte[] pdf = JasperExportManager.exportReportToPdf(jasperPrint);
        return pdf;
    } catch (JRException e) {
        throw new RuntimeException("Could not create PDF.", e);
    }
}

原因是只有在未安装属性fontName中的字体时才会抛出JRFontNotFoundException

Exception raised when a font name used as value for the fontName attribute in the report template, is not found in any of the runtime available JasperReports font extensions, nor among the font names available to the Java Virtual Machine.

如果未安装属性pdfFontName中的字体(而不是使用任何其他安装的字体),有没有办法中止PDF的生成?

解决方法:

您正在设置pdfFontName而不是fontName

pdfFontName是一种旧的方式,现在已弃用以指示itext库应使用的字体,如果字体丢失,jasper-reports将不会抛出JRFontNotFoundException,而是itext将抛出作为JRRuntimeException捕获并重新启动的异常.

在itext中,Helvetica作为afm文件包含在内,因此如果使用它,itext将不会抛出任何异常,但是如果你在jasper中指示另一种字体(在你的情况下不表示=默认字体),这并不能保证你的文本正确呈现-reports.事实上,这是一个烂摊子,不推荐使用pdfFontName和pfdEncoding.

如果未安装属性pdfFontName中的字体,是否有任何方法可以中止PDF的生成?

不要使用pdfFontName,但如果你坚持(为了问题)然后也设置fontName =“Helvetica”,设置jasper-reports字体将引发JRFontNotFoundException,如果没有找到.

正确的方法是只设置fontName,然后提供font-extensions,在font-extension中包含实际的ttf,指示编码以及是否应该嵌入.

顺便说一句:我会使用编码Identity-H这是推荐用于较新的PDF标准,并使您能够混合不同的编码.

标签:java,fonts,pdf-generation,jasper-reports,export-to-pdf
来源: https://codeday.me/bug/20190608/1199338.html