Please enable Javascript to view the contents

用 ONLYOFFICE 在线查看Word Excel PPT

 ·  ☕ 2 分钟

项目需要在线浏览 Office 文件,以前用过 SharePoint ,现在有 Office Online Server 可以处理。但是OOS安装配置比较复杂且需要 Windows 的服务器,不合适。一番查找,最后发现 ONLYOFFICE 的功能就是我需要的,马上部署!

为了方便,就采用Docker镜像部署的方式

创建容器

首先我们必须安装有Docker(假设Docker宿主机IP:192.168.1.2),然后进入网页 Docker安装帮助文档,创建容器:

1
sudo docker run -i -t -d -p 9080:80 --restart=always onlyoffice/documentserver

在生产环境,数据不要存储在容器内,就自行创建保存目录,并使用以下命令:

1
2
3
4
5
sudo docker run -i -t -d -p 9080:80 --restart=always \
    -v /app/onlyoffice/DocumentServer/logs:/var/log/onlyoffice  \
    -v /app/onlyoffice/DocumentServer/data:/var/www/onlyoffice/Data  \
    -v /app/onlyoffice/DocumentServer/lib:/var/lib/onlyoffice \
    -v /app/onlyoffice/DocumentServer/db:/var/lib/postgresql  onlyoffice/documentserver
  • /var/log/onlyoffice for Document Server logs
  • /var/www/onlyoffice/Data for certificates
  • /var/lib/onlyoffice for file cache
  • /var/lib/postgresql for database

查看是否成功

浏览器访问宿主IP+端口(http://192.168.1.2:9080),如果出现以下字样代表容器运行正常

建立文档编辑HTML页面

创建一个html文件

  • 嵌入编辑器

    1
    
    <div id="placeholder"></div>
    
  • 引用 js

    1
    
    <script type="text/javascript" src="http://192.168.1.2:9080/web-apps/apps/api/documents/api.js"></script>
    
  • 设置编辑器的参数,具体参数含义查看这里

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
config = {
       "document": {
          "fileType": "XLSX",
		   "info": {
            "folder": "Example Files",
            "owner": "John Smith",
            "sharingSettings": [
                {
                    "permissions": "Full Access",
                    "user": "John Smith"
                },
                {
                    "permissions": "Read Only",
                    "user": "Kate Cage"
                },
            ],
            "uploaded": "2010-07-07 3:46 PM"
        },
          "key": "12NaaaaAFE2", //每个文档唯一的KEY
          "title": "test6.docx",
          "url": "https://bugs.red/201901.XLSX", //文档的地址
		  "permissions": { //权限
            "comment": false,
            "download": false,
            "edit": true,
            "fillForms": false,
            "modifyContentControl": false,
            "modifyFilter": false,
            "print": false,
            "review": true
			},
      },
      "documentType": "spreadsheet", // WORD:text,PPT:presentation
      //"width": "1600px", 
     "height": "900px",
      "editorConfig": {
           // "actionLink": ACTION_DATA,
        "callbackUrl": "",
        "createUrl": "",
        "customization": {
            "autosave": false,
            "chat": false,
            "commentAuthorOnly": false,
            "comments": false,
            "compactHeader": false,
            "compactToolbar": false,
            "customer": {
                "address": "My City, 123a-45",
                "info": "Some additional information",
                "logo": "https://example.com/logo-big.png",
                "mail": "john@example.com",
                "name": "John Smith and Co.",
                "www": "example.com"
            },
            "feedback": {
                "url": "https://example.com",
                "visible": false
            },
            "forcesave": false,
            "goback": {
                "blank": false,
                "requestClose": true,
                "text": "Open file location",
                "url": "https://bugs.red/"
            },
            "help": false,
            "hideRightMenu": true,
            "logo": {
                "image": "https://bugs.red/favicon-32x32.png",
                "imageEmbedded": "https://bugs.red/favicon-32x32.png",
                "url": "https://bugs.red/"
            },
            "mentionShare": false,
            "reviewDisplay": "original",
            "showReviewChanges": false,
            "spellcheck": false,
            "toolbarNoTabs": true,
            "unit": "cm",
            "zoom": 100
        },
		"embedded": {
            "embedUrl": "https://example.com/embedded?doc=exampledocument1.docx",
            "fullscreenUrl": "https://example.com/embedded?doc=exampledocument1.docx#fullscreen",
            "saveUrl": "https://example.com/download?doc=exampledocument1.docx",
            "shareUrl": "https://example.com/view?doc=exampledocument1.docx",
            "toolbarDocked": "top"
        },
        "lang": "zh-cn",
        "mode": "edit",
        }	,
		"type": "desktop",
	 "width": "100%"

};
  • 实例化编辑器
1
var docEditor = new DocsAPI.DocEditor("placeholder", config);

最终效果

image-20200812173704709

相关链接

ONLYOFFICE 社区版下载地址

ONLYOFFICE API 基本设置


eddy
作者
eddy
半吊子程序员