office文档转pdf服务 本地或远程 OpenOffcie、LibreOffcie

网上说 转的主流是 jacod和 aspose,

aspose是商用软件,跨平台,不需要第三方软件。

jacod依赖 windows环境,在linux下需要安装openOffice。

结果走了弯路,以为破解版的aspose好使,windows下好使,linux下则是各种问题。处理速度也是差强人意。

最后还是改回了开源的jacod,网上给出了5种方案。

仿百度文库解决方案(二)——利用Jacob调用MS Office转换文档为PDF (windows)
仿百度文库解决方案(三)——利用JCom调用MS Office或者Acrobat API转换文档为PDF
仿百度文库解决方案(四)——利用JODConverter调用OpenOffice.org服务转换文档为PDF
仿百度文库解决方案(五)——利用SWFTools转换PDF文档为SWF
仿百度文库解决方案(六)——利用FlexPaper显示Flash(SWF)

比较之后,发现2,3 需要依赖windows平台,不能跨平台。

                  5,6所用的flash技术已经淘汰,不在支持, 4成了唯一的选择。

jodconverter:
  local:
    enabled: true
    port-numbers: 8101,8102,8103,8104,8105
    office-home: /opt/openoffice4
    max-tasks-per-process: 5
    process-timeout: 60000
    task-execution-timeout: 60000

pom 引入两个包

 <!--jodconverter 核心包 -->
        <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-core -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.2.2</version>
        </dependency>
 
        <!--springboot支持包,里面包括了自动配置类 -->
        <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-spring-boot-starter -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.2.2</version>
        </dependency>

<!--jodconverter 本地支持包 -->
        <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local -->
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.2.2</version>
        </dependency>

代码只有几行:
 

@Autowired

private DocumentConverter converter; //用于转换


//隐式的进行转换
converter.convert(file).to(new File(newFileMix)).execute();


//显示的进行转换 docx转pdf
converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();

我更倾向于第一种方式,省心。省力

后记:

项目上线的时候,运维要求,很多台服务器公用一个Office服务,开放了端口。

端口启动 libraOffice / openOffice

soffice --headless --accept="socket,host=0.0.0.0,port=8100;urp;" --nofirststartwizard

用online的版本尝试了好几天,转换的时候卡住了。无可奈何,配置改了好几天,没有任何进展。

        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-online</artifactId>
            <version>4.2.2</version>
        </dependency>

改引用的包。

<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter</artifactId>
    <scope>system</scope>
    <systemPath>${project.basedir}
/src/main/resources/lib/jodconverter-2.2.2.jar</systemPath>
    <version>2.2.2</version>
</dependency>

<dependency>
    <groupId>org.libreoffice</groupId>
    <artifactId>jurt</artifactId>
    <version>5.4.2</version>
</dependency>

换了另外一个版本的jodconverter

@Service
@Slf4j
@RefreshScope
public class PdfJacobUtil {

    @Value("${jodconverter.host:10.255.134.68}")
    public  String ROMOTE_HOST;
    @Value("${jodconverter.port:8100}")
    public  int ROMOTE_PORT;

    // Format
    public static DocumentFormatRegistry formatFactory = new DefaultDocumentFormatRegistry();

    /**
     *
     * @desc
     * @auth josnow
     * @date 2017年6月9日 下午4:11:04
     * @param inputFilePath
     *            待转换的文件路径
     * @param outputFilePath
     *            输出文件路径
     */
    public boolean convert(String inputFilePath, String outputFilePath) throws ConnectException, FileNotFoundException {
       return convert(inputFilePath, outputFilePath, ROMOTE_HOST, ROMOTE_PORT);
    }

    /**
     *
     * @desc
     * @auth josnow
     * @date 2017年6月9日 下午4:12:29
     * @param inputFilePath
     *            待转换的文件路径
     * @param outputFilePath
     *            输出文件路径
     * @param connectIp
     *            远程调用ip
     * @param connectPort
     *            远程调用端口
     */
    public static boolean convert(String inputFilePath, String outputFilePath, String connectIp, int connectPort) {
        if (StringUtils.isEmpty(inputFilePath) || StringUtils.isEmpty(outputFilePath)
                || StringUtils.isEmpty(connectIp)) {
            return false;
        }
        try{
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
            connection.connect();

            DocumentConverter converter = getConverter(connectIp, connection);
            File input = new File(inputFilePath);
            File out =new File(outputFilePath);
            converter.convert(input,out);
            connection.disconnect();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     *
     * @desc
     * @auth josnow
     * @date 2017年6月9日 下午4:08:26
     * @param inputStream
     * @param inputFileExtension
     *            待转换文件的扩展名,例如: xls,doc
     * @param outputStream
     * @param outputFileExtension
     *            输出文件扩展名,例如:pdf
     */
    public void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
                               String outputFileExtension) throws ConnectException {
        convert(inputStream, inputFileExtension, outputStream, outputFileExtension, ROMOTE_HOST, ROMOTE_PORT);
    }

    /**
     *
     * @desc
     * @auth josnow
     * @date 2017年6月9日 下午4:10:21
     * @param inputStream
     * @param inputFileExtension
     *            待转换文件的扩展名,例如: xls,doc
     * @param outputStream
     * @param outputFileExtension
     *            输出文件扩展名,例如:pdf
     * @param connectIp
     *            远程调用ip
     * @param connectPort
     *            远程调用端口
     */
    public static void convert(InputStream inputStream, String inputFileExtension, OutputStream outputStream,
                               String outputFileExtension, String connectIp, int connectPort) throws ConnectException {

        if (inputStream == null || StringUtils.isEmpty(inputFileExtension) || outputStream == null
                || StringUtils.isEmpty(outputFileExtension) || StringUtils.isEmpty(connectIp)) {
            throw new IllegalArgumentException("参数异常!!");
        }

        OpenOfficeConnection connection = new SocketOpenOfficeConnection(connectIp, connectPort);
        connection.connect();
        DocumentConverter converter = getConverter(connectIp, connection);

        converter.convert(inputStream, formatFactory.getFormatByFileExtension(inputFileExtension), outputStream,
                formatFactory.getFormatByFileExtension(outputFileExtension));
        connection.disconnect();
    }

    private static DocumentConverter getConverter(String connectIp, OpenOfficeConnection connection) {
        DocumentConverter converter = "localhost".equals(connectIp) || "127.0.0.1".equals(connectIp)
                || "0:0:0:0:0:0:0:1".equals(connectIp) ? new OpenOfficeDocumentConverter(connection)
                : new StreamOpenOfficeDocumentConverter(connection);
        return converter;
    }
}

可以链接了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大巨魔战将

如果对您有帮助,请打赏1分钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值