赞
踩
参考官方文档:https://api.onlyoffice.com/editors/conversionapi
官方文档很简单,看时序图:
支持的文档转换:
上代码:
@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效果图:
注意:html转word,针对简单的文档效果还是比较好的,但是word和图片之类的转换效果要差点。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。