Error during font loading: Failed to execute ‘postMessage‘ on ‘Worker‘: ArrayBuffer at index 0 is al

2023-05-16

vue-pdf 展示出现字体丢失问题

解决办法:重写两个js CMapReaderFactory buffer-loader

// CMapReaderFactory.js
import { CMapCompressionType } from 'pdfjs-dist/es5/build/pdf.js'

// see https://github.com/mozilla/pdf.js/blob/628e70fbb5dea3b9066aa5c34cca70aaafef8db2/src/display/dom_utils.js#L64

export default function() {
  this.fetch = function(query) {
    return import('./buffer-loader!pdfjs-dist/cmaps/' + query.name + '.bcmap' /* webpackChunkName: "noprefetch-[request]" */)
      .then(function(bcmap) {
        delete require.cache[require.resolve('./buffer-loader!pdfjs-dist/cmaps/' + query.name + '.bcmap')]
        return {
          cMapData: bcmap.default,
          compressionType: CMapCompressionType.BINARY
        }
      })
  }
}

// buffer-loader
module.exports = function(content) {

	var data;
	if ( content instanceof Buffer )
		data = content;
	else
		data = Buffer.from(content);

	return 'export default Buffer.from("'+data.toString('base64')+'", "base64")';
}

module.exports.raw = true;

pdf vue组件

<template>
  <!-- 弹窗组件 -->
  <!-- top="20vh"  距离浏览器上面距离 -->
  <elian-dialog
    ref="ElianPdf"
    :visible.sync="visible"
    top="1vh"
    :width="widths"
    :title="pageNum + '/' + pageTotalNum"
    destroy-on-close
    @close="beforeClosed"
  >
    <div class="pdf" :style="{ height: heights + 'px' }">
      <div class="main">
        <pdf
          ref="pdf"
          :src="pdfUrl"
          :page="pageNum"
          :rotate="pageRotate"
          @password="password"
          @progress="loadedRatio = $event"
          @page-loaded="pageLoaded($event)"
          @num-pages="pageTotalNum = $event"
          @error="pdfError($event)"
          @link-clicked="page = $event"
        >
        </pdf>
      </div>
      <div class="flexbox">
        <div class="btn-def btn-pre" @click.stop="prePage">
          <img src="@/assets/images/syy.png" width="26px" alt="" />
        </div>
        <div class="btn-def btn-next" @click.stop="nextPage">
          <img src="@/assets/images/xyy.png" width="26px" alt="" />
        </div>
        <div
          :class="{ select: idx == 0 }"
          @touchstart="idx = 0"
          @touchend="idx = -1"
          @click="scaleD"
        >
          <img src="@/assets/images/fd.png" width="26px" alt="" />
        </div>
        <div
          :class="{ select: idx == 1 }"
          @touchstart="idx = 1"
          @touchend="idx = -1"
          @click="scaleX"
        >
          <img src="@/assets/images/sx.png" width="26px" alt="" />
        </div>
        <div @click="fileDownload(pdfUrl)">
          <img src="@/assets/images/xz.png" width="22px" alt="" />
        </div>
      </div>
    </div>
  </elian-dialog>
