PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
事件分为两类:Core 事件,由 documentViewer、annotationManager 等 Core 对象触发;UI 事件,由 instance.UI 触发。
Core 对象提供 addEventListener(name, handler, options?) 和 removeEventListener(name, handler):
const { documentViewer, annotationManager } = instance.Core;
documentViewer.addEventListener('pageNumberUpdated', (pageNumber) => console.log(pageNumber));
annotationManager.addEventListener('annotationChanged', (annotations, action) => console.log(action));documentViewer 事件 | 事件 | 参数 | 触发时机 |
|---|---|---|
documentLoaded | — | 文档加载完成。 |
documentUnloaded | — | 文档关闭。 |
annotationsLoaded | — | 文档的注释加载完成。 |
pageNumberUpdated | pageNumber | 当前页变化。 |
pagesUpdated | changes | 页面被添加、删除、移动或旋转。 |
zoomUpdated | zoom | 缩放比例变化。 |
rotationUpdated | rotation | 旋转角度变化。 |
fitModeUpdated | fitMode | 适应模式变化。 |
toolModeUpdated | newTool, oldTool | 当前工具变化。 |
textSelected | quads, text, pageNumber | 选中文本。 |
searchResultsChanged | results | 搜索结果变化。 |
finishedRendering | — | 可见页面渲染完成。 |
click | event | 点击文档区域。 |
annotationManager 事件 | 事件 | 参数 | 触发时机 |
|---|---|---|
annotationChanged | annotations, action, info | 注释被添加 / 修改 / 删除(action 为 'add'、'modify' 或 'delete')。 |
annotationSelected | annotations, action | 注释被选中('selected')或取消选中('deselected')。 |
annotationDeselected | annotations | 注释被取消选中。 |
历史管理器触发 historyChanged(documentViewer.getAnnotationHistoryManager())和 undoRedoStatusChanged(documentViewer.getContentEditHistoryManager())。
instance.UI.addEventListener(name, handler, options?) 和 removeEventListener(name, handler) 使用 instance.UI.Events 中的事件名:
| 事件 | 参数 | 触发时机 |
|---|---|---|
VIEWER_LOADED | — | UI 挂载完成。之后添加的监听器仍会被调用一次。 |
LOAD_ERROR | error | UI 或文档加载失败。 |
VISIBILITY_CHANGED | dataElement, isVisible | 元素打开或关闭。 |
TOOLBAR_GROUP_CHANGED | toolbarGroup | 当前功能区变化。 |
THEME_CHANGED | theme | 主题变化。 |
LANGUAGE_CHANGED | previousLanguage, newLanguage | 语言变化。 |
PANEL_RESIZED | dataElement, width | 面板宽度变化。 |
FULLSCREEN_MODE_TOGGLED | isFullscreen | 全屏模式变化。 |
FILE_DOWNLOADED | — | 文档被下载。 |
MODULAR_UI_IMPORTED | — | importModularComponents 完成。 |
ACTIVE_DOCUMENT_VIEWER_CHANGED | newKey, oldKey | 当前文档查看器变化。 |
PERMISSION_DENIED | { permission, action, documentViewerKey } | 操作不被文档权限允许。 |
PERMISSION_VERIFIED | { documentViewerKey } | 权限密码验证通过。 |
TRUSTED_CERTIFICATE_ADDED | { fieldName, certificate } | 用户在证书查看器中信任了签名者证书。certificate 为 DER 字节的副本。调用 UI.VerificationOptions.addTrustedCertificates 不会触发。信任列表只保存在内存中:可自行保存证书,下次加载后再传给 addTrustedCertificates 以恢复信任。 |
创建实例时,UI 把处理函数绑定到每个文档查看器的 Core 事件上(event-listeners/)。这些处理函数更新 store——例如 documentLoaded 更新页数和大纲,annotationSelected 更新选择,toolModeUpdated 更新当前工具按钮。实例销毁时所有绑定都会被移除。
生命周期提示: 不再需要时,请用
removeEventListener移除您添加的监听器;只需执行一次的监听器可以传入{ once: true }。
instance.Core.documentViewer.addEventListener('documentLoaded', () => {
console.log('文档已加载');
});const handler = (annotations, action) => {
console.log('annotations', action, annotations);
};
instance.Core.annotationManager.addEventListener('annotationChanged', handler);
// 稍后
instance.Core.annotationManager.removeEventListener('annotationChanged', handler);instance.Core.documentViewer.addEventListener('toolModeUpdated', (newTool) => {
console.log('当前工具:', newTool.name);
});const { UI } = instance;
UI.addEventListener(UI.Events.THEME_CHANGED, (theme) => console.log('theme', theme));
UI.addEventListener(UI.Events.TOOLBAR_GROUP_CHANGED, (group) => console.log('group', group));ComPDFKitViewer() 在 UI 挂载后以 instance resolve,因此 .then() 中的代码可以直接使用 instance。在配置脚本(config 选项)中,请等待 UI.Events.VIEWER_LOADED:
instance.UI.addEventListener(instance.UI.Events.VIEWER_LOADED, () => {
console.log('查看器已加载');
});