This commit is contained in:
2026-03-10 19:12:35 +08:00
parent 8877cf9691
commit 1bcb4a9c2e

View File

@@ -104,6 +104,29 @@
text-align: right;
min-width: 140px;
}
.header-actions {
margin-top: 8px;
}
.print-btn {
display: inline-flex;
align-items: center;
padding: 6px 12px;
border-radius: 999px;
border: 1px solid rgba(255, 255, 255, 0.7);
background: rgba(255, 255, 255, 0.1);
color: #ffffff;
font-size: 12px;
cursor: pointer;
transition:
background 0.15s ease,
color 0.15s ease,
transform 0.1s ease;
}
.print-btn:hover {
background: #ffffff;
color: var(--primary-dark);
transform: translateY(-1px);
}
.header-score .score {
font-size: 32px;
font-weight: 700;
@@ -356,6 +379,9 @@
.top-nav {
display: none !important;
}
.print-btn {
display: none !important;
}
/* 抬头简化为白底、去圆角和阴影 */
.header {
background: #ffffff !important;
@@ -426,10 +452,17 @@
综合评分
<span id="riskLevelPill" class="pill low">风险:-</span>
</div>
<div class="header-actions">
<button id="btnSavePdf" class="print-btn">保存为 PDF</button>
</div>
</div>
</header>
<main id="reportSections" class="report-sections"></main>
</div>
<!-- 前端截图并生成 PDF 所需依赖 -->
<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jspdf@2.5.1/dist/jspdf.umd.min.js"></script>
<script>
(function () {
// 后端通过模板引擎注入的企业报告数据QYGLJ1U9 聚合结果)
@@ -2067,6 +2100,82 @@
}
}
loadReport();
// 绑定「保存为 PDF」按钮使用 html2canvas + jsPDF 截图生成 PDF避免依赖浏览器打印对话框
var saveBtn = document.getElementById("btnSavePdf");
if (saveBtn && window.html2canvas && window.jspdf && window.jspdf.jsPDF) {
saveBtn.addEventListener("click", function () {
try {
var pageEl = document.querySelector(".page");
if (!pageEl) {
console.error("未找到 .page 容器");
return;
}
html2canvas(pageEl, {
scale: 2,
useCORS: true,
scrollX: 0,
scrollY: -window.scrollY,
})
.then(function (canvas) {
var imgData = canvas.toDataURL("image/png");
var pdf = new jspdf.jsPDF("p", "mm", "a4");
var pageWidth = pdf.internal.pageSize.getWidth();
var pageHeight = pdf.internal.pageSize.getHeight();
var imgWidth = pageWidth;
var imgHeight =
(canvas.height * imgWidth) / canvas.width;
var position = 0;
var heightLeft = imgHeight;
pdf.addImage(
imgData,
"PNG",
0,
position,
imgWidth,
imgHeight,
);
heightLeft -= pageHeight;
while (heightLeft > 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(
imgData,
"PNG",
0,
position,
imgWidth,
imgHeight,
);
heightLeft -= pageHeight;
}
var fileName = "企业全景报告.pdf";
if (
reportData &&
reportData.entName &&
typeof reportData.entName === "string"
) {
fileName =
reportData.entName +
"_企业全景报告.pdf";
}
pdf.save(fileName);
})
.catch(function (e) {
console.error("生成 PDF 失败", e);
});
} catch (e) {
console.error("触发生成 PDF 失败", e);
}
});
}
})();
</script>
</body>