本文章介紹 HTML && jQuery Web項目中 PDF 批注插件庫 ElasticPDF 在線版 API 示例教程,API 包含 ① 導出批注后PDF數據;② 導出純批注 json 數據;③ 加載舊批注;④ 切換文檔;⑤ 切換用戶;⑥ 清空批注 等數據處理功能,可滿足通常業務需求。本教程可用于月付許可和在線測試版,歡迎 聯系我們 咨詢和獲取接入 key。
0 ElasticPDF 產品介紹
ElasticPDF基于開源pdf.js,增加了多種開箱即用的 PDF 批注功能。代碼包延續了 pdf.js-dist 獨立且完全離線的結構風格,僅增加了用于支持批注的離線 Javascript 代碼,可以快速完美集成到任何可以運行Javascript, HTML, CSS 的項目環境中,在公網及內網環境都可以完美的運行。
根據不同的功能及預算需求,有兩個版本的產品可供選擇,兩者僅在最終的批注保存階段有區別,產品 Demo 地址如下:
① 批注合成版: https://demos.libertynlp.com/#/pdfjs-annotation
② 專業批注版: https://www.elasticpdf.com/demo
1 導入頁面 HTML 及初始化
首先將以下的 HTML 代碼導入到目標頁面,其中包含了初始化代碼 initialPDFEditor()
和接收所有回報信息的函數 listenPDFEditorMessage()
,所有導出的 PDF數據,批注數據 都在函數listenPDFEditorMessage()
下,可以與后續的業務融合。
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title><link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="https://pdfmaster.libertynlp.com/annotation/css/trail-style.css"></head><body style="overflow: auto;"><div class='project-title'><img src="elasticpdf-logo.png" alt="" /><h2>HTML && jQuery項目在線版 API 示例教程</h2><a style="cursor: pointer;text-decoration: none;" href='https://www.elasticpdf.com/contact-us.html'target="_blank"><div class='message-div info-message'><i class="fa fa-info-circle" aria-hidden="true"></i><span>點我 聯系我們 獲取測試 key</span></div></a><button style="margin-left: 80px;" class='theme-btn btn-outline-warning'onclick="getPDFData()">獲取PDF數據</button><button class='theme-btn btn-outline-help' onclick="outputAnnotation()">導出批注</button><button class='theme-btn btn-outline-success' onclick="changeFile()">切換文檔</button><button class='theme-btn btn-outline-warning' onclick="setMember()">切換用戶</button><button class='theme-btn btn-outline-danger' onclick="clearAnnotation()">清空批注</button><button class='theme-btn btn-outline-info' onclick="reloadOldAnnotationData()">加載舊批注</button></div><!-- 以下代碼并不包含測試或者月付 key, 請聯系我們獲取 --><iframe onload='initialPDFEditor()' id='elasticpdf-iframe'src="https://pdfmaster.libertynlp.com/web/viewer.html?file=tutorial.pdf"frameborder="0" width="100%" height="720px"></iframe> </body><script type="text/javascript">var elasticpdf_viewer = null;function initialPDFEditor() {// 監聽 pdf 編輯各種信息的回調listenPDFEditorMessage();elasticpdf_viewer = document.getElementById('elasticpdf-iframe').contentWindow;// 在線版只支持打開在線文檔var pdf_url = 'https://pdfmaster.libertynlp.com/web/tutorial.pdf';elasticpdf_viewer.postMessage({"source": "test-elasticpdf","function_name": "initialApp","content": {'language': 'zh-cn', // 交互語言'pdf_url': pdf_url,'member_info': { //用戶信息'id': 'elasticpdf_id','name': 'elasticpdf',},}}, '*');}// 出于安全考慮, 控制臺輸出已被測試網站禁用無法使用 console.log 打印出內容// 請用 alert 打印內容function listenPDFEditorMessage() {window.addEventListener('message', (e) => {if (e.data.source != 'elasticpdf') {return;}// pdf 加載結束的回調,可以在此處導入服務器上儲存的批注文件if (e.data.function_name == 'pdfLoaded') {// console.log 無效,請使用 alert 打印內容// alert('PDF加載成功');reloadData();}// pdf 批注編輯回調,可以在此處導出批注并傳輸到服務器if (e.data.function_name == 'annotationsModified') {// 僅獲取 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 無效,請使用 alert 打印內容// alert('批注被修改');postService('upload-annotation-data', {'file_name': file_name,'file_id': '123ddasfsdffads','file_annotation': annotation_content,});}// pdf 批注導出回調,可以在此處導出批注并傳輸到服務器if (e.data.function_name == 'outputAnnotation') {// 僅獲取 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 無效,請使用 alert 打印內容// alert('批注信息\n'+annotation_content);}// 接收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'];// 如果文檔沒有被編輯過,則 pdf_base64 仍然是文件名// 接收到 pdf 數據,其中 pdf_base64 可以快捷上傳到服務器postService('upload-pdf-data', {'file_name': file_name,'file_id': '123ddasfsdffads','file_data': pdf_base64,});// alert('獲取到 pdf base64 數據,如有需要請到postService中完善后續邏輯');}});}</script>
</html>
2 調用 API
① 導出批注數據
導出 pdf 批注的 json 數據,可以用于后續的篩選、合并、入庫保存等業務流程,非常適用于在線批注流程,因為只需要保存一個原 pdf 文檔,然后從數據庫中僅加載和回顯批注,可以節省很多的服務器性能、流量和帶寬費用。
//導出可保存的批注對象,導出的數據在 listenPDFEditorMessage() 下
// if(e.data.function_name == 'outputAnnotation') 代碼塊下
function outputAnnotation() {elasticpdf_viewer.postMessage({"source": "test-elasticpdf","function_name": "outputAnnotation","content": ""}, '*');
}
② 導入舊批注
從服務器中依據文件 ID 或 PDF 鏈接加載 ① 中導出的批注數據并回顯至文檔上,支持再次操作編輯,以此來實現批注數據的云端同步。
// 加載舊批注
function reloadOldAnnotationData() {var old_annotation = getOldAnnotation();elasticpdf_viewer.postMessage({"source": "test-elasticpdf","function_name": "setFileAnnotation","content": old_annotation}, '*');
}// 生成模擬舊批注
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);
}
③ 導出 PDF 文件
將批注合并到批注文件并導出批注后 PDF 文檔 base64 數據,可以直接入庫保存。
// 獲取pdf數據
function getPDFData() {elasticpdf_viewer.postMessage({"source": "test-elasticpdf","function_name": "getPDFData","content": ""}, '*');
}
④ 切換和打開文檔
打開在線文檔,其中文件服務器或站點需要允許 CORS 跨域
,否則加載文檔會失敗。
// 切換打開的文檔,可以把 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}, '*');
}
⑤ 設置用戶信息
設置插件內當前操作用戶的 ID 和用戶名,這些信息會被記錄到每個批注中,后續可以用于做權限差異設置,例如是否允許當前用戶操作他人批注。
//設置用戶
function setMember(id) {var this_member = {'id': 'test-id','name': 'test-name',};elasticpdf_viewer.postMessage({"source": "test-elasticpdf","function_name": "setMember","content": this_member}, '*');
}
⑥ 清空批注數據
將當前文檔對應的操作批注完全清空。
// 清空批注
function clearAnnotation() {elasticpdf_viewer.postMessage({"source": "test-elasticpdf","function_name": "clearFileAnnotation","content": ""}, '*');
}
總結
至此,elasticpdf 在線測試版集成于 jQuery 及 HTML 項目并調用數據業務 API 的代碼介紹完畢,測試代碼文件已上傳至 Github(網址:jQuery-html-use-pdf.js-elasticpdf),歡迎聯系我們咨詢和獲取 Key。
溫馨提示:本文首發于?https://www.elasticpdf.com?,轉載請注明出處:https://www.elasticpdf.com/blog/html-jquery-web-project-pdf-annotation-library-plugin-elasticpdf-online-api-tutorial-zh.html