«Может не записать нулевые значения в поток» при записи в стандартный ввод node.js дочернего процесса для pdftotext - PullRequest
0 голосов
/ 10 января 2020

Я пытаюсь программно передать содержимое файла в памяти в pdftotext, который находится в свободном доступе: http://www.xpdfreader.com/about.html Другие, кажется, сделали это: Передача строки, хранящейся в памяти в pdftotext, antiword, catdo c, et c

Я могу программно обработать вывод:

const { spawn } = require( 'child_process' );
const fs = require( 'fs' );

const pdftotextExe = 'bin/bin64/pdftotext.exe';
const inputFile = 'simple.pdf';

const command = spawn( pdftotextExe, [inputFile, '-'], { stdio: ['pipe', 'pipe', 'pipe'] } );
command.stdout.on( 'data', chunk => console.log( `starting chars: ${chunk.toString( 'utf8' ).slice( 0, 5 )}` ) );
command.stdout.on( 'end', () => console.log( 'done run' ) );
command.stderr.on( 'data', ( err => console.log( `got error: >> ${err} <<` ) ) );

Работает выше. Но попытка предоставить ввод через стандартный ввод не работает. Аналогичный код, приведенный ниже, приводит к «TypeError [ERR_STREAM_NULL_VALUES]: не может записать нулевые значения в поток»

const command = spawn( pdftotextExe, ['-', '-'], { stdio: ['pipe', 'pipe', 'pipe'] } );
fs.readFile( inputFile, {}, ( contents ) => {
  command.stdin.write( contents ); // write file contents
  command.stdin.end(); // end input
  command.stdout.on( 'data', chunk => console.log( `starting chars: ${chunk.toString( 'utf8' ).slice( 0, 5 )}` ) );
  command.stdout.on( 'end', () => console.log( 'done run' ) );
  command.stderr.on( 'data', ( err => console.log( `got error: >> ${err} <<` ) ) );
} );

Что не так с приведенным выше кодом?

...