Vue PDF Annotation plugin library online API examples

news2025/4/17 5:43:49

This article introduces the online version of the ElasticPDF API tutorial for the PDF annotation plug-in library in Vue projects. The API includes ① Export edited PDF data; ② Export annotations json data; ③ Reload old annotations; ④ Change files; ⑤ Set member info; ⑥ Clear annotations and other data processing functions to meet common business needs. This tutorial can be used for monthly licenses and online trial versions. Welcome Contact us for consultation and obtain trial keys.

在这里插入图片描述

0 ElasticPDF Introduction

ElasticPDF is based on the open source pdf.js (Demo:https://mozilla.github.io/pdf.js/web/viewer.html) and adds a variety of out-of-the-box PDF annotation features. The code package continues the independent and completely offline structure style of pdf.js-dist, and only adds offline Javascript code to support annotations. It can be quickly and perfectly integrated into any project environment that can run Javascript, HTML, and CSS, and run perfectly in both public and intranet environments.

在这里插入图片描述

For the different features and budget requirements, there are two versions of the products, they are only differ in the final annotation saving stage, demos are as follows:

① Annotation synthesis version: https://demos.libertynlp.com/#/pdfjs-annotation
② Professional annotation version: https://www.elasticpdf.com/demo

1 Import Viewer HTML and Initial

First, import the following HTML code into the target page, which includes the initialization code initialPDFEditor() and the function listenPDFEditorMessage() that receives all feedback information. All exported PDF data and annotation data are under the function listenPDFEditorMessage() and can be integrated with subsequent functions.

<template>
	<div id='english_content' style="display: none;" class='project-title'>
		<img src="https://elasticpdf.com/elasticpdf-image/logo-no-back.png" alt="" />
		<h2>Vue Web PDF editor library Online API examples</h2>
		<a style="cursor: pointer;text-decoration: none;" href='https://www.elasticpdf.com/contact-us.html'
			target="_blank">
			<div title='contact us for trial key' class='message-div info-message'>
				<i class="fa fa-info-circle" aria-hidden="true"></i>
				<span>Get trial key</span>
			</div>
		</a>
		<button style="margin-left: 20px;" class='theme-btn btn-outline-warning'
			onclick="getPDFData()">Export Edited PDF</button>
		<button class='theme-btn btn-outline-help' onclick="outputAnnotation()">Export Annotations</button>
		<button class='theme-btn btn-outline-success' onclick="changeFile()">Change File</button>
		<button class='theme-btn btn-outline-warning' onclick="setMember()">Change User</button>
		<button class='theme-btn btn-outline-danger' onclick="clearAnnotation()">Clear</button>
		<button class='theme-btn btn-outline-info' onclick="reloadOldAnnotationData()">Reload Annotations</button>
	</div>
	<iframe @load='initialPDFEditor' id='elasticpdf-iframe'
		src="https://pdfmaster.libertynlp.com/web/viewer.html?file=tutorial.pdf"
		style="position: relative; width: 100%; height: 660px;border: none;"></iframe>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'

	onMounted(() => {
		
	})
	
	var elasticpdf_viewer = null;
	var default_lang='en';
	function initialPDFEditor() {
		showContentByLanguage();
		// Listen for callbacks of various information about PDF editing
		// 监听 pdf 编辑各种信息的回调
		listenPDFEditorMessage();
		elasticpdf_viewer = document.getElementById('elasticpdf-iframe').contentWindow;
		// The online version only supports opening online documents
		// 在线版只支持打开在线文档
		// var pdf_url = 'https://pdfmaster.libertynlp.com/web/tutorial.pdf';
		var pdf_url = 'https://app.elasticpdf.com/web/tutorial.pdf';
		elasticpdf_viewer.postMessage({
			"source": "test-elasticpdf",
			"function_name": "initialApp",
			"content": {
				'language': default_lang, // GUI language 交互语言
				'pdf_url': pdf_url,
				'member_info': { //Member Info  用户信息
					'id': 'elasticpdf_id',
					'name': 'elasticpdf',
				},
			}
		}, '*');
	}
	
	// For security reasons, console output has been disabled by the test website and cannot use console.log to print out content
	// Please use alert to print content
	// 出于安全考虑, 控制台输出已被测试网站禁用无法使用 console.log 打印出内容
	// 请用 alert 打印内容
	function listenPDFEditorMessage() {
		window.addEventListener('message', (e) => {
			if (e.data.source != 'elasticpdf') {
				return;
			}
			
			// PDF loading completed callback, you can import the annotation file stored on the server here
			// pdf 加载结束的回调,可以在此处导入服务器上储存的批注文件
			if (e.data.function_name == 'pdfLoaded') {
				// console.log is invalid, please use alert to print content
				// console.log 无效,请使用 alert 打印内容
				// alert('PDF loaded successfully PDF加载成功');
				reloadData();
			}
	
			// PDF annotation editing callback, where annotations can be exported and transferred to the server
			// pdf 批注编辑回调,可以在此处导出批注并传输到服务器
			if (e.data.function_name == 'annotationsModified') {
				// Only get the PDF annotation data, do not write it into the PDF
				// 仅获取 pdf 批注文件,不写入到 pdf 中
				let this_data = e.data.content;
				let annotation_content = JSON.stringify(this_data['file_annotation']);
				let file_name = this_data['file_name'];
				
				// console.log is invalid, please use alert to print content
				// alert('annotation modified 批注被修改');
				postService('upload-annotation-data', {
					'file_name': file_name,
					'file_id': '123ddasfsdffads',
					'file_annotation': annotation_content,
				});
			}
			
			// PDF annotation export callback, where annotations can be exported and transferred to the server
			// pdf 批注导出回调,可以在此处导出批注并传输到服务器
			if (e.data.function_name == 'outputAnnotation') {
				// Only get the PDF annotation data, do not write it into the PDF
				// 仅获取 pdf 批注文件,不写入到 pdf 中
				let this_data = e.data.content;
				let annotation_content = JSON.stringify(this_data['file_annotation']);
				let file_name = this_data['file_name'];
				// console.log is invalid, please use alert to print content
				// console.log 无效,请使用 alert 打印内容
				// alert('Annotation data 批注数据\n'+annotation_content);
			}
	
			// Receive the edited PDF data, and the annotations are written into the PDF
			// 接收编辑后的pdf数据,批注被写入 PDF 中
			if (e.data.function_name == 'downloadPDF') {
				let file_name = e.data.content['file_name'];
				let pdf_blob = e.data.content['pdf_blob'];
				let pdf_base64 = e.data.content['pdf_base64'];
				// If the document has not been edited, pdf_base64 is still the file name or file url
				// Receive pdf data, where pdf_base64 can be quickly uploaded to the server
				// 如果文档没有被编辑过,则 pdf_base64 仍然是文件名或文件链接
				// 接收到 pdf 数据,其中 pdf_base64 可以快捷上传到服务器
				postService('upload-pdf-data', {
					'file_name': file_name,
					'file_id': '123ddasfsdffads',
					'file_data': pdf_base64,
				});
				// alert('Get the pdf base64 data. Please go to postService function to add the subsequent function.\n\n获取到 pdf base64 数据,如有需要请到postService中增加业务函数');
			}
		});
	}
</script>

2 Call APIs

① Export annotation data

The json data of the exported PDF annotations can be used for subsequent business processes such as filtering, merging, storage and saving. It is very suitable for online annotation application, because you only need to save an original PDF document, and then reload only the annotations from the database, which can save a lot of server, internet and bandwidth costs.

// export annotations data 导出可保存的批注对象
function outputAnnotation() {
        elasticpdf_viewer.postMessage({
                "source": "test-elasticpdf",
                "function_name": "outputAnnotation",
                "content": ""
        }, '*');
}

② Import old annotations

Load the annotation data exported in ① from the server according to the file ID or PDF link and reshow it on the document, supporting continuely editing, so as to achieve cloud synchronization of annotation data.

// reload old annotation data
// 加载旧批注
function reloadOldAnnotationData() {
        var old_annotation = getOldAnnotation();
        elasticpdf_viewer.postMessage({
                "source": "test-elasticpdf",
                "function_name": "setFileAnnotation",
                "content": old_annotation
        }, '*');
}

// Generate simulated old annotation data
// 生成模拟旧批注数据
function getOldAnnotation() {
        var old_annotation = {
                "annos-for-page-1": {
                        "page_id": "annos-for-page-1",
                        "page_canvas_container": {},
                        "page_annotations": [],
                        "page_canvas": {
                                "fabric_canvas": {
                                        "version": "5.2.0",
                                        "objects": [{
                                                "type": "rect",
                                                "version": "5.2.0",
                                                "left": 64.38,
                                                "top": 159.99,
                                                "width": 608.27,
                                                "height": 290.3,
                                                "fill": "rgba(255,237,0,0.3)",
                                                "stroke": "rgba(17,153,158,1)",
                                                "erasable": true
                                        }],
                                        "background": "rgba(255, 255, 255, 0)"
                                },
                                "width": 994,
                                "height": 1407,
                                "fabric_canvas_json": {
                                        "version": "5.2.0",
                                        "objects": [{
                                                "type": "rect",
                                                "version": "5.2.0",
                                                "left": 64.38,
                                                "top": 159.99,
                                                "width": 608.27,
                                                "height": 290.3,
                                                "fill": "rgba(255,237,0,0.3)",
                                                "stroke": "rgba(17,153,158,1)",
                                                "erasable": true,
                                                "id": "1742436474916_1",
                                                "hasControls": true,
                                                "hasBorders": true,
                                                "selectable": true,
                                                "lockMovementX": false,
                                                "lockMovementY": false,
                                                "member_id": "elasticpdf_id",
                                                "member_name": "elasticpdf",
                                                "my_type": "rectangle",
                                                "comment": "添加批注",
                                                "backup_opacity": 1,
                                                "lockRotation": false
                                        }],
                                        "background": "rgba(255, 255, 255, 0)"
                                }
                        }
                }
        }
        return JSON.stringify(old_annotation);
}

③ Export Edited PDF file

Export edited PDF file after merging the annotations into it, the base64 PDF data can be directly saved in the database.

// export edited pdf data
// 导出批注编辑后pdf数据
function getPDFData() {
        elasticpdf_viewer.postMessage({
                "source": "test-elasticpdf",
                "function_name": "getPDFData",
                "content": ""
        }, '*');
}

④ Change and open files

Open online documents, where the file server or file site needs to allow CORS, otherwise the document loading will fail.

// You can change test_pdf with any online pdf url
// The file server needs to be configured to allow cross-domain
// 切换打开的文档,可以把 test_pdf 换成任意在线pdf链接
// 文件服务器需要配置允许跨域
function changeFile() {
        var test_pdf = 'https://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf';
        elasticpdf_viewer.postMessage({
                "source": "test-elasticpdf",
                "function_name": "openFile",
                "content": test_pdf
        }, '*');
}

⑤ Set member information

Set userID and userName of current user in elasticpdf. This information will be recorded in each annotation and can be used to set permissions, such as whether the current user is allowed to edit other people’s annotations.

// set member info including id and name
// 设置用户的 id 和 name
function setMember(id) {
        var this_member = {
                'id': 'test-id',
                'name': 'test-name',
        };
        elasticpdf_viewer.postMessage({
                "source": "test-elasticpdf",
                "function_name": "setMember",
                "content": this_member
        }, '*');
}

⑥ Clear annotation data

Clear the annotations of current document.

// clear all annotations
// 清空批注
function clearAnnotation() {
        elasticpdf_viewer.postMessage({
                "source": "test-elasticpdf",
                "function_name": "clearFileAnnotation",
                "content": ""
        }, '*');
}

Summary

So far, the code for integrating the elasticpdf online trial version into Vue projects and calling the data business API has been introduced. The test code file has been uploaded to Github (URL: https://github.com/ElasticPDF/Vue-use-pdf.js-elasticpdf). You are welcome to contact us for consultation and to obtain the key.

Tips: This article was first published on https://www.elasticpdf.com ,Please indicate the source when republishing: https://www.elasticpdf.com/blog/vue-pdf-annotation-plugin-library-online-api-examples.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2330509.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C语言传参寄存器压栈流程总结

相关 《Linux函数调用栈的实现原理&#xff08;X86&#xff09;》 总结 rsp向低地址生长&#xff08;栈顶&#xff09;&#xff0c;rbp记录旧值&#xff08;栈底&#xff09;。 intel x86测试&#xff0c;六个和六个以内的参数用寄存器传递。8个参数场景&#xff0c;6个用寄存…

C盘清理——快速处理

C盘清理 | 快速处理 软件&#xff1a;小番茄C盘清理 https://ccleancdn.xkbrowser.com/cleanmaster/FanQieClean_13054_st.exe 前言&#xff1a;为什么需要专业的C盘清理工具&#xff1f; 作为一位长期与Windows系统打交道的技术博主&#xff0c;我深知C盘空间不足带来的痛苦…

前端服务配置详解:从入门到实战

前端服务配置详解&#xff1a;从入门到实战 一、环境配置文件&#xff08;.env&#xff09; 1.1 基础结构 在项目根目录创建 .env 文件&#xff1a; # 开发环境 VUE_APP_API_BASE_URL http://localhost:3000/api VUE_APP_VERSION 1.0.0# 生产环境&#xff08;.env.produc…

历年跨链合约恶意交易详解(四)——Chainswap20210711

漏洞合约函数 function receive(uint256 fromChainId, address to, uint256 nonce, uint256 volume, Signature[] memory signatures) virtual external payable {_chargeFee();require(received[fromChainId][to][nonce] 0, withdrawn already);uint N signatures.length;r…

Python基于OpenCV和SVM实现中文车牌识别系统GUI界面

说明&#xff1a;这是一个系统实战项目&#xff0c;如需项目代码可以直接到文章最后关注获取。 项目背景 随着智能交通系统和智慧城市的发展&#xff0c;车牌识别技术在车辆管理、交通监控、停车场收费等领域发挥着重要作用。传统的车牌识别系统主要针对英文和数字的识别&…

有瓶颈设备的多级生产计划问题:基于Matlab的深度解析与实践

内容摘要 本文围绕有瓶颈设备的多级生产计划问题展开&#xff0c;通过实例详细阐述问题背景、建立数学模型&#xff0c;并用Matlab代码进行求解。旨在帮助读者理解该问题的本质&#xff0c;掌握利用Matlab解决此类生产计划优化问题的方法&#xff0c;为企业在实际生产中合理规…

网络性能优化参数关系解读 | TCP Nagle / TCP_NODELAY / TCP_QUICKACK / TCP_CORK

注&#xff1a;本文为 “网路性能优化” 相关文章合辑。 未整理去重。 如有内容异常&#xff0c;请看原文。 TCP_NODELAY 详解 lenky0401 发表于 2012-08-25 16:40 在网络拥塞控制领域&#xff0c;Nagle 算法&#xff08;Nagle algorithm&#xff09;是一个非常著名的算法&…

如何将内网的IP地址映射到外网?详细方法与步骤解析

01 为什么需要将内网IP映射到外网 在当今数字化时代&#xff0c;远程访问内网资源已成为许多企业和个人的刚需。将内网IP地址映射到外网的主要目的是允许外部网络访问内网中的特定服务&#xff0c;比如Web服务器、远程桌面、文件共享等应用场景。无论是企业需要远程办公访问内…

HTTP 响应头 Strict-Transport-Security 缺失漏洞

HTTP 响应头 Strict-Transport-Security 缺失漏洞 这个漏洞就是说明网站的HTTP响应头中没有设置Strict-Transport-Security&#xff0c;没有设置则可以通过将https自己手动改成htttp的方式进行访问。不安全 解决方法 1.nginx配置 nginx中增加如下配置&#xff1a; location / …

【SPSS/EXCEl】主成分分析构建__综合评价指数

学习过程中实验操作的记录 1.数据准备和标准化&#xff1a; (1)区分正负相关性:判断每个因子是正向指标还是负向指标,计算每个的最大值和最小值 (2) 标准化: Min-Max标准化 Min-Max标准化&#xff08;最大最小值法&#xff09;&#xff1a; 将数据映射到指定的区间&#xff…

电池分选机:新能源时代的品质守护者|深圳比斯特自动化

在这个新能源蓬勃发展的时代&#xff0c;电池作为能量的存储与释放单元&#xff0c;其性能与质量直接关系到整个系统的稳定运行与效率提升。而电池分选机&#xff0c;作为电池生产流程中的关键一环&#xff0c;正扮演着品质守护者的角色&#xff0c;为新能源产业的高质量发展保…

STM32江科大----IIC

声明&#xff1a;本人跟随b站江科大学习&#xff0c;本文章是观看完视频后的一些个人总结和经验分享&#xff0c;也同时为了方便日后的复习&#xff0c;如果有错误请各位大佬指出&#xff0c;如果对你有帮助可以点个赞小小鼓励一下&#xff0c;本文章建议配合原视频使用❤️ 如…

顺序表——C语言实现

目录 一、线性表 二、顺序表 1.实现动态顺序表 SeqList.h SeqList.c Test.c 问题 经验&#xff1a;free 出问题&#xff0c;2种可能性 解决问题 &#xff08;2&#xff09;尾删 &#xff08;3&#xff09;头插&#xff0c;头删 &#xff08;4&#xff09;在 pos 位…

LTSPICE仿真电路:(二十六)跨阻放大器简单仿真

1.前言 由于有个机会刚好了解了下跨阻&#xff0c;简单做个这个仿真&#xff0c;实际上跨阻放大器应该要复杂的多&#xff0c;由于跨阻放大器实际上是将电流转换为电压&#xff0c;最需要注意的参数肯定是运放的偏置电流 2.跨阻放大器仿真 这篇是纯记录 这是一个将0-50uA电流…

特辣的海藻!15

题 1.迷宫 - 蓝桥云课 2.外卖店优先级 - 蓝桥云课 3.后缀表达式 - 蓝桥云课 题 1.迷宫 - 蓝桥云课 import java.util.*;public class Main {static class Node {int x;int y;String str;public Node(int x, int y, String str) {this.x x;this.y y;this.str str;} …

算法-- js排序

汇总 注&#xff1a;以下log n 是 O(log2n) 注&#xff1a;快速排序实际应用中通常最优&#xff0c;但需避免最坏情况。 1 快速排序 [快速排序的思路] 分区&#xff1a;从数组中任意选择一个“基准”&#xff0c;所有比基准小的元素放在基准前面&#xff0c;比基准大的元素…

stm32week10

stm32学习 七.CAN 7.STM32 CAN外设 标识符过滤器&#xff1a; 每个过滤器的核心由两个32位寄存器组成&#xff1a;R1[31:0]和R2[31:0] FSCx&#xff1a;位宽设置&#xff0c;置0为16位&#xff0c;置1为32位 FBMx&#xff1a;模式设置&#xff0c;置0为屏蔽模式&#xff0c;…

看雪 get_pwn3(2016 CCTF 中的 pwn3)

get_pwn3(2016 CCTF 中的 pwn3) 格式化字符串漏洞 get_pwn3(2016 CCTF 中的 pwn3) (1) motalymotaly-VMware-Virtual-Platform:~/桌面$ file pwn3 pwn3: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, …

python全栈-JavaScript

python全栈-js 文章目录 js基础变量与常量JavaScript引入到HTML文件中JavaScript注释与常见输出方式 数据类型typeof 显示数据类型算数运算符之加法运算符运算符之算术运算符运算符之赋值运算符运算符之比较运算符运算符之布尔运算符运算符之位运算符运算符优先级类型转换 控制…

操作系统概述(3)

批处理系统 1.单道批处理系统 单道批处理系统是成批地处理作用&#xff0c;并且始终只有一道作业在内存中的系统。优点&#xff1a;提高系统资源的利用率和系统吞吐量。缺点&#xff1a;系统中的资源得不到充分利用。 2.多道批处理系统 引入多道程序设计技术&#xff0c;是…