简体   繁体   中英

JsPDF, how get javascript variable

This is what I would like to do:

  • I would like to print a value that is caught from an operation on some field in a form.

I have successfully used jsPDF but I am not able to send to pdf the value of the variable 'CPS', as shown in the following code error:

The error is:
Uncaught Error: Type of text must be string or Array. "undefined" is not recognized.
    at Object.H.text (jspdf.min.js:1)
    at demo (forweb.html:11)
    at HTMLInputElement.onclick (forweb.html:46)

I am unable to find a solution everywhere.

Here is my code:

<!DOCTYPE HTML> 
<html> 
<script type="text/javascript" src="jspdf.js"></script>
<script type="text/javascript" src="jspdf.min.js"></script>
<script type="text/javascript">

function demo() {

var pdf = new jsPDF()
    var uno_ = document.getElementById("uno").innerHTML;
        pdf.text(10,10),uno_;
    var due_ = document.getElementById("due").innerHTML;
        pdf.text(10,20),chest;
    var chest = document.getElementById("CPS").innerHTML;
        pdf.text(10,30),chest;
                }
function calcola() {    a_uno = document.getElementById("uno").value;
                        a_due = document.getElementById("due").value;
                            var answer =parseFloat(a_uno)+ 
parseFloat(a_due);
                            var CPS = document.getElementById('CPS');
                            CPS.value=answer;
                            }           
</script>

<head> 
<title>print result </title> 
</head> 
<form name="myForm"> 
one
            <select id="uno"  onchange="calcola(this.value)">
            <option value="0"SELECTED>-</option>
            <option value="1">1</option>
            <option value="2">2</option>
            </select></span>
due 
            <select id="due"   onchange="calcola(this.value)">
            <option value="0"SELECTED>-</option>
            <option value="1">1</option>
            <option value="2">2</option>
            </select></span>

<input type="button" name="reset_form" value="Resetta Valori" 
 onclick="this.form.reset(); CPS.value = 0;">           
            CPS = <input type="button" name="CPS" id="CPS"/>

</form>
<input type="button" name="stampa" value="STAMPA" onclick="demo()">
</body> 
</html>

Thanks in advance for your help - Ivan

Your demo function has errors when calling the JSPDF text function. The text function needs 3 arguments and since you closed the parenthesis after setting the position, the variable value was never sent.

function demo() {
  var pdf = new jsPDF()
  var uno_ = document.getElementById("uno").value;
  pdf.text(10,10,uno_);
  var due_ = document.getElementById("due").value;
  pdf.text(10,20,due_);
  var chest = document.getElementById("CPS").value;
  pdf.text(10,30,chest);
  pdf.save('my.pdf');
}

Here I changed innerHTML to value to show the selected value on the select tag. This might fix the error you're getting. I also added the save function just so you can view the result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM