IText7 html2pdf页脚CSS不起作用 [英] IText7 html2pdf footer CSS does not work

查看:932
本文介绍了IText7 html2pdf页脚CSS不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在HTML和CSS下面的结果PDF中看到页脚.此代码在 IText版本2 中可以正常使用.

I am not able to see footer in resultant PDF for below HTML and CSS. This code works fine with IText version 2.

IText版本7 中,显示页眉和页脚的方式是否发生了变化?还是缺少新版本所需的任何配置?

Does the way to show header and footer is changed in IText version 7? or am I missing any configuration required for newer version?

谢谢.

Java代码

public void createPdf(String src, String dest, String resources) throws IOException {
try {
        FileOutputStream outputStream = new FileOutputStream(dest);

        WriterProperties writerProperties = new WriterProperties();
        //Add metadata
        writerProperties.addXmpMetadata();

        PdfWriter pdfWriter = new PdfWriter(outputStream, writerProperties);

        PdfDocument pdfDoc = new PdfDocument(pdfWriter);
        pdfDoc.getCatalog().setLang(new PdfString("en-US"));

        // pdf conversion
        ConverterProperties props = new ConverterProperties();

        MediaDeviceDescription mediaDeviceDescription =
            new MediaDeviceDescription(MediaType.PRINT);
        props.setMediaDeviceDescription(mediaDeviceDescription);

        HtmlConverter.convertToPdf(new FileInputStream(src), pdfDoc, props);
        pdfDoc.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

HTML内容

<html>
<head>
    <meta charset="UTF-8"/>

    <style type="text/css" media="print">

        #footer {
            display: block;
            position: running(footer);
            color:gray;
            font-size: 50%
        }

        @page {
            @bottom-center {
                content: element(footer);
            }
            margin-bottom : 1in;
            size: 6in 6in;
        }


    </style>
</head>
<body>
<div id="footer">
This is the text that goes at the bottom of every page.
</div>

....
..
.
</body>
</html>

推荐答案

您所做的一切正确.它对您不起作用,因为仅在最新版本 iText 7.1.2 中添加了运行元素(position: running(..)content: element(..)属性)支持.它于4月底发布.

You're doing everything right. It wasn't working for you because running elements (position: running(..) and content: element(..) properties) support is added only in the latest version, iText 7.1.2. It was released in the end of April.

这篇关于IText7 html2pdf页脚CSS不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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