IText7 html2pdf footer CSS does not work

♀尐吖头ヾ 提交于 2019-12-24 19:16:11

问题


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

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

Thanks.

Java code

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 content

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

回答1:


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.



来源:https://stackoverflow.com/questions/47749409/itext7-html2pdf-footer-css-does-not-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!