新增PrintDot桥接功能,支持本地打印机连接和配置,优化打印模板设计,允许多页表格重复显示,改进打印预览和设计器界面,确保用户体验流畅。

This commit is contained in:
geht
2026-04-17 19:00:30 +08:00
parent 2bd4c5584d
commit efb6a9f838
14 changed files with 1196 additions and 110 deletions

View File

@@ -0,0 +1,38 @@
import type { NativeTemplateSchema } from '../native/core/types';
import { renderNativePrintHtml } from '../native/core/printRenderer';
import { buildPdfBase64FromHtmlFragment, extractBodyInnerHtmlFromFullDocument } from './printHtmlToPdfBase64';
import { fetchPrintDotPrinters, printDotSendPdf, resolvePrintDotPrinterName } from './printDotBridge';
const PRINTER_STORAGE_KEY = 'print_template_selected_printer';
/**
* 原生模板:渲染 HTML → 转 PDF → 经 PrintDot 本地桥接器送打印机
*/
export async function printNativeSchemaViaPrintDot(params: {
schema: NativeTemplateSchema;
data: Record<string, any>;
jobName?: string;
/** 与模板列表一致:可为具体打印机名或 __system_default__缺省读 localStorage */
printerSelection?: string;
}): Promise<void> {
const fullHtml = await renderNativePrintHtml(params.schema, params.data);
const inner = extractBodyInnerHtmlFromFullDocument(fullHtml);
const pdfBase64 = await buildPdfBase64FromHtmlFragment(inner, params.schema.page.width, params.schema.page.height, {
paginate: true,
});
const printers = await fetchPrintDotPrinters();
const fromStore = params.printerSelection ?? localStorage.getItem(PRINTER_STORAGE_KEY) ?? '__system_default__';
const resolved = resolvePrintDotPrinterName(fromStore, printers);
if (!resolved) {
throw new Error('未解析到可用打印机:请在模板列表选择打印机,或启动 PrintDot 后刷新打印机列表');
}
const result = await printDotSendPdf({
printer: resolved,
pdfBase64,
jobName: params.jobName,
timeoutMs: 180000,
});
if (!result.ok) {
throw new Error(result.message || 'PrintDot 打印失败');
}
}