利用openoffice对html,word,pdf进展转换乱码,利用OpenOffice对html、word、pdf进展转换

利用OpenOffice对html、word、pdf进行转换

背景及编码思路

关于用OpenOffice的场景更多是将word中的内容另存为(转化)html。我的应用场景是将在线编辑器的内容保存为word格式,系细览转换为word、pdf的一种需求。

下载Openoffice(http://download.openoffice.org/index.html),类似于windows office的一个套件,并额外支持服务的运行方式,可理解为数据库

下载Jodconverter(http://www.artofsolving.com/opensource/jodconverter),类似于jdbc

按照OpenOffice,并启动服务:soffice -headless -accept="socket,port=8100;urp;"

引入jodconverter的jar包,并编码第2章开发过程中遇到的问题

1536409307.png

public class OpenOfficeService {

private Logger logger = Logger.getLogger(this.getClass());

OpenOfficeConnection con = null;

public void convert(File sourceFile, File _targetFile) {

try {

if (con == null) {

con = new SocketOpenOfficeConnection(ip, port);

}

con.connect();

DocumentConverter converter = new OpenOfficeDocumentConverter(con);

converter.convert(sourceFile, _targetFile);

// DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();

// converter.convert(sourceFile, formatReg.getFormatByFileExtension("html"), _targetFile, formatReg.getFormatByFileExtension("pdf"));

} catch (ConnectException e) {

logger.error("OpenOffice异常", e);

try {

if (con != null) {

con.disconnect();

con = null;

}

} catch (Exception e1) {

logger.error(e);

}

}

}

public static void main(String[] args) {

OpenOfficeService openoffice = new OpenOfficeService();

// 生成odt,本质上可以用word打开(可以在下载时修改为doc文件,以屏蔽用户感觉到的差异),如果生成doc,则图片会丢失

openoffice.convert(new File("WebRoot/jsp/data/docs/a.html"), new File("WebRoot/jsp/data/docs/a.odt"));

// 生成pdf

openoffice.convert(new File("WebRoot/jsp/data/docs/a.html"), new File("WebRoot/jsp/data/docs/a.pdf"));

}

public void setIp(String ip) {

this.ip = ip;

}

public void setPort(int port) {

this.port = port;

}

private String ip = "127.0.0.1";

private int port = 8100;

}

过程中遇到的问题

html中的图片可以转化到pdf,但是在不能转化到word中,经测试可以转化到odt文件中,所以我在开发过程中实际上是由hmt转换为odt,再在下载的过程中将后缀改为doc格式,另外,html中的图片,最好与html文件放到同一目录下,引用方式为相对目录,也可以修改html的图片路径为绝对路径。

1 楼

bluerose

2012-05-04

大哥  中文是乱码 我在windows下

请问你遇到了么 怎么解决的 谢谢

2 楼

zhang_637

2012-05-14

bluerose 写道

大哥  中文是乱码 我在windows下

请问你遇到了么 怎么解决的 谢谢

没有遇到,不知道你的源文件编码是否是本地编码?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值