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