pdf.js 1000 B

1234567891011121314151617181920212223242526272829
  1. import Config from '@/config/index'
  2. const { Host80 } = Config
  3. import getWebBase from '@/utils/webBase'
  4. const webBase = getWebBase()
  5. // utils/pdf.js 或直接在组件 methods 中定义
  6. export function openLocalPdf(fileName, title, type = 'pdf') {
  7. // #ifdef H5
  8. // H5 端直接打开相对路径,会被浏览器渲染或下载(取决于浏览器配置)
  9. if (type === 'pdf') {
  10. window.open(`${Host80}${webBase}static/${fileName}`);
  11. } else if (type === 'pdf1') {
  12. window.open(`${fileName}`);
  13. }
  14. // #endif
  15. // #ifdef APP-PLUS
  16. // App 端:将静态资源路径转为系统绝对路径,再用 uni.openDocument 调用系统阅读器打开
  17. if (type === 'pdf') {
  18. uni.navigateTo({
  19. url: `/pages/common/webview?url=${encodeURIComponent(`${Host80}${webBase}static/${fileName}`)}&title=${title}`
  20. });
  21. } else if (type === 'pdf1') {
  22. uni.navigateTo({
  23. url: `/pages/common/webview?url=${encodeURIComponent(`${fileName}`)}&title=${title}`
  24. });
  25. }
  26. // #endif
  27. }