api 插件
约 466 字大约 2 分钟
...
2026-09-28
分类介绍
api 插件(plugin-api-*)用于扩展模板实例的 API,注册后即可直接调用 toPdf / toImage / toExcel 等导出方法。
主要使用 newPrintTemplate hook:注册后把导出方法挂到每个模板实例上。
| 插件 | 挂载方法 | 功能 |
|---|---|---|
| plugin-api-pdf | template.toPdf | 导出 PDF |
| plugin-api-pdf3 | template.toPdf | 导出 PDF(jspdf 3.x) |
| plugin-api-pdf-vector | template.toVectorPdf | 导出矢量 PDF:文字可选中 |
| plugin-api-image | template.toImage | 导出长图 |
| plugin-api-excel | template.toExcel | 导出 Excel |
| plugin-api-word | template.toWord | 导出 Word(docx) |
注意
pdf 与 pdf3 二选一,不可同时注册(都挂载 toPdf)。
示例代码
统一注册(唯一入口 hiprint.register,异步,须在创建模板之前):
import pluginApiPdf from '@sv-print/plugin-api-pdf';
import pluginApiImage from '@sv-print/plugin-api-image';
import pluginApiExcel from '@sv-print/plugin-api-excel';
// 须在创建模板之前注册
await hiprint.register({
plugins: [pluginApiPdf(), pluginApiImage(), pluginApiExcel()], // 工厂函数,可传 config
});注册后模板实例即拥有导出方法:
const template = new hiprint.PrintTemplate(options);
// 导出 PDF
await template.toPdf(printData, '出库单', {
isDownload: false,
type: 'blob',
onProgress: (cur, total) => {
console.log('toPdf 进度', Math.floor((cur / total) * 100));
},
});
// 导出长图 / Excel
await template.toImage(printData, { limit: 10 });
await template.toExcel(printData, { name: '导出.xlsx' });各插件文档
版权所有
版权归属:sv-print
