Refactor PDF generation in week report view. Removed temporary container for PDF content and implemented direct printing in a new window. Enhanced styling for printed output and improved user feedback during the PDF generation process.

This commit is contained in:
Ethanfly 2026-01-09 12:06:55 +08:00
parent cdec96e852
commit 3955dd6049

View File

@ -1530,9 +1530,6 @@
document.querySelector('.loading-text').textContent = '正在生成PDF...';
try {
// 创建临时容器用于PDF生成
const tempDiv = document.createElement('div');
tempDiv.id = 'pdf-content';
// 转换Markdown为HTML
let htmlContent = '';
if (typeof marked !== 'undefined') {
@ -1547,100 +1544,76 @@
.replace(/\n/gim, '<br>');
}
tempDiv.innerHTML = `
<div class="pdf-wrapper">
// 使用新窗口打印方式生成PDF
const printWindow = window.open('', '_blank');
printWindow.document.write(`
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>周报_${document.getElementById('week-start').value}</title>
<style>
@media print {
body { margin: 0; padding: 20mm; }
}
body {
font-family: "Microsoft YaHei", "SimHei", "PingFang SC", sans-serif;
font-size: 12pt;
line-height: 1.8;
color: #333;
padding: 20mm;
max-width: 210mm;
margin: 0 auto;
background: white;
}
h1 {
font-size: 18pt;
color: #1a1a1a;
border-bottom: 2px solid #3b82f6;
padding-bottom: 8px;
margin: 0 0 16px 0;
}
h2 {
font-size: 14pt;
color: #1e40af;
margin: 20px 0 10px 0;
padding-left: 10px;
border-left: 4px solid #3b82f6;
}
h3 {
font-size: 12pt;
color: #374151;
margin: 16px 0 8px 0;
font-weight: 600;
}
p { margin: 0 0 10px 0; }
ul, ol { margin: 0 0 10px 20px; padding: 0; }
li { margin-bottom: 5px; }
strong { color: #1e40af; }
img { max-width: 100%; height: auto; margin: 10px 0; }
blockquote {
margin: 10px 0;
padding: 10px 15px;
border-left: 4px solid #3b82f6;
background: #f0f9ff;
}
</style>
</head>
<body>
${htmlContent}
</div>
`;
tempDiv.style.cssText = `
position: absolute;
left: -9999px;
top: 0;
width: 210mm;
background: white;
font-family: "Microsoft YaHei", "SimHei", "PingFang SC", -apple-system, sans-serif;
font-size: 12pt;
line-height: 1.6;
color: #333;
`;
<script>
window.onload = function() {
setTimeout(function() {
window.print();
}, 300);
};
<\/script>
</body>
</html>
`);
printWindow.document.close();
// 添加内联样式
const style = document.createElement('style');
style.textContent = `
#pdf-content .pdf-wrapper {
padding: 15mm 20mm;
box-sizing: border-box;
}
#pdf-content h1 {
font-size: 18pt;
color: #1a1a1a;
border-bottom: 2px solid #4a90d9;
padding-bottom: 8px;
margin: 0 0 15px 0;
}
#pdf-content h2 {
font-size: 14pt;
color: #2c3e50;
margin: 20px 0 10px 0;
padding-left: 10px;
border-left: 4px solid #4a90d9;
}
#pdf-content h3 {
font-size: 12pt;
color: #34495e;
margin: 15px 0 8px 0;
}
#pdf-content p {
margin: 0 0 10px 0;
text-align: justify;
}
#pdf-content ul, #pdf-content ol {
margin: 0 0 10px 20px;
padding: 0;
}
#pdf-content li {
margin-bottom: 5px;
}
#pdf-content img {
max-width: 100%;
height: auto;
margin: 10px 0;
border: 1px solid #ddd;
border-radius: 4px;
}
#pdf-content strong {
color: #2c3e50;
}
#pdf-content blockquote {
margin: 10px 0;
padding: 10px 15px;
border-left: 4px solid #4a90d9;
background: #f8f9fa;
}
`;
tempDiv.prepend(style);
document.body.appendChild(tempDiv);
const opt = {
margin: 0,
filename: `周报_${document.getElementById('week-start').value}.pdf`,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: {
scale: 2,
useCORS: true,
logging: false,
width: tempDiv.querySelector('.pdf-wrapper').scrollWidth,
windowWidth: tempDiv.querySelector('.pdf-wrapper').scrollWidth
},
jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },
pagebreak: { mode: ['avoid-all', 'css', 'legacy'] }
};
await html2pdf().set(opt).from(tempDiv.querySelector('.pdf-wrapper')).save();
document.body.removeChild(tempDiv);
showToast('PDF 下载成功', 'success');
showToast('请在打印对话框中选择"另存为PDF"', 'info');
} catch (error) {
showToast('下载失败: ' + error.message, 'error');
console.error('PDF生成错误:', error);