无法读取未定义的属性OwnerDocument



运行到这个错误,

Uncaught (in promise) TypeError: Cannot read property 'ownerDocument'未定义的

,我认为这可能与我传递元素对象ID的方式有关。有人能告诉我我的语法是否正确吗?

//从HTML创建PDf

function CreatePDFfromHTML(divName) {
var HTML_Width = document.getElementById(divName).style.width;
var HTML_Height = document.getElementById(divName).style.height;
var top_left_margin = 15;
var PDF_Width = HTML_Width + (top_left_margin * 2);
var PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
var canvas_image_width = HTML_Width;
var canvas_image_height = HTML_Height;
var totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;
html2canvas(document.getElementById(divName)[0]).then(function (canvas) {
var imgData = canvas.toDataURL("image/jpeg", 1.0);
var pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);
pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);
for (var i = 1; i <= totalPDFPages; i++) {
pdf.addPage(PDF_Width, PDF_Height);
pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height * i) + (top_left_margin * 4), canvas_image_width, canvas_image_height);
}
pdf.save("Your_PDF_Name.pdf");
document.getElementById(divName).style.visibility = "hidden";
});
}
<button type="submit" onclick="CreatePDFfromHTML('calResult')" />Download</button>

在调用html2canvas时不需要[0]访问器。div元素没有0属性,所以[0]返回undefined

function CreatePDFfromHTML(divName) {
// This is incorrect.
console.log(document.getElementById(divName)[0]);
// Use this instead.
console.log(document.getElementById(divName));
}
<div id="calResult">Hello, World!</div>
<button type="submit" onclick="CreatePDFfromHTML('calResult')">Download</button>

最新更新