I am currently in development on a web portal using Django. I was using jsPDF-autotable library via github to convert my tables into a pdf file. However, I have tried multiple ways - via js file:
import jsPDF from 'jspdf'
import 'jspdf-autotable'
function generatePDF() {
var doc = new jsPDF();
doc.autoTable({ html: '#volume-results' });
doc.save('table.pdf');
}
and <script> in my html:
<script type="module">
import jsPDF from 'jspdf'
import 'jspdf-autotable'
function generatePDF() {
var doc = new jsPDF();
doc.autoTable({ html: '#volume-results' });
doc.save('table.pdf');
}
</script>
Hi @schulzy175, anything defined in a module is scoped to that module, so it won’t be available to iline JS; a quick and dirty fix would be to explicitly expose that function to the global scope:
// In you JS file
window.generatePDF = generatePDF
However the cleaner solution would be adding an event listener so you don’t have to pollute the global scope in the first place (note that your JS needs to be included at the bottom of the page then):
Edit: As for the imports, you have to specify the actual path to where the module is located. The documentation probably assumes you’re using a module bundler such as webpack, where you can include modules just by their name. If you’re not using a module bundler the library will have to support native modules though.
Sadly, with your solution, it seems Pycharm won’t recognized the path to the js file (located within node_modules). Considering it’s Django and Pycharm, I feel the solution may be more python related. I’ve already included my node_modules in my project structure, but Pycharm refuses to recognize the path. The saddest part about this ~10 line script is that all I need to do is to get python/Pycharm to recognize the location of the imports - extremely frustrating.