I am quite new to js and want to turn a value inside an json array into contents of a downloadable pdf
like:
<div class="container">
<input type="button" value="Create PDF"
onclick="generatePDF()">
</div>
<script type="text/javascript">
const data = {
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true
}
const pdfvalue = tree[1];
function generatePDF() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
doc.text(pdfvalue, 100, 100);
doc.save("newFile.pdf");
}
</script>
I just want to put "Metro City" only into the PDF, but it says invalid arguments passed to jsPDF, is there any way that i could do it?
The issue in your code is that you're trying to access tree[1], but there is no tree variable defined. Instead, you should access the value from the data object directly. Here's an updated version of your code that should work:
<div class="container">
<input type="button" value="Create PDF" onclick="generatePDF()">
</div>
<script type="text/javascript">
const data = {
"squadName": "Super hero squad",
"homeTown": "Metro City",
"formed": 2016,
"secretBase": "Super tower",
"active": true
};
const pdfvalue = data.homeTown;
function generatePDF() {
const doc = new jsPDF();
doc.text(pdfvalue, 10, 10);
doc.save("newFile.pdf");
}
</script>