node.js - AWS Lambda function - convert PDF to Image -


i developing application user can upload drawings in pdf format. uploaded files stored on s3. after uploading, files has converted images. purpose have created lambda function downloads file s3 /tmp folder in lambda execution environment , call ‘convert’ command imagemagick.

convert sourcefile.pdf targetfile.png

lambda runtime environment nodejs 4.3. memory set 128mb, timeout 30 sec.

now problem files converted while others failing following error:

{ [error: command failed: /bin/sh -c convert /tmp/sourcefile.pdf /tmp/targetfile.png convert: %s' (%d) "gs" -q -dquiet -dsafer -dbatch -dnopause -dnoprompt -dmaxbitmap=500000000 -daligntopixels=0 -dgridfittt=2 "-sdevice=pngalpha" -dtextalphabits=4 -dgraphicsalphabits=4 "-r72x72" "-soutputfile=/tmp/magick-qrh6nvlv--0000001" "-f/tmp/magick-b610l5uo" "-f/tmp/magick-tie1mjer" @ error/utility.c/systemcommand/1890. convert: postscript delegate failed/tmp/sourcefile.pdf': no such file or directory @ error/pdf.c/readpdfimage/678. convert: no images defined `/tmp/targetfile.png' @ error/convert.c/convertimagecommand/3046. ] killed: false, code: 1, signal: null, cmd: '/bin/sh -c convert /tmp/sourcefile.pdf /tmp/targetfile.png' }

at first did not understand why happens, tried convert problematic files on local ubuntu machine same command. output terminal:

**** warning: considering '0000000000 xxxxx n' free entry. **** file had errors repaired or ignored. **** file produced by: **** >>>> mac os x 10.10.5 quartz pdfcontext <<<< **** please notify author of software produced **** file not conform adobe's published pdf **** specification.

so message clear, file gets converted png anyway. if try convert source.pdf target.pdf , after convert target.pdf image.png, file repaired , converted without errors. doesn’t work lambda.

since same thing works on 1 environment not on other, best guess version of ghostscript problem. installed version on ami 8.70. on local machine ghostsript version 9.18.

my questions are:

  • is version of ghostscript problem? bug older version of ghostscript? if not, how can tell ghostscript (with or without using imagemagick) repair or ignore errors on local environment?
  • if old version problem, possible build ghostscript source, create nodejs module , use version of ghostscript instead 1 installed?
  • is there easier way convert pdf image without using imagemagick , ghostscript?

update relevant part of lambda code:

var exec = require('child_process').exec; var aws = require('aws-sdk'); var fs = require('fs'); ...  var localsourcefile = '/tmp/sourcefile.pdf'; var localtargetfile = '/tmp/targetfile.png';  var writestream = fs.createwritestream(localsourcefile); writestream.write(body); writestream.end();  writestream.on('error', function (err) {     console.log("error writing data s3 tmp folder.");     context.fail(err); });  writestream.on('finish', function () {     var cmd = 'convert ' + localsourcefile + ' ' + localtargetfile;      exec(cmd, function (err, stdout, stderr ) {          if (err) {             console.log("error executing convert command.");             context.fail(err);         }          if (stderr) {             console.log("command executed returned error.");             context.fail(stderr);         }else{             //file converted - something...         }     }); }); 

you can find compiled version of ghostscript lambda in following repository. should add files zip file uploading source code aws lambda.

https://github.com/sina-masnadi/lambda-ghostscript

this npm package call ghostscript functions:

https://github.com/sina-masnadi/node-gs

after copying compiled ghostscript files project , adding npm package, can use executablepath('path ghostscript') function point package compiled ghostscript files added earlier.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -