zhou zhou
5 天以前 40905cbd04c2e332cd4bc2b9e0c5b3e1da9cccfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
function escapeHtml(value) {
  return String(value ?? '')
    .replaceAll('&', '&')
    .replaceAll('<', '&lt;')
    .replaceAll('>', '&gt;')
    .replaceAll('"', '&quot;')
    .replaceAll("'", '&#39;')
}
 
function getReportStyle(meta = {}) {
  return meta?.reportStyle && typeof meta.reportStyle === 'object' ? meta.reportStyle : {}
}
 
function getTitleAlignClass(titleAlign) {
  const alignMap = {
    left: 'title-left',
    center: 'title-center',
    right: 'title-right'
  }
  return alignMap[titleAlign] ?? alignMap.center
}
 
function getTitleLevelClass(titleLevel) {
  const levelMap = {
    normal: 'title-normal',
    strong: 'title-strong',
    prominent: 'title-prominent'
  }
  return levelMap[titleLevel] ?? levelMap.strong
}
 
function getPageOrientation(reportStyle = {}) {
  return reportStyle.orientation === 'landscape' ? 'landscape' : 'portrait'
}
 
function getMetaItems(meta = {}) {
  return [
    { key: 'reportDate', label: '报表日期', value: meta.reportDate ?? '--' },
    { key: 'operator', label: '打印人', value: meta.operator ?? '--' },
    { key: 'printedAt', label: '打印时间', value: meta.printedAt ?? '--' },
    { key: 'count', label: '记录数', value: meta.count ?? '--' }
  ]
}
 
function getCellText(row, column, index) {
  if (column?.source === '__sequence__') {
    return index + 1
  }
  return row?.[column?.source] ?? '--'
}
 
function getAlignClass(column = {}) {
  const alignMap = {
    left: 'cell-left',
    center: 'cell-center',
    right: 'cell-right'
  }
  return alignMap[column.align] ?? alignMap.left
}
 
export function buildPrintDocumentHtml({ title = '报表', meta = {}, rows = [], columns = [] } = {}) {
  const reportStyle = getReportStyle(meta)
  const reportTitle = meta.reportTitle || title
  const titleClass = `${getTitleAlignClass(reportStyle.titleAlign)} ${getTitleLevelClass(reportStyle.titleLevel)}`
  const orientation = getPageOrientation(reportStyle)
  const showBorder = reportStyle.showBorder !== false
  const metaItems = getMetaItems(meta)
  const tableWrapClass = showBorder ? 'report-table-wrap' : 'report-table-wrap report-table-wrap-borderless'
  const cellClassName = showBorder ? 'cell-bordered' : 'cell-borderless'
 
  const headerHtml = columns
    .map(
      (column) =>
        `<th class="${getAlignClass(column)} ${cellClassName}">${escapeHtml(column.label ?? '--')}</th>`
    )
    .join('')
 
  const bodyHtml =
    rows.length > 0
      ? rows
          .map((row, index) => {
            const cells = columns
              .map(
                (column) =>
                  `<td class="${getAlignClass(column)} ${cellClassName}">${escapeHtml(getCellText(row, column, index))}</td>`
              )
              .join('')
            return `<tr>${cells}</tr>`
          })
          .join('')
      : `<tr><td colspan="${Math.max(columns.length, 1)}" class="empty-cell">暂无打印数据</td></tr>`
 
  const metaHtml = metaItems
    .map(
      (item) =>
        `<div class="report-meta-item"><span class="report-meta-label">${escapeHtml(item.label)}</span><span class="report-meta-value">${escapeHtml(item.value)}</span></div>`
    )
    .join('')
 
  return `<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>${escapeHtml(reportTitle)}</title>
    <style>
      :root {
        color-scheme: light;
      }
      * {
        box-sizing: border-box;
      }
      @page {
        size: A4 ${orientation};
      }
      html,
      body {
        margin: 0;
        padding: 0;
        background: #ffffff;
        color: #0f172a;
        font-family: "Microsoft YaHei", "PingFang SC", "Helvetica Neue", Arial, sans-serif;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
      }
      body {
        padding: 0;
      }
      .report-page {
        width: 100%;
      }
      .report-title-wrap {
        border-bottom: 1px solid #cbd5e1;
        padding-bottom: 16px;
      }
      .report-title {
        line-height: 1.2;
        color: #0f172a;
      }
      .title-left {
        text-align: left;
      }
      .title-center {
        text-align: center;
      }
      .title-right {
        text-align: right;
      }
      .title-normal {
        font-size: 18px;
        font-weight: 500;
      }
      .title-strong {
        font-size: 22px;
        font-weight: 600;
      }
      .title-prominent {
        font-size: 26px;
        font-weight: 700;
      }
      .report-meta {
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 10px;
        margin-top: 12px;
        font-size: 11px;
        color: #475569;
      }
      .report-meta-item {
        min-width: 0;
      }
      .report-meta-label {
        color: #94a3b8;
      }
      .report-meta-value {
        margin-left: 8px;
        color: #334155;
        word-break: break-word;
      }
      .report-table-wrap {
        margin-top: 16px;
        border: 1px solid #cbd5e1;
        overflow: hidden;
      }
      .report-table-wrap-borderless {
        border: 0;
      }
      table {
        width: 100%;
        border-collapse: collapse;
        table-layout: auto;
        font-size: 11px;
      }
      th,
      td {
        padding: 4px 6px;
        vertical-align: top;
        word-break: break-all;
      }
      .cell-bordered {
        border: 1px solid #cbd5e1;
      }
      .cell-borderless {
        border: 0;
      }
      th {
        background: #f1f5f9;
        color: #475569;
        font-weight: 600;
      }
      td {
        color: #334155;
      }
      .cell-left {
        text-align: left;
      }
      .cell-center {
        text-align: center;
      }
      .cell-right {
        text-align: right;
      }
      .empty-cell {
        padding: 32px 12px;
        text-align: center;
        color: #94a3b8;
      }
      @media print {
        html,
        body {
          background: #ffffff;
        }
        body {
          padding: 0;
        }
      }
    </style>
  </head>
  <body>
    <div class="report-page">
      <div class="report-title-wrap">
        <div class="report-title ${titleClass}">${escapeHtml(reportTitle)}</div>
      </div>
      <div class="report-meta">${metaHtml}</div>
      <div class="${tableWrapClass}">
        <table>
          <thead>
            <tr>${headerHtml}</tr>
          </thead>
          <tbody>${bodyHtml}</tbody>
        </table>
      </div>
    </div>
    <script>
      window.addEventListener('load', () => {
        window.focus();
        setTimeout(() => {
          window.print();
        }, 50);
      });
      window.addEventListener('afterprint', () => {
        window.close();
      });
    </script>
  </body>
</html>`
}
 
export function printReportDocument(payload = {}) {
  if (typeof window === 'undefined') {
    return
  }
 
  const printWindow = window.open('', '_blank')
  if (!printWindow) {
    return
  }
 
  printWindow.document.open()
  printWindow.document.write(buildPrintDocumentHtml(payload))
  printWindow.document.close()
}