0

I'm working on an AWS Lambda application that needs to take a TIFF file and convert it to a PDF. I'm using ImageMagick exensively, so the easiest thing to do was: convert input.tif output.pdf. That works fine in my Mac environment, but fails in to convert to a true PDF in the Lambda environment.

The ImageMagick build on Lambda seems to not support PDFs. If I run convert -list format in the Lambda environment, there's no entry for PDF. Here's my test Lambda function:

const im = require('imagemagick');
const fs = require('fs');

exports.handler = (event, context, callback) => {
  var inputFileName = 'input.tif';
  var imagesPath = 'assets/images';
  var outputFile = '/tmp/output.pdf';


  var args = [
    imagesPath+'/'+inputFileName,
    '-format',
    'pdf',
    outputFile
  ];

  im.convert(args,
    function(err, stdout, stderr){
      if (err) throw err;
      console.log('stdout:', stdout);
      var imageRef = fs.readFileSync(outputFile);
      callback(null, {
        statusCode: 200,
        headers: {
          'Content-Type': 'application/pdf',
          'Content-Disposition': 'attachment; filename=output.pdf'
        },
        body: imageRef.toString('base64'),
        isBase64Encoded: true
      });
    });
}

When I run identify output.pdf (i.e. the downloaded file), the file is reported as a TIFF file:

/Users/myuser/Downloads/output.pdf TIFF 517x243 517x243+0+0 8-bit CMYK 1.1314MiB 0.000u 0:00.009

So ImageMagick seems to just be passing it through as a TIFF file.

I've tried using tiff2pdf - which is installed locally; not sure about Lambda - but that doesn't even work on my Mac. I get an error saying:

tiff2pdf: No support for /path/to/input.tif with 5 samples per pixel.
antun
  • 2,038
  • 2
  • 22
  • 34
  • You may need to install Ghostscript to handle PDF files. Also see https://stackoverflow.com/questions/52861946/imagemagick-not-authorized-to-convert-pdf-to-an-image/52863413#52863413 about modifying the policy.xml file to enable PDF processing. – fmw42 Jul 01 '19 at 16:30
  • I think the policy.xml is PHP specific, right? My question is about AWS Lambda environments. From what I've read, GhostScript is available in Lambda, but it's an old version, and has significant problems. – antun Jul 01 '19 at 16:42
  • policy.xml file is not PHP specific. It is imagemagick specific. If pertains to permission of all kinds within imagemagick and to PDF/PS/EPS files due to a security bug in Ghostscript. Sorry, I no experience with AWS. Another possibility is that you may need to modify the delegates.xml file to put the full path to Ghostscript, since AWS environment may be different from system environment PATh variables. – fmw42 Jul 01 '19 at 17:48
  • I see. However in a Lambda environment, you don't get to modify config files - you can upload code to be executed, but you can't adjust a global config file. (At least I don't know of any way to do so.) – antun Jul 01 '19 at 20:03
  • OK. I probably cannot help further. But do you have Ghostscript installed such that ImageMagick can recognize it. Do you see gs or gslib listed when you do `convert -version` or do you see PDF listed when you do `convert -list format`? If not, then you need to install Ghostscript into your ImageMagick. – fmw42 Jul 01 '19 at 21:32
  • If I do `convert -version`, there's very little output. If I do `convert -list configure`, --with-gslib is listed in the CONFIGURE line and gs is listed under DELEGATES. If I do `convert -list format`, PDF isn't mentioned in the output. I want to mention that my question wasn't about ImageMagick specifically - that's the first approach I tried because I was already using IM extensively, and it was easy on my local dev environment. I'm looking for the most reliable way to convert from TIFF->PDF on Lambda. – antun Jul 02 '19 at 06:47

0 Answers0