为什么在Linux下用PDF截断文本? [英] Why is text truncated in PDF with Linux?

查看:234
本文介绍了为什么在Linux下用PDF截断文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着格式化



使用Linux的结果



PDF属性
$ b 所有生成的PDF文件在Acrobat Reader for Windows中都具有相同的字体属性:




  • http://community.jaspersoft.com/问题/ 803503 /为什么文本提交的价值,将待截断,如果-application-running-linux



  • 解决方法似乎是嵌入字体,但是不起作用。



    我使用字体Helvetica,这是默认字体之一,这就是原因,请参阅

    https://stackoverflow.com/a/27345103/5277820


    如果您在iText中使用这些字体,iText将忽略嵌入参数,因为假定Adobe Reader和其他查看器可以正确呈现这些字体是安全的。
    < blockquote>

    问题:

    为什么Windows和Linux有相同的字体宽度?或为什么文本截断和/或换行不同?

    解决方案

    要正确计算字体度量,字体需要可用于Java的虚拟macchine。



    看到这个历史问题:

    然而,使用jasper-reports的正确解决方案是使用字体扩展 如果使用分布式<$ c $它包含了这些字体:


    DejaVu Sans
    DejaVu Serif< / code> DejaVu Sans Mono

    您需要在字体名称 fontName =DejaVu中使用其中的一个Sans,没有自动映射到其他字体,jar实际上只包含这些 .ttf ,没有其他的(打开jar和验证不同版本的jasper-reports)。

    因此只有安装在电脑上的字体或如果您需要其他字体,最好的解决办法是生成自己的字体扩展名,包括有效的 .ttf 字体,这可以在IDE中完成。


    I try to format a Date in Jasper Reports and it works with Windows but not with Linux. With Linux the resulting text is truncated.

    Code:

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

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

    Results:

    Result with Windows:

    Result with Linux:

    PDF properties:

    Both generated PDF files have the same font properties in Acrobat Reader for Windows:

    As you can see, the font is not embedded. (Second font "Helvetica" disappears if I add dependency jasperreports-fonts and remove attributes pdfFontName, pdfEncodingand isPdfEmbedded).

    Research:

    I read:

    and the solution seems to be to embed the font, but it doesn't work.

    I use font "Helvetica" which is one of the default fonts and that's the reason, see Wikipedia:

    These fonts, or suitable substitute fonts with the same metrics, must always be available in all PDF readers and so need not be embedded in a PDF

    and https://stackoverflow.com/a/27345103/5277820:

    If you use these fonts in iText, iText will ignore the embedded parameter, because it is safe to assume that Adobe Reader and other viewers can render these fonts correctly.

    Question:

    Why have the same font different widths with Windows and Linux? Or why is the text truncating and/or line wrapping different?

    解决方案

    To calculate the font-metrics correctly, the font needs to be available to the java virtual macchine.

    See this historical question: Font is not available to the JVM with Jasper Reports, that show various solution to the old error launched

    However the correct solution with jasper-reports is to use font-extensions

    If you use the distributed jasperreports-font.jar it contains these fonts:

    DejaVu Sans
    DejaVu Serif
    DejaVu Sans Mono

    You need to use one of these in font name example fontName="DejaVu Sans", there is no automatic mapping to other fonts, the jar physically contains only these .ttf and no other (open jar and verify for different version of jasper-reports).

    Hence only font installed on the pc or included in font-extension are available for the JVM.

    If you need other fonts, the best solution is to generate your own font-extension including a valid .ttf font this can be done from within the IDE.

    这篇关于为什么在Linux下用PDF截断文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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