我正在尋找一種使用自簽名證書對 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)
uj5u.com熱心網友回復:
看來問題與作業系統有關。事實上,該代碼在 linux 作業系統下運行良好,但在 Windows 作業系統中存在一些與匯入命令相關的問題。對于 Windows 作業系統,用“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 commands 。 https://jobs.czconsultants.com/utils/convert-imports-to-require-online/0 這個網站有助于快速轉換多個命令
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452240.html
標籤:javascript pdf ssl 证书 电子签名 pdf库
