I am going to use package(jspdf) loaded from **CDN **
this is CDN
<script src=".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 = '.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
So, how can I fix this error?
I am going to use package(jspdf) loaded from **CDN **
this is CDN
<script src="https://cdnjs.cloudflare./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./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
So, how can I fix this error?
Share Improve this question edited Mar 9, 2022 at 17:50 kissu 46.8k16 gold badges90 silver badges189 bronze badges asked Mar 9, 2022 at 17:29 morteza mortezaiemorteza mortezaie 1,5643 gold badges27 silver badges59 bronze badges 8- 1 You loaded the CDN but where is the jsPDF variable declaration? – Diesan Romero Commented Mar 9, 2022 at 17:32
- Does this answer your question? How to add a 3rd party script code into Nuxt? – kissu Commented Mar 9, 2022 at 17:33
-
Also, are you sure that you want to use a CDN here? Pretty sure you can find the NPM package for that, it will be far better in a
package.json
tbh. – kissu Commented Mar 9, 2022 at 17:34 - @DiesanRomero , variable declaration ? how can i do it ? – morteza mortezaie Commented Mar 9, 2022 at 17:35
- @kissu i a pretty sure that this way that i used cdn is worked because i can see that script is loaded , the problem is how to use it – morteza mortezaie Commented Mar 9, 2022 at 17:37
2 Answers
Reset to default 10You can use const { jsPDF } = window.jspdf;
as shown in the official documentation if you want to use the CDN.
Even tho, I still do remend the NPM package way (so does the package itself).
This one seems to be working :
<script src="https://cdnjs.cloudflare./ajax/libs/jspdf/1.4.1/jspdf.debug.js"></script>