1

我正在构建一个 Azure 函数,该函数在将 pdf 上传到 blob 存储时触发,然后将 pdf 转换为 jpg 文件并将其保存到另一个存储中,我看到该库pdf2pic擅长 pdf 转换,我安装了 GraphicsMagik、Ghostscript 和gm包来自 npm。我收到此错误:

[2021-11-10T17:31:30.919Z] ERROR: Error: write EOF
[2021-11-10T17:31:30.922Z]     at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:94:16) {
[2021-11-10T17:31:30.926Z]   errno: -4095,
[2021-11-10T17:31:30.928Z]   code: 'EOF',
[2021-11-10T17:31:30.929Z]   syscall: 'write'
[2021-11-10T17:31:30.930Z] }

这是我的代码:

const pdf2pic = require('pdf2pic');
const gm = require('gm').subClass({ imageMagick: true });

module.exports = async function (context, pdfBuffer) {
    try {
        if (context.bindingData.blobTrigger.toLowerCase().endsWith('.pdf')) {
            const options = {
                saveFilename: "tmp-file",
                savePath: "./tmp/",
                format: "jpg",
                density: 100,
                width: 600,
                height: 600
            };
            context.log('Converting pdf to jpg...');
            const convert = pdf2pic.fromBuffer(pdfBuffer, options);
            convert.bulk(-1, false).then(output => console.log('OUTPUT:', output)).catch(err => console.log('ERROR:',err));
        }
        else {
            throw new Error('The uploaded file is not in PDF format')
        }
    }
    catch (error) {
        context.log("ERROR CONVERTING PDF:", error);
        return;
    }
};

我正在使用 fromBuffer ,因为该函数正在接收 pdf 的缓冲区。我不确定我是否正确实施了图书馆,有人可以帮助我吗?

4

0 回答 0