问题描述
为此,我想将HTML页面转换为pdf,我使用了flying-saucer-pdf-itext5。添加了flying-saucer-pdf-itext5和flying-saucer-core依赖项,但是在生成pdf文件时,其格式会更改一些CSS不适用于PDF文件。 我的代码是这样的:
String xHtml = convertToXhtml(htmlContent);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(xHtml);
renderer.setScaletoFit(true);
renderer.layout();
final File pdfFile = new File("fileName.pdf");
OutputStream outputStream = new FileOutputStream(pdfFile);
renderer.createPDF(outputStream);
outputStream.close();
要将HTML转换为XHTML,我编写以下代码:
Tidy tidy = new Tidy();
tidy.setInputEncoding(UTF-8);
tidy.setoutputEncoding(UTF-8);
tidy.setXHTML(true);
ByteArrayInputStream inputStream = new
ByteArrayInputStream(htmlContent.getBytes(UTF-8));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
tidy.parseDOM(inputStream,outputStream);
pom.xml是:
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.6</version>
<scope>compile</scope>
</dependency>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)