gpt4 book ai didi

java - : "margin : auto and display block" and parsing from XHTML to PDF using FlyingSaucer (Itext5) 图像居中时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:28:35 24 4
gpt4 key购买 nike

我正在尝试使用 FlyingSaucer 将带有居中图像的 xhtml 解析为 PDF。

图像使用 css 居中:

 img.center {
display: block;
margin-bottom: 1px;
margin-left: auto;
margin-right: auto;
margin-top: 1px;
}

并且在浏览器上显示时完全居中。 codePen link

事实是,当我尝试使用相同的 xhtml 文件和 FlyingSaucer 库创建 PDF 时:图像不再居中。

 PDFRenderer.renderToPDF(htmlFile, "pdf_file.pdf");

pdf showing not centered image

我尝试了几种不同的方法,但似乎没有一个能正常工作。

唯一对我有用的方法是向 img 添加宽度属性。

但是,xhtml 是外部 Web 服务提供的输入,无法更改

在文件接收后添加宽度属性是可行的,但非常脏(您可以在下面的 github 存储库中找到示例代码)

您是否知道如何在 PDF 中以与在浏览器上相同的方式显示此图像而不更改 html 或 css

源项目

https://github.com/Shask/FlyingSaucerXHTMLToPdfBug

PS:我很高兴更改解析库,到目前为止我已经尝试了不同版本的 FlyingSaucer(使用 Itext5 和另一个使用 OpenPDF)

最佳答案

一些更新:

到目前为止,我已经尝试了一些 Java 库来完成这项工作。

似乎只有 Itext7 才能在开箱即用的 PDF 上获得良好的结果。

详细结果可以在github上找到.

关于java - : "margin : auto and display block" and parsing from XHTML to PDF using FlyingSaucer (Itext5) 图像居中时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57392191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com