Skip to content

DocumentViewer ​

Props ​

属性类型默认值说明
fileFile—本地文件,与 url 同时传入时优先
urlstring—可由浏览器跨域读取的远程地址
filenamestring—URL 不含扩展名时用于格式识别
heightstring | number'100%'预览区域高度;数字按像素处理
strictbooleantrue是否拒绝支持清单之外的扩展名
emptyTextstring内置提示未传入文档时的空状态文字
errorTextstring'文档加载失败,请检查文件是否受损'文档无法读取或解析时的提示文字
optionsDocumentViewerOptions{}工具栏、主题、缩放和渲染限制

事件 ​

事件参数触发时机
load-startDocumentLoadContext开始读取文档
progressDocumentProgress解析或渲染进度变化
load-completeDocumentLoadContext文档可预览
unload-startDocumentLoadContext开始卸载当前文档
unload-completeDocumentLoadContext当前文档完成卸载
zoom-changeDocumentZoomState缩放比例改变
errorstring文档加载或渲染失败时使用 errorText;打印准备失败时返回对应提示
vue
<DocumentViewer
  :file="file"
  @load-complete="({ filename, pageCount }) => console.log(filename, pageCount)"
  @error="message => console.error(message)"
/>

实例方法 ​

通过模板 ref 获取 DocumentViewerInstance:

vue
<script setup lang="ts">
import { ref } from 'vue'
import { DocumentViewer } from 'document-viewer-vue3'
import type { DocumentViewerInstance } from 'document-viewer-vue3'

const viewer = ref<DocumentViewerInstance>()
</script>

<template>
  <DocumentViewer ref="viewer" :file="file" />
  <button @click="viewer?.zoomIn()">放大</button>
  <button @click="viewer?.resetZoom()">重置</button>
</template>
方法返回值说明
reload()Promise<void>重新读取并渲染当前来源
destroy()Promise<void>销毁当前渲染内容
zoomIn()DocumentZoomState放大一级
zoomOut()DocumentZoomState缩小一级
resetZoom()DocumentZoomState回到初始缩放比例
setZoom(scale)DocumentZoomState设置缩放比例
getZoomState()DocumentZoomState读取当前缩放状态
rotateLeft()number图片向左旋转 90°,返回当前角度
rotateRight()number图片向右旋转 90°,返回当前角度
setRotation(rotation)number设置图片旋转角度(自动归一为 90° 的倍数)
getRotation()number读取当前图片旋转角度
downloadOriginalFile()void下载当前文档原始数据
print()void仅打印当前文档的完整内容,保留文档分页
getScrollContainer()HTMLDivElement | null获取预览滚动容器

打印使用文档的完整分页内容,不受当前预览缩放、滚动位置和宿主页面布局影响。PDF 的每页画布会复制到打印页面,文档自身的页眉、页脚会保留。

浏览器自动添加的日期、网页标题、网址和页码由浏览器的“页眉和页脚”选项控制。组件打印页使用零外边距,避免这些信息占用文档空间;如果浏览器仍显示这些内容,可在打印对话框的“更多设置”中取消勾选“页眉和页脚”。组件不能通过 JavaScript 修改该浏览器设置。

插槽 ​

插槽插槽参数说明
toolbar-startzoom, status插入工具栏起始区域
toolbar-endzoom, status插入工具栏结束区域

在浏览器中完成文档读取与预览