node.js - 从 nodeJS 读取 PDF 文档属性

标签 node.js pdf metadata

我正在尝试从 nodeJS 读取 PDF 文档属性。我找不到任何用于读取文档属性的 Node 模块。我可以使用 file-metadata 读取文件元数据但它只提供基本属性。我想阅读文档限制摘要之类的属性(请查看附图以供引用。enter image description here

最佳答案

灵感来自@DietrichvonSeggern 的 suggestion我写了小 Node 脚本。

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
}

关于node.js - 从 nodeJS 读取 PDF 文档属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54109660/

相关文章:

android - 在 onCreate 方法中获取 activityInfo 元数据

node.js - 使用 fs.writeFile 时未定义值

node.js - 创建新的 React Native 应用程序

node.js - Mocha 在测试后没有退出

node.js - arangoDb + nodejs : db. useBasicAuth(...) 不是函数

android - itext android 印地语 pdf 创建

windows - 按文件名搜索文件夹并打印内容

iphone - 如何在 iOS 4+ 上正确使用 CGImageProperties.h

clojure - 如何获取 clojure 函数参数的元数据?

c# - 从 PDF 转换为 HTML