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

Passing string stored in memory to pdftotext, antiword, catdoc, etc

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

目前,我阅读使用 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 returns a ChildProcess object, you can pass the string (or binary) in memory to it by write to its stdin. The string should be converted to a ReadableStream first, then you can write the string to stdin of the CLI by pipe.

createReadStream creates a 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(''))
      }))
  })
}