Java通过openoffice将下载的文件转成pdf供前端预览

一、下载安装openoffice

1、进入openoffice官网http://www.openoffice.org/download/index.html下载最新版RPM安装包Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_zh-CN.tar.gz

2、解压下载的文件

tar zxxvf Apache_OpenOffice_4.1.6_Linux_x86-64_install-rpm_zh-CN.tar.gz

3、进入zh-CN/RPMS目录

cd zh-CN/RPMS

4、安装所有rpm文件

rpm -ivh *.rpm

5、进入desktop-integration目录

desktop-integration

6、安装openoffice4.1.6-redhat-menus-4.1.6-9790.noarch.rpm,不同版本该文件名可能不同

rpm -ivh openoffice4.1.6-redhat-menus-4.1.6-9790.noarch.rpm

可能会出现如下问题:

file /usr/bin/soffice from install of openoffice4.1.6-redhat-menus-4.1.6-9790.noarch conflicts with file from package libreoffice-core-1:4.3.7.2-5.el7.x86_64
意思是:包libreoffice-core-1:4.3.7.2-5.el7.x86_64冲突,执行以下命令卸载该包,然后再执行一次上面的安装命令即可。

yum -y remove libreoffice-core-1:4.3.7.2-5.el7.x86_64

7、启动openoffice服务,完成安装后,默认安装到 /opt/openoffice4 目录下,进入该目录下的program目录,执行命令进行后台启动

nohup soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &

可通过 ps ef|grep openoffice 查看 进程是否启动。

二、编写Java代码

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

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;

@Component
public class DocConverter {
	
	private static final Logger log = LoggerFactory.getLogger(DocConverter.class);
    
    @Value("${ccf.jodconverter.port}")
    private String port;
    
    @Value("${ccf.doc.dir}")
    private String docDir;
    
    @Value("${ccf.pdf.dir}")
    private String pdfDir;
    
	@Value("${ccf.doc.request.url}")
	private String docRequestUrl;
	
	@Value("${ccf.doc.download.url.reg}")
	private String reg;
	
	public DocResponse officeToPdf(String url) {
		if(StringUtils.isEmpty(url)) {
			return DocResponse.fail(505, "文档地址不能为空!");
		}
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(url);
		if(!matcher.matches()) {
			return DocResponse.fail(504, "文档格式不支持,暂只支持[pdf、doc、docx、ppt、pptx、xls、xlsx]!");
		}
		String fileName = DigestUtils.md5DigestAsHex(url.getBytes());
		File pdfFile = new File(pdfDir+File.separator+fileName+".pdf");
		if(pdfFile.exists()) {
			return DocResponse.success(docRequestUrl+pdfFile.getName());
		}
		DocResponse downloadResponse = downloadByNIO(url);
		if(downloadResponse.getCode() != 0) {
			return downloadResponse;
		}
		DocResponse response = doc2pdf(downloadResponse.getMessage());
		
		return response;
	}
	
    
	private DocResponse downloadByNIO(String url) {
		 
		 String suffixName = url.substring(url.lastIndexOf("."), url.length());
		 //将下载地址转换唯一的字符串
		 String fileName = DigestUtils.md5DigestAsHex(url.getBytes())+suffixName;
		 File file = new File(docDir+File.separator+fileName);
		 if(!file.exists()) {
			 long start = System.currentTimeMillis();
			 log.info("***开始下载文件:{}***",url);
		        try (InputStream ins = new URL(url).openStream()) {
		            Path target = Paths.get(docDir,fileName);
		            Files.createDirectories(target.getParent());
		            Files.copy(ins, target, StandardCopyOption.REPLACE_EXISTING);
		        } catch(FileNotFoundException e) {
		        	log.error("***文件不存在,无法下载:{}",url);
		        	return DocResponse.fail(404, "文件不存在,无法预览:"+url);
		        }catch (IOException e) {
		            e.printStackTrace();
		            log.error("***文件下载失败:{}***",url);
		            return DocResponse.fail(500, "件下载失败:"+url);
		        }finally {
					log.info("***文件下载完成,耗时:{} ms***",(System.currentTimeMillis()-start));
				}
		 }
	     return DocResponse.success(docDir+File.separator+fileName);
	 }
      
    /** 
     * 转为PDF 
     *  
     * @param file 
     */  
    private DocResponse doc2pdf(String fileString) {
        String fileName = fileString.substring(fileString.lastIndexOf(File.separator), fileString.lastIndexOf("."));  
        File docFile = new File(fileString); 
        File pdfFile = new File(pdfDir+fileName + ".pdf");
        if (docFile.exists()) {  
            if (!pdfFile.exists()) {
            	long start = System.currentTimeMillis();
                OpenOfficeConnection connection = new SocketOpenOfficeConnection(Integer.valueOf(port));  
                try {  
                    connection.connect();  
                    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);  
                    converter.convert(docFile, pdfFile);  

                    log.info("***转换成功,PDF输出:{},总耗时:{}***",pdfFile.getPath(),System.currentTimeMillis()-start);
                } catch (java.net.ConnectException e) {  
                    e.printStackTrace();  
                    log.error("****转换器异常,openoffice服务未启动!****");
                    return DocResponse.fail(501, "转换器异常,openoffice服务未启动!");
                } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {  
                    e.printStackTrace();  
                    log.error("***转换器异常,读取转换文件[{}]失败!***",fileString);
                    return DocResponse.fail(502, "转换器异常,读取转换文件["+fileString+"]失败!");
                } catch (Exception e) {  
                    e.printStackTrace();  
                    log.error("***转换器发生未知异常!***");
                    return DocResponse.fail(503, "转换器发生未知异常!");
                }finally {
                    // close the connection  
                    connection.disconnect();  
				}
            } else {  
            	log.info("***对应pdf文件[{}]已存在***",pdfFile.getPath());
            }  
        } else {  
        	log.error("***文档[{}]不存在,无法转换!***",fileString);
        	return DocResponse.fail(404, "文档不存在,无法转换!");
        }  
        return DocResponse.success(docRequestUrl+pdfFile.getName());
    }  

}

三、完整源码下载https://download.csdn.net/download/ccf199201261/11827702

你可能感兴趣的:(java)