完成打印缩放功能

master
lynn 12 months ago
parent 36772a5700
commit 9694459ac8

@ -510,7 +510,22 @@ export default {
const inputs = element.getElementsByTagName('input') const inputs = element.getElementsByTagName('input')
Array.from(inputs).forEach(input => { Array.from(inputs).forEach(input => {
if (input.type === 'checkbox' || input.type === 'radio') { if (input.type === 'checkbox' || input.type === 'radio') {
// checkboxradio if (input.type === 'radio') {
// radio
const name = input.name
const checkedRadio = element.querySelector(`input[type="radio"][name="${name}"]:checked`)
if (checkedRadio) {
// radiotd
const td = checkedRadio.closest('td')
if (td) {
// div
const hiddenDiv = td.querySelector('div[style*="display: none"]')
if (hiddenDiv) {
hiddenDiv.textContent = checkedRadio.value
}
}
}
}
return return
} }
const span = document.createElement('span') const span = document.createElement('span')
@ -538,9 +553,11 @@ export default {
].forEach(key => { ].forEach(key => {
span.style[key] = style[key] span.style[key] = style[key]
}) })
span.style.width = '100%' span.style.whiteSpace = 'normal'
span.style.wordBreak = 'break-all'
span.style.overflowWrap = 'break-word'
span.style.display = 'block' span.style.display = 'block'
span.style.textAlign = 'center' span.style.width = '100%'
input.parentNode.replaceChild(span, input) input.parentNode.replaceChild(span, input)
}) })
@ -570,6 +587,7 @@ export default {
span.style.width = '100%' span.style.width = '100%'
span.style.display = 'block' span.style.display = 'block'
span.style.textAlign = 'center' span.style.textAlign = 'center'
span.style.whiteSpace = 'pre-line' //
select.parentNode.replaceChild(span, select) select.parentNode.replaceChild(span, select)
}) })
@ -599,6 +617,7 @@ export default {
span.style.width = '100%' span.style.width = '100%'
span.style.display = 'block' span.style.display = 'block'
span.style.textAlign = 'center' span.style.textAlign = 'center'
span.style.whiteSpace = 'pre-line' //
textarea.parentNode.replaceChild(span, textarea) textarea.parentNode.replaceChild(span, textarea)
}) })
}, },
@ -638,12 +657,46 @@ export default {
return Number(val).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') return Number(val).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,')
}, },
printHtml() { printHtml() {
const printNode = this.$refs.printtable.cloneNode(true) // HTML
const dom = this.$refs.printtable
if (dom) {
//
const inputs = dom.querySelectorAll('input, select, textarea')
inputs.forEach(input => {
const fieldName = input.getAttribute('data-field')
if (fieldName) {
if (input.type === 'checkbox' || input.type === 'radio') {
//
const checkedInput = dom.querySelector(`[data-field="${fieldName}"]:checked`)
if (checkedInput) {
// radio
const name = checkedInput.name
const radioGroup = dom.querySelectorAll(`input[type="radio"][name="${name}"]`)
radioGroup.forEach(radio => {
// radiochecked
radio.removeAttribute('checked')
})
// radiochecked
checkedInput.setAttribute('checked', 'checked')
}
} else {
// value
input.setAttribute('value', input.value)
}
}
})
}
// 使HTML
const printNode = document.createElement('div')
printNode.innerHTML = dom.innerHTML
// //
const financeTable = printNode.querySelector('.finance-review-table') const financeTable = printNode.querySelector('.finance-review-table')
if (financeTable) { if (financeTable) {
financeTable.remove() financeTable.remove()
} }
// //
this.replaceControls(printNode) this.replaceControls(printNode)
@ -661,7 +714,7 @@ export default {
totalInput.value = total.toFixed(2) totalInput.value = total.toFixed(2)
} }
// //
const upperCaseInput = printNode.querySelector('input[data-field="upperCaseAmount"]') const upperCaseInput = printNode.querySelector('input[data-field="upperCaseAmount"]')
if (upperCaseInput) { if (upperCaseInput) {
upperCaseInput.value = numberToChinese(total) upperCaseInput.value = numberToChinese(total)
@ -672,35 +725,52 @@ export default {
? '2mm 10mm 10mm 2mm' ? '2mm 10mm 10mm 2mm'
: '10mm 10mm 10mm 10mm'; : '10mm 10mm 10mm 10mm';
const win = window.open('', '_blank') const win = window.open('', '_blank')
//
const isPortrait = orientation === 'portrait';
const pageWidth = isPortrait ? 210 : 297;
const scale = 0.8;
const translateX = ((pageWidth - pageWidth * scale) / 2).toFixed(2) + 'mm';
win.document.write(` win.document.write(`
<html> <html>
<head> <head>
<title>打印</title> <title>打印</title>
<style> <style>
@page { size: A4 ${orientation}; margin: ${margin}; } @page { size: A4 ${orientation}; margin: ${margin}; }
body { body {
margin: 0; margin: 0;
padding: 0; padding: 0;
width: ${pageWidth}mm;
display: flex;
justify-content: center;
align-items: flex-start;
} }
.white-container { .white-container {
width: 100% !important; display: inline-block;
margin: 0 auto;
margin-left: 0px;
background: #fff; background: #fff;
padding: 0px;
box-sizing: border-box; box-sizing: border-box;
transform: scale(${scale}) translateX(${translateX});
transform-origin: top left;
margin: 0;
padding: 0;
} }
.form-container { .form-container { width: 100% !important; margin: 0 auto; }
width: 100% !important; table { width: 100%; border-collapse: collapse; }
margin: 0 auto; td { border: 1px solid #000; padding: 8px; }
} @media print {
table { body {
width: 100%; width: ${pageWidth}mm;
border-collapse: collapse; margin: 0;
} padding: 0;
td { display: flex;
border: 1px solid #000; justify-content: center;
padding: 8px; align-items: flex-start;
}
.white-container {
transform: scale(${scale}) translateX(${translateX});
transform-origin: top left;
display: inline-block;
}
} }
</style> </style>
</head> </head>

Loading…
Cancel
Save