原料码放大

This commit is contained in:
2026-01-10 11:58:50 +08:00
parent 407d4e0955
commit 8b8c3c1359

View File

@@ -150,17 +150,21 @@ export default {
// 5. 用html2canvas生成高清Canvas与批量导出完全一致 // 5. 用html2canvas生成高清Canvas与批量导出完全一致
// 关键:直接截图 labelContainer与批量打印完全一致 // 关键:直接截图 labelContainer与批量打印完全一致
// 确保canvas的宽高与labelContainer的实际尺寸一致
const containerWidth = labelContainer.offsetWidth || labelContainer.scrollWidth;
const containerHeight = labelContainer.offsetHeight || labelContainer.scrollHeight;
const canvas = await html2canvas(labelContainer, { const canvas = await html2canvas(labelContainer, {
backgroundColor: '#ffffff', backgroundColor: '#ffffff',
scale: 3, scale: 3,
useCORS: true, useCORS: true,
// 让 html2canvas 为频繁读回优化 Canvas与批量导出一致 // 让 html2canvas 为频繁读回优化 Canvas与批量导出一致
willReadFrequently: true, willReadFrequently: true,
// 确保按元素尺寸截图(与批量导出完全一致) // 确保按元素尺寸截图,明确指定宽高
width: labelContainer.offsetWidth, width: containerWidth,
height: labelContainer.offsetHeight, height: containerHeight,
windowWidth: labelContainer.scrollWidth, windowWidth: containerWidth,
windowHeight: labelContainer.scrollHeight, windowHeight: containerHeight,
}); });
// 5. 使用 pdf-lib 生成单页 PDF占满整张纸无边距 // 5. 使用 pdf-lib 生成单页 PDF占满整张纸无边距
@@ -170,21 +174,17 @@ export default {
const pdfDoc = await PDFDocument.create(); const pdfDoc = await PDFDocument.create();
const imgPng = await pdfDoc.embedPng(canvas.toDataURL('image/png')); const imgPng = await pdfDoc.embedPng(canvas.toDataURL('image/png'));
// 确保页面尺寸正确宽100mm高80mm横向
const page = pdfDoc.addPage([pageWidthPt, pageHeightPt]); const page = pdfDoc.addPage([pageWidthPt, pageHeightPt]);
const imgW = imgPng.width; // 直接拉伸填充整个PDF页面不留边距确保占满整张纸
const imgH = imgPng.height; // 从(0,0)开始,直接填充整个页面尺寸
// 直接占满整个PDF页面不留边距 page.drawImage(imgPng, {
// 确保图片按页面尺寸等比缩放,占满整张纸 x: 0,
const scale = Math.min(pageWidthPt / imgW, pageHeightPt / imgH); y: 0,
const drawW = imgW * scale; width: pageWidthPt,
const drawH = imgH * scale; height: pageHeightPt
});
// 居中放置(如果图片比例与页面不完全一致)
const x = (pageWidthPt - drawW) / 2;
const y = (pageHeightPt - drawH) / 2;
page.drawImage(imgPng, { x, y, width: drawW, height: drawH });
const pdfBytes = await pdfDoc.save(); const pdfBytes = await pdfDoc.save();
const blob = new Blob([pdfBytes], { type: 'application/pdf' }); const blob = new Blob([pdfBytes], { type: 'application/pdf' });