1 回答
TA贡献1876条经验 获得超7个赞
您需要从表中获取 HTML - 您只获取对象 - 而不是执行
var table = document.getElementById('mytable').outerHTML
或
将表对象附加到 win.document 但无论如何你真的应该只写一次
function createPDF(){
const table = document.getElementById('mytable').outerHTML;
const style = `<style>
table {width: 100%;font: 17px Calibri;}
table, th, td {border: solid 1px #DDD; border-collapse: collapse;
padding: 2px 3px;text-align: center;}
</style>`;
// CREATE A WINDOW OBJECT.
const win = window.open('', '', 'height=700,width=700');
const html = `<title>Profile</title>
${style}
${table}`;
win.document.write(html);
win.document.close();
win.print();
}
使用对象(未测试):
function createPDF(){
const table = document.getElementById('mytable');
const style = document.createElement("style");
style.textContent = `table {width: 100%;font: 17px Calibri;}
table, th, td {border: solid 1px #DDD; border-collapse: collapse;
padding: 2px 3px;text-align: center;}`
// CREATE A WINDOW OBJECT.
const win = window.open('', '', 'height=700,width=700');
const doc = win.document;
const title = document.createElement("title");
title.textContent = "Profile"
doc.body.append(title);
doc.body.append(style);
doc.body.append(table);
win.print();
}
添加回答
举报