I have tried different configuration, but I cannot seem to succeed to execute "gs" (Ghostscript) in a node.js environment.
我尝试了不同的配置,但似乎无法成功地在节点中执行“gs”(Ghostscript)。js的环境。
var fs = require( "fs" ),
child_process = require( 'child_process' );
...
…
var spawn = child_process.spawn;
var opts = [
"-q ",
"-dQUIET ",
"-dSAFER ",
"-dBATCH ",
"-dNOPAUSE ",
"-dNOPROMPT ",
"-dMaxBitmap=500000000 ",
"-dAlignToPixels=0 ",
"-dGridFitTT=2 ",
"-sDEVICE=jpeg ",
"-dTextAlphaBits=4 ",
"-dGraphicsAlphaBits=4 ",
"-r150 ",
"-sOutputFile=afile.jpg",
" afile.pdf"
];
var gs = spawn( "gs", opts, { cwd: "/mnt/drive/" } );
gs.stdout.on( 'data', function( data ) {
console.log( 'stdout: ' + data );
} );
gs.stderr.on( 'data', function( data ) {
console.log( 'stderr: ' + data );
} );
gs.on( 'close', function( code ) {
console.log( 'child process exited with code ' + code );
} );
---Output ---------------------------------------------------------
——与产出- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
stdout: Unknown device: jpeg
stdout: Unrecoverable error: undefined
stdout: in .uninstallpagedevice
stdout: Operand stack:
defaultdevice
stdout:
child process exited with code 1
-------------------------------------------------------------------
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The /mnt/drive
directory is +read+write for all users. The gs -help
execution returns:
/mnt/驱动器目录为+read+write。g帮助执行返回:
root@Machine:/# gs -help
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc. All rights reserved.
Usage: gs [switches] [file1.ps file2.ps ...]
Most frequently used switches: (you can use # in place of =)
-dNOPAUSE no pause after page | -q `quiet', fewer messages
-g<width>x<height> page size in pixels | -r<res> pixels/inch resolution
-sDEVICE=<devname> select device | -dBATCH exit after last file
-sOutputFile=<file> select output file: - for stdout, |command for pipe,
embed %d or %ld for page #
Input formats: PostScript PostScriptLevel1 PostScriptLevel2 PostScriptLevel3 PDF
Default output device: bbox
Available devices:
...
ijs imagen inferno inkcov iwhi iwlo iwlq jetp3852 jj100 jpeg jpegcmyk
jpeggray la50 la70 la75 la75plus laserjet lbp310 lbp320 lbp8 lex2050
...
txtwrite uniprint xcf xes
Search path:
/usr/share/ghostscript/9.05/Resource/Init :
/usr/share/ghostscript/9.05/lib :
/usr/share/ghostscript/9.05/Resource/Font :
/usr/share/ghostscript/fonts : /var/lib/ghostscript/fonts :
/usr/share/cups/fonts : /usr/share/ghostscript/fonts :
/usr/local/lib/ghostscript/fonts : /usr/share/fonts
For more information, see /usr/share/doc/ghostscript/Use.htm.
Please report bugs to bugs.ghostscript.com.
root@Machine:/#
Where the device jpeg is available. The gs
execution is not perform. Any hint would be help ?
设备jpeg在哪里可用。gs执行没有执行。有什么提示吗?
2 个解决方案
#1
0
The only way I got it to work, is by upgrading node from 10.26 to the latest 10.32, AND encapsulate the gs execution in a simple bash script file. Otherwise, even with the 10.32 node version, I still get the same error. I suspected an environment problem like suggest @Rudie,
我让它工作的唯一方法是将node从10.26升级到最新的10.32,并将gs执行封装在一个简单的bash脚本文件中。否则,即使使用10.32节点版本,我仍然会得到相同的错误。我怀疑环境问题,比如建议@Rudie,
#2
0
You could consider to use Ghostscript4JS, it's a module that binds the Ghostscript C command API to bring its power to the Node.JS world. https://www.npmjs.com/package/ghostscript4js
您可以考虑使用Ghostscript4JS,它是一个将Ghostscript C命令API绑定到节点的模块。JS的世界。https://www.npmjs.com/package/ghostscript4js
Ghostscript4JS is a native Node.JS addon so you can call the C command Ghostscript API directly from your JavaScript code. In this way you have two benefits:
Ghostscript4JS是一个本地节点。JS addon可以直接从JavaScript代码调用C命令Ghostscript API。这样你有两个好处:
- Better error handling: You directly intercept the error through the try/catch for the sync method and then/catch promise for the async method.
- 更好的错误处理:通过尝试/捕获同步方法直接拦截错误,然后/catch承诺异步方法。
- Performance The call to the shell command takes more time and more resources than the call to the C or C++ API directly from Node.js environment.
- 与从节点直接调用C或c++ API相比,调用shell命令需要更多的时间和更多的资源。js的环境。
As programmer you have more control against call external tool.
作为程序员,您可以对调用外部工具进行更多的控制。
#1
0
The only way I got it to work, is by upgrading node from 10.26 to the latest 10.32, AND encapsulate the gs execution in a simple bash script file. Otherwise, even with the 10.32 node version, I still get the same error. I suspected an environment problem like suggest @Rudie,
我让它工作的唯一方法是将node从10.26升级到最新的10.32,并将gs执行封装在一个简单的bash脚本文件中。否则,即使使用10.32节点版本,我仍然会得到相同的错误。我怀疑环境问题,比如建议@Rudie,
#2
0
You could consider to use Ghostscript4JS, it's a module that binds the Ghostscript C command API to bring its power to the Node.JS world. https://www.npmjs.com/package/ghostscript4js
您可以考虑使用Ghostscript4JS,它是一个将Ghostscript C命令API绑定到节点的模块。JS的世界。https://www.npmjs.com/package/ghostscript4js
Ghostscript4JS is a native Node.JS addon so you can call the C command Ghostscript API directly from your JavaScript code. In this way you have two benefits:
Ghostscript4JS是一个本地节点。JS addon可以直接从JavaScript代码调用C命令Ghostscript API。这样你有两个好处:
- Better error handling: You directly intercept the error through the try/catch for the sync method and then/catch promise for the async method.
- 更好的错误处理:通过尝试/捕获同步方法直接拦截错误,然后/catch承诺异步方法。
- Performance The call to the shell command takes more time and more resources than the call to the C or C++ API directly from Node.js environment.
- 与从节点直接调用C或c++ API相比,调用shell命令需要更多的时间和更多的资源。js的环境。
As programmer you have more control against call external tool.
作为程序员,您可以对调用外部工具进行更多的控制。