</template>
<script>
import download from 'downloadjs'
import pdf from 'vue-pdf'
import CMapReaderFactory from './CMapReaderFactory.js'
export default {
  name: 'ElianPdf',
  components: {
    pdf
  },
  data() {
    return {
      pdfUrl: '',
      pageNum: 1,
      pageTotalNum: 1,
      loadedRatio: 0, // 加载进度
      curPageNum: 0,
      pageRotate: 0,
      scale: 70, //放大系数
      idx: -1,
      visible: false,
      heights: '',
      widths: '',
      fileList: []
    }
  },
  computed: {},
  created() {
    this.heights = window.innerHeight - 90
  },
  methods: {
    pdfTask(pdfUrl){
      var self = this
      // 传参 CMapReaderFactory
      this.pdfUrl = pdf.createLoadingTask({url: pdfUrl, CMapReaderFactory})
      this.pdfUrl.promise.then(pdf => {
        self.numPages = pdf.numPages
      }).catch((err) => {
        console.error('pdf加载失败')
      })
    },
    open(fileurl) {
      //this.pdfUrl = fileurl
      this.pdfTask(fileurl)
      this.visible = true
    },
    /**
     * 确定 - 调用父页面新增操作
     */
    handleSubmit() {
      this.visible = false
    },
    /**
     * 关闭 - 当前弹窗,并刷新父页面
     */
    beforeClosed() {
      this.visible = false
    },
    fileDownload(item) {
      // console.log(item)
      // let fileurls
      // fileurls = window.location.origin + item
      download(item)
    },
    //放大
    scaleD() {
      if (this.scale == 100) {
        return
      }
      this.scale += 5
      // this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%'
      this.widths = parseInt(this.scale) + '%'
    },

    //缩小
    scaleX() {
      if (this.scale == 40) {
        return
      }
      this.scale += -5
      // this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%'
      this.widths = parseInt(this.scale) + '%'
    },
    prePage() {
      var p = this.pageNum
      p = p > 1 ? p - 1 : this.pageTotalNum
      this.pageNum = p
    },
    nextPage() {
      var p = this.pageNum
      p = p < this.pageTotalNum ? p + 1 : 1
      this.pageNum = p
    },
    password(updatePassword, reason) {
      updatePassword(prompt('password is "123456"'))
      console.log('...reason...')
      console.log(reason)
      console.log('...reason...')
    },
    pageLoaded(e) {
      this.curPageNum = e
    },
    pdfError(error) {
      console.error(error)
    },
    pdfPrintAll() {
      this.$refs.pdf.print()
    },
    pdfPrint() {
      this.$refs.pdf.print(100, [1, 2])
    }
  }
}
</script>

