IText7 html2pdf页脚CSS不起作用

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.