WPS在线预览方案--onlyoffice+java

**环境搭建**

docker pull onlyoffice/documentserver
 
docker run -i -t -d -p 8013:8013 --restart=always onlyoffice/documentserver
 
别忘记开放对应端口 

firewall-cmd --zone=public --add-port=8013/tcp --permanent

firewall-cmd --reload

之后访问demo页面

http://IP:8013



这样就成功一半了

一般场景是后端提供给前端文件的链接地址,前端再拿去预览
如果觉得上面麻烦可以用现成的,能用是能用可能不稳定或有点卡

https://view.officeapps.live.com/op/view.aspx?src=https://www.xxx.com/oss/202309/2114/vc-upload-1695277140771-1695278579636.ppt

使用https://view.officeapps.live.com/op/view.aspx?src=

加上你要预览的文件就可以了

接下来是自己实现的预览

在对应的yaml文件添加 我们刚刚搭建好的预览服务
```
onlyoffice:
  document:
    host: http://ip:8013  
```

配置类

@Data
@Slf4j
@Configuration("onlyoffice")
public class OnlyOfficeProperties {

    @Value("${onlyoffice.document.host}")
    private String officeDocumentHost;

    /**
     * 获取接口地址
     * @return
     */
    public String getOfficeDocumentApiJs(){
        return officeDocumentHost.concat("/web-apps/apps/api/documents/api.js");
    }


}

预览模板

<!DOCTYPE html>
<html style="height: 100%;" xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
<#--    <title th:text="${title}"></title>-->
</head>
<body>
    <div class="form">
        <div id="iframeEditor"></div>
    </div>
    <script type="text/javascript" src="${apiJs}"></script>
    <!-- 先通过 th:inline=“javascript” 添加到标签,这样js代码即可访问model中的属性 -->
    <script th:inline="javascript">
        window.docEditor = new DocsAPI.DocEditor("iframeEditor",
            ${editor});
    </script>
</body>
</html>
<style>
    html {
        height: 100%;
        width: 100%;
    }

    body {
        background: #fff;
        color: #333;
        font-family: Arial, Tahoma, sans-serif;
        font-size: 12px;
        font-weight: normal;
        height: 100%;
        margin: 0;
        overflow-y: hidden;
        padding: 0;
        text-decoration: none;
    }

    .form {
        height: 100%;
    }

    div {
        margin: 0;
        padding: 0;
    }

</style>

控制层

@Api(tags="网络文档预览")
@Slf4j
@RestController
@RequestMapping("office")
public class OnlyOfficeController {

    @Autowired
    private OfficeCtl officeCtl;

    @SneakyThrows
    @RequestMapping("preview/embedded/remote")
    @ApiOperation(value = "网络文档预览(嵌入)",notes = "支持各种类型文档")
    public ModelAndView previewRemoteOnEmbedded(@RequestParam("url") String url) {
        return officeCtl.previewRemoteFileOnEmbedded(url);
    }
}

接口

public interface OfficeCtl {
    /**
     * 根据文件后缀 获取office 中类型
     * @param extName
     * @return 文件类型
     */
     String getDocumentType(String extName);

    /**
     * 构建远程文档访问 Document
     * @param remoteUrl
     * @return
     */
     Document buildRemoteDocument(String remoteUrl);

    /**
     * 构建文档预览 DocEditor
     * @param document
     * @return
     */
     DocEditor buildPreviewDocEditor(Document document);

    /**
     * 嵌入式预览远程文件
     * @param remoteUrl
     * @return
     */
     ModelAndView previewRemoteFileOnEmbedded(String remoteUrl);


    /**
     * 文件预览
     * @param editor
     * @return
     */
    ModelAndView previewFile(DocEditor editor);

    /**
     * 文件预览 制定预览标题
     * @param editor
     * @param title
     * @return
     */
    ModelAndView previewFile(DocEditor editor, String title);


}

实现类


@Slf4j
@Service
@AllArgsConstructor
public class OfficeCtlImpl implements OfficeCtl {
    OnlyOfficeProperties officeProps;

    @Resource
    ApplicationContext context;

    @Override
    public String getDocumentType(String extName) {
        if (DocumentType.WORD_FILE.contains(extName)){
            return "word";
        }
        else if (DocumentType.CELL_FILE.contains(extName)){
            return "cell";
        }
        else if (DocumentType.SLIDE_FILE.contains(extName)){
            return "slide";
        }
        return null;
    }

    @SneakyThrows
    @Override
    public Document buildRemoteDocument(String remoteUrl) {
        HttpResponse response = HttpRequest.get(remoteUrl).execute();
        String fileType = StringUtils.substringAfterLast(remoteUrl,".");
        // 进行MD5 编码
        String fileKey = SecureUtil.md5(response.bodyStream());
        // 构建 Document
        Document document = new Document();
        document.setKey(fileKey);
        document.setTitle("tempView".concat(fileType));
        document.setFileType(fileType);
        document.setUrl(URLUtil.encode(remoteUrl));
        return document;
    }

