OpenOffice实现word文档在线预览

共 4280字,需浏览 9分钟

 ·

2021-11-21 04:40

linux下安装openoffice

1、解压压缩包

tar -zxvf Apache_OpenOffice_4.1.9_Linux_x86-64_install-rpm_zh-CN.tar.gz
tar -zxvf Apache_OpenOffice_4.1.9_Linux_x86-64_langpack-rpm_zh-CN.tar.gz

2、软件安装

cd zh-CN/RPMS/
rpm -ivh *.rpm
cd desktop-integration
rpm -ivh openoffice4.1.9-redhat-menus-4.1.9-9805.noarch.rpm

3、将window下所有字体上传到服务器应用文件夹
windows字体目录:C:\WINDOWS\Fonts
应用字体目录:/opt/openoffice4/share/fonts/truetype/
4、启动openofiice4(指定服务器IP和端口,服务器外可以调用)

/opt/openoffice4/program/soffice -headless -accept="socket,host=192.0.0.22,port=8852;urp;" -nofirststartwizard &

5、相关问题解决
(1)缺少库文件
/opt/openoffice4/program/soffice.bin: error while loading shared libraries: libXext.so.6: cannot open shared object file: No such file or directory

yum install libXext.x86_64
cp -a /usr/lib64/libXext.so.6 /opt/openoffice4/program/

(2)未装插件
no suitable windowing system found, exiting.

yum groupinstall "X Window System"

项目集成

1、项目引入jodconverter-2.2.2.jar(解决word文档不支持docx、xlsx的问题)
解决方法一:下载jar包,用idea导入
解决方法二:下载jar包,上传到maven私服

mvn deploy:deploy-file -Dfile=jodconverter-2.2.2.jar -DgroupId=com.artofsolving -DartifactId=jodconverter -Dversion=2.2.2 -Dpackaging=jar -Durl=http://www.baidu.com:28081/repository/powersi-release/ -DrepositoryId=maven-repository-releases --settings D:/soft/apache-maven-3.5.4/conf/settings.xml

2、pom.xml引入依赖

        <dependency>
<groupId>com.artofsolvinggroupId>
<artifactId>jodconverterartifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>com.github.livesensegroupId>
<artifactId>jodconverter-coreartifactId>
<version>1.0.5version>
dependency>

3、application.yml加入配置信息

jodconverter:
ip: 192.0.0.22
port: 8852

4、提供转换工具(后缀为pdf文件不做转换)

@Service
@Slf4j
public class FileTransService {

@Value("${jodconverter.ip}")
private String ip;

@Value("${jodconverter.port}")
private int port;

private static final String PDF = "pdf";

public synchronized byte[] transFileToPdf(byte[] orignal, String suffix, String localPath) throws Exception {
byte[] rs = null;
// 原始文件数组为空或者后缀为pdf直接返回原数据
if (orignal == null || StringUtils.equals(suffix, PDF)) {
return orignal;
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection(ip, port);
connection.connect();
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
String filename = UUID.randomUUID().toString();

// 首先将dtoBlob中的BLOB字段存到本地文件夹中
String sourcePath = localPath + File.separator + "emrDecode" + File.separator + filename + "." + suffix;
UtilFunc.writeFile(sourcePath, orignal);
String targetPath = localPath + File.separator + "emrDecode" + File.separator + filename + ".pdf";
File fileSource = new File(sourcePath);
File fileTarget = new File(targetPath);
try {
// 文件转化
converter.convert(fileSource, fileTarget);
if (fileTarget.exists()) {
rs = file2byte(fileTarget);
}
} catch (Exception e) {
log.warn(e.getMessage(), e);
} finally {
if (fileTarget.exists()) {
fileTarget.delete();
}
if (fileSource.exists()) {
fileSource.delete();
}
}
return rs;
}

private static byte[] file2byte(File tradeFile) {
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(tradeFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (IOException e) {
log.warn(e.getMessage(), e);
}
return buffer;
}

}

5、提供预览接口

@RestController
@RequestMapping("/web/sysFileAid")
public class SysFileAidController extends BaseController {
@Autowired
FileTransService fileTransService;

@GetMapping("/preview/{id}")
public void preview(@PathVariable("id") String id) throws Exception {
SysFileAidDTO sysFileAidDTO = sysFileAidService.querySysFileAidById(Long.valueOf(id));
DownloadByteArray callback = new DownloadByteArray();
byte[] bytes = fastFileStorageClient.downloadFile(sysFileAidDTO.getGroupName(), sysFileAidDTO.getFilePath(), callback);
byte[] pdfBytes = fileTransService.transFileToPdf(bytes, sysFileAidDTO.getFileType(), FileProperties.getProfile());
httpServletResponse.setContentType("application/pdf");
httpServletResponse.getOutputStream().write(pdfBytes);
httpServletResponse.getOutputStream().close();
}

}
浏览 95
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报