跳到正文

只读查看器 ​

有些文档只应阅读而不应修改,例如订单或理赔的附件。这个示例在只读查看器中逐个显示它们,用一个列表切换文档,并提供下载按钮。

html
<select id="documents">
  <option value="0">发票</option>
  <option value="1">送货单</option>
</select>
<div id="viewer" style="height: 480px"></div>
ts
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'

const documents = [
  { src: '/orders/42/invoice.jpg', alt: '发票' },
  { src: '/orders/42/delivery-note.jpg', alt: '送货单' },
]

const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
  ...documents[0],
  editable: false,
  download: true,
  wheelZoom: false,
})

document.querySelector('#documents')!.addEventListener('change', (event) => {
  const index = Number((event.target as HTMLSelectElement).value)
  pic.setOptions(documents[index]!)
})

说明 ​

  • 设置 editable: false 时,工具栏没有“编辑”按钮,旋转按钮只旋转显示的图片。
  • download: true 会添加一个按钮,按原样保存文档,使用它自己的文件名。通过 download 事件,你可以改为从你的服务器提供文件。
  • wheelZoom: false 让滚轮滚动页面,适合放在较长页面中的查看器。
  • 新的 src 会加载下一个文档,并让它适合查看器。

基于 MIT 许可证发布。