3 spawn child-process node.js pdftotext unoconv
我正在将文档转换为内存中的pdf(unoconv)并在终端中打印(pdftotext):
unoconv -f pdf --stdout sample.doc | pdftotext -layout -enc UTF-8 - out.txt
Run Code Online (Sandbox Code Playgroud)
工作中.现在我想用这个命令child_process.spawn:
let filePath = "...",
process = child_process.spawn("unoconv", [
"-f",
"pdf",
"--stdout",
filePath,
"|",
"pdftotext",
"-layout",
"-enc",
"UTF-8",
"-",
"-"
]);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,只有第一个命令(在|之前)正在工作.我有可能做我正在尝试的事情吗?
谢谢.
最新情况:
的结果: sh -c- ....
bash-3.2$ sh -c- unoconv -f pdf --stdout /Users/fatimaalves/DEV/xx/_input/sample.doc | pdftotext -layout -enc UTF-8 - -
sh: --: invalid option
Usage: sh [GNU long option] [option] ...
sh [GNU long option] [option] script-file ...
GNU long options:
--debug
--debugger
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--protected
--rcfile
--restricted
--verbose
--version
--wordexp
Shell options:
-irsD or -c command or -O shopt_option (invocation only)
-abefhkmnptuvxBCHP or -o option
Syntax Warning: May not be a PDF file (continuing anyway)
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't find trailer dictionary
Syntax Error: Couldn't read xref table
Run Code Online (Sandbox Code Playgroud)
如果您不想使用上述sh命令,则必须创建多个 child_process.spawn 实例,然后将它们通过管道相互连接,如下所示:
const getModule = spawn('curl', [url, '-ks']);
const unTar = spawn('tar', ['-xvz', '-C', fileName, '--strip-components', 1]);
getModule.stdout.pipe(unTar.stdin);
Run Code Online (Sandbox Code Playgroud)
上面的代码理论上会从 中检索 tarurl并解压到一个目录中fileName
从管道开始的一切都不是参数unoconv.它由shell处理,而不是由unoconv.所以你不能把它作为参数数组的一部分传递给unoconv.
根据您的需要,有很多方法可以解决这个问题.如果您知道将仅在类UNIX操作系统上运行,则可以将命令作为参数传递给sh:
process = child_process.spawn('sh', ['-c', 'unoconv -f pdf --stdout sample.doc | pdftotext -layout -enc UTF-8 - out.txt']);
Run Code Online (Sandbox Code Playgroud)