我有以下html
<div style="border: 2px solid black; padding: 5px;">
<p align="CENTER">test1</p>
<p align="CENTER">test2</p>
</div>
并使用 open office lib 将其转换为 pdf
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
和这个功能
public synchronized static byte[] htmlToPdf(String html) throws Exception {
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
DefaultDocumentFormatRegistry defaultDocumentFormatRegistry = new DefaultDocumentFormatRegistry();
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(IOUtils.toInputStream(html, Charset.forName("UTF-8")), defaultDocumentFormatRegistry.getFormatByFileExtension("html"), pdfOutputStream, defaultDocumentFormatRegistry.getFormatByFileExtension("pdf"));
connection.disconnect();
return pdfOutputStream.toByteArray();
}
我们正在运行这个 ooversion
@linux-9jgs:~> soffice --version
LibreOffice 4.3.7.2 430m0(Build:2)
但在 pdf 中,每一行都有自己的边框,在浏览器中,html 看起来不错
我记得在以前版本的开放式办公室中它工作正常