Selaa lähdekoodia

add: 添加数字签名

liutian 11 kuukautta sitten
vanhempi
commit
b25e2c8205
2 muutettua tiedostoa jossa 72 lisäystä ja 0 poistoa
  1. 12 0
      packages/core/src/index.js
  2. 60 0
      packages/core/src/worker/compdfkit_worker.js

+ 12 - 0
packages/core/src/index.js

@@ -547,6 +547,18 @@ class ComPDFKitViewer {
 
           await this.initPage()
 
+          const response = await fetch('./lib/Certificate.pfx')
+          const blob = await response.blob()
+          const certFile = new File([blob], 'Certificate.pfx', { type: blob.type })
+
+          const a = await this.messageHandler.sendWithPromise('AddSignature', {
+            doc: this.doc,
+            pagePtr: this.pagesPtr[0],
+            certFile,
+          })
+
+          console.log(a)
+
           if (this.webviewerServer) {
             if (this.controller) {
               this.controller.abort()

+ 60 - 0
packages/core/src/worker/compdfkit_worker.js

@@ -228,6 +228,66 @@ class CPDFWorker {
     messageHandler.on('GetSignatures', (data) => {
       return getSignatures(data)
     })
+    messageHandler.on('AddSignature', async (data) => {
+      const doc = data.doc
+      const pagePtr = data.pagePtr.pagePtr
+      const buf = await convertFileToBuffer(data.certFile)
+
+      ComPDFKitJS.opened_cert[0] = buf
+      const pass = stringToNewUTF8('ComPDFKit')
+      const res = Module._GetX509ByPKCS12Cert(0, buf.length, pass)
+      console.log(res)
+      const subject = []
+      for (let i = 0; i < 7; i++) {
+        Module._GetSignSubject(res, i)
+        subject.push(U8StringData)
+      }
+      const rect = {
+        left: 200,
+        top: 70,
+        right: 270,
+        bottom: 100
+      }
+
+      const annotPtr = Module._CreateWidget(doc, pagePtr, 6)
+      setAnnotCreationDate({
+        annotPtr,
+        date: new Date()
+      })
+
+      setAnnotRect({
+        pagePtr,
+        annotPtr,
+        rect
+      })
+
+      Module._UpdateFormAp(annotPtr)
+
+
+      const signaturePtr = Module._InitSignature2(annotPtr)
+      
+      DataArray = []
+      Module._SaveDocumentByStream(doc, 2)
+
+      const addResult = Module._AddSignSaveDocument(doc, signaturePtr, res)
+      console.log(addResult)
+
+      let pdfData = []
+      for (let i = 0; i < DataArray.length; i++) {
+        let subArray = new Uint8Array(DataArray[i])
+        pdfData.push(subArray)
+      }
+      const blobData = new Blob(pdfData, { type: "application/pdf" })
+      const buffer = await convertFileToBuffer(blobData)
+      const length = ComPDFKitJS.opened_files.length
+      ComPDFKitJS.opened_files[length] = buffer
+      const newDoc = Module._InitDocument()
+      const passwordPtr = stringToNewUTF8('')
+      Module._LoadDocumentByStream(newDoc, length, buffer.length, passwordPtr)
+
+      const modifyResult = Module._AddSignModifyDocument(signaturePtr, res, 1, ComPDFKitJS.opened_files[1].length)
+      console.log(modifyResult)
+    })
 
     messageHandler.on('PushRenderTask', (data) => {
       const { pagePtr, scale, left, right, bottom, top } = data