如果缺少PDF的字体,为什么碧玉报告不抛出JRFontNotFoundException? [英] Why doesn't jasper report throw a JRFontNotFoundException if font for PDF is missing?

查看:112
本文介绍了如果缺少PDF的字体,为什么碧玉报告不抛出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会抛出> c0> (如果计算机上未安装字体).

and I expected that Jasper Reports throws a JRFontNotFoundException if a font is not installed on the machine.

但是我的应用程序没有抛出> c0> ,尽管我尚未在任何(Jaspersoft Studios,JasperReports,Adobe Acrobat Reader)计算机上安装使用的字体"Helvetica".

But my application doesn't throw a JRFontNotFoundException, although I have not installed the used font "Helvetica" on any (Jaspersoft Studios, JasperReports, Adobe Acrobat Reader) machine.

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);
    }
}

原因是 JRFontNotFoundException fontName中的字体时,才会抛出a>:

The reason is that JRFontNotFoundException is only thrown if font in the attribute fontName is not installed:

当在任何运行时可用的JasperReports字体扩展中,或在Java虚拟机可用的字体名称中都找不到用作报告模板中fontName属性值的字体名称时,引发异常.

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的生成?

Is there any way to abort generation of PDF if the font in attribute pdfFontName is not installed (instead of using any other installed font)?

推荐答案

您正在设置pdfFontName而不是fontName

pdfFontName是一种旧方法,现在已已弃用,以指示itext库应使用哪种字体,如果缺少该字体,jasper-reports将不会抛出JRFontNotFoundException,相反,itext将抛出一个被捕获并重新发布为JRRuntimeException的异常.

pdfFontName was an old way, now deprecated to indicate what font the itext library should use, jasper-reports will not throw JRFontNotFoundException if the font is missing, instead itext will throw an exception that is caught and relaunched as a JRRuntimeException.

在ext中,Helvetica作为afm文件包含在内,因此,如果使用ext,它将不会引发任何异常,但这不能保证不能正确地呈现文本 . >如果您在jasper-reports中指示另一种字体(在您的情况下未指示=默认字体).实际上,这是一团糟,pdfFontNamepfdEncoding均已弃用.

In itext Helvetica is included as afm file, hence itext will not throw any exception if this is used, however this does not guarantee that your text is rendered correctly if you are indicating another font (in your case not indicating = default font) in jasper-reports. In fact this is a mess and both pdfFontName and pfdEncoding was deprecated.

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

Is there any way to abort generation of PDF if the font in attribute pdfFontName is not installed?

不要使用pdfFontName ,但是如果您坚持(出于问题的考虑),还请设置fontName="Helvetica",则设置jasper-reports字体将引发JRFontNotFoundException找不到.

Don't use pdfFontName, but if you insist (for the sake of the question) then also set fontName="Helvetica", setting the jasper-reports font will raise a JRFontNotFoundException if not found.

正确的方法是仅设置 fontName ,然后提供

The correct way is to only set the fontName and then provide font-extensions, in the font-extension you include the actual ttf, indicate the encoding and if it should be embedded.

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

BTW: I would use encoding Identity-H this is recommend for newer PDF standards and gives you the ability to mix different encoding.

这篇关于如果缺少PDF的字体,为什么碧玉报告不抛出JRFontNotFoundException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