Word文档转换成Html格式
一、更新服务器
yum源
[root@APP2 /]# yum clean all
[root@APP2 /]# yum makecache
[root@APP2 /]# yum update
1、首先先下载好需要的rpm包:Apache_OpenOffice_4.0.0_Linux_x86-64_install-rpm_zh-CN.tar.gz
或直接命令下载:wget http://heanet.dl.sourceforge.net/project/openofficeorg.mirror/4.0.0/binaries/zh-CN/Apache_OpenOffice_4.0.0_Linux_x86-64_install-rpm_zh-CN.tar.gz
放到服务器的目录下(我放到了opt下)
删除之前安装的OpenOffice
[root@APP2 opt]# yum remove openoffice* libreoffice*
2、将下载的openoffice解压(我直接解压到opt目录):tar -zxvf Apache_OpenOffice_4.0.0_Linux_x86-64_install-rpm_zh-CN.tar.gz
3、解压后生成文件夹zh-CN 进到RPMS目录下,直接yum localinstall *.rpm
4、进入到desktop-integration目录,选择安装相关的套件,我选择的是redhat
rpm -ivh openoffice4.1.1-redhat-menus-4.1.1-9775.noarch.rpm
5、安装完成直接启动Openoffice服务:
进入opt下的openofiice4目录:cd /opt/openoffice4
进入program目录
执行 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
完成启动
临时启动 /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
一直后台启动 nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
windows启动命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
windows启动命令里面短连接号后面不带空格“ -nofirststartwizard ”
linux启动命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
6、查看服务是否启动(端口8100是否被soffice占用):netstat -lnp |grep 8100
显示结果:tcp 0 0 127.0.0.1:8100 0.0.0.0:* LISTEN 19501/soffice.bin
7.-安装遇到的问题和解决------原则是缺什么补什么包
1、/opt/openoffice4/program/soffice.bin: error while loading shared libraries: libXext.so.6: cannot open shared object file: No such file or directory
https://pkgs.org/download/libXext.so.6()(64bit)
安装 libXext rpm 或者yum install libXext.i686
如果出现这种情况,说明在/opt/openoffice4/program/下缺少libXext.so.6文件,可以去/usr/lib64 或者 /usr/lib 查看有没有这个文件,如果有就copy到/opt/openoffice4/program/目录里面,如果没有在安装
# yum install libXext.x86_64
2、 /opt/openoffice4/program/soffice.bin: error while loading shared libraries: libfreetype.so.6: cannot open shared object file: No such file or directory
找不到libfreetype.so.6文件,同上输入:yum install libfreetype.i686,安装完之后去那两个目录找libXext.so.6复制到/opt/openoffice4/program/目录里面,赋予权限。
3、报问题:no suitable windowing system found, exiting.
输入安装:yum groupinstall "X Window System" ,一路安装完之后重启系统,在启动openoffice服务看看。
4、出现中文乱码
4.1、先从你本机 C:\Windows\Fonts 拷贝或者网络上下载你想要安装的字体文件(*.ttf文件)到 /usr/share/fonts/chinese/TrueType 目录下(如果系统中没有此目录,则自行mkdir创建,亦可重命名为自己喜欢的文件夹名)
4.2、修改字体文件的权限,使root用户以外的用户也可以使用
# cd /usr/share/fonts/chinese/TrueType
# chmod 755 *.ttf
4.3、建立字体缓存
# mkfontscale (如果提示 mkfontscale: command not found,需自行安装 # yum install mkfontscale )
# mkfontdir
# fc-cache -fv (如果提示 fc-cache: command not found,则需要安装# yum install fontconfig )
4.4、重启计算机
8.java调用openOffice服务
1.下载JodConverter http://download.csdn.net/detail/kerryzb/9529876
2.java调用
package com.kerryzb.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
public class OfficeToPDF {
/**
* 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为
* http://www.openoffice.org/
*
* <pre>
* 方法示例:
* String sourcePath = "F:\\office\\source.doc";
* String destFile = "F:\\pdf\\dest.pdf";
* Converter.office2PDF(sourcePath, destFile);
* </pre>
*
* @param sourceFile
* 源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,
* .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc
* @param destFile
* 目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf
* @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,
* 则表示操作成功; 返回1, 则表示转换失败
*/
public static int office2PDF(String sourceFile, String destFile) throws FileNotFoundException {
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
return -1;// 找不到源文件, 则返回-1
}
// 如果目标路径不存在, 则新建该路径
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(
"127.0.0.1", 8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(
connection);
converter.convert(inputFile, outputFile);
// close the connection
connection.disconnect();
return 0;
} catch (ConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}
}
为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl+V。看起来也不麻烦,但是,如果文档中包含大量图片呢?尴尬的事是图片都需要重新上传吧?
如果可以将已经编写好的word文档上传到服务器就可以在相应页面进行展示,将会是一件非常惬意的事情,最起码信息发布人员会很开心。程序员可能就不会这么想了,囧。
将Word转Html的原理是这样的:
1、客户上传Word文档到服务器
2、服务器调用OpenOffice程序打开上传的Word文档
3、OpenOffice将Word文档另存为Html格式
4、Over
至此可见,这要求服务器端安装OpenOffice软件,其实也可以是MS Office,不过OpenOffice的优势是跨平台,你懂的。恩,说明一下,本文的测试基于 MS Win7 Ultimate X64 系统。
下面就是规规矩矩的实现。
1、下载OpenOffice,http://download.openoffice.org/index.html So easy...
2、下载Jodconverter http://www.artofsolving.com/opensource/jodconverter 这是一个开启OpenOffice进行格式转化的第三方jar包。
3、泡杯热茶,等待下载。
4、安装OpenOffice,安装结束后,调用cmd,启动OpenOffice的一项服务:C:\Program Files (x86)\OpenOffice.org 3\program>soffice -headless -accept="socket,port=8100;urp;"

