OnlyOffice文档转换--文档转换届的葵花宝典

参考官方文档:https://api.onlyoffice.com/editors/conversionapi
官方文档很简单,看时序图:
OnlyOffice文档转换--文档转换届的葵花宝典_第1张图片
支持的文档转换:
OnlyOffice文档转换--文档转换届的葵花宝典_第2张图片
OnlyOffice文档转换--文档转换届的葵花宝典_第3张图片
OnlyOffice文档转换--文档转换届的葵花宝典_第4张图片

上代码:

@Operation(summary = "文档转换")
    @Parameters({
            @Parameter(name = "fileUrl",description = "待转换文档地址",required = true,in = ParameterIn.QUERY),
            @Parameter(name = "inType",description = "输入文档类型",required = true,in = ParameterIn.QUERY),
            @Parameter(name = "outType",description = "输出文档类型",required = true,in = ParameterIn.QUERY)
    })
    @GetMapping("conversion")
    public void conversion(String fileUrl,String filename,String inType,String outType,HttpServletResponse servletResponse){
        String url = onlyofficeServer.concat("ConvertService.ashx");
        JSONObject params = new JSONObject();
        params.put("async",false);
        params.put("filetype",inType);
        params.put("key", MD5.create().digestHex(fileUrl));
        params.put("outputtype", outType);
        params.put("title",filename);
        params.put("url", fileUrl);
        String res = HttpUtil.post(url, params.toJSONString(JSONWriter.Feature.FieldBased));
        JSONObject resObj = JSONObject.parseObject(res);
        //失败
        if(resObj.containsKey("error")){
            ConversionErrorType errorType = ConversionErrorType.of(resObj.getInteger("error"));
            throw new BusinessException(errorType.getMsg());
        }
        String downloadUrl = resObj.getString("fileUrl");
        HttpRequest get = HttpUtil.createGet(downloadUrl);
        HttpResponse response = get.execute();
        HttpUtil.export(servletResponse,response.bodyStream(), new HttpUtil.InputstreamWriteStream(),filename+"."+outType);
    }

html转docx效果图:
OnlyOffice文档转换--文档转换届的葵花宝典_第5张图片
OnlyOffice文档转换--文档转换届的葵花宝典_第6张图片

注意:html转word,针对简单的文档效果还是比较好的,但是word和图片之类的转换效果要差点。

你可能感兴趣的:(#,Office,onlyoffice,文档转换)