This commit is contained in:
2026-03-11 15:00:25 +08:00
parent 8441e66e93
commit 03cb6fd92b

View File

@@ -2213,16 +2213,65 @@
requestAnimationFrame(function () { requestAnimationFrame(function () {
requestAnimationFrame(function () { requestAnimationFrame(function () {
try { try {
(function () {
var scale = 1.25;
var pdf = new jspdf.jsPDF("p", "mm", "a4");
var pageWidthMm = pdf.internal.pageSize.getWidth();
var pageHeightMm = pdf.internal.pageSize.getHeight();
// 以元素宽度为基准,计算每一页对应的像素高度(避免生成超长大图导致 jsPDF 白页)
var targetWidthPx = Math.max(
1,
Math.floor(pageEl.scrollWidth),
);
var pageHeightPx = Math.max(
1,
Math.floor((targetWidthPx * pageHeightMm) / pageWidthMm),
);
var totalHeightPx = Math.max(
1,
Math.floor(pageEl.scrollHeight),
);
var offsetY = 0;
var pageIndex = 0;
function renderSlice() {
var sliceHeight = Math.min(
pageHeightPx,
totalHeightPx - offsetY,
);
if (sliceHeight <= 0) {
// 结束,保存
var fileName = "企业全景报告.pdf";
if (
reportData &&
reportData.entName &&
typeof reportData.entName === "string"
) {
fileName =
reportData.entName +
"_企业全景报告.pdf";
}
pdf.save(fileName);
restoreBtn();
return;
}
html2canvas(pageEl, { html2canvas(pageEl, {
scale: 1.5, scale: scale,
useCORS: true, useCORS: true,
allowTaint: false, allowTaint: false,
backgroundColor: "#ffffff", backgroundColor: "#ffffff",
x: 0,
y: offsetY,
width: targetWidthPx,
height: sliceHeight,
scrollX: 0, scrollX: 0,
scrollY: 0, scrollY: 0,
windowWidth: pageEl.scrollWidth, windowWidth: targetWidthPx,
windowHeight: pageEl.scrollHeight, windowHeight: sliceHeight,
onclone: function (clonedDoc, node) { onclone: function (clonedDoc) {
var body = clonedDoc.body; var body = clonedDoc.body;
if (body) { if (body) {
body.style.backgroundColor = "#ffffff"; body.style.backgroundColor = "#ffffff";
@@ -2237,78 +2286,49 @@
}) })
.then(function (canvas) { .then(function (canvas) {
if (!canvas.width || !canvas.height) { if (!canvas.width || !canvas.height) {
console.error("截图为空,宽或高为 0"); console.error("分片截图为空,宽或高为 0", {
offsetY: offsetY,
sliceHeight: sliceHeight,
});
restoreBtn(); restoreBtn();
return; return;
} }
// 调试:确认截图是否全白/是否有内容 // 调试:输出每页分片信息
try { console.info("PDF分片截图信息", {
var ctx2d = canvas.getContext("2d"); page: pageIndex + 1,
if (ctx2d) {
var p = ctx2d.getImageData(0, 0, 1, 1).data;
console.info(
"PDF截图信息",
{
w: canvas.width, w: canvas.width,
h: canvas.height, h: canvas.height,
firstPixel: Array.prototype.slice.call(p), offsetY: offsetY,
}, sliceHeight: sliceHeight,
); totalHeightPx: totalHeightPx,
} });
} catch (e) {
console.warn("读取截图像素失败(可能被跨域资源污染)", e); var imgData = canvas.toDataURL("image/jpeg", 0.9);
}
var imgData = canvas.toDataURL( if (pageIndex > 0) pdf.addPage();
"image/jpeg",
0.95,
);
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( pdf.addImage(
imgData, imgData,
"JPEG", "JPEG",
0, 0,
position,
imgWidth,
imgHeight,
);
heightLeft -= pageHeight;
while (heightLeft > 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(
imgData,
"JPEG",
0, 0,
position, pageWidthMm,
imgWidth, pageHeightMm,
imgHeight,
); );
heightLeft -= pageHeight;
} pageIndex += 1;
var fileName = "企业全景报告.pdf"; offsetY += sliceHeight;
if (
reportData && // 给 UI 一点喘息,避免长任务卡死
reportData.entName && setTimeout(renderSlice, 0);
typeof reportData.entName === "string"
) {
fileName =
reportData.entName +
"_企业全景报告.pdf";
}
pdf.save(fileName);
restoreBtn();
}) })
.catch(function (e) { .catch(function (e) {
console.error("生成 PDF 失败", e); console.error("生成 PDF 失败(分片截图阶段)", e);
restoreBtn(); restoreBtn();
}); });
}
renderSlice();
})();
} catch (e) { } catch (e) {
console.error("触发生成 PDF 失败", e); console.error("触发生成 PDF 失败", e);
restoreBtn(); restoreBtn();