Browse Source

add: test 数字签名

liutian 11 months ago
parent
commit
653d194243

+ 27 - 21
packages/core/src/index.js

@@ -550,14 +550,14 @@ class ComPDFKitViewer {
           const response = await fetch('./lib/Certificate.pfx')
           const blob = await response.blob()
           const certFile = new File([blob], 'Certificate.pfx', { type: blob.type })
-
+          this.certFile = certFile
           const a = await this.messageHandler.sendWithPromise('AddSignature', {
             doc: this.doc,
             pagePtr: this.pagesPtr[0],
             certFile,
           })
 
-          console.log(a)
+          // console.log(a)
 
           if (this.webviewerServer) {
             if (this.controller) {
@@ -643,6 +643,15 @@ class ComPDFKitViewer {
     )
   }
 
+  async addSignature(ptr) {
+    const a = await this.messageHandler.sendWithPromise('AddSignature', {
+      doc: this.doc,
+      pagePtr: this.pagesPtr[0],
+      certFile,
+      ptr
+    })
+  }
+
   getSelectedText(pageNumber) {
     if (pageNumber) {
       const page = this.pdfViewer._pages[pageNumber - 1]
@@ -2496,7 +2505,6 @@ class ComPDFKitViewer {
       case "Print":
         const url = await this.download(false, null, 3)
         return url
-        break;
 
       case "SaveAs":
         this.downloadOrSave();
@@ -3603,27 +3611,25 @@ class ComPDFKitViewer {
     }
   }
 
-  async getSignature(annotPtr) {
-    await this.messageHandler.sendWithPromise('CreateSignatureAP', {
-      annotPtr,
-      certFile: this.certFile
-    })
-
+  async getSignature() {
     const ratio = window.devicePixelRatio || 1
+
     const imgRect = {
-      width: parseInt(582 * ratio + 1),
-      height: parseInt(187 * ratio + 1)
-    }
+      width: parseInt(580 * ratio + 1),
+      height: parseInt(187 * ratio + 1),
+      top: 0,
+      left: 0,
+      right: 582,
+      bottom: 187
+    }
+    await this.messageHandler.sendWithPromise('AddSignature', {
+      doc: this.doc,
+      pagePtr: this.pagesPtr[0],
+      certFile: this.certFile,
+    })
 
-    const imageArray = await this.messageHandler.sendWithPromise('GetRenderAnnot', {
-      annotPtr,
-      rect: {
-        left: 0,
-        right: 582,
-        top: 0,
-        bottom: 187
-      },
-      scale: ratio
+    const imageArray = await this.messageHandler.sendWithPromise('CreateSignatureAP', {
+      rect: imgRect
     })
 
     const canvas = document.createElement('canvas')

+ 101 - 22
packages/core/src/worker/compdfkit_worker.js

@@ -108,25 +108,26 @@ class CPDFWorker {
         Module._ClearDocument(data.doc)
       }
 
-      if (!data.fontFile) return true
-
-      await new Promise((resolve, reject) => {
-        const fileReader = new FileReader()
-        fileReader.onload = () => {
-          const buf = new Uint8Array(fileReader.result)
-
-          ComPDFKitJS.opened_Font[0] = buf
-          ComPDFKitJS.opened_FontNames[0] = data.fontName
-
-          ComPDFKitJS.opened_DefFont[0] = buf
+      if (data.fontFile) {
+        await new Promise((resolve, reject) => {
+          const fileReader = new FileReader()
+          fileReader.onload = () => {
+            const buf = new Uint8Array(fileReader.result)
+  
+            ComPDFKitJS.opened_Font[0] = buf
+            ComPDFKitJS.opened_FontNames[0] = data.fontName
+  
+            ComPDFKitJS.opened_DefFont[0] = buf
+  
+            resolve(true)
+          }
+          fileReader.onerror = () => {
+            reject(fileReader.error)
+          }
+          fileReader.readAsArrayBuffer(data.fontFile)
+        })
+      }
 
-          resolve(true)
-        }
-        fileReader.onerror = () => {
-          reject(fileReader.error)
-        }
-        fileReader.readAsArrayBuffer(data.fontFile)
-      })
       const doc = Module._InitDocument()
 
       return doc
@@ -229,12 +230,88 @@ class CPDFWorker {
       return getSignatures(data)
     })
     messageHandler.on('CreateSignatureAP', async (data) => {
-      const { annotPtr } = data
+      const rect = data.rect
+
+      const newDoc = Module._InitDocument()
+      const a = Module._CreateDocument(newDoc)
+      console.log(count)
+      Module._InsertPage(newDoc, 0, 595, 842)
+      const count = Module._GetPageCount(newDoc)
+      console.log(count)
+      const pagePtr = Module._GetPage(newDoc, 0)
+      const annotPtr = Module._CreateWidget(newDoc, pagePtr, 6)
+      setAnnotCreationDate({
+        annotPtr,
+        date: new Date()
+      })
+
+      setAnnotRect({
+        pagePtr,
+        annotPtr,
+        rect
+      })
+
+      Module._UpdateFormAp(annotPtr)
+
       const content = stringToNewUTF8('名称:数字签名\n时间:2024.04.21\n原因:我是文件的所有者哈哈哈哈哈哈')
       const text = stringToNewUTF8('我是数字签名')
       const res = Module._UpdateApWithDigitalSigConfig(annotPtr, content, false, false, false, text, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
       console.log(res)
+
+      const { width, height } = rect
+      let pixelNum = width * height
+
+      let imageBytes = pixelNum * 4
+      let imageptr = _malloc(imageBytes)
+      for (var i = 0; i < pixelNum; i++) {
+        Module.HEAP32[imageptr / 4 + i] = 0
+      }
+
+      Module._RenderAnnot(annotPtr, width, height, imageptr, 0)
+      let imageArray = new Uint8Array(imageBytes)
+      for (var i = 0; i < imageBytes; i += 4) {
+        //bgra 转 rgba
+        imageArray[i] = Module.HEAPU8[imageptr + i + 2]
+        imageArray[i + 1] = Module.HEAPU8[imageptr + i + 1]
+        imageArray[i + 2] = Module.HEAPU8[imageptr + i]
+        imageArray[i + 3] = Module.HEAPU8[imageptr + i + 3]
+      }
+      _free(imageptr)
+      return imageArray
     })
+
+    messageHandler.on('TestAddSignature', async (data) => {
+      const doc = data.doc
+      const buf = await convertFileToBuffer(data.certFile)
+      ComPDFKitJS.opened_cert[0] = buf
+      const pass = stringToNewUTF8('ComPDFKit')
+      const certPtr = Module._GetX509ByPKCS12Cert(0, buf.length, pass)
+
+      const signaturePtr = Module._InitSignature2(annotPtr)
+
+      DataArray = []
+      Module._SaveDocumentByStream(doc, 2)
+
+      const addResult = Module._AddSignSaveDocument(doc, signaturePtr, certPtr)
+      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('AddSignature', async (data) => {
       const doc = data.doc
       const pagePtr = data.pagePtr.pagePtr
@@ -292,8 +369,10 @@ class CPDFWorker {
       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)
+      const modifyResult = Module._AddSignModifyDocument(signaturePtr, res, 1, ComPDFKitJS.opened_files[1].length)
+      console.log(modifyResult)
+
+      return blobData
     })
 
     messageHandler.on('PushRenderTask', (data) => {
@@ -2526,7 +2605,7 @@ async function getSignatures({
 
             const fileLength = ComPDFKitJS.opened_files[0].length
             const signerResult = Module._VerifySigner(signaturePtr, signerPtr, 0, fileLength)
-
+            console.log('signerResult', signerResult)
             const signatureResult = Module._VerifyDocument(doc, signaturePtr, 0, fileLength)
             if (signatureResult === 1) {
               signer.isSignVerified = true

+ 1 - 1
packages/webview/src/components/DocumentContainer/DocumentContainer.vue

@@ -201,7 +201,7 @@ onMounted(async () => {
     toggleButton: document.querySelector('.toggle-button')
   })
   let initialDoc = getHashParameters('d', '')
-  initialDoc = initialDoc ? JSON.parse(initialDoc) : './example/PDF Tech Admin Console Guidelines.pdf'
+  initialDoc = initialDoc ? JSON.parse(initialDoc) : './example/Blank Page.pdf'
   initialDoc = Array.isArray(initialDoc) ? initialDoc : [initialDoc]
   const activeTab = useViewer.activeTab || 0
   initialDoc = initialDoc[activeTab]