I am trying to save HTML div as pdf in javascript and PHP. I have tried a lot of libraries, but all of them are returning to the pdf-only HTML structure. The page I want to convert to pdf is generated from a database table in which I have put everything for well structure HTML. Now I am at the point where I can serialize all my input data in string like action=saveWorkPermit&text_CLMC_491=1515&date_CLMC_494l495d=2021-12-06&time_CLMC_496l497t=10%3A34&
. I also can get them in array as {“action”: “saveWorkPermit”, “text_CLMC_491”: “1515” …}. I have my HTML structure also and then. I am stuck. I do not know how to input in the HTML the values and then get this result HTML and put it in some pdf function. I will really appreciate your answers if you can help me. Here is the Ajax code:
case "GeneratePdf":
var ex = data.details.structure ; // here it is the html
var array = data.details.data; // this is the data as array
for (var key in array) {
var type = key.split("_")[0];
var value = array[key];
if(type == 'check' && value == 'on') {
$("#"+key).prop("checked", true);
} else if (type == 'gr'){
$("input[name="+key+"][value=" + value + "]").prop('checked', true);
} else {
$("[name='"+key+"']").val(value);
}
}
let mywindow = window.open('', 'SAVE', 'height=650,width=900,top=100,left=150');
mywindow.document.write(`<html><head><title>MyPdf</title>`);
mywindow.document.write('</head><body >');
var params2 = $("#AllWP").serialize();
mywindow.document.write(document.getElementById("AllWP").innerHTML); // one more time the html structure
mywindow.document.write(params2); // this is the serialized data
mywindow.document.write(ex); // because of this line it is not working
mywindow.document.write('</body></html>');
mywindow.document.close();
mywindow.focus();
mywindow.print();
mywindow.close();
break;
This is one of the forms I want to export as pdf:
If there is no way to do that in javascript, I can also get the above data in PHP and loop them there.