我正在寻找一种解决方案,用自签署证书对pdf进行数字签名,并在youtube上看到一段视频,其中解释了使用pdf-lib和节点-签名pdf库的过程。但是,在执行了指示之后,它没有按预期工作,遇到了一个问题:
所示。
存储库: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.
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)
发布于 2022-03-26 13:11:27
这个问题似乎与操作系统有关。实际上,代码在linux操作系统下运行良好,但与导入命令相关的Windows操作系统存在一些问题。对于windows,将“导入”命令替换为"require“命令解决了这个问题。
在此之前:
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本网站帮助快速转换多个命令
https://stackoverflow.com/questions/71617826
复制相似问题