0

I'm trying to convert an image to a PDF via a function on Firebase. I've tried using the example script to convert images to JPG using ImageMagick but the default convert jpg to PDF isnt working.

await spawn('convert', [tempLocalFile, tempLocalJPEGFile]);

Using the example I can convert to PNG etc fine but when I set the extension to be .PDF I get an error.

ChildProcessError: `convert /tmp/1610435415353.jpg /tmp/1610435415353.pdf` failed with code 1
    at ChildProcess.<anonymous> (/workspace/node_modules/child-process-promise/lib/index.js:132:23)
    at ChildProcess.emit (events.js:198:13)
    at ChildProcess.EventEmitter.emit (domain.js:466:23)
    at maybeClose (internal/child_process.js:982:16)
    at Socket.stream.socket.on (internal/child_process.js:389:11)
    at Socket.emit (events.js:198:13)
    at Socket.EventEmitter.emit (domain.js:466:23)
    at Pipe._handle.close (net.js:607:12) 
exports.imageToJPG = functions.storage.object().onFinalize(async (object) => {
    const filePath = object.name;
    const baseFileName = path.basename(filePath, path.extname(filePath));
    const fileDir = path.dirname(filePath);
    const JPEGFilePath = path.normalize(path.format({ dir: fileDir, name: baseFileName, ext: JPEG_EXTENSION }));
    const tempLocalFile = path.join(os.tmpdir(), filePath);
    const tempLocalDir = path.dirname(tempLocalFile);
    const tempLocalJPEGFile = path.join(os.tmpdir(), JPEGFilePath);

    // Exit if this is triggered on a file that is not an image.
    if (!object.contentType.startsWith('image/')) {
        console.log('This is not an image.');
        return null;
    }

    // Exit if the image is already a JPEG.
    if (object.contentType.startsWith('image/png')) {
        console.log('Already a png.');
        return null;
    }

    const bucket = admin.storage().bucket(object.bucket);
    // Create the temp directory where the storage file will be downloaded.
    await mkdirp(tempLocalDir);
    // Download file from bucket.
    await bucket.file(filePath).download({ destination: tempLocalFile });
    console.log('The file has been downloaded to', tempLocalFile);
    // Convert the image to JPEG using ImageMagick.
    await spawn('convert', [tempLocalFile, tempLocalJPEGFile]);
    console.log('JPEG image created at', tempLocalJPEGFile);
    // Uploading the JPEG image.
    await bucket.upload(tempLocalJPEGFile, { destination: JPEGFilePath });
    console.log('JPEG image uploaded to Storage at', JPEGFilePath);
    // Once the image has been converted delete the local files to free up disk space.
    fs.unlinkSync(tempLocalJPEGFile);
    fs.unlinkSync(tempLocalFile);
    return null;
});

thoughts?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Did you try the solutions you can find with a search like https://www.google.com/search?q=ChildProcessError%3A+failed+with+code+1? – Renaud Tarnec Jan 12 '21 at 08:20
  • 1
    Does this answer your question? [Cloud Functions for Firebase - Converting PDF to image](https://stackoverflow.com/questions/43242998/cloud-functions-for-firebase-converting-pdf-to-image) – Ralemos Jan 15 '21 at 16:47
  • I've seen the posts re going from PDF to an image but nothing about going the other way. – Wallstudios Jan 19 '21 at 01:29

0 Answers0