Search code examples
javascriptnode.jsprocessghostscriptspawn

How to spawn ghostcript into node.js environment


I have tried different configuration, but I cannot seem to succeed to execute "gs" (Ghostscript) in a node.js environment.

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:

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 ?


Solution

  • 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,