首页 > 解决方案 > 使用 node-qpdf 加密 pdf 文件

问题描述

我正在尝试使用node-qpdf

我只是做了简单的:

Test.pdf 在 index.js 所在的目录中

var qpdf = require('node-qpdf');

var options = {
    keyLength: 128,
    password: 'test123',
    restrictions: {
        print: 'low',
        useAes: 'y'
    }
}
var localFilePath ='Test.pdf';
var outputFilePath ='test';
qpdf.encrypt(localFilePath, options, outputFilePath);

我收到如下错误:

运行:nodemon index.js

[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
C:\Users\Rad\Desktop\node-qpdf-master\test\node_modules\node-qpdf\index.js:56
        return callback(null, outputStream);
               ^

TypeError: callback is not a function
    at Object.Qpdf.encrypt (C:\Users\Rad\Desktop\node-qpdf-master\test\node_modules\node-qpdf\index.js:56:16)
    at Object.<anonymous> (C:\Users\Rad\Desktop\node-qpdf-master\test\index.js:13:6)
    at Module._compile (internal/modules/cjs/loader.js:945:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10)
    at Module.load (internal/modules/cjs/loader.js:798:32)
    at Function.Module._load (internal/modules/cjs/loader.js:711:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1014:10)
    at internal/main/run_main_module.js:17:11
[nodemon] app crashed - waiting for file changes before starting...

标签: node.js

解决方案


我认为他们没有更新文档,我阅读了他们的代码,您必须像这样放置outputFile和更改您的代码options

 var options = {
    keyLength: 256,
    password: '12345',
    outputFile: '<your output name file>',
    restrictions: {
      modify: 'none',
      extract: 'n'
    }

  var doc = await qpdf.encrypt(`${fileLocation}.pdf`, options)


推荐阅读