在 Java 中从 html 生成 pdf 时渲染错误

Wrong rendering while generating pdf from html in Java

我正在尝试将简单的 html 转换为 pdf,但我浏览器中的样式与 pdf 中的样式不同。我正在使用 flying-saucer-pdf v9.1.18。你知道为什么吗?

以下是它在网络浏览器中的外观以及在生成的 pdf 中的外观: https://i.stack.imgur.com/MB7DB.jpg

这是我的简单 html 模板:

<html>

<head>
    <style>
        .aligned {
            vertical-align: bottom;
        }
        
        .colored {
            color: red;
            font-weight: bold;
        }
    </style>
</head>

<body>
    <table border="1" width="300px" height="300px">
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td class="colored">4</td>
        </tr>
        <tr>
            <td>5</td>
            <td class="aligned">6</td>
        </tr>
    </table>
</body>

</html>

来自文档:

No support for legacy or "street" HTML, although there are several open source Java HTML cleaners of varying quality available. We render well-formed XML; XHTML is a well-formed XML document which uses a special set of tags. We can't render most HTML "in the wild". At best, you can "clean up" old HTML with TagSoup or JTidy or similar library and hope for the best. But without a bunch of work, you won't be able to use Flying Saucer for a real web browser component. However, note that's not a technical limitation, just a lack of time and resources.

TABLE标签的"height"属性是标准的,我猜飞碟忽略了它。

参见例如Setting the height of a table in HTML has no effect