1

This firebase function should take a pdf in /test/testfile.pdf, convert it to grey and save it somewhere. I want to use this function in a more complicated process, but the exec('convert') is really not helping me.

The issue is the 'exec' command keeps failing. In the shell, the exact command line you see here is working:

convert -colorspace GRAY -density 300 test/testfile.pdf /tmp/out.pdf

The error in the logs is this:

{ ChildProcessError: Command failed: convert -colorspace GRAY -density 300 test/testfile.pdf /tmp/out.pdf convert: no images defined `/tmp/out.pdf' @ error/convert.c/ConvertImageCommand/3210. `convert -colorspace GRAY -density 300 test/testfile.pdf /tmp/out.pdf\` (exited with error code 1) at callback (/user_code/node_modules/child-process-promise/lib/index.js:33:27) at ChildProcess.exithandler (child_process.js:205:5) at emitTwo (events.js:106:13) at ChildProcess.emit (events.js:191:7) at maybeClose (internal/child_process.js:920:16) at Process.ChildProcess._handle.onexit (internal/child_process.js:230:5) name: 'ChildProcessError', code: 1, childProcess: { ChildProcess: { [Function: ChildProcess] super_: [Object] }, fork: [Function], _forkChild: [Function], exec: [Function], execFile: [Function], spawn: [Function], spawnSync: [Function: spawnSync], execFileSync: [Function: execFileSync], execSync: [Function: execSync] }, stdout: '', stderr: 'convert: no images defined `/tmp/out.pdf\' @ error/convert.c/ConvertImageCommand/3210.\n' }

This is the function:

const functions = require('firebase-functions');
const rp = require('request-promise');
const request = require('request');
const baseURL = "https://www.google.com/cloudprint/"

const exec = require('child-process-promise').exec;
const mkdirp = require('mkdirp-promise');
const path = require('path');
const os = require('os');
const fs = require('fs');

exports.convertPDF = functions.https.onRequest((req, res) => {

  const tempLocalThumbFile = path.join(os.tmpdir(), "out.pdf");

  try {
   let tempLocalFile  = "test/testfile.pdf"
   exec('convert -colorspace GRAY -density 300 test/testfile.pdf '+tempLocalThumbFile).then((a) => {

      console.log('Conversion created at', tempLocalThumbFile);

  }, function (err) { 
      console.log(err)
  })
  } catch(err) {
      console.log(err)
  }
})

I am pretty stuck. How to get this convert to work in Firebase Functions?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Riël
  • 1,251
  • 1
  • 16
  • 31
  • It looks like there is a backslash in your output file name `/tmp/out.pdf\\`. That exact string, with the trailing backslash, appears twice in your log. I imagine you have to figure out where that's coming from. – Doug Stevenson Feb 15 '18 at 14:50
  • Likely the same issue as this: https://superuser.com/questions/819277/cant-convert-pdf-into-image-because-of-no-images-defined-error it’s likely that Cloud Functions instances are missing ghostscript which is required to convert pdfs. – Nicolas Garnier Feb 15 '18 at 15:16
  • Could be. I try to find out if that is the issue. – Riël Feb 15 '18 at 22:52
  • Possible duplicate of [Cloud Functions for Firebase - Converting PDF to image](https://stackoverflow.com/questions/43242998/cloud-functions-for-firebase-converting-pdf-to-image) – VictorGGl Feb 16 '18 at 14:58

1 Answers1

0

Actually the problem is what has been stated by @Nivco: Cloud Functions are missing the ghostscript package. There is a feature request already asking for making ghostscript package available. You can go to the link and click on the start icon to get email notifications when there are some news.

There is another StackoverFlow thread where it is mentioned a workaround consisting on getting gs binaries on your own.

VictorGGl
  • 1,848
  • 10
  • 15