【问题标题】:How to spawn ghostcript into node.js environment如何在 node.js 环境中生成 ghostcript
【发布时间】:2014-10-05 19:21:45
【问题描述】:

我尝试了不同的配置,但我似乎无法在 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 目录对所有用户都是 +read+write。 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 执行未执行。任何提示都会有所帮助?

【问题讨论】:

  • 愚蠢的问题:如果您在 cmd 行上运行完全相同的 cmd 会发生什么? Unknown device: jpeg 似乎是一个 gs 错误(虽然是 stdout?)..
  • 不傻,我没有对那个测试发表评论,但是是的,它确实适用于 linux 行上的这个命令。
  • 有关信息,我将 -sOutputFile=afile.jpg 更改为 png 扩展名,而 -sDEVICE=png256 没有运气。相同的“未知设备”。
  • 也许节点以不同的用户、chroot 或沙箱的身份运行子进程?老实说,我不知道。也许人们知道的ghostscript。它是否适用于 cronjob 等?
  • @Rudie,node.js 代码以与用于在 shell 命令上测试成功的原始命令的用户 ID 相同的用户启动。

标签: javascript node.js process ghostscript spawn


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    Ghostscript4JS 是一个原生 Node.JS 插件,因此您可以直接从 JavaScript 代码调用 C 命令 Ghostscript API。这样您有两个好处:

    1. 更好的错误处理: 您通过同步方法的 try/catch 和异步方法的 then/catch 承诺直接拦截错误。
    2. 性能 与直接从 Node.js 环境调用 C 或 C++ API 相比,调用 shell 命令需要更多时间和更多资源。

    作为程序员,您可以更好地控制调用外部工具。

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 2014-12-15
      • 1970-01-01
      相关资源
      最近更新 更多