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依赖

  <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.0.0-RELEASE</version>
        </dependency>

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!!!!!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值