@jcyao/print-sdk 1.1.2 → 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/CHANGELOG.md +4 -0
- package/README.md +44 -0
- package/dist/PrintSDK.d.ts +35 -0
- package/dist/index.esm.js +123 -3
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +123 -3
- package/dist/index.js.map +1 -1
- package/dist/sdk.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1995,7 +1995,7 @@ class PrintSDK {
|
|
|
1995
1995
|
* @param options 批量打印选项
|
|
1996
1996
|
*/
|
|
1997
1997
|
async printMultiple(template, dataList, options = {}) {
|
|
1998
|
-
var _a, _b;
|
|
1998
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
1999
1999
|
const { preview = false, onProgress, } = options;
|
|
2000
2000
|
const { page } = template;
|
|
2001
2001
|
const progress = {
|
|
@@ -2038,6 +2038,10 @@ class PrintSDK {
|
|
|
2038
2038
|
const styles = generateBatchPrintStyles({
|
|
2039
2039
|
pageWidthMm,
|
|
2040
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,
|
|
2041
2045
|
isContinuous: page.size === 'CONTINUOUS',
|
|
2042
2046
|
minHeightMm: page.minHeightMm,
|
|
2043
2047
|
});
|
|
@@ -2067,7 +2071,7 @@ class PrintSDK {
|
|
|
2067
2071
|
iframe.style.top = '-9999px';
|
|
2068
2072
|
iframe.style.left = '-9999px';
|
|
2069
2073
|
document.body.appendChild(iframe);
|
|
2070
|
-
const iframeDoc = (
|
|
2074
|
+
const iframeDoc = (_j = iframe.contentWindow) === null || _j === void 0 ? void 0 : _j.document;
|
|
2071
2075
|
if (!iframeDoc) {
|
|
2072
2076
|
throw new Error('Failed to access iframe document');
|
|
2073
2077
|
}
|
|
@@ -2075,13 +2079,129 @@ class PrintSDK {
|
|
|
2075
2079
|
iframeDoc.close();
|
|
2076
2080
|
// 等待所有图片加载完成后再打印
|
|
2077
2081
|
await waitForImagesLoaded(iframeDoc);
|
|
2078
|
-
(
|
|
2082
|
+
(_k = iframe.contentWindow) === null || _k === void 0 ? void 0 : _k.print();
|
|
2079
2083
|
// 打印完成后移除 iframe
|
|
2080
2084
|
setTimeout(() => {
|
|
2081
2085
|
document.body.removeChild(iframe);
|
|
2082
2086
|
}, 1000);
|
|
2083
2087
|
}
|
|
2084
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
|
+
}
|
|
2085
2205
|
}
|
|
2086
2206
|
/**
|
|
2087
2207
|
* 创建 SDK 实例(无需配置)
|