Commit f1d1ffaebf101ad6d56b71468775c21d5510334c
1 parent
cd4095bd
fix:打印调整
Showing
1 changed file
with
63 additions
and
42 deletions
src/utils/index.js
| ... | ... | @@ -276,7 +276,7 @@ export function b64DecodeUnicode(str) { |
| 276 | 276 | }) |
| 277 | 277 | .join("") |
| 278 | 278 | ); |
| 279 | - } catch (e) {} | |
| 279 | + } catch (e) { } | |
| 280 | 280 | return uni; |
| 281 | 281 | } |
| 282 | 282 | |
| ... | ... | @@ -885,12 +885,12 @@ export function formatGradeNameClass(data) { |
| 885 | 885 | |
| 886 | 886 | ///试卷定制化打印 |
| 887 | 887 | export async function paperPrint(paper) { |
| 888 | - let printWin = window.open("", "_blank", "width=800,height=600,resizable=no"); | |
| 888 | + let printWin = window.open("", "_blank", "width=800,height=600,resizable=no"); | |
| 889 | 889 | var browser = getBrowserEngine(printWin); |
| 890 | 890 | var subjectName = paper.subjectName; |
| 891 | 891 | var paperTitle = paper.title; |
| 892 | 892 | var paperQuestions = paper.questionList; |
| 893 | - function getBrowserEngine(windowParams) { | |
| 893 | + function getBrowserEngine(windowParams) { | |
| 894 | 894 | if (windowParams.navigator.userAgent.indexOf("Edge") > -1) { |
| 895 | 895 | return "EdgeHTML"; |
| 896 | 896 | } |
| ... | ... | @@ -911,6 +911,10 @@ export async function paperPrint(paper) { |
| 911 | 911 | } |
| 912 | 912 | return "Blink"; // Assume it's Chrome, Safari, or an alternative Blink-based browser |
| 913 | 913 | } |
| 914 | + function windowPrint(windowParams) { | |
| 915 | + windowParams.print(); | |
| 916 | + windowParams.close(); | |
| 917 | + } | |
| 914 | 918 | function numberToChinese(num) { |
| 915 | 919 | const chineseDigits = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]; |
| 916 | 920 | const chineseUnits = ["", "十", "百", "千", "万", "亿"]; |
| ... | ... | @@ -967,19 +971,21 @@ export async function paperPrint(paper) { |
| 967 | 971 | printWin.document.documentElement.scrollHeight, // 整个文档高度 |
| 968 | 972 | printWin.document.body.scrollHeight // Body 的高度 |
| 969 | 973 | ); |
| 970 | - const totalHeightMM = totalHeightPx * 25.4 / 96; | |
| 971 | - // ie浏览器高度 | |
| 972 | - var A4HeightMM = 287; | |
| 973 | - if (browser == "Google Chrome") { | |
| 974 | - A4HeightMM = 297; | |
| 975 | - } | |
| 976 | - var pages = Math.ceil(totalHeightMM / A4HeightMM) + 1; | |
| 977 | - for (var page = 0; page < pages; page++) { | |
| 978 | - var pageFooteDiv = printWin.document.createElement('div'); | |
| 979 | - pageFooteDiv.classList.add('page-footer'); | |
| 980 | - pageFooteDiv.innerText = `${paperTitle} ${subjectName} 第${page + 1}页(共${pages}页)`; | |
| 981 | - pageFooteDiv.style.top = (page + 1) * A4HeightMM + (page * 5.5) + 'mm'; | |
| 982 | - printWin.document.body.appendChild(pageFooteDiv); | |
| 974 | + const a4Box = printWin.document.createElement('div'); | |
| 975 | + a4Box.classList.add('altA4Box'); | |
| 976 | + printWin.document.body.appendChild(a4Box); | |
| 977 | + const a4BoxHeightPx = a4Box.offsetHeight; | |
| 978 | + if (a4BoxHeightPx > 0) { | |
| 979 | + printWin.document.body.removeChild(a4Box); | |
| 980 | + var pages = Math.ceil(totalHeightPx / a4BoxHeightPx); | |
| 981 | + if (pages > 2) pages += 1; | |
| 982 | + for (var page = 0; page < pages; page++) { | |
| 983 | + var pageFooteDiv = printWin.document.createElement('div'); | |
| 984 | + pageFooteDiv.classList.add('page-footer'); | |
| 985 | + pageFooteDiv.innerText = `${paperTitle} ${subjectName} 第${page + 1}页(共${pages}页)`; | |
| 986 | + pageFooteDiv.style.top = (page + 1) * a4BoxHeightPx + (page * 25) + 'px'; | |
| 987 | + printWin.document.body.appendChild(pageFooteDiv); | |
| 988 | + } | |
| 983 | 989 | } |
| 984 | 990 | } |
| 985 | 991 | // size: A4 portrait; |
| ... | ... | @@ -991,25 +997,33 @@ export async function paperPrint(paper) { |
| 991 | 997 | @media print |
| 992 | 998 | { |
| 993 | 999 | @page { |
| 994 | - size: A4 portrait; | |
| 995 | - height: 300mm; | |
| 996 | - margin-top:5mm; | |
| 1000 | + size: A4 portrait; | |
| 1001 | + margin-top:0mm; | |
| 997 | 1002 | margin-bottom:10mm; |
| 998 | - margin-left:2mm; | |
| 999 | - margin-right:2mm; | |
| 1003 | + margin-left:4mm; | |
| 1004 | + margin-right:4mm; | |
| 1000 | 1005 | } |
| 1001 | - | |
| 1002 | - body { | |
| 1003 | - counter-reset: page-number; /* 重置页码计数器 */ | |
| 1004 | - } | |
| 1005 | - | |
| 1006 | - mn { | |
| 1007 | - padding-top:2px; | |
| 1008 | - } | |
| 1009 | - } | |
| 1006 | + .altA4Box { | |
| 1007 | + display:none; | |
| 1008 | + } | |
| 1009 | + } | |
| 1010 | + mn { | |
| 1011 | + padding-top:2px; | |
| 1012 | + } | |
| 1013 | + .bodyBox { | |
| 1014 | + width: 210mm; | |
| 1015 | + padding-top:0mm; | |
| 1016 | + padding-bottom:0mm; | |
| 1017 | + padding-left:4mm; | |
| 1018 | + padding-right:4mm; | |
| 1019 | + } | |
| 1020 | + .altA4Box { | |
| 1021 | + height: 317mm; | |
| 1022 | + } | |
| 1010 | 1023 | table tfoot { |
| 1011 | - height: 0px; | |
| 1024 | + height: 30px; | |
| 1012 | 1025 | display: table-footer-group; |
| 1026 | + | |
| 1013 | 1027 | } |
| 1014 | 1028 | table thead { |
| 1015 | 1029 | height: 20px; |
| ... | ... | @@ -1092,6 +1106,9 @@ export async function paperPrint(paper) { |
| 1092 | 1106 | |
| 1093 | 1107 | printWin.document.head.appendChild(style); |
| 1094 | 1108 | |
| 1109 | + const bodyBoxDom = printWin.document.createElement('div'); | |
| 1110 | + bodyBoxDom.classList.add('bodyBox'); | |
| 1111 | + | |
| 1095 | 1112 | const titleBoxDom = printWin.document.createElement('div'); |
| 1096 | 1113 | titleBoxDom.classList.add('title-header'); |
| 1097 | 1114 | const titleDom = printWin.document.createElement('h3'); |
| ... | ... | @@ -1102,7 +1119,7 @@ export async function paperPrint(paper) { |
| 1102 | 1119 | subjectDom.classList.add('title-content'); |
| 1103 | 1120 | titleBoxDom.appendChild(titleDom); |
| 1104 | 1121 | titleBoxDom.appendChild(subjectDom); |
| 1105 | - printWin.document.body.appendChild(titleBoxDom); | |
| 1122 | + bodyBoxDom.appendChild(titleBoxDom); | |
| 1106 | 1123 | |
| 1107 | 1124 | const studentInputBoxDom = printWin.document.createElement('div'); |
| 1108 | 1125 | studentInputBoxDom.classList.add('student-input'); |
| ... | ... | @@ -1115,9 +1132,9 @@ export async function paperPrint(paper) { |
| 1115 | 1132 | studentInputBoxDom.appendChild(studentClassInputDom); |
| 1116 | 1133 | studentInputBoxDom.appendChild(studentNameInputDom); |
| 1117 | 1134 | studentInputBoxDom.appendChild(studentNoInputDom); |
| 1118 | - printWin.document.body.appendChild(studentInputBoxDom); | |
| 1135 | + bodyBoxDom.appendChild(studentInputBoxDom); | |
| 1119 | 1136 | |
| 1120 | - const tableDom = printWin.document.createElement('table'); | |
| 1137 | + const tableDom = printWin.document.createElement('table'); | |
| 1121 | 1138 | const theadDom = printWin.document.createElement('thead'); |
| 1122 | 1139 | const theadTrDom = printWin.document.createElement('tr'); |
| 1123 | 1140 | theadTrDom.appendChild(printWin.document.createElement('th')); |
| ... | ... | @@ -1166,12 +1183,12 @@ export async function paperPrint(paper) { |
| 1166 | 1183 | qTbodyContentTdDom.appendChild(questionDom); |
| 1167 | 1184 | qTbodyTrDom.appendChild(qTbodyLeftAltTdDom); |
| 1168 | 1185 | qTbodyTrDom.appendChild(qTbodyContentTdDom); |
| 1169 | - qTbodyTrDom.appendChild(qTbodyRightAltTdDom); | |
| 1186 | + qTbodyTrDom.appendChild(qTbodyRightAltTdDom); | |
| 1170 | 1187 | tbodyDom.appendChild(qTbodyTrDom); |
| 1171 | 1188 | } |
| 1172 | 1189 | } |
| 1173 | 1190 | tableDom.appendChild(tbodyDom); |
| 1174 | - printWin.document.body.appendChild(tableDom); | |
| 1191 | + bodyBoxDom.appendChild(tableDom); | |
| 1175 | 1192 | |
| 1176 | 1193 | const tfootDom = printWin.document.createElement('tfoot'); |
| 1177 | 1194 | const tfootTrDom = printWin.document.createElement('tr'); |
| ... | ... | @@ -1180,17 +1197,21 @@ export async function paperPrint(paper) { |
| 1180 | 1197 | tfootTrDom.appendChild(printWin.document.createElement('td')); |
| 1181 | 1198 | tfootDom.appendChild(tfootTrDom); |
| 1182 | 1199 | tableDom.appendChild(tfootDom); |
| 1200 | + printWin.document.body.appendChild(bodyBoxDom); | |
| 1183 | 1201 | // generatePageParams(); |
| 1184 | 1202 | const iamges = printWin.document.querySelectorAll('img'); |
| 1185 | 1203 | if (iamges.length >= 1) { |
| 1186 | - iamges[iamges.length - 1].onload = function () { | |
| 1187 | - printWin.print(); | |
| 1188 | - printWin.close(); | |
| 1204 | + var lastImage = iamges[iamges.length - 1]; | |
| 1205 | + if (lastImage.complete) { | |
| 1206 | + windowPrint(printWin); | |
| 1207 | + } else { | |
| 1208 | + lastImage.onload = function () { | |
| 1209 | + windowPrint(printWin); | |
| 1210 | + } | |
| 1189 | 1211 | } |
| 1190 | 1212 | } else { |
| 1191 | - printWin.print(); | |
| 1192 | - printWin.close(); | |
| 1193 | - } | |
| 1213 | + windowPrint(printWin); | |
| 1214 | + } | |
| 1194 | 1215 | } |
| 1195 | 1216 | |
| 1196 | 1217 | export function tablePrint(options) { | ... | ... |