node.js - 将存储在内存中的字符串传递给 pdftotext、antiword、catdoc 等

标签 node.js pdf text child-process

是否可以调用 CLI 工具,如 pdftotext、antiword、catdoc(文本提取器脚本)传递字符串而不是文件?

目前,我阅读了使用 child_process.spawn 调用 pdftotext 的 PDF 文件。我生成一个新进程并将结果存储在一个新变量中。一切正常。

我想从 fs.readFile 而不是文件本身传递 binary:

fs.readFile('./my.pdf', (error, binary) => {
    // Call pdftotext with child_process.spawn passing the binary.
    let event = child_process.spawn('pdftotext', [
        // Args here!
    ]);
});

我该怎么做?

最佳答案

如果命令可以处理管道输入,那绝对是可能的。

spawn返回 ChildProcess对象,您可以通过写入其 stdin 将内存中的字符串(或二进制)传递给它.该字符串应为 convertedReadableStream首先,然后您可以通过 pipe 将字符串写入 CLI 的 stdin .

createReadStream创建一个 ReadableStream来自文件。

以下示例下载一个 pdf 文件并将内容通过管道传输到 pdftotext,然后显示结果的前几个字节。

const source = 'http://static.googleusercontent.com/media/research.google.com/en//archive/gfs-sosp2003.pdf'
const http = require('http')
const spawn = require('child_process').spawn

download(source).then(pdftotext)
.then(result => console.log(result.slice(0, 77)))

function download(url) {
  return new Promise(resolve => http.get(url, resolve))
}

function pdftotext(binaryStream) {
  //read input from stdin and write to stdout
  const command = spawn('pdftotext', ['-', '-'])
  binaryStream.pipe(command.stdin)

  return new Promise(resolve => {
    const result = []
    command.stdout.on('data', chunk => result.push(chunk.toString()))
    command.stdout.on('end', () => resolve(result.join('')))
  })
}

对于 CLI 没有从 stdin 读取的选项,您可以使用 named pipes .

编辑:添加另一个带有命名管道的示例。

创建命名管道后,您可以像使用文件一样使用它们。以下示例创建临时命名管道以发送输入和获取输出,并显示结果的前几个字节。

const fs = require('fs')
const spawn = require('child_process').spawn

pipeCommand({
  name: 'wvText',
  input: fs.createReadStream('document.doc'),
}).then(result => console.log(result.slice(0, 77)))

function createPipe(name) {
  return new Promise(resolve =>
    spawn('mkfifo', [name]).on('exit', () => resolve()))
}

function pipeCommand({name, input}) {
  const inpipe = 'input.pipe'
  const outpipe = 'output.pipe'
  return Promise.all([inpipe, outpipe].map(createPipe)).then(() => {
    const result = []
    fs.createReadStream(outpipe)
    .on('data', chunk => result.push(chunk.toString()))
    .on('error', console.log)

    const command = spawn(name, [inpipe, outpipe]).on('error', console.log)
    input.pipe(fs.createWriteStream(inpipe).on('error', console.log))
    return new Promise(resolve =>
      command.on('exit', () => {
        [inpipe, outpipe].forEach(name => fs.unlink(name))
        resolve(result.join(''))
      }))
  })
}

关于node.js - 将存储在内存中的字符串传递给 pdftotext、antiword、catdoc 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38533754/

相关文章:

node.js - 如何使用node-orm2编写OR条件?

android - 如何使 aChartEngine 中的文本变大?

unity3d - RectTransform坐标根据TextMesh Pro文字大小

android - 使用 google docs 在 webview 中显示 pdf 文件(存储在 google drive 中)

python - 有条件地合并文本文件中的行

node.js - 如何使用对话流实现从列表响应的上下文中提取参数

node.js - npm install module -g 给出 MaxListenersExceededWarning

javascript - 更有效的字符串创建方式

macos - OS X PDF : Get Font Information

PHP:通过从数据库获取数据生成PDF