翻譯|使用教程|編輯:莫成敏|2019-08-22 15:56:07.040|閱讀 267 次
概述:PrizmDoc是最快速、最有品質的HTML5文檔查看器,提供安全的、全定制化的查看和協作功能。它通過一個簡單部署的機制減少成本,降低安全風險和提高生產率。本文主要介紹通過PrizmDoc創建Node.js服務
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
PrizmDoc是最快速、最有品質的HTML5文檔查看器,提供安全的、全定制化的查看和協作功能。它通過一個簡單部署的機制減少成本,降低安全風險和提高生產率。同時,它是基于服務器的查看器,消除了不必要的許可和下載。
PrizmDoc? e-Signer支持填寫使用PrizmDoc?模板設計器創建的表格,證明了這里。使用e-Signer,表單字段可以手動填寫、編程填寫或者預先填充默認值。
填寫完字段后,用戶可以單擊“下載簽名表單”按鈕下載包含填寫數據的PDF。按下此按鈕時,e-Signer使用PrizmDoc服務器“MarkupBurner”API將數據“刻錄”到文檔中。
但是,如果您已擁有所有用戶的數據,并且想要直接跳過將數據刻錄到文檔而不將表單加載到e-Signer中,該怎么辦?這篇文章將概述如何創建Node.js服務。
1、處理路由表單燃燒器GET請求
此服務將處理將GET請求路由到表單的GET請求,此初始步驟將設置路由處理。
對于此示例,您需要將表單定義文件(或多個文件)放在名為“FormDefinitions”的文件夾中以及表單數據文件(具有相同名稱但具有“data.json”文件擴展名)。表單數據文件必須包含鍵值對,其中每個鍵都是表單字段ID,每個值都是用于填充字段的值。例如:
{ "Text1": "Doe", "Text2": "John" }
需要將相應的表單文檔放入名為“Documents”的文件夾中。
您需要安裝Node.js和PrizmDoc服務器。
使用npm安裝express和axios。Express將用于偵聽端口3001并為“表單刻錄機”發送GET請求,如下所示。Axios將用于向PrizmDoc Server發出http請求(在步驟3中)。
創建一個main.js文件,如下所示,需要express、axios和fs(將用于加載表單數據文件,如下所示,以及表單文檔和表單定義文件)。
const express = require('express'); const axios = require('axios'); const app = express(); const fs = require('fs'); // PrizmDoc Server must be installed to use this. const host = '//localhost:18681'; // Use the following instead to use PrizmDoc Cloud. // const host = '//api.accusoft.com'; const apiKey = 'YOUR_API_KEY'; app.get('/formBurner/:id', function (req, res) { // This example uses the field value provided in the data.json file in the FormDefinitions folder. // You can update the code to instead load the data from elsewhere, such as a database. fs.readFile(`${__dirname}/FormDefinitions/${req.params.id}.data.json`, 'utf8', function (err, data) { const fieldValues = !err ? JSON.parse(data) : {}; // See step 2 for the implementation of convertForm. convertForm(fieldValues, req.params.id, res); }); });
2、將每個表單字段轉換為標記
PrizmDoc Server MarkupBurner API將標記JSON作為輸入。有必要將每個表單字段轉換為適當的標記格式。下面的convertForm方法打開具有指定表單定義ID的表單定義文件,并使用指定的字段值將每個字段轉換為標記以填充字段。然后將標記刻錄到表單文檔中(參見步驟3)。
此示例僅演示轉換文本字段,但可以更新以轉換其他字段類型,例如簽名和復選框。
const convertForm = (fieldValues, formDefinitionId, res) => { fs.readFile(`${__dirname}/FormDefinitions/${formDefinitionId}.json`, 'utf8', function (err, data) { const formDefinition = JSON.parse(data); let marks = []; const globalFontName = (formDefinition.globalSettings && formDefinition.globalSettings.fontName) || 'Fira Sans'; const globalFontColor = (formDefinition.globalSettings && formDefinition.globalSettings.fontColor) || '#000000'; formDefinition.formData.forEach(field => { if (field.template === 'TextTemplate') { let mark = {}; if (field.multiline) { mark.type = 'TextAreaSignature'; mark.maxFontSize = field.fontSize || 8; mark.fontStyle = []; } else { mark.type = 'TextInputSignature'; } mark.uid = field.fieldId; mark.interactionMode = 'Full'; mark.creationDateTime = '2019-06-25T19:28:13.396Z'; mark.modificationDateTime = '2019-06-25T19:28:13.396Z'; mark.mask = null; mark.maxLength = 0; mark.rectangle = { x: field.rectangle.x, y: field.rectangle.y, width: field.rectangle.width, height: field.rectangle.height }; mark.pageData = { width: field.pageData.width, height: field.pageData.height }; mark.pageNumber = field.pageNumber; mark.fontColor = (field.fontColor === 'UseGlobalFontColorSetting') ? globalFontColor : field.fontColor; mark.fontName = (field.fontName === 'UseGlobalFontNameSetting') ? globalFontName : field.fontName; mark.horizontalAlignment = field.horizontalAlignment ? (field.horizontalAlignment.charAt(0).toUpperCase() + field.horizontalAlignment.slice(1)) : 'Left'; // If a field value is not provided, this example uses the value of "example". mark.text = (fieldValues !== undefined) ? fieldValues : 'example'; marks.push(mark); } }); // See step 3 for the implementation of burnForm. burnForm(marks, formDefinition.templateDocumentId, res); }); };
3、將標記刻錄到表單中
使用PrizmDoc Server MarkupBurner API將標記刻錄到表單文檔中,如下所示。在對formBurner請求的響應中返回已刻錄的文檔。
const burnForm = async (marks, documentName, res) => { const { affinityToken, markupFileId } = await postMarkup(marks); console.log(`markupFileId: ${markupFileId}`); const documentFileId = await postDocument(documentName, affinityToken); console.log(`documentFileId: ${documentFileId}`); const processId = await postBurner(documentFileId, markupFileId, affinityToken); console.log(`processId: ${processId}`); const burnedDocumentFileId = await getBurner(processId, affinityToken); console.log(`burnedDocumentFileId: ${burnedDocumentFileId}`); const burnedDocument = await getBurnedDocument(burnedDocumentFileId, documentName, affinityToken); res.end(burnedDocument, 'binary'); };
首先,將標記和表單文檔作為“工作文件”上載到PrizmDoc Server。
const postMarkup = async marks => { const response = await axios({ url: `${host}/PCCIS/V1/WorkFile?FileExtension=json`, data: { marks }, method: 'POST', headers: { 'Content-Type': 'octet-stream', 'acs-api-key': apiKey } }); return { markupFileId: response.data.fileId, affinityToken: response.data.affinityToken }; }; const postDocument = async (documentName, affinityToken) => { const response = await axios({ url: `${host}/PCCIS/V1/WorkFile`, data: fs.readFileSync(__dirname + '/Documents/' + documentName), method: 'POST', headers: { 'Content-Type': 'octet-stream', 'acs-api-key': apiKey, 'Accusoft-Affinity-Token': affinityToken || '' } }); return response.data.fileId; };
接下來,使用上載的標記和表單文檔作為輸入創建標記刻錄機。
const postBurner = async (documentFileId, markupFileId, affinityToken) => { const response = await axios({ url: `${host}/PCCIS/V1/MarkupBurner`, data: { 'input': { 'documentFileId': documentFileId, 'markupFileId': markupFileId } }, method: 'POST', headers: { 'Content-Type': 'application/json', 'acs-api-key': apiKey, 'Accusoft-Affinity-Token': affinityToken || '' } }); return response.data.processId; };
然后,檢查標記刻錄機的狀態,直到完成為止。
}; const getBurner = async (processId, affinityToken) => { const response = await axios({ url: `${host}/PCCIS/V1/MarkupBurner/${processId}`, method: 'GET', headers: { 'Content-Type': 'application/json', 'acs-api-key': apiKey, 'Accusoft-Affinity-Token': affinityToken || '' } }); console.log(`MarkupBurner percentComplete: ${response.data.percentComplete}`); if (response.data.state === 'complete') { return response.data.output.documentFileId; } if (response.data.state === 'error') { return; } await sleep(1000); return getBurner(processId, affinityToken); };
最后,檢索已刻錄的文檔。
const sleep = (ms) => { return new Promise(resolve => setTimeout(resolve, ms)); const getBurnedDocument = async (documentFileId, documentName, affinityToken) => { const response = await axios({ url: `${host}/PCCIS/V1/WorkFile/${documentFileId}`, method: 'GET', responseType: 'arraybuffer', headers: { 'Content-Type': 'application/pdf', 'acs-api-key': apiKey, 'Accusoft-Affinity-Token': affinityToken || '' } }); // Uncomment the line below to save the burned document to disk. // fs.writeFileSync(`${__dirname}/${documentName}_burned.pdf`, response.data); return response.data; };
使用服務
完成這些步驟后,您可以使用該服務將用戶數據直接刻錄到表單中。
運行“npm install”,然“node main.js”運行該服務。然后向http:// localhost:3001 / formBurner / {your-form-id}發出GET請求,以獲取包含已填充數據的表單的PDF。
想要購買PrizmDoc正版授權,或了解更多產品信息請點擊
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn