如何使用 Javascript 对带有证书的 PDF 文档进行数字签名?
how to digitally sign a PDF document with a certificat using Javascript?
我正在寻找一种使用自签名证书对 pdf 进行数字签名的解决方案,并在 youtube 上看到一个视频,解释了使用 pdf-lib 和 node-signpdf 库的过程。但是,按照说明进行操作后,它没有按预期工作并遇到了问题:
- 我执行了命令 ( npm i ) 和 (npm start ),如项目的 github 仓库中所示。
回购:https://github.com/RichardBray/pdf_sign
演示(8 分钟视频):https://www.youtube.com/watch?v=OFZK5lc70OI&list=PLXUfmx2SIgyICZP-rA84Sle_ghq30W89N&index=4&t=1s
import SignPDF from "./SignPDF";
import fs from "node:fs";
import path from "node:path";
async function main() {
const pdfBuffer = new SignPDF(
path.resolve("test_assets/minions.pdf"),
path.resolve("test_assets/certificate.p12")
);
const signedDocs = await pdfBuffer.signPDF();
const randomNumber = Math.floor(Math.random() * 5000);
const pdfName = `./exports/exported_file_${randomNumber}.pdf`;
fs.writeFileSync(pdfName, signedDocs);
console.log(`New Signed PDF created called: ${pdfName}`);
}
main();
> pdf_sign@1.0.0 start
> npm run build && node dist/index.js
> pdf_sign@1.0.0 build
> ./node_modules/.bin/babel ./src -d ./dist
'.' is not recognized as an internal command
or external, an executable program or a batch file.
- 我尝试使用 VScode 调试器 运行 代码,但它显示了以下错误:
Uncaught SyntaxError c:\Users\ACER\Documents\GitHub\pdf_sign\src\index.js:1
import SignPDF from "./SignPDF";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at compileFunction (undefined:352:18)
at wrapSafe (undefined:1031:15)
at Module.\_compile (undefined:1065:27)
at Module.\_extensions..js (undefined:1153:10)
at Module.load (undefined:981:32)
at Module.\_load (undefined:822:12)
at executeUserEntryPoint (undefined:81:12)
at \<anonymous\> (undefined:17:47)
看来问题与 OS 相关。事实上,代码在 linux OS 下工作正常,但在 Windows OS 下有一些与导入命令相关的问题。
windows OS 用“require”命令替换“import”命令解决了问题。
之前:
import SignPDF from "./SignPDF";
import fs from "node:fs";
import path from "node:path";
之后:
const SignPDF = require("./SignPDF");
const fs = require("node:fs");
const path = require("node:path");
编辑:只要在项目中发现导入,就应将其替换为 require 命令。
https://jobs.czconsultants.com/utils/convert-imports-to-require-online/0
这个网站有助于快速转换多个命令
我正在寻找一种使用自签名证书对 pdf 进行数字签名的解决方案,并在 youtube 上看到一个视频,解释了使用 pdf-lib 和 node-signpdf 库的过程。但是,按照说明进行操作后,它没有按预期工作并遇到了问题:
- 我执行了命令 ( npm i ) 和 (npm start ),如项目的 github 仓库中所示。
回购:https://github.com/RichardBray/pdf_sign
演示(8 分钟视频):https://www.youtube.com/watch?v=OFZK5lc70OI&list=PLXUfmx2SIgyICZP-rA84Sle_ghq30W89N&index=4&t=1s
import SignPDF from "./SignPDF";
import fs from "node:fs";
import path from "node:path";
async function main() {
const pdfBuffer = new SignPDF(
path.resolve("test_assets/minions.pdf"),
path.resolve("test_assets/certificate.p12")
);
const signedDocs = await pdfBuffer.signPDF();
const randomNumber = Math.floor(Math.random() * 5000);
const pdfName = `./exports/exported_file_${randomNumber}.pdf`;
fs.writeFileSync(pdfName, signedDocs);
console.log(`New Signed PDF created called: ${pdfName}`);
}
main();
> pdf_sign@1.0.0 start
> npm run build && node dist/index.js
> pdf_sign@1.0.0 build
> ./node_modules/.bin/babel ./src -d ./dist
'.' is not recognized as an internal command
or external, an executable program or a batch file.
- 我尝试使用 VScode 调试器 运行 代码,但它显示了以下错误:
Uncaught SyntaxError c:\Users\ACER\Documents\GitHub\pdf_sign\src\index.js:1
import SignPDF from "./SignPDF";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at compileFunction (undefined:352:18)
at wrapSafe (undefined:1031:15)
at Module.\_compile (undefined:1065:27)
at Module.\_extensions..js (undefined:1153:10)
at Module.load (undefined:981:32)
at Module.\_load (undefined:822:12)
at executeUserEntryPoint (undefined:81:12)
at \<anonymous\> (undefined:17:47)
看来问题与 OS 相关。事实上,代码在 linux OS 下工作正常,但在 Windows OS 下有一些与导入命令相关的问题。 windows OS 用“require”命令替换“import”命令解决了问题。
之前:
import SignPDF from "./SignPDF";
import fs from "node:fs";
import path from "node:path";
之后:
const SignPDF = require("./SignPDF");
const fs = require("node:fs");
const path = require("node:path");
编辑:只要在项目中发现导入,就应将其替换为 require 命令。 https://jobs.czconsultants.com/utils/convert-imports-to-require-online/0 这个网站有助于快速转换多个命令