Skip to content

PDF 生成模板编辑器

开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。

查看 GitHub

事件 ​

事件分为两类:Core 事件,由 documentViewer、annotationManager 等 Core 对象触发;UI 事件,由 instance.UI 触发。

Core 事件 ​

Core 对象提供 addEventListener(name, handler, options?) 和 removeEventListener(name, handler):

javascript
const { documentViewer, annotationManager } = instance.Core;
documentViewer.addEventListener('pageNumberUpdated', (pageNumber) => console.log(pageNumber));
annotationManager.addEventListener('annotationChanged', (annotations, action) => console.log(action));

常用 documentViewer 事件 ​

事件参数触发时机
documentLoaded—文档加载完成。
documentUnloaded—文档关闭。
annotationsLoaded—文档的注释加载完成。
pageNumberUpdatedpageNumber当前页变化。
pagesUpdatedchanges页面被添加、删除、移动或旋转。
zoomUpdatedzoom缩放比例变化。
rotationUpdatedrotation旋转角度变化。
fitModeUpdatedfitMode适应模式变化。
toolModeUpdatednewTool, oldTool当前工具变化。
textSelectedquads, text, pageNumber选中文本。
searchResultsChangedresults搜索结果变化。
finishedRendering—可见页面渲染完成。
clickevent点击文档区域。

常用 annotationManager 事件 ​

事件参数触发时机
annotationChangedannotations, action, info注释被添加 / 修改 / 删除(action 为 'add'、'modify' 或 'delete')。
annotationSelectedannotations, action注释被选中('selected')或取消选中('deselected')。
annotationDeselectedannotations注释被取消选中。

历史管理器触发 historyChanged(documentViewer.getAnnotationHistoryManager())和 undoRedoStatusChanged(documentViewer.getContentEditHistoryManager())。

UI 事件 ​

instance.UI.addEventListener(name, handler, options?) 和 removeEventListener(name, handler) 使用 instance.UI.Events 中的事件名:

事件参数触发时机
VIEWER_LOADED—UI 挂载完成。之后添加的监听器仍会被调用一次。
LOAD_ERRORerrorUI 或文档加载失败。
VISIBILITY_CHANGEDdataElement, isVisible元素打开或关闭。
TOOLBAR_GROUP_CHANGEDtoolbarGroup当前功能区变化。
THEME_CHANGEDtheme主题变化。
LANGUAGE_CHANGEDpreviousLanguage, newLanguage语言变化。
PANEL_RESIZEDdataElement, width面板宽度变化。
FULLSCREEN_MODE_TOGGLEDisFullscreen全屏模式变化。
FILE_DOWNLOADED—文档被下载。
MODULAR_UI_IMPORTED—importModularComponents 完成。
ACTIVE_DOCUMENT_VIEWER_CHANGEDnewKey, oldKey当前文档查看器变化。
PERMISSION_DENIED{ permission, action, documentViewerKey }操作不被文档权限允许。
PERMISSION_VERIFIED{ documentViewerKey }权限密码验证通过。
TRUSTED_CERTIFICATE_ADDED{ fieldName, certificate }用户在证书查看器中信任了签名者证书。certificate 为 DER 字节的副本。调用 UI.VerificationOptions.addTrustedCertificates 不会触发。信任列表只保存在内存中:可自行保存证书,下次加载后再传给 addTrustedCertificates 以恢复信任。

UI 的监听方式 ​

创建实例时,UI 把处理函数绑定到每个文档查看器的 Core 事件上(event-listeners/)。这些处理函数更新 store——例如 documentLoaded 更新页数和大纲,annotationSelected 更新选择,toolModeUpdated 更新当前工具按钮。实例销毁时所有绑定都会被移除。

生命周期提示: 不再需要时,请用 removeEventListener 移除您添加的监听器;只需执行一次的监听器可以传入 { once: true }。

示例 ​

监听文档加载 ​

javascript
instance.Core.documentViewer.addEventListener('documentLoaded', () => {
  console.log('文档已加载');
});

监听注释变化 ​

javascript
const handler = (annotations, action) => {
  console.log('annotations', action, annotations);
};
instance.Core.annotationManager.addEventListener('annotationChanged', handler);
// 稍后
instance.Core.annotationManager.removeEventListener('annotationChanged', handler);

监听工具变化 ​

javascript
instance.Core.documentViewer.addEventListener('toolModeUpdated', (newTool) => {
  console.log('当前工具:', newTool.name);
});

监听 UI 事件 ​

javascript
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:

javascript
instance.UI.addEventListener(instance.UI.Events.VIEWER_LOADED, () => {
  console.log('查看器已加载');
});

相关 ​