@nstc-business/imes 1.1.12 → 1.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nstc-business/imes",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
4
  "private": false,
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -1194,9 +1194,27 @@ export default {
1194
1194
  return
1195
1195
  }
1196
1196
  if (action === 'print') {
1197
- // PDF blob 直接 printJS 时,Edge「将打印输出另存为」文件名常为空;
1198
- // 改为带 <title> 的 HTML 壳嵌入 PDF 再打印,便于另存为预填文件名
1199
- this.printPdfWithFileName(res.data, this.resolvePrintFileBaseName())
1197
+ // 打印必须走 printJS(PDF blob):HTML+embed 方案在 Edge/Chrome 常打不出 PDF
1198
+ // 另存为文件名尽力用 document.title;稳定带名请用「下载」
1199
+ const printTitle = this.resolvePrintFileBaseName()
1200
+ const prevTitle = document.title
1201
+ if (printTitle) {
1202
+ document.title = printTitle
1203
+ }
1204
+ try {
1205
+ printJS({
1206
+ printable: res.data,
1207
+ type: 'pdf',
1208
+ base64: true,
1209
+ documentTitle: printTitle
1210
+ })
1211
+ } finally {
1212
+ if (printTitle) {
1213
+ setTimeout(() => {
1214
+ document.title = prevTitle
1215
+ }, 1500)
1216
+ }
1217
+ }
1200
1218
  } else {
1201
1219
  this.downloadPdfBase64(res.data)
1202
1220
  }
@@ -1222,127 +1240,6 @@ export default {
1222
1240
  .replace(/[\\/:*?"<>|]/g, '_')
1223
1241
  .trim() || '测算结果'
1224
1242
  },
1225
- /**
1226
- * 带文件名打印 PDF(base64)
1227
- * 时间:2026-10-29 18:30:29
1228
- * 人员:AI && 徐红飞
1229
- * 入参:base64 - PDF 内容;fileBaseName - 另存为预填名(不含 .pdf)
1230
- * 出参:无
1231
- * 方法内容:HTML iframe+title+embed 打印;等 embed load 后再 print;afterprint/长兜底再 cleanup,避免另存为未完成就 revoke
1232
- */
1233
- printPdfWithFileName(base64, fileBaseName) {
1234
- const title = String(fileBaseName || '测算结果').replace(/[\\/:*?"<>|]/g, '_').trim() || '测算结果'
1235
- const pure = String(base64 || '').replace(/^data:application\/pdf;base64,/, '')
1236
- let bytes
1237
- try {
1238
- const binary = atob(pure)
1239
- const len = binary.length
1240
- bytes = new Uint8Array(len)
1241
- for (let i = 0; i < len; i++) {
1242
- bytes[i] = binary.charCodeAt(i)
1243
- }
1244
- } catch (e) {
1245
- printJS({
1246
- printable: pure,
1247
- type: 'pdf',
1248
- base64: true,
1249
- documentTitle: title
1250
- })
1251
- return
1252
- }
1253
- const blob = new Blob([bytes], { type: 'application/pdf' })
1254
- const pdfUrl = URL.createObjectURL(blob)
1255
- const iframe = document.createElement('iframe')
1256
- // 非零屏外尺寸:部分 Edge 对 0x0 iframe 打印/另存为不稳定
1257
- iframe.setAttribute(
1258
- 'style',
1259
- 'position:fixed;left:-9999px;top:0;width:800px;height:600px;border:0;opacity:0;pointer-events:none'
1260
- )
1261
- document.body.appendChild(iframe)
1262
- const frameWin = iframe.contentWindow
1263
- if (!frameWin) {
1264
- URL.revokeObjectURL(pdfUrl)
1265
- if (iframe.parentNode) {
1266
- iframe.parentNode.removeChild(iframe)
1267
- }
1268
- printJS({
1269
- printable: pure,
1270
- type: 'pdf',
1271
- base64: true,
1272
- documentTitle: title
1273
- })
1274
- return
1275
- }
1276
- const frameDoc = frameWin.document
1277
- const safeTitle = title
1278
- .replace(/&/g, '&amp;')
1279
- .replace(/</g, '&lt;')
1280
- .replace(/>/g, '&gt;')
1281
- .replace(/"/g, '&quot;')
1282
- frameDoc.open()
1283
- frameDoc.write(
1284
- `<!DOCTYPE html><html><head><meta charset="utf-8"><title>${safeTitle}</title></head>` +
1285
- `<body style="margin:0"><embed id="pdfEmbed" src="${pdfUrl}" type="application/pdf" width="100%" height="100%" /></body></html>`
1286
- )
1287
- frameDoc.close()
1288
-
1289
- let cleaned = false
1290
- const cleanup = () => {
1291
- if (cleaned) {
1292
- return
1293
- }
1294
- cleaned = true
1295
- try {
1296
- frameWin.removeEventListener('afterprint', onAfterPrint)
1297
- } catch (e) {
1298
- /* ignore */
1299
- }
1300
- try {
1301
- if (iframe.parentNode) {
1302
- iframe.parentNode.removeChild(iframe)
1303
- }
1304
- } catch (e) {
1305
- /* ignore */
1306
- }
1307
- URL.revokeObjectURL(pdfUrl)
1308
- }
1309
- const onAfterPrint = () => {
1310
- cleanup()
1311
- }
1312
- const doPrint = () => {
1313
- try {
1314
- frameWin.addEventListener('afterprint', onAfterPrint)
1315
- frameWin.focus()
1316
- frameWin.print()
1317
- } catch (e) {
1318
- printJS({
1319
- printable: pure,
1320
- type: 'pdf',
1321
- base64: true,
1322
- documentTitle: title
1323
- })
1324
- cleanup()
1325
- return
1326
- }
1327
- // 另存为对话框可能远超数秒;afterprint 优先,60s 兜底防泄漏
1328
- setTimeout(cleanup, 60000)
1329
- }
1330
-
1331
- const embed = frameDoc.getElementById('pdfEmbed')
1332
- let printed = false
1333
- const tryPrint = () => {
1334
- if (printed) {
1335
- return
1336
- }
1337
- printed = true
1338
- doPrint()
1339
- }
1340
- if (embed) {
1341
- embed.addEventListener('load', tryPrint)
1342
- }
1343
- // embed load 在部分浏览器不可靠,最长等 3s 再打
1344
- setTimeout(tryPrint, 3000)
1345
- },
1346
1243
  downloadPdfBase64(base64) {
1347
1244
  const pure = String(base64 || '').replace(/^data:application\/pdf;base64,/, '')
1348
1245
  const binary = atob(pure)