NodeJS 是否有PDF元数据的NPM模块?

1sbrub3j  于 2023-06-22  发布在  Node.js
关注(0)|答案(3)|浏览(117)

我正在寻找一个npm模块,我可以用它来编辑元标签,如 * 作者 * 和 * 标题 * 的PDF文件。
或者,一个开放许可的JavaScript库也可以。
有一个名为pdftk的程序,如果它是一个npm模块,它将是合适的。

cnh2zyt3

cnh2zyt31#

您可以安装exiftool命令行实用程序来编辑PDF的元数据:

sudo apt install libimage-exiftool-perl

然后你可以使用Node.js child_process.exec()函数从你的程序中调用命令行工具:

'use strict';

const exec = require('util').promisify(require('child_process').exec);

const execute = async command => {
  const {stdout, stderr} = await exec(command);

  console.log((stderr || stdout).trim());
};

(async () => {
  await execute('exiftool -title="Example PDF" -author="John Doe" /var/www/example.com/public_html/example.pdf');
})();
sd2nnvve

sd2nnvve2#

可以使用https://www.npmjs.com/package/pdf-lib

import { PDFDocument } from 'pdf-lib';
const arrayBuffer = await readFile(file);
const pdf = await PDFDocument.load(arrayBuffer);
const pageCount = pdf.getPagesCount();

你有以下funcntions从PDF文件中获取 meta数据。参考:https://pdf-lib.js.org/docs/api/classes/pdfdocument

getAuthor
getCreationDate
getCreator
getForm
getKeywords
getModificationDate
getPage
getPageCount
getPageIndices
getPages
getProducer
getSubject
getTitle
h43kikqp

h43kikqp3#

我还没有测试过这个软件包,但node-exiftool似乎提供了pdf元数据版。
另一种可能性是使用 pdftk(如果可用)和child_process编写自己的模块。
也许我会尝试自己做一个。

相关问题