我正在尝试编写一个 Azure 函数来在 Node.js 中将 pdf 转换为图像,但没有成功。直接在azure门户中编写。使用开箱即用的 pdf-poppler 包。这里 sourcepdf 和 targetimage 是我的 blob 容器。
下面是代码
const pdf = require('pdf-poppler');
const path = require('path');
const fs = require('fs');
const URL = require('url');
const storage = require('azure-storage');
module.exports = async function (context, myBlob) {
context.log(context.bindingData.blobTrigger);
//context.log(context.bindingData.uri);
let file = '/sourcepdf/sample.pdf';
let opts = {
format: 'jpeg',
out_dir: '/targetimage/sample.jpg',
out_prefix: path.baseName(file, path.extname(file)),
page: null
}
pdf.convert(file, opts)
.then(res => {
console.log('Successfully converted');
})
.catch(error => {
console.error(error);
})
//context.log("JavaScript blob trigger function processed blob \n Blob:", context.bindingData.blobTrigger, "\n Blob Size:", myBlob.length, "Bytes");
};
任何建议,
最佳答案
以下是我的工作代码:
context.log('JavaScript HTTP trigger function processed a request.');
let file = 'D:\\home\\site\\wwwroot\\nodejs.pdf'
let opts = {
format: 'jpeg',
out_dir: path.dirname(file),
out_prefix: path.basename(file, path.extname(file)),
page: null
}
pdf.convert(file, opts)
.then(res => {
console.log('Successfully converted');
})
.catch(error => {
console.error(error);
})
除此之外,您还可以定义输出目录和文件名前缀,例如 out_dir
可以是 context.executionContext.functionDirectory
和 out_prefix
> 只是像 output
这样的字符串。它将在函数文件夹下创建图像。
关于javascript - 在node.js中将pdf转换为图像的azure函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61749912/