首页 > 解决方案 > Firebase Cloud Functions-ImageMagick CLI PDF to images

问题描述

我正在尝试使用 Firebase Cloud Functions 和 ImageMagick,类似于缩略图演示的完成方式。通过重新调整演示脚本的用途,我想为ImageMagick 执行 CLI 命令以将 PDF 页面拆分为图像

转换-密度 150 演示文稿.pdf -质量 90 输出-%3d.jpg

片段

exports.splitPdfPages = functions.storage.object().onFinalize(async (object) => {
    const fileBucket = object.bucket; // The Storage bucket that contains the file.
    const filePath = object.name; // File path in the bucket.
    const contentType = object.contentType; // File content type.
    const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.

    // Download file from bucket.
    const bucket = admin.storage().bucket(fileBucket);
    const tempFilePath = path.join(os.tmpdir(), fileName);
    const tempSplitImagesPath = tempFilePath.replace(".png", "_%3d.png");

    await bucket.file(filePath).download({destination: tempFilePath});
    console.log('PDF downloaded locally to', tempFilePath);

    // Generate split page images using ImageMagick.
    await spawn('convert', ['-density', '150', tempFilePath, '-quality', '90', tempSplitImagesPath]);
    console.log('pages split images created at', tempFilePath);

    ...
    // Uploading the split images.
    ...

    // Once the thumbnail has been uploaded delete the local file to free up disk space.
    return fs.unlinkSync(tempFilePath);
});

不幸的是,我在 Cloud Functions 日志中遇到错误,指示语句错误

ChildProcessError:convert -density 150 /tmp/7eCxdDKqCb0rlYVw3AYf__foobar.pdf -quality 100 /tmp/7eCxdDKqCb0rlYVw3AYf__foobar_%3d.png失败,代码为 1

我搜索了错误的解决方案,但它仅表明空格是问题的主要原因(根据我的陈述没有任何问题)。调用 generateThumbnail 函数可以正常工作,所以我假设它基于我的更改

我是否缺少正确调用 ImageMagick 命令以将 PDF 页面转换为图像的内容?

期待您的来信。

标签: google-cloud-functionsimagemagickfirebase-storageimagemagick-convert

解决方案


推荐阅读