springboot2.x libreoffice centos7下office转pdf

centos7下用boot2+libreoffice转pdf

1,安装libreoffice

#安装文件

yum -y install libreoffice

#安装中文包

yum -y install libreoffice-langpack-zh-Han*

#安装HOME在/usr/lib64/libreoffice

2,构建boot项目,写上传类(略)

4,maven依赖

  
            org.jodconverter
            jodconverter-core
            4.0.0-RELEASE
        

5,转换代码

/**
 * @author xiaonanfeng
 * @date 2018/9/18
 */

public interface PdfConvertService extends BaseService {

    /**
     * 文档转化并返回url路径
     * @param sourceFile 源文件路径
     * @param tarPdfFile 输出文件路径
     * @return
     * @throws OfficeException
     */
    String doDocToFdpLibre(String sourceFile, String tarPdfFile) throws OfficeException;

}


yml配置:
file:
  uploadDiskPath: /java/resources/
  serverUrl: http://127.0.0.1:${server.port}${server.servlet.context-path}/
  libreOfficeHome: /usr/lib64/libreoffice


/**
 * @author xiaonanfeng
 * @date 2018-09-13
 *
 */
@Component
@ConfigurationProperties(prefix = "file")
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class PopFileConfig {

    private String uploadDiskPath;
    private String serverUrl;
    private String libreOfficeHome;

}


/**
 * @author xiaonanfeng
 * @date 2018/9/18
 */

@Service("pdfConvertService")
public class PdfConvertServiceImpl  implements PdfConvertService {


    @Resource
    private PopFileConfig popFileConfig;


    @Override
    public String doDocToFdpLibre(String sourceFile, String tarPdfFile) throws OfficeException {
        File inputFile = new File(sourceFile);
        //liberOffice安装路径
        String libreOfficePath = popFileConfig.getLibreOfficeHome();
        DefaultOfficeManagerBuilder builder = new DefaultOfficeManagerBuilder();
        builder.setOfficeHome(new File(libreOfficePath));
        //端口号
        builder.setPortNumber(8100);
        builder.setTaskExecutionTimeout(1000 * 60 * 5L);
        //设置任务执行超时为5分钟
        builder.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
        //设置任务队列超时为24小时
        OfficeManager officeManager = builder.build();
        startService(officeManager);
        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        String pdfName = StringUtil.getUUID() + ".pdf";
        File outputFile = new File(popFileConfig.getUploadDiskPath() + pdfName);
        converter.convert(inputFile, outputFile);
        stopService(officeManager);

        //物理输出路径
        String pdfPath = outputFile.getPath();

        return popFileConfig.getServerUrl() + pdfName;
    }


    /**
     * 停止转换服务
     *
     * @param officeManager
     * @throws OfficeException
     */
    private void stopService(OfficeManager officeManager) throws OfficeException {
        if (null != officeManager) {
            officeManager.stop();
        }
    }

    /**
     * 开启转换服务
     *
     * @param officeManager
     */
    private void startService(OfficeManager officeManager) {
        try {
            // 准备启动服务
            officeManager.start(); // 启动服务
        } catch (Exception ce) {
            ce.printStackTrace();
        }
    }


}

enjoy!!!!!

你可能感兴趣的:(springboot,Java技术)