如何将ghostcript生成到node.js环境中

时间:2014-10-05 19:21:45

标签: javascript node.js process ghostscript spawn

我尝试了不同的配置,但似乎无法在node.js环境中成功执行“gs”(Ghostscript)。

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 );
    } );

---输出---------------------------------------- -----------------

stdout: Unknown device: jpeg 

stdout: Unrecoverable error: undefined
stdout:  in .uninstallpagedevice

stdout: Operand stack:
    defaultdevice
stdout: 

child process exited with code 1

-------------------------------------------- -----------------------

/mnt/drive目录为所有用户+读+写。 gs -help执行返回:

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:/# 

设备 jpeg 可用。 gs执行未执行。任何提示都会有帮助吗?

2 个答案:

答案 0 :(得分:0)

我让它工作的唯一方法是将节点从10.26升级到最新的10.32,并将gs执行封装在一个简单的bash脚本文件中。否则,即使使用10.32节点版本,我仍然会得到相同的错误。我怀疑像@Rudie,

这样的环境问题

答案 1 :(得分:0)

你可以考虑使用Ghostscript4JS,它是一个绑定Ghostscript C命令API的模块,可以将其强大的功能带到Node.JS世界。 https://www.npmjs.com/package/ghostscript4js

Ghostscript4JS是一个本机Node.JS插件,因此您可以直接从JavaScript代码调用C命令Ghostscript API。通过这种方式,您可以获得两个好处:

  1. 更好的错误处理: 您可以通过try / catch为sync方法直接拦截错误,然后通过/ catch方法拦截异步方法。
  2. 性能 与直接从Node.js环境调用C或C ++ API相比,对shell命令的调用需要更多的时间和更多的资源。
  3. 作为程序员,您可以更好地控制调用外部工具。