function printToPdf() {
const element = document.getElementById('html2pdf_id');
const originalStyles = element.getAttribute('style');
// 临时应用打印样式
element.style.width = '100%';
element.style.margin = '0';
element.style.padding = '20px';
// 隐藏不需要打印的元素
const noPrintElements = element.querySelectorAll('.no-export, button, .actions');
noPrintElements.forEach(el => {
el.style.display = 'none';
});
// 触发浏览器打印
window.print();
// 恢复样式
element.setAttribute('style', originalStyles || '');
noPrintElements.forEach(el => {
el.style.display = '';
});
}
// 添加打印样式到页面
const printStyles = `
@media print {
body * {
visibility: hidden;
}
#html2pdf_id, #html2pdf_id * {
visibility: visible;
}
#html2pdf_id {
position: absolute;
left: 0;
top: 0;
width: 100%;
padding: 20px;
}
.no-print, button, .actions {
display: none !important;
}
}
`;
const styleSheet = document.createElement("style");
styleSheet.textContent = printStyles;
document.head.appendChild(styleSheet);