I want to save data related to a particular user into a pdf file , this is to be done on button click and without displaying the table on the page , I am using jspdf for this ,
var div_cr= document.createElement("div");
div_cr.id="xdiv";
var Table_Pull = document.createElement("TABLE");
var header = Table_Pull.createTHead();
var row = header.insertRow(0);
var cell = row.insertCell(0);
cell.innerHTML = "Date";
var cell = row.insertCell(1);
cell.innerHTML = "Attitude";
var cell = row.insertCell(2);
cell.innerHTML = "DeadlineMet";
var cell = row.insertCell(3);
cell.innerHTML = "Discippline";
var row = Table_Pull.insertRow(-1);
var cell1 = row.insertCell(0);
cell1.innerHTML="HEllo";
var cell2 = row.insertCell(1);
cell2.innerHTML="Hi";
var cell3 = row.insertCell(2);
cell3.innerHTML="Goodday";
var doc = new jsPDF();
var dvTable = document.getElementById("xdiv");
dvTable.appendChild(Table_Pull);
var specialElementHandlers = {
'#editor': function(element, renderer){
return true;
}
};
doc.fromHTML($('#dvTable').get(0), 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
doc.save('Test.pdf');
the code above creates a div and table in javascript , adds data to the table and appends the table to the div and passes the div to the jspdf function to create a div.
I am getting the following error,
Uncaught TypeError: Cannot read property 'appendChild' of null
the code attempts to download the table data into a pdf file without actually displaying the table on page , it works fine if I append the table to a div created in html and then pass the div to the jspdf function but as I donot want to display the table on the page this is not the right way to achieve it
I made changes in your code, basically you can use the reference straight away.
var div_cr= document.createElement("div");
div_cr.id="xdiv";
var Table_Pull = document.createElement("TABLE");
var header = Table_Pull.createTHead();
var row = header.insertRow(0);
var cell = row.insertCell(0);
cell.innerHTML = "Date";
var cell = row.insertCell(1);
cell.innerHTML = "Attitude";
var cell = row.insertCell(2);
cell.innerHTML = "DeadlineMet";
var cell = row.insertCell(3);
cell.innerHTML = "Discippline";
var row = Table_Pull.insertRow(-1);
var cell1 = row.insertCell(0);
cell1.innerHTML="HEllo";
var cell2 = row.insertCell(1);
cell2.innerHTML="Hi";
var cell3 = row.insertCell(2);
cell3.innerHTML="Goodday";
var doc = new jsPDF();
div_cr.appendChild(Table_Pull);
var specialElementHandlers = {
'#editor': function(element, renderer){
return true;
}
};
doc.fromHTML(div_cr, 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
doc.save('Test.pdf');
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.