    @SneakyThrows
    @Override
    public DocEditor buildPreviewDocEditor(Document document) {
        // 配置模式
        EditorConfig editorConfig = new EditorConfig();
        editorConfig.setMode("view");
        editorConfig.setLang("zh-CN");
        // 构建 Editor
        DocEditor docEditor = new DocEditor();
        docEditor.setDocument(document);
        docEditor.setDocumentType(getDocumentType(document.getFileType()));
        docEditor.setEditorConfig(editorConfig);
        docEditor.setHeight("100%");
        docEditor.setWeight("100%");
        return docEditor;
    }


    @SneakyThrows
    @Override
    public ModelAndView previewRemoteFileOnEmbedded(String remoteUrl) {
        Document document = buildRemoteDocument(remoteUrl);
        // 构建 Editor
        DocEditor docEditor = buildPreviewDocEditor(document);
        //设置预览端类型
        docEditor.setType(DocEditorType.EMBEDDED);
        return previewFile(docEditor);
    }


    @SneakyThrows
    @Override
    public ModelAndView previewFile(DocEditor editor) {
        return previewFile(editor, null);
    }

    @SneakyThrows
    @Override
    public ModelAndView previewFile(DocEditor editor, String title) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("OfficeView");
        modelAndView.addObject("editor",JSONObject.toJSONString(editor));
        if (Validator.isEmpty(title)){
            modelAndView.addObject("title",editor.getDocument().getTitle());
        }
        else {
            modelAndView.addObject("title", title);
        }
        modelAndView.addObject("apiJs",officeProps.getOfficeDocumentApiJs());
        return modelAndView;
    }


}

预览端类型

public class DocEditorType {
    // 移动端预览
    public static final String MOBILE = "mobile";
    // 嵌入式预览
    public static final String EMBEDDED = "embedded";
}

文档类型


public class DocumentType {

    // 文本文档类型
    public static final Set<String> WORD_FILE = new HashSet<String>(){
        {
        add("djvu");
        add("doc");
        add("docm");
        add("docx");
        add("docxf");
        add("dot");
        add("dotm");
        add("dotx");
        add("epub");
        add("fb2");
        add("fodt");
        add("htm");
        add("html");
        add("mht");
        add("mhtml");
        add("odt");
        add("ofor");
        add("m");
        add("ott");
        add("oxps");
        add("pdf");
        add("rtf");
        add("stw");
        add("sxw");
        add("txt");
        add("wps");
        add("wpt");
        add("xml");
        add("xps");
        }
    };
    // 电子表格类型
    public static final Set<String> CELL_FILE = new HashSet<String>(){
        {
            add("csv");
            add("et");
            add("ett");
            add("fods");
            add("ods");
            add("ots");
            add("sxc");
            add("xls");
            add("xlsb");
            add("xlsm");
            add("xlsx");
            add("xlt");
            add("xltm");
            add("xltx");
            add("xml");
        }
    };
    // 演示文稿类型
    public static final Set<String> SLIDE_FILE = new HashSet<String>(){
        {
            add("dps");
            add("dpt");
            add("fodp");
            add("odp");
            add("otp");
            add("pot");
            add("potm");
            add("potx");
            add("pps");
            add("ppsm");
            add("ppsx");
            add("ppt");
            add("pptm");
            add("pptx");
            add("sxi");
        }
    };

}

马马虎虎,主打能用

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
wps-view-vue是一个基于Vue框架开发的文件在线预览组件,它可以帮助我们实现对blob格式文件的在线预览。下面我将详细介绍如何使用wps-view-vue来预览blob格式文件。 首先,我们需要在项目中引入wps-view-vue组件,可以通过npm或者其他方式进行安装。在Vue的组件中,我们可以使用import语句导入wps-view-vue组件。 ``` import WpsView from 'wps-view-vue' ``` 接下来,我们在Vue组件中使用wps-view-vue组件,并传入blob格式的文件数据作为参数。 ``` <template> <div> <wps-view :blob="fileBlob" /> </div> </template> <script> import WpsView from 'wps-view-vue' export default { components: { WpsView }, data() { return { fileBlob: null } }, mounted() { // 获取blob格式文件数据,可以是通过接口请求获取到的 // 这里假设我们通过axios发送请求获取到了blob格式的文件数据 axios.get('http://example.com/api/file', { responseType: 'blob' }) .then(response => { this.fileBlob = response.data }) .catch(error => { console.error(error) }) } } </script> ``` 以上代码中,我们将获取到的blob格式文件数据赋值给fileBlob变量,并作为参数传递给wps-view组件。 通过以上步骤,我们就可以在Vue项目中使用wps-view-vue组件实现对blob格式文件的在线预览了。wps-view-vue组件会根据文件类型选择合适的预览方式,确保用户可以在浏览器中直接查看和操作blob格式文件。这样可以提升用户的使用体验,并且方便用户对文件进行相关操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值