5、打开eclipse
6、喝杯热茶,等待eclipse打开。
7、新建eclipse项目,导入Jodconverter/lib 下得jar包。
8、Coding...
package com.mzule.doc2html.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
* 将Word文档转换成html字符串的工具类
*
* @author MZULE
*
*/
public class Doc2Html {
public static void main(String[] args) {
System.out
.println(toHtmlString(new File("C:/test/test.doc"), "C:/test"));
}
/**
* 将word文档转换成html文档
*
* @param docFile
* 需要转换的word文档
* @param filepath
* 转换之后html的存放路径
* @return 转换之后的html文件
*/
public static File convert(File docFile, String filepath) {
// 创建保存html的文件
File htmlFile = new File(filepath + "/" + new Date().getTime()
+ ".html");
// 创建Openoffice连接
OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);
try {
// 连接
con.connect();
} catch (ConnectException e) {
System.out.println("获取OpenOffice连接失败...");
e.printStackTrace();
}
// 创建转换器
DocumentConverter converter = new OpenOfficeDocumentConverter(con);
// 转换文档问html
converter.convert(docFile, htmlFile);
// 关闭openoffice连接
con.disconnect();
return htmlFile;
}
/**
* 将word转换成html文件,并且获取html文件代码。
*
* @param docFile
* 需要转换的文档
* @param filepath
* 文档中图片的保存位置
* @return 转换成功的html代码
*/
public static String toHtmlString(File docFile, String filepath) {
// 转换word文档
File htmlFile = convert(docFile, filepath);
// 获取html文件流
StringBuffer htmlSb = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(htmlFile)));
while (br.ready()) {
htmlSb.append(br.readLine());
}
br.close();
// 删除临时文件
htmlFile.delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// HTML文件字符串
String htmlStr = htmlSb.toString();
// 返回经过清洁的html文本
return clearFormat(htmlStr, filepath);
}
/**
* 清除一些不需要的html标记
*
* @param htmlStr
* 带有复杂html标记的html语句
* @return 去除了不需要html标记的语句
*/
protected static String clearFormat(String htmlStr, String docImgPath) {
// 获取body内容的正则
String bodyReg = "<BODY .*</BODY>";
Pattern bodyPattern = Pattern.compile(bodyReg);
Matcher bodyMatcher = bodyPattern.matcher(htmlStr);
if (bodyMatcher.find()) {
// 获取BODY内容,并转化BODY标签为DIV
htmlStr = bodyMatcher.group().replaceFirst("<BODY", "<DIV")
.replaceAll("</BODY>", "</DIV>");
}
// 调整图片地址
htmlStr = htmlStr.replaceAll("<IMG SRC=\"", "<IMG SRC=\"" + docImgPath
+ "/");
// 把<P></P>转换成</div></div>保留样式
// content = content.replaceAll("(<P)([^>]*>.*?)(<\\/P>)",
// "<div$2</div>");
// 把<P></P>转换成</div></div>并删除样式
htmlStr = htmlStr.replaceAll("(<P)([^>]*)(>.*?)(<\\/P>)", "<p$3</p>");
// 删除不需要的标签
htmlStr = htmlStr
.replaceAll(
"<[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^>]*?>",
"");
// 删除不需要的属性
htmlStr = htmlStr
.replaceAll(
"<([^>]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>",
"<$1$2>");
return htmlStr;
}
}
Word文档转换成Html格式的更多相关文章
- JAVA:借用OpenOffice将上传的Word文档转换成Html格式
为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...
- OpenOffice Word文档转换成Html格式
为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...
- C# word文档转换成PDF格式文档
最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...
- java将XML文档转换成json格式数据
功能 将xml文档转换成json格式数据 说明 依赖包:1. jdom-2.0.2.jar : xml解析工具包;2. fastjson-1.1.36.jar : 阿里巴巴研发的高性能json工具包 ...
- 将html版API文档转换成chm格式的API文档
文章完全转载自: https://blog.csdn.net/u012557538/article/details/42089277 将html版API文档转换成chm格式的API文档并不是一件难事, ...
- 》》》Java利用aspose-words将word文档转换成pdf(破解 无水印)
参考转载:Java利用aspose-words将word文档转换成pdf(破解 无水印) (bbsmax.com) 1.引入 aspose.words 包2.添加解水印 license.xml3.写业 ...
- ASP.NET将word文档转换成pdf的代码
一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...
- poi解析word文档转换成html(包括图片解析)
需求:将本地上传的word文档解析并放入数据库中 代码: import java.io.ByteArrayOutputStream;import java.io.File;import java.io ...
- Python将word文档转换成PDF文件
如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...
- Java利用aspose-words将word文档转换成pdf(破解 无水印)
首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...
随机推荐
- 题解: CF768D Jon and Orbs
题解:CF768D Jon and Orbs 一句话题面:有k种不同的物品,每天等概率任取一种(不一定是新的种类).q组询问,每组给出一个p,问取完这k件物品的概率不小于\(\frac{p}{2000 ...
- 说说Java的类加载机制?究竟什么是双亲委派模型?
首先引入一个概念,什么是Java类加载器? 一句话总结:类加载器(class loader)用来加载 Java 类到 Java 虚拟机中. 官方总结:Java类加载器(英语:Java Classloa ...
- 题解:CF685A Robbers' watch
题解:CF685A Robbers' watch 感觉这题难点主要在理解题意. 题意 一天 \(n\) 个小时,一小时 \(m\) 分钟,手表用 \(7\) 进制表示时间(位数未填满补前导零),求问这 ...
- 干货分享:开启PWM调光之门,一起来做呼吸灯
PWM作为一种灵活且高效的信号调制手段,在电气设备的性能控制和调节中发挥着重要作用,常用于电机控制.灯光调光.音频信号生成.加热控制等应用. 本文将以合宙低功耗4G模组经典型号--Air780E为例, ...
- .NET9 - 新功能体验(一)
被微软形容为"迄今为止最高效.最现代.最安全.最智能.性能最高的.NET版本"--.NET 9已经发布有一周了,今天想和大家一起体验一下新功能. 此次.NET 9在性能.安全性和功 ...
- typescript 安装调试(二)
一.安装ts-node 今天介绍另外一种更加方便的ts调试方式,需要安装以下几个库 第一个库是ts-node,这个库可以直接运行ts文件无需实时编译成js文件后再运行 安装命令 npm i ts-n ...
- Hibernate 之Hibernate缓存
1.缓存:缓存是什么,解决什么问题? 位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之为 Cache(摘自Robbin的<缓存技术浅谈>).目的:让数据 ...
- VScode之远程开发
之前使用过PyCharm的远程开发,很好用,不过还是有几个局限性: 只能用于Python语言: 本地和服务器都需要有一份代码,这两份代码是完全同步的: 一.配置免密远程登录 1.首先检查本地是否有已生 ...
- jQuery 元素信息
先贴出元素模型信息 1.获取内容区大小 css():返回值是带单位的(getComputedStyle(node).width) <script> $(function(){ consol ...
- datagridview点击列头对当前列进行排序的功能无效
DataGridView 的默认行为是支持通过单击列头对列进行排序,但在以下情况下可能会取消该功能或无法使用: 1. 绑定的数据源不支持排序 如果 DataGridView 的数据源是绑定到一个不支持 ...