打印出货单修改

This commit is contained in:
2026-01-31 16:33:10 +08:00
parent 2357b14957
commit ea9cad9874
2 changed files with 172 additions and 88 deletions

View File

@@ -76,13 +76,22 @@ spring:
username: klp username: klp
password: KeLunPu@123 password: KeLunPu@123
# 从库数据源 # 从库数据源
slave: acid:
lazy: true lazy: true
type: ${spring.datasource.type} type: ${spring.datasource.type}
driverClassName: com.mysql.cj.jdbc.Driver driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://140.143.206.120:13306/klp_pocketfactory?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true url: jdbc:mysql://140.143.206.120:13306/klp_pocketfactory?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
username: klp username: klp
password: KeLunPu@123 password: KeLunPu@123
# 从库数据源(镀锌一库)
galvanize1:
lazy: true
type: ${spring.datasource.type}
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://140.143.206.120:3306/cgldb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&autoReconnect=true&rewriteBatchedStatements=true
username: klp
password: KeLunPu123@
# oracle: # oracle:
# type: ${spring.datasource.type} # type: ${spring.datasource.type}
# driverClassName: oracle.jdbc.OracleDriver # driverClassName: oracle.jdbc.OracleDriver

View File

@@ -53,20 +53,20 @@
<th>数量</th> <th>数量</th>
<th>重量T</th> <th>重量T</th>
<th>单价</th> <th>单价</th>
<th>备注</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<!-- 无明细提示 --> <!-- 无明细提示 -->
<tr v-if="localWaybillDetails.length === 0"> <tr v-if="localWaybillDetails.length === 0">
<td colspan="13" class="no-data"> <td colspan="12" class="no-data">
<div class="no-data-content"> <div class="no-data-content">
<el-empty description="暂无发货单明细" /> <el-empty description="暂无发货单明细" />
</div> </div>
</td> </td>
</tr> </tr>
<!-- 明细数据 --> <!-- 明细数据 -->
<tr v-for="(item, index) in localWaybillDetails" :key="index"> <tr v-for="(item, index) in displayWaybillDetails" :key="index">
<td><input type="text" class="transparent-input" v-model="item.productName" /></td> <td><input type="text" class="transparent-input" v-model="item.productName" /></td>
<td><input type="text" class="table-input transparent-input" v-model="item.edgeType" /> <td><input type="text" class="table-input transparent-input" v-model="item.edgeType" />
</td> </td>
@@ -87,11 +87,7 @@
<td><input type="number" class="table-input transparent-input" v-model.number="item.weight" <td><input type="number" class="table-input transparent-input" v-model.number="item.weight"
placeholder="0.00" /></td> placeholder="0.00" /></td>
<td><input type="text" class="table-input transparent-input" v-model="item.unitPrice" /></td> <td><input type="text" class="table-input transparent-input" v-model="item.unitPrice" /></td>
<td>
<div class="table-cell-with-action">
<input type="text" class="table-input transparent-input" v-model="item.remark" />
</div>
</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
@@ -130,6 +126,11 @@
</div> </div>
<!-- 操作按钮 --> <!-- 操作按钮 -->
<div class="waybill-actions"> <div class="waybill-actions">
<div class="waybill-pagebar">
<el-button size="mini" @click="changePage(currentPage - 1)" :disabled="currentPage <= 1">上一页</el-button>
<span class="page-info"> {{ currentPage }} / {{ totalPages }} </span>
<el-button size="mini" @click="changePage(currentPage + 1)" :disabled="currentPage >= totalPages">下一页</el-button>
</div>
<el-button type="primary" @click="saveAsImage">保存为图片</el-button> <el-button type="primary" @click="saveAsImage">保存为图片</el-button>
<el-button type="success" @click="printWaybill">打印</el-button> <el-button type="success" @click="printWaybill">打印</el-button>
</div> </div>
@@ -155,6 +156,9 @@ export default {
}, },
data() { data() {
return { return {
currentPage: 1,
perPage: 5,
totalPages: 1,
// 本地可编辑的发货单数据 // 本地可编辑的发货单数据
localWaybill: { localWaybill: {
consigneeUnit: '', consigneeUnit: '',
@@ -168,7 +172,9 @@ export default {
pickupLocation: '' pickupLocation: ''
}, },
// 本地可编辑的发货单明细 // 本地可编辑的发货单明细
localWaybillDetails: [] localWaybillDetails: [],
// 预览/打印用明细每页固定5行不足补空行
displayWaybillDetails: []
}; };
}, },
watch: { watch: {
@@ -194,18 +200,52 @@ export default {
}, },
waybillDetails: { waybillDetails: {
handler(newVal) { handler(newVal) {
console.log('waybillDetails', newVal);
if (newVal && Array.isArray(newVal)) { if (newVal && Array.isArray(newVal)) {
this.localWaybillDetails = [...newVal]; this.localWaybillDetails = [...newVal];
} else { } else {
this.localWaybillDetails = []; this.localWaybillDetails = [];
} }
this.refreshPagination();
}, },
immediate: true, immediate: true,
deep: true deep: true
} }
}, },
methods: { methods: {
buildPage(details, page) {
const perPage = this.perPage;
const src = Array.isArray(details) ? details : [];
const start = (page - 1) * perPage;
const pageRows = src.slice(start, start + perPage).map((x) => ({ ...x }));
while (pageRows.length < perPage) {
pageRows.push({
productName: '',
edgeType: '',
packageType: '',
actualWarehouseName: '',
settlementType: '',
rawMaterialFactory: '',
coilNumber: '',
specification: '',
material: '',
quantity: '',
weight: '',
unitPrice: ''
});
}
return pageRows;
},
refreshPagination() {
const src = Array.isArray(this.localWaybillDetails) ? this.localWaybillDetails : [];
this.totalPages = Math.max(1, Math.ceil(src.length / this.perPage));
if (this.currentPage > this.totalPages) this.currentPage = this.totalPages;
if (this.currentPage < 1) this.currentPage = 1;
this.displayWaybillDetails = this.buildPage(src, this.currentPage);
},
changePage(next) {
this.currentPage = next;
this.refreshPagination();
},
// 从日期字符串中提取年份 // 从日期字符串中提取年份
getYearFromDate(dateStr) { getYearFromDate(dateStr) {
if (dateStr) { if (dateStr) {
@@ -273,55 +313,57 @@ export default {
// 打印发货单 // 打印发货单
async printWaybill() { async printWaybill() {
const node = this.$refs.waybillRef; const node = this.$refs.waybillRef;
// 确保容器在打印时能完整显示所有内容 const paperWidthMm = 241;
const originalWidth = node.style.width; const paperHeightMm = 140;
const originalHeight = node.style.height;
const paperWidthMm = 297;
const paperHeightMm = 180;
const originalOverflow = node.style.overflow;
// 临时调整容器样式,确保所有内容可见 const mmToPx = (mm) => (mm * 96) / 25.4;
node.style.width = 'auto';
node.style.overflow = 'visible';
// 获取实际内容宽度
const contentWidth = node.scrollWidth;
const contentHeight = node.scrollHeight;
// console.log('contentWidth', contentWidth, originalWidth, originalHeight);
// 改用html2canvas + pdf-lib打印pdf
const canvas = await html2canvas(node, {
backgroundColor: '#ffffff',
scale: 3,
useCORS: true,
// 让 html2canvas 为频繁读回优化 Canvas与批量导出一致
willReadFrequently: true,
// 确保按元素尺寸截图,明确指定宽高
width: contentWidth,
height: contentHeight,
windowWidth: contentWidth,
windowHeight: contentHeight,
});
// 5. 使用 pdf-lib 生成单页 PDF占满整张纸无边距
const mmToPt = 72 / 25.4; const mmToPt = 72 / 25.4;
const pageWidthPt = paperWidthMm * mmToPt; const pageWidthPt = paperWidthMm * mmToPt;
const pageHeightPt = paperHeightMm * mmToPt; const pageHeightPt = paperHeightMm * mmToPt;
const pdfDoc = await PDFDocument.create(); const pageWidthPx = Math.round(mmToPx(paperWidthMm));
console.log('canvas', canvas); const pageHeightPx = Math.round(mmToPx(paperHeightMm));
const png = canvas.toDataURL('image/png');
const imgPng = await pdfDoc.embedPng(png);
// 确保页面尺寸正确宽100mm高80mm横向
const page = pdfDoc.addPage([pageWidthPt, pageHeightPt]);
// 直接拉伸填充整个PDF页面不留边距确保占满整张纸 const allDetails = Array.isArray(this.localWaybillDetails) ? this.localWaybillDetails : [];
// 从(0,0)开始,直接填充整个页面尺寸 const totalPages = Math.max(1, Math.ceil(allDetails.length / this.perPage));
page.drawImage(imgPng, {
x: 0, const originalPage = this.currentPage;
y: 0, const originalDisplay = this.displayWaybillDetails;
width: pageWidthPt,
height: pageHeightPt const pdfDoc = await PDFDocument.create();
});
try {
for (let page = 1; page <= totalPages; page++) {
this.currentPage = page;
this.displayWaybillDetails = this.buildPage(allDetails, page);
await this.$nextTick();
const canvas = await html2canvas(node, {
backgroundColor: '#ffffff',
scale: 3,
useCORS: true,
willReadFrequently: true,
width: pageWidthPx,
height: pageHeightPx,
windowWidth: pageWidthPx,
windowHeight: pageHeightPx
});
const png = canvas.toDataURL('image/png');
const imgPng = await pdfDoc.embedPng(png);
const pdfPage = pdfDoc.addPage([pageWidthPt, pageHeightPt]);
pdfPage.drawImage(imgPng, {
x: 0,
y: 0,
width: pageWidthPt,
height: pageHeightPt
});
}
} finally {
this.currentPage = originalPage;
this.displayWaybillDetails = originalDisplay;
this.refreshPagination();
}
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' });
@@ -331,18 +373,11 @@ export default {
if (!win) { if (!win) {
const a = document.createElement('a'); const a = document.createElement('a');
a.href = url; a.href = url;
a.download = `标签_${new Date().getTime()}.pdf`; a.download = `发货单_${this.waybill.waybillName || this.waybill.waybillNo || new Date().getTime()}.pdf`;
document.body.appendChild(a); document.body.appendChild(a);
a.click(); a.click();
document.body.removeChild(a); document.body.removeChild(a);
} }
// 恢复原始样式
setTimeout(() => {
node.style.width = originalWidth;
node.style.overflow = originalOverflow;
}, 100);
} }
} }
} }
@@ -350,16 +385,15 @@ export default {
<style scoped> <style scoped>
.waybill-container { .waybill-container {
width: 960px; width: 241mm;
max-width: none; height: 140mm;
min-width: 850px;
margin: 0 auto; margin: 0 auto;
padding: 20px; padding: 6mm;
background: white; padding-top: 22mm;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); background: #fff;
font-family: SimSun, serif; box-shadow: none;
overflow-x: auto; font-family: SimSun, "Courier New", monospace;
overflow-y: visible; overflow: hidden;
} }
/* 头部样式 */ /* 头部样式 */
@@ -367,8 +401,8 @@ export default {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
margin-bottom: 20px; margin-bottom: 3mm;
font-size: 16px; font-size: 11px;
} }
.header-left, .header-left,
@@ -432,22 +466,22 @@ export default {
/* 表格样式 */ /* 表格样式 */
.waybill-table { .waybill-table {
width: 100%; width: 100%;
max-width: 100%;
border-collapse: collapse; border-collapse: collapse;
margin-bottom: 20px; margin-bottom: 2mm;
font-size: 12px; font-size: 10px;
table-layout: fixed; table-layout: fixed;
} }
.waybill-table th, .waybill-table th,
.waybill-table td { .waybill-table td {
border: 1px solid #000; border: 0.4px solid #000;
box-sizing: border-box; box-sizing: border-box;
line-height: 24px; line-height: 6mm;
text-align: center; text-align: center;
vertical-align: middle; vertical-align: middle;
word-wrap: break-word; padding: 0;
word-break: break-all; overflow: hidden;
white-space: nowrap;
} }
/* 表格列宽设置 */ /* 表格列宽设置 */
@@ -523,11 +557,6 @@ export default {
/* 单价 */ /* 单价 */
} }
.waybill-table th:nth-child(13),
.waybill-table td:nth-child(13) {
width: 100px;
/* 备注 */
}
.waybill-table th { .waybill-table th {
background-color: #f5f7fa; background-color: #f5f7fa;
@@ -543,11 +572,57 @@ export default {
box-sizing: border-box; box-sizing: border-box;
width: 100%; width: 100%;
height: 100%; height: 100%;
padding: 4px; margin: 0;
padding: 0;
border: none;
outline: none;
background: transparent;
font-family: inherit;
font-size: inherit;
line-height: 6mm;
text-align: inherit;
vertical-align: middle;
} }
.table-input:focus { .transparent-input {
border-color: #409eff; border: none !important;
background: transparent !important;
padding: 0 !important;
margin: 0 !important;
height: 100% !important;
line-height: 6mm !important;
vertical-align: middle !important;
}
.waybill-table th,
.waybill-table td {
padding: 0 !important;
margin: 0 !important;
line-height: 6mm !important;
height: 6mm !important;
vertical-align: middle !important;
}
.waybill-table input {
vertical-align: middle !important;
height: 6mm !important;
line-height: 6mm !important;
padding: 0 2px !important;
margin: 0 !important;
border: none !important;
background: transparent !important;
box-shadow: none !important;
width: 100% !important;
max-width: 100% !important;
overflow: hidden !important;
text-overflow: ellipsis !important;
white-space: nowrap !important;
}
.waybill-table input:focus {
outline: none !important;
box-shadow: none !important;
border: 1px dashed #999 !important;
} }
/* 无数据样式 */ /* 无数据样式 */