从 nodeJS 读取 PDF 文档属性

Read PDF Document properties from nodeJS

我正在尝试从 nodeJS 读取 PDF 文档属性。我找不到任何用于读取文档属性的节点模块。我可以使用 file-metadata but its only giving basic properties. I want to read the properties like Document restriction summary(please check attached image for reference.

读取文件元数据

您是否考虑过使用 exiftool?您必须将它集成到 nodejs 中,但 afaics 它或多或少地提供了您正在寻找的所有数据。

受@DietrichvonSeggern 的启发我写了小节点脚本。

const { spawnSync } = require('child_process');

const { stdout } = spawnSync('exiftool',
  ['-b', '-UserAccess', 'test.pdf'],
  { encoding: 'ascii' });
const bits = (parseInt(stdout, 10) || 0b111111111110);

const perms = {
  'Print': 1 << 2,
  'Modify': 1 << 3,
  'Copy': 1 << 4,
  'Annotate': 1 << 5,
  'Fill forms': 1 << 8,
  'Extract': 1 << 9,
  'Assemble': 1 << 10,
  'Print high-res': 1 << 11
};

Object.keys(perms).forEach((title) => {
  const bit = perms[title];
  const yesno = (bits & bit) ? 'YES' : 'NO';
  console.log(`${title} => ${yesno}`);
});

它将打印如下内容:

Print => YES
Modify => NO
Copy => NO
Annotate => NO
Fill forms => NO
Extract => NO
Assemble => NO
Print high-res => YES

您应该在系统中安装 exiftool,并向该脚本添加必要的错误检查。

ExifTool UserAccess tag reference.


稍作修改:

const perms = {
  'Print': 1 << 2,
  'Modify': 1 << 3,
  'Copy': 1 << 4,
  'Annotate': 1 << 5,
  'FillForms': 1 << 8,
  'Extract': 1 << 9,
  'Assemble': 1 << 10,
  'PrintHighRes': 1 << 11
};

const access = {};
Object.keys(perms).forEach((perm) => {
  const bit = perms[perm];
  access[perm] = !!(bits & bit);
});

console.log(access);

将产生:

{
  Print: true,
  Modify: false,
  Copy: false,
  Annotate: false,
  FillForms: false,
  Extract: false,
  Assemble: false,
  PrintHighRes: true
}