在Vue应用中集成PDF创建、查看和标注功能

Dynamsoft Document Viewer是为文档图像提供查看、管理功能的SDK。我们可以使用它集成PDF创建、查看和标注功能到我们的Web应用中。在本文中,我们将使用Vue编写一个demo。

新建项目

使用Vite创建一个新的Vue + TypeScript项目:

bash 复制代码
npm create vite@latest pdf-app -- --template vue-ts

安装Dynamsoft Document Viewer

首先,通过npm安装Dynamsoft Document Viewer。

javascript 复制代码
npm install dynamsoft-document-viewer

然后,我们需要将Dynamsoft Document Viewer的资源复制到public文件夹。

  1. 在以下路径创建一个文件夹:public/assets/ddv-resources

  2. 安装ncpnpm install ncp --save-dev

  3. 修改package.json ,将资源从node_modules复制到public文件夹。

    diff 复制代码
    - "dev": "vite",
    - "build": "tsc -b && vite build",
    + "dev": "ncp node_modules/dynamsoft-document-viewer/dist public/assets/ddv-resources && vite",
    + "build": "ncp node_modules/dynamsoft-document-viewer/dist public/assets/ddv-resources && tsc -b && vite build",

初始化Dynamsoft Document Viewer

重写App.vue。挂载后,使用许可证初始化Dynamsoft Document Viewer。可以在此处申请许可证

html 复制代码
<script setup lang="ts">
import { DDV, UiConfig } from 'dynamsoft-document-viewer';
import { onMounted, ref } from 'vue';

const initialized = ref(false);
onMounted(()=>{
  if (initialized.value === false) {
    initDDV();
  }
})

const initDDV = async () => {
  DDV.Core.license = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ=="; // Public trial license which is valid for 24 hours
  DDV.Core.engineResourcePath = "assets/ddv-resources/engine";// Lead to a folder containing the distributed WASM files
  await DDV.Core.loadWasm();
  await DDV.Core.init();
  // Configure image filter feature which is in edit viewer
  DDV.setProcessingHandler("imageFilter", new DDV.ImageFilter());
  initialized.value = true;
}
</script>

创建Edit Viewer

  1. 在template中使用以下内容:

    html 复制代码
    <template>
      <div id="app">
        <h2>Document Viewer Demo</h2>
        <div v-if="!initialized">Initializing...</div>
        <div id="container"></div>
      </div>
    </template>

    CSS:

    css 复制代码
    #app {
      display: flex;
      align-items: center;
      flex-direction: column;
      width: 100%;
    }
    
    #container {
      max-width: 80%;
      width: 1280px;
      height: 480px;
    }
  2. 初始化Edit Viewer的实例,并通过ID将其绑定到一个容器。

    tsx 复制代码
    const config = DDV.getDefaultUiConfig("editViewer", {includeAnnotationSet: true}) as UiConfig;
    // Create an edit viewer
    editViewer.current = new DDV.EditViewer({
      container: "container",
      uiConfig: config,
    });
  3. 导入Dynamsoft Document Viewer的CSS。

    tsx 复制代码
    import "dynamsoft-document-viewer/dist/ddv.css";

好的,我们已将Dynamsoft Document Viewer集成到了我们的Vue应用中。

UI包含工具栏、缩略图查看器和一个主的页面内容查看器。我们可以点击工具栏上的按钮来加载新图像、编辑图像、添加PDF标注并将文档保存为PDF文件。

除了UI,我们还可以通过代码来操作文档。阅读文档了解更多信息。

源代码

查看demo的源代码并尝试使用:

github.com/tony-xlh/do...

相关推荐
Z兽兽5 小时前
React@18+Vite项目配置env文件
前端·react.js·前端框架
SuniaWang5 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
A_nanda6 小时前
根据AI提示排查vue前端项目
前端·javascript·vue.js
happymaker06266 小时前
web前端学习日记——DAY05(定位、浮动、视频音频播放)
前端·学习·音视频
~无忧花开~6 小时前
React状态管理完全指南
开发语言·前端·javascript·react.js·前端框架
LegendNoTitle7 小时前
计算机三级等级考试 网络技术 选择题考点详细梳理
服务器·前端·经验分享·笔记·php
@大迁世界7 小时前
1.什么是 ReactJS?
前端·javascript·react.js·前端框架·ecmascript
BJ-Giser8 小时前
Cesium 基于EZ-Tree的植被效果
前端·可视化·cesium
王码码20359 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
发现一只大呆瓜9 小时前
深入浅出 AST:解密 Vite、Babel编译的底层“黑盒”
前端·面试·vite