# 文件预览组件

# 概述

HOS基础平台为用户提供了广泛而全面的文档预览支持,涵盖了日常工作中最为常见的文件格式,包括但不限于PDF、Word、Excel、PowerPoint(PPT)、MP3音频文件以及MP4视频文件等。

# 导入依赖

文件预览支持纯文本文件、图片文件、PDF 文件、office 文件及媒体文件的预览,根据项目需要选择对应的依赖包自行引入。
(1) 纯文本、图片、PDF 文件需要引入 hos-framework-preview-starter 依赖

  <dependency>
      <groupId>com.mediway.hos</groupId>
      <artifactId>hos-framework-preview-starter</artifactId>
  </dependency>

(2) office 文件需要引入 hos-framework-preview-office-starter 依赖

  <dependency>
      <groupId>com.mediway.hos</groupId>
      <artifactId>hos-framework-preview-office-starter</artifactId>
  </dependency>

office 文件预览需要借助第三方服务 openoffice/libreoffice。 如果是服务端,则需要在服务器安装 openoffice/libreoffice;可参考Linux 下安装 LibreOffice (opens new window)
如果本地开发需要自行安装,可以从https://gitee.com/kekingcn/file-online-preview下载开源项目,将其中的LibreOfficePortable复制到本地目录,并在yml文件(hos.preview.office.home)配置其中libreoffice目录的路径。

(3) 媒体文件需要引入 hos-framework-preview-media-starter 依赖

  <dependency>
      <groupId>com.mediway.hos</groupId>
      <artifactId>hos-framework-preview-media-starter</artifactId>
  </dependency>

# 配置及使用

hos:
  ##### 基础配置
  server-url: #默认使用http://localhost:8080
  preview:
    enable: # 是否开启文件预览,默认false
    base:
      tmpDir: #临时文件目录 默认使用java.io.tmpdir/preview
    previewlPath: #文件预览路径,预览接口的根路径。默认使用file-preview
    watermark:
      enable: #是否开启水印 默认false
      watermarkTxtv: #水印文字内容
      watermarkTxtHandler: #如需定制水印需要实现接口WatermarkTxtSupplier,配置bean名称
    ##### 使用office预览时需配置
    office:
      home: #openoffice或LibreOffice  home路径(示例:D:/devtools/LibreOfficePortable/App/libreoffice)
      previewType: #office类型文档样式,默认为图片(image),可配置为pdf(预览时也有按钮切换)

spring:
   freemarker:
      template-loader-path: classpath:/web/
      cache: false
      charset: UTF-8
      check-template-location: true
      content-type: text/html
      expose-request-attributes: true
      expose-session-attributes: true
      request-context-attribute: request
      suffix: .ftl

整个文件的预览都是统一的接口,默认为:http://ip:port/file-preview/onlinePreview?fileId=<fileId>,其中‘file-preview’可通过配置进行更改。