@jcyao/print-sdk 1.1.1 → 1.1.3

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/dist/index.js CHANGED
@@ -211,8 +211,9 @@ function generatePrintPageStyles(config) {
211
211
 
212
212
  @media print {
213
213
  body {
214
- background: white;
215
- padding: 0;
214
+ margin: 0 !important;
215
+ padding: 0 !important;
216
+ background: white !important;
216
217
  }
217
218
 
218
219
  @page {
@@ -221,9 +222,9 @@ function generatePrintPageStyles(config) {
221
222
  }
222
223
 
223
224
  .print-page {
225
+ margin: 0 !important;
226
+ box-shadow: none !important;
224
227
  page-break-after: always;
225
- margin-bottom: 0 !important;
226
- box-shadow: none; /* 移除阴影 */
227
228
  }
228
229
 
229
230
  .print-page:last-child {
@@ -250,7 +251,7 @@ function generatePrintPageStyles(config) {
250
251
  * 用于 PrintSDK 的 printMultiple
251
252
  */
252
253
  function generateBatchPrintStyles(config) {
253
- const { pageWidthMm, pageHeightMm, isContinuous = false, minHeightMm = 100, } = config;
254
+ const { pageWidthMm, pageHeightMm, marginTop = 0, marginRight = 0, marginBottom = 0, marginLeft = 0, isContinuous = false, minHeightMm = 100, } = config;
254
255
  return `
255
256
  * { margin: 0; padding: 0; box-sizing: border-box; }
256
257
  body { margin: 0; padding: 0; background: #f5f5f5; }
@@ -261,8 +262,8 @@ function generateBatchPrintStyles(config) {
261
262
  }
262
263
 
263
264
  @media print {
264
- body { margin: 0; padding: 0; background: white; }
265
- .print-page { margin: 0; page-break-after: always; box-shadow: none !important; }
265
+ body { margin: 0 !important; padding: 0 !important; background: white !important; }
266
+ .print-page { margin: 0 !important; page-break-after: always; box-shadow: none !important; }
266
267
  .print-page:last-child { page-break-after: auto; }
267
268
  }
268
269
 
@@ -273,7 +274,7 @@ function generateBatchPrintStyles(config) {
273
274
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
274
275
  }
275
276
  }
276
-
277
+
277
278
  .print-page {
278
279
  width: ${pageWidthMm}mm;
279
280
  height: ${isContinuous ? 'auto' : pageHeightMm + 'mm'};
@@ -281,6 +282,7 @@ function generateBatchPrintStyles(config) {
281
282
  background: white;
282
283
  position: relative;
283
284
  box-sizing: border-box;
285
+ padding: ${marginTop}mm ${marginRight}mm ${marginBottom}mm ${marginLeft}mm;
284
286
  }
285
287
 
286
288
  ${generateComponentStyles()}
@@ -1993,7 +1995,7 @@ class PrintSDK {
1993
1995
  * @param options 批量打印选项
1994
1996
  */
1995
1997
  async printMultiple(template, dataList, options = {}) {
1996
- var _a, _b;
1998
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
1997
1999
  const { preview = false, onProgress, } = options;
1998
2000
  const { page } = template;
1999
2001
  const progress = {
@@ -2036,6 +2038,10 @@ class PrintSDK {
2036
2038
  const styles = generateBatchPrintStyles({
2037
2039
  pageWidthMm,
2038
2040
  pageHeightMm,
2041
+ marginTop: (_b = (_a = page.marginMm) === null || _a === void 0 ? void 0 : _a.top) !== null && _b !== void 0 ? _b : 0,
2042
+ marginRight: (_d = (_c = page.marginMm) === null || _c === void 0 ? void 0 : _c.right) !== null && _d !== void 0 ? _d : 0,
2043
+ marginBottom: (_f = (_e = page.marginMm) === null || _e === void 0 ? void 0 : _e.bottom) !== null && _f !== void 0 ? _f : 0,
2044
+ marginLeft: (_h = (_g = page.marginMm) === null || _g === void 0 ? void 0 : _g.left) !== null && _h !== void 0 ? _h : 0,
2039
2045
  isContinuous: page.size === 'CONTINUOUS',
2040
2046
  minHeightMm: page.minHeightMm,
2041
2047
  });
@@ -2065,7 +2071,7 @@ class PrintSDK {
2065
2071
  iframe.style.top = '-9999px';
2066
2072
  iframe.style.left = '-9999px';
2067
2073
  document.body.appendChild(iframe);
2068
- const iframeDoc = (_a = iframe.contentWindow) === null || _a === void 0 ? void 0 : _a.document;
2074
+ const iframeDoc = (_j = iframe.contentWindow) === null || _j === void 0 ? void 0 : _j.document;
2069
2075
  if (!iframeDoc) {
2070
2076
  throw new Error('Failed to access iframe document');
2071
2077
  }
@@ -2073,13 +2079,129 @@ class PrintSDK {
2073
2079
  iframeDoc.close();
2074
2080
  // 等待所有图片加载完成后再打印
2075
2081
  await waitForImagesLoaded(iframeDoc);
2076
- (_b = iframe.contentWindow) === null || _b === void 0 ? void 0 : _b.print();
2082
+ (_k = iframe.contentWindow) === null || _k === void 0 ? void 0 : _k.print();
2077
2083
  // 打印完成后移除 iframe
2078
2084
  setTimeout(() => {
2079
2085
  document.body.removeChild(iframe);
2080
2086
  }, 1000);
2081
2087
  }
2082
2088
  }
2089
+ /**
2090
+ * 多模板批量打印
2091
+ * 支持多个模板各自绑定数据列表,一次打印确认
2092
+ *
2093
+ * 注意:所有模板需使用相同纸张尺寸,混合尺寸暂不支持
2094
+ * @param groups 模板+数据组列表
2095
+ * @param options 打印选项
2096
+ */
2097
+ async printMultiTemplate(groups, options = {}) {
2098
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
2099
+ const { preview = false, onProgress } = options;
2100
+ if (!groups || groups.length === 0) {
2101
+ console.warn('[PrintSDK] printMultiTemplate: groups 为空,跳过打印');
2102
+ return;
2103
+ }
2104
+ const totalDataItems = groups.reduce((sum, g) => sum + g.dataList.length, 0);
2105
+ const progress = {
2106
+ totalGroups: groups.length,
2107
+ completedGroups: 0,
2108
+ totalDataItems,
2109
+ completedDataItems: 0,
2110
+ failed: 0,
2111
+ currentGroupIndex: 0,
2112
+ currentDataIndex: -1,
2113
+ };
2114
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress(progress);
2115
+ const allPagesHTML = [];
2116
+ for (let groupIdx = 0; groupIdx < groups.length; groupIdx++) {
2117
+ const group = groups[groupIdx];
2118
+ progress.currentGroupIndex = groupIdx;
2119
+ progress.currentDataIndex = -1;
2120
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress(progress);
2121
+ for (let dataIdx = 0; dataIdx < group.dataList.length; dataIdx++) {
2122
+ const data = group.dataList[dataIdx];
2123
+ progress.currentDataIndex = dataIdx;
2124
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress(progress);
2125
+ try {
2126
+ const engine = createPrintEngine(group.template, data);
2127
+ const html = await engine.generatePrintHTML();
2128
+ const bodyContent = extractBodyContent(html);
2129
+ if (bodyContent) {
2130
+ allPagesHTML.push(bodyContent);
2131
+ }
2132
+ progress.completedDataItems++;
2133
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress(progress);
2134
+ }
2135
+ catch (error) {
2136
+ progress.failed++;
2137
+ progress.completedDataItems++;
2138
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress(progress);
2139
+ console.error(`[PrintSDK] 处理失败: groupIndex=${groupIdx}, dataIndex=${dataIdx}`, error);
2140
+ }
2141
+ }
2142
+ progress.completedGroups++;
2143
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress(progress);
2144
+ }
2145
+ progress.currentGroupIndex = -1;
2146
+ progress.currentDataIndex = -1;
2147
+ onProgress === null || onProgress === void 0 ? void 0 : onProgress(progress);
2148
+ const { page } = groups[0].template;
2149
+ const { widthMm: pageWidthMm, heightMm: pageHeightMm } = getPageSizeFromConfig(page);
2150
+ const styles = generateBatchPrintStyles({
2151
+ pageWidthMm,
2152
+ pageHeightMm,
2153
+ marginTop: (_b = (_a = page.marginMm) === null || _a === void 0 ? void 0 : _a.top) !== null && _b !== void 0 ? _b : 0,
2154
+ marginRight: (_d = (_c = page.marginMm) === null || _c === void 0 ? void 0 : _c.right) !== null && _d !== void 0 ? _d : 0,
2155
+ marginBottom: (_f = (_e = page.marginMm) === null || _e === void 0 ? void 0 : _e.bottom) !== null && _f !== void 0 ? _f : 0,
2156
+ marginLeft: (_h = (_g = page.marginMm) === null || _g === void 0 ? void 0 : _g.left) !== null && _h !== void 0 ? _h : 0,
2157
+ isContinuous: page.size === 'CONTINUOUS',
2158
+ minHeightMm: page.minHeightMm,
2159
+ });
2160
+ const fullHTML = generatePrintHTML({
2161
+ title: '多模板批量打印',
2162
+ styles,
2163
+ bodyContent: allPagesHTML.join('\n'),
2164
+ });
2165
+ if (preview) {
2166
+ const printWindow = window.open('', '_blank');
2167
+ if (!printWindow) {
2168
+ throw new Error('Failed to open print window');
2169
+ }
2170
+ printWindow.document.write(fullHTML);
2171
+ printWindow.document.close();
2172
+ await waitForImagesLoaded(printWindow.document);
2173
+ printWindow.print();
2174
+ }
2175
+ else {
2176
+ const iframe = document.createElement('iframe');
2177
+ iframe.style.position = 'fixed';
2178
+ iframe.style.top = '-9999px';
2179
+ iframe.style.left = '-9999px';
2180
+ document.body.appendChild(iframe);
2181
+ const iframeDoc = (_j = iframe.contentWindow) === null || _j === void 0 ? void 0 : _j.document;
2182
+ if (!iframeDoc) {
2183
+ throw new Error('Failed to access iframe document');
2184
+ }
2185
+ iframeDoc.write(fullHTML);
2186
+ iframeDoc.close();
2187
+ await waitForImagesLoaded(iframeDoc);
2188
+ const cleanup = () => {
2189
+ if (iframe.parentNode) {
2190
+ document.body.removeChild(iframe);
2191
+ }
2192
+ };
2193
+ if (iframe.contentWindow) {
2194
+ iframe.contentWindow.addEventListener('afterprint', cleanup, { once: true });
2195
+ }
2196
+ (_k = iframe.contentWindow) === null || _k === void 0 ? void 0 : _k.print();
2197
+ setTimeout(() => {
2198
+ if (iframe.parentNode) {
2199
+ console.warn('[PrintSDK] afterprint 事件未触发,执行兜底清理');
2200
+ cleanup();
2201
+ }
2202
+ }, 5000);
2203
+ }
2204
+ }
2083
2205
  }
2084
2206
  /**
2085
2207
  * 创建 SDK 实例(无需配置)