<style lang="scss" scoped>
.pdf {
  overflow: auto;
  width: 100%;
  & .flexbox {
    display: flex;
    position: fixed;
    width: 260px;
    height: 36px;
    padding: 10px 16px;
    left: -webkit-calc(50% - 130px);
    left: -moz-calc(50% - 130px);
    left: calc(50% - 130px);
    bottom: 20px;
    background: rgba(170, 170, 170, 0.6);
    border-radius: 20px;
    & div {
      width: 25%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Error during font loading: Failed to execute ‘postMessage‘ on ‘Worker‘: ArrayBuffer at index 0 is al 的相关文章

  • 加载 Comet/服务器推送 iframe 时停止浏览器“厄运的悸动”

    当使用 Comet 或 Ajax Long Pull 技术时 通常使用 iframe 当 iframe 等待长连接关闭时 浏览器正在旋转其 throbber 进度 加载指示器 一些网站 例如 etherpad com 设法让它停止 他们是如
  • jQuery动态图片加载

    当我在输入中键入引用时 我尝试加载图像
  • 用于加载 PNG 图像的替代库

    我的 java J2SE 应用程序正在从 Web 读取大量 png 图像 其中一些图像使用诸如真彩色图像的透明度颜色 tRNS 部分 之类的功能 而 Sun 的 Oracle 的 PNGImageReader 实现会简单地忽略这些功能 因此
  • 如何判断图像是否已正确加载

    有没有办法在事后判断图像 与 img 标签 不是通过 JS 已正确加载到页面中 我有一个头像库 有时第三方图像服务器最终会提供 404 我可以更改服务器端代码以使用onerror showGenericHeadshot 但我真的想避免对服务
  • Android:如何知道Fragment是否已完成加载整个内容?

    无论如何都知道Fragment已完成加载全部内容Fragment 我问的原因是因为onResume当我将应用程序置于后台时的状态我重新加载Fragment通过执行AsyncTask但我只想重新加载Fragment仅当Fragment尚未完成
  • 如何减少 Blazor WebAssembly 的加载时间

    我正在开发 Blazor WebAssembly 应用程序 加载时间超过30s 这是正常的吗 我的临时链接 http chhinsras 001 site1 itemurl com http chhinsras 001 site1 item
  • 加载器在 chrome 中的同步 ajax 调用期间不工作

    加载程序在 google chrome 中的同步 异步 假 ajax 调用期间不工作 在 Firefox 和 IE 中工作正常 在我的调试测试期间 Loader 显示直到 ajax 请求开始 当请求发送到服务器时被删除或消失 我在服务器上保
  • 如何在dojo中加载一个不是模块的JS文件?

    首先我要说的是 我是一个 javascript 和 dojo 菜鸟 不过 我一直在使用 D O H 框架为我的 js 代码编写一些单元测试 我注意到的一件事是该框架似乎没有办法模拟 XHR 请求 所以我决定使用 sinon 来进行嘲笑 这是
  • ajax 请求完成后浏览器继续“加载”

    您已经看到过这一点 在 ajax 密集的页面上 例如 meebo com 浏览器永远不会停止 旋转 它永远不会表明它已完成加载 造成这种情况的原因是什么 是否有任何方法可以一致地告诉浏览器 无论它是什么浏览器 加载已完成 编辑 抱歉 我应该
  • Xamarin.Forms - 如何使页面上的元素绝对居中?

    我有一个使用 StackLayout 来显示内容 用户名 密码 登录按钮 的登录页面 用户单击登录按钮后 我希望在页面的绝对中心 现有 StackLayout 内容的顶部设置一个 加载 块 由于某些恼人的原因 这并不简单 这似乎是一件简单
  • 在 Flask 中执行耗时函数时显示“正在加载”消息

    我对 Flask 还比较陌生 总体来说还是一个网络菜鸟 但到目前为止我已经取得了一些不错的结果 现在我有一个表单 用户可以在其中输入查询 该查询被提供给一个函数 该函数可能需要 5 到 30 秒的时间才能返回结果 使用 Freebase A
  • 加载动画png [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我正在寻找一个透明的动画加载图标 然而 我知道我很懒 我不想自己做 如果这不是这篇文章的正确论坛 请原谅我 我过去在这里运气很好 有谁知道我在哪里可以
  • 需要帮助创建架构以将 CSV 加载到 BigQuery 中

    我正在尝试从 Google Cloud Storage 将一些 CSV 文件加载到 BigQuery 中 并努力解决架构生成问题 有一个自动生成选项 但记录很少 问题是 如果我选择让 BigQuery 生成架构 它会很好地猜测数据类型 但有
  • 填充形状的图像不会在 FabricJS 中使用 loadFromJSON 加载

    I have JSON带有 Fabric Rect 对象的数据 当我尝试加载这个时JSON数据到织物画布上从JSON加载 形状加载完美 但形状中填充的图案未渲染 仅在单击形状后才会渲染图案 我已将我的代码复制到织物厨房水槽执行模块中 并尝试
  • 如何在 R 中加载多特征 geojson 文件的*部分*?

    我有一个 geojson 是FeatureCollection包含 2 种地理数据类型 LineString and a waypoint 查看原始文件here https raw githubusercontent com Robinlo
  • Kendo Grid - 它自己的加载指示器

    Kendo grid在网格加载 分页 排序时自动提供自己的Loading指示器 它运行良好 但我不希望显示 隐藏这个内置加载指示器 如何禁用此功能 请建议我 谢谢 维诺斯 很简单 用CSS隐藏就可以了 您的选择器需要比内置 Kendo 类更
  • 从服务器序列故障转移加载 .js?

    让我们想象一个网页需要加载一个 javascript 文件 即my js 是否可以组织以下故障转移加载顺序 如果服务器 A 已启动 则加载my js来自服务器A 否则 如果服务器 B 已启动 则加载my js来自服务器 B 否则 如果服务器
  • 在 Phonegap InAppBrowser 中显示加载指示器

    当使用以下代码加载页面时 我尝试在 Phonegap InAppBrowser 中显示加载指示器 var ref ref window open http www google com top location no ref addEven
  • Rails:页面加载时间很长

    我有一个 Rails 项目 我在一个控制器中遇到了非常奇怪的事情 浏览器需要大约 3 分钟来加载页面 但 Rails 在日志中写入 Completed 200 OK in 20563ms Views 17144 2ms ActiveReco
  • “_加载小部件时出现问题”消息

    加载小部件时 如果找不到资源或其他内容 则会显示 加载小部件时出现问题 就这样 惊人的 此消息保留在主屏幕上 甚至没有说明加载时遇到问题的小部件 我通过反复试验弄清楚了这一点 但我想知道发生这种情况时是否有任何地方可以找到错误消息 Andr

随机推荐