PageOffice 6 最简集成代码(VUE+Springboot)

发布时间 2023-08-15 17:44:13作者: qianxi

本文描述了PageOffice产品在(VUE+Springboot)前后端分离的项目中如何集成调用。调用PageOffice打开文件的主要核心代码是:后端Springboot项目中第6步和前端VUE项目中第6步的代码,其他代码都属于环境配置代码。

假设开发环境电脑IP为:
192.168.1.100

后端Springboot项目

  1. 新建Springboot后端项目:springboot-back,在配置文件application.properties中设置项目端口为:8081
server.port=8081
  1. 在您项目的pom.xml中通过下面的代码引入PageOffice依赖。pageoffice.jar已发布到Maven中央仓库 (opens new window),建议使用最新版本。

如果使用Tomcat10及以上版本,使用下面的pom.xml配置

<dependency>
  <groupId>com.zhuozhengsoft</groupId>
  <artifactId>pageoffice</artifactId>
  <version>6.0.0.8</version>
</dependency>

如果使用Tomcat9及以下的版本,使用下面的pom.xml配置

<dependency>
  <groupId>com.zhuozhengsoft</groupId>
  <artifactId>pageoffice</artifactId>
  <version>6.0.0.8-javax</version>
</dependency>
  1. 新建一个pageoffice文件夹,用来存放PageOffice的系统文件(如license.lic、客户端安装包等),比如windows环境下创建:D:/pageoffice,linux环境下创建:/root/pageoffice

  2. 拷贝pageoffice客户端安装程序到上一步创建的pageoffice文件夹下。

  • 客户端是windows环境:拷贝posetup_6.x.x.x.exe到pageoffice文件夹下;
  • 客户端是国产操作系统环境:拷贝对应芯片的PageOffice客户端deb安装包到pageoffice文件夹下;
  1. 打开springboot-back项目的配置文件application.properties,添加一个posyspath变量,值为上一步创建的pageoffice文件夹的路径
server.port=8081
posyspath=D:/pageoffice
  1. 在您项目的启动类Application类中添加一项@Bean配置,此为PageOffice服务器端的必要配置,代码如下:
@Value("${posyspath}")
private String poSysPath;

@Bean
public ServletRegistrationBean pageofficeRegistrationBean()  {
  com.zhuozhengsoft.pageoffice.poserver.Server poserver 
      						= new com.zhuozhengsoft.pageoffice.poserver.Server();
  poserver.setSysPath(poSysPath);//设置PageOffice注册成功后,license.lic文件存放的目录

  ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
  srb.addUrlMappings("/poserver.zz");
  srb.addUrlMappings("/poclient");
  srb.addUrlMappings("/pageoffice.js");
  srb.addUrlMappings("/sealsetup.exe");
  return srb;
}
  1. 新建Controller并调用PageOffice在线打开D盘根目录下的test.docx文件,例如DocumentController代码如下:
@RestController
@RequestMapping(value = "/doc")
public class DocumentController {

  @RequestMapping(value="/openFile")
  public String openFile(HttpServletRequest request) {
      PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
      poCtrl.setSaveFilePage("saveFile");
      //webOpen的第一个参数支持能够输出下载文件的Url相对地址或者文件在服务器上的磁盘路径两种方式
      //查看详细,请在本站搜索“PageOffice属性或方法中涉及到的URL路径或磁盘路径的说明”
      poCtrl.webOpen("D:\\test.docx", OpenModeType.docNormalEdit, "张三");
      return  poCtrl.getHtmlCode();
  }

  @RequestMapping("/saveFile")
  public void saveFile(HttpServletRequest request, HttpServletResponse response) {
      FileSaver fs = new FileSaver(request, response);
      fs.saveToFile("D:\\" + fs.getFileName());
      fs.close();
  }

}

前端Vue项目

  1. 新建Vue前端项目:vue-front

  2. Vue配置代理。

  devServer: {
    proxy: {
      '/api': { // "/api"对应后端项目"http://192.168.1.100:8081"地址 
        target: 'http://192.168.1.100:8081',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
        '^/api': ''
        }
      },
    }
  }
  1. 在您Vue项目的index.html中引用后端项目中的pageoffice.js文件(后端项目中pageofficeRegistrationBean配置了pageoffice.js文件的访问地址)。

注意
pageoffice.js文件必须来自于后端项目。

<script type="text/javascript" src="/api/pageoffice.js"></script>
  1. 新建一个Vue页面:src/views/DocView.vue,用来显示在线打开的文档。
<template>
  <div class="doc">
    演示: 文档<br/><br/>
    <!-- 此div用来加载PageOffice客户端控件 -->
    <div style="width:auto; height:700px;" v-html="poHtmlCode" ></div>
  </div>
</template>

<script>
    const axios=require('axios');
    export default {
        name: 'DocView',
        data(){
          return {
            poHtmlCode: '',
          }
        },
        created: function(){
          // 请求后端项目打开文件的controller方法
          axios.post("/api/doc/openFile").then((response) => {
            this.poHtmlCode = response.data;
          }).catch(function (err) {
            console.log(err)
          })
        },
        methods:{
          OnPageOfficeCtrlInit() {
            // PageOffice的初始化事件回调函数,您可以在这里添加自定义按钮
          },
          AfterDocumentOpened(){
            // PageOffice的文档打开后事件回调函数
          },
          BeforeDocumentSaved() {
            // PageOffice的文档保存前事件回调函数
          },
          AfterDocumentSaved() {
            // PageOffice的文档保存后事件回调函数
          }
        },
        mounted: function () {
          // 以下的为PageOffice事件的回调函数,名称不能改,否则PageOffice控件调用不到
          window.OnPageOfficeCtrlInit = this.OnPageOfficeCtrlInit;
          window.AfterDocumentOpened = this.AfterDocumentOpened;
          window.BeforeDocumentSaved = this.BeforeDocumentSaved;
          window.AfterDocumentSaved = this.AfterDocumentSaved;
        }
    }
</script>
  1. 配置DocView.vue的访问路由
const routes = [
  // 其他路由配置项...
  // 下面添加DocView.vue的路由
  ,
  {
    path: '/showDoc',
    name: 'doc',
    component: () => import('../views/DocView.vue')
  }
]
  1. 在您要打开文件的Vue页面(比如首页)添加一个超链接,点击超链接调用POBrowser对象的openWindow方法,弹出新浏览器窗口访问DocView.vue在线打开文件,代码如下:
<a href="javascript:POBrowser.openWindow('/showDoc','width=1150px;height=900px;');">
    在线打开文档
</a>
  1. 启动springboot-back和vue-front项目,点击“在线打开文档”超链接,查看在线打开编辑保存Office文件的效果。

参考链接:PageOffice最简集成代码(VUE+Springboot)