使用纯 JavaScript 签署 PDF [英] Sign PDF with plain JavaScript
问题描述
随着 WebCrypto API 的发展和 Chrome 和 Firefox 的支持,我想用它来对 PDF 文档进行数字签名.周围的文献并不多,但我找到了一些示例 [1] 和一个名为 PKI.js [2] 的库.在示例中,描述了签名过程,但最终返回了签名.我希望我的 Base64 PDF 文件以已签名的 Base64 字符串再次返回,但遗憾的是,事实并非如此.据我所知,PKI.js 也不提供对我的 Base64 PDF 进行签名的方法.
With WebCrypto API evolving and being supported by Chrome and Firefox, I would like to use it for digitally signing a PDF document. There is not much of literature around, but I found some examples [1] and a library called PKI.js [2]. In the examples, the signing process is described, but in the end, a signature is returned. I would expect my Base64 PDF file returned again in a signed Base64 string, but sadly, this is not what happens. PKI.js too, to my knowledge, does not provide a way to sign my Base64 PDF.
是否可以仅使用 JavaScript 和 WebCrypto API 对 PDF 进行签名?私钥可以输入到 中,或者更好地存储在浏览器的证书设置中.
Is there a way to sign a PDF with JavaScript and the WebCrypto API only? The private key can be entered in a <textarea>
or, even better, stored in the certificate settings of the browser.
Base64 PDF(来自 REST API)→与 JS & 签署证书→签名的 Base64 PDF(发送到 REST)
Base64 PDF (from REST API) → Sign with JS & certificate → Signed Base64 PDF (send to REST)
推荐答案
这在技术上是可行的,事实上这是我们在制作 PKIjs 时考虑的场景之一(这就是为什么有这个示例) - https://pkijs.org/examples/PDFexample.html
It is technically possible to do this, in-fact it is one of the scenarios we had in mind when we made PKIjs (which is why there is this sample) - https://pkijs.org/examples/PDFexample.html
据说进行签名需要使用 PDF 结构本身,这需要自定义解析器或对现有解析器(例如 pdfjs)的修改.
That said to do signing requires working with the PDF structure itself, which either requires a custom parser or modifications to an existing one (pdfjs for example).
长话短说,在浏览器中签署 PDF 需要很多工作,但我们正在努力.
Long story short, signing a PDF in browser will take a lot of work, it is something we are working on though.
这篇关于使用纯 JavaScript 签署 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!