从 CDN 加载时 jspdf 不起作用

发布于 2025-01-12 20:36:01 字数 1312 浏览 2 评论 0原文

我将使用从 **CDN ** 加载的包(jspdf)

这是 CDN

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>

,我有 <在页面中像这样强>加载它:

mounted() {
  if (document.getElementById('myScript')) { return }
  let src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'
  let script = document.createElement('script')
  script.setAttribute('src', src)
  script.setAttribute('type', 'text/javascript')
  script.setAttribute('id', 'myScript')
  document.head.appendChild(script)
}

我有一个按钮,当您单击它时,下面的方法将调用并会生成一些 pdf。

generateReport() {
  var doc = new jsPDF('l', 'mm', [62, 32])
  const margins = {
    top: 0,
    bottom: 60,
    left: 0,
    width: 122
  }

  doc.fromHTML(this.$refs.print, margins.left, margins.top, {
    width: margins.width
  })

  doc.save('test.pdf')
}

但是我收到错误

在此处输入图像描述

那么,我该如何修复这个错误呢?

I am going to use package(jspdf) loaded from **CDN **

this is CDN

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>

and I have loaded it like this in a page :

mounted() {
  if (document.getElementById('myScript')) { return }
  let src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'
  let script = document.createElement('script')
  script.setAttribute('src', src)
  script.setAttribute('type', 'text/javascript')
  script.setAttribute('id', 'myScript')
  document.head.appendChild(script)
}

and I have a button that when you click on it a below method will called and some pdf will be generated.

generateReport() {
  var doc = new jsPDF('l', 'mm', [62, 32])
  const margins = {
    top: 0,
    bottom: 60,
    left: 0,
    width: 122
  }

  doc.fromHTML(this.$refs.print, margins.left, margins.top, {
    width: margins.width
  })

  doc.save('test.pdf')
}

BUT I get an error

enter image description here

So, how can I fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2025-01-19 20:36:01

您可以使用 const { jsPDF } = window.jspdf;

尽管如此,我仍然推荐 NPM 包方式(包本身也是如此)。

You can use const { jsPDF } = window.jspdf; as shown in the official documentation if you want to use the CDN.

Even tho, I still do recommend the NPM package way (so does the package itself).

故人爱我别走 2025-01-19 20:36:01

这似乎有效:

   <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.debug.js"></script>

This one seems to be working :

   <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.debug.js"></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文