Merge branch '0.8.X' of https://gitee.com/hdka/klp-oa into 0.8.X

This commit is contained in:
砂糖
2026-01-09 19:06:36 +08:00
4 changed files with 179 additions and 172 deletions

View File

@@ -63,7 +63,7 @@
<el-col :span="1.5">
<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExportAll">导出</el-button>
</el-col>
<el-col :span="2" v-if="canExportAll">
<el-col :span="2">
<el-button type="info" plain icon="el-icon-printer" size="mini" :disabled="multiple" @click="handleBatchPrintLabel">批量打印标签</el-button>
</el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
@@ -246,7 +246,7 @@
<!-- 渲染容器屏幕隐藏仅用于截图生成PDF -->
<div ref="batchPdfContainer" class="batch-pdf-root" aria-hidden="true">
<div v-for="(item, idx) in batchPrint.list" :key="item.coilId || idx" class="batch-pdf-page">
<OuterTagPreview :content="item" :paperWidthMm="180" :paperHeightMm="100" />
<label-render :content="item" :labelType="labelType" :hideActions="true" />
</div>
</div>
</el-dialog>
@@ -343,10 +343,6 @@ export default {
type: Boolean,
default: false,
},
canExportAll: {
type: Boolean,
default: false,
}
},
data() {
return {
@@ -822,40 +818,44 @@ export default {
// 纸张尺寸
const paperWidthMm = 180;
const paperHeightMm = 100;
// 留白:左右 4mm上下更小一些,避免底部空得太多
// 边距:左右 4mm上下对称 2mm确保垂直居中
const marginXmm = 4;
const marginTopMm = 0;
const marginBottomMm = 0;
const marginYmm = 0.5; // 上下对称边距(不裁切前提下尽量贴边)
const pageWidthPt = paperWidthMm * mmToPt;
const pageHeightPt = paperHeightMm * mmToPt;
const marginXPt = marginXmm * mmToPt;
const marginTopPt = marginTopMm * mmToPt;
const marginBottomPt = marginBottomMm * mmToPt;
const marginYPt = marginYmm * mmToPt;
const contentWidthPt = pageWidthPt - marginXPt * 2;
const contentHeightPt = pageHeightPt - marginTopPt - marginBottomPt;
const contentHeightPt = pageHeightPt - marginYPt * 2;
const pdfDoc = await PDFDocument.create();
// 关键:只截取标签身(OuterTagPreview不要把页面菜单/弹窗带进去
// 关键:只截取标签身(.label-container不要把外层容器/按钮高度算进去
const pageEls = container.querySelectorAll('.batch-pdf-page');
for (let i = 0; i < pageEls.length; i++) {
const el = pageEls[i];
// 强制用标签的mm尺寸作为截图基准避免被外层布局影响
const canvas = await html2canvas(el, {
// 在每一页内部优先查找标签根节点
const labelEl =
el.querySelector('.label-container') ||
el.querySelector('.material-label-container') ||
el; // 兜底:找不到时退回整页
// 强制用标签的实际尺寸作为截图基准,避免被外层布局影响
const canvas = await html2canvas(labelEl, {
backgroundColor: '#ffffff',
scale: 2,
scale: 3,
useCORS: true,
// 让 html2canvas 为频繁读回优化 Canvas浏览器会提示 willReadFrequently
willReadFrequently: true,
// 确保按元素尺寸截图
width: el.offsetWidth,
height: el.offsetHeight,
windowWidth: el.scrollWidth,
windowHeight: el.scrollHeight,
width: labelEl.offsetWidth,
height: labelEl.offsetHeight,
windowWidth: labelEl.scrollWidth,
windowHeight: labelEl.scrollHeight,
});
const imgDataUrl = canvas.toDataURL('image/png');
@@ -866,18 +866,16 @@ export default {
// 图片铺满页面(保持比例,居中)
const imgW = pngImage.width;
const imgH = pngImage.height;
// 按“内容区域(去掉边距)”计算缩放,让四周留下约 4mm 空白
// 标准适配:确保完整显示且不变形(在内容区域内等比缩放)
const scale = Math.min(contentWidthPt / imgW, contentHeightPt / imgH);
const drawW = imgW * scale;
const drawH = imgH * scale;
// 内容区域内居中 + 外层边距
// 内容区域内居中 + 外层边距(上下对称)
const x = marginXPt + (contentWidthPt - drawW) / 2;
const y = marginBottomPt + (contentHeightPt - drawH) / 2;
const y = marginYPt + (contentHeightPt - drawH) / 2;
console.log(pngImage)
page.drawImage(pngImage, { x, y: y - 20, width: drawW, height: drawH + 20 });
page.drawImage(pngImage, { x, y, width: drawW, height: drawH });
}
const pdfBytes = await pdfDoc.save();