OpenOffice實(shí)現(xiàn)word文檔在線(xiàn)預(yù)覽
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下所有字體上傳到服務(wù)器應(yīng)用文件夾
windows字體目錄:C:\WINDOWS\Fonts
應(yīng)用字體目錄:/opt/openoffice4/share/fonts/truetype/
4、啟動(dòng)openofiice4(指定服務(wù)器IP和端口,服務(wù)器外可以調(diào)用)
/opt/openoffice4/program/soffice -headless -accept="socket,host=192.0.0.22,port=8852;urp;" -nofirststartwizard &
5、相關(guān)問(wèn)題解決
(1)缺少庫(kù)文件
/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"
項(xiàng)目集成
1、項(xiàng)目引入jodconverter-2.2.2.jar(解決word文檔不支持docx、xlsx的問(wèn)題)
解決方法一:下載jar包,用idea導(dǎo)入
解決方法二:下載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引入依賴(lài)
<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、提供轉(zhuǎn)換工具(后綴為pdf文件不做轉(zhuǎn)換)
@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;
// 原始文件數(shù)組為空或者后綴為pdf直接返回原數(shù)據(jù)
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 {
// 文件轉(zhuǎn)化
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、提供預(yù)覽接口
@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();
}
}
評(píng)論
圖片
表情
