Node.js 如何使用node imagemagick从缓冲区转换pdf
我想使用imagemagick从包含pdf(不是pdf文件,所以我不必先保存pdf并运行imagemagick)的URL获取的缓冲区转换pdf文件。我使用节点imagemagick,而在docs中只是显示了convert()方法,其文件路径如下:(kittens.jpg)Node.js 如何使用node imagemagick从缓冲区转换pdf,node.js,pdf,imagemagick,Node.js,Pdf,Imagemagick,我想使用imagemagick从包含pdf(不是pdf文件,所以我不必先保存pdf并运行imagemagick)的URL获取的缓冲区转换pdf文件。我使用节点imagemagick,而在docs中只是显示了convert()方法,其文件路径如下:(kittens.jpg) im.convert(['kittens.jpg','-resize','25x120','kittens small.jpg',], 函数(错误、标准输出){ 如果(错误)抛出错误; log('stdout:',stdout
im.convert(['kittens.jpg','-resize','25x120','kittens small.jpg',],
函数(错误、标准输出){
如果(错误)抛出错误;
log('stdout:',stdout);
});
但如果我想使用缓冲区,我该怎么做呢?这是我的密码
request.get({ url: 'url to pdf', encoding: null }, (err, resp, body) => {
im.convert([body, '-resize', '25x120', 'kittens-small.jpg'],
function(err, stdout){
if (err) throw err;
console.log('stdout:', stdout);
});
})
您可以检查节点imagemagik的源代码: 节点imagemagik是
convert
工具的包装器
convert函数的定义如下
exports.convert = function(args, timeout, callback) {
var procopt = {encoding: 'binary'};
if (typeof timeout === 'function') {
callback = timeout;
timeout = 0;
} else if (typeof timeout !== 'number') {
timeout = 0;
}
if (timeout && (timeout = parseInt(timeout)) > 0 && !isNaN(timeout))
procopt.timeout = timeout;
return exec2(exports.convert.path, args, procopt, callback);
}
exports.convert.path = 'convert';
它假定您提供与命令行中相同的参数,即源映像路径。但是,convert
支持stdin的输入,这就是您将pdf数据提供给流程的方式
在源代码中,有一个有用的示例。resize函数的定义,它接受二进制数据,并使用适当的参数将其输入convert函数
var resizeCall = function(t, callback) {
var proc = exports.convert(t.args, t.opt.timeout, callback);
if (t.opt.srcPath.match(/-$/)) {
if ('string' === typeof t.opt.srcData) {
proc.stdin.setEncoding('binary');
proc.stdin.write(t.opt.srcData, 'binary');
proc.stdin.end();
} else {
proc.stdin.end(t.opt.srcData);
}
}
return proc;
}
exports.resize = function(options, callback) {
var t = exports.resizeArgs(options);
return resizeCall(t, callback)
}
调用convert
将输入文件名替换为“-”。终端的等效用法如下所示:
my_process_that_outputs_pdf | convert - <convertion options here...> my_output.png
my_process_输出的_pdf | convert-my_output.png
您可以查看节点imagemagik:的源代码
节点imagemagik是convert
工具的包装器
convert函数的定义如下
exports.convert = function(args, timeout, callback) {
var procopt = {encoding: 'binary'};
if (typeof timeout === 'function') {
callback = timeout;
timeout = 0;
} else if (typeof timeout !== 'number') {
timeout = 0;
}
if (timeout && (timeout = parseInt(timeout)) > 0 && !isNaN(timeout))
procopt.timeout = timeout;
return exec2(exports.convert.path, args, procopt, callback);
}
exports.convert.path = 'convert';
它假定您提供与命令行中相同的参数,即源映像路径。但是,convert
支持stdin的输入,这就是您将pdf数据提供给流程的方式
在源代码中,有一个有用的示例。resize函数的定义,它接受二进制数据,并使用适当的参数将其输入convert函数
var resizeCall = function(t, callback) {
var proc = exports.convert(t.args, t.opt.timeout, callback);
if (t.opt.srcPath.match(/-$/)) {
if ('string' === typeof t.opt.srcData) {
proc.stdin.setEncoding('binary');
proc.stdin.write(t.opt.srcData, 'binary');
proc.stdin.end();
} else {
proc.stdin.end(t.opt.srcData);
}
}
return proc;
}
exports.resize = function(options, callback) {
var t = exports.resizeArgs(options);
return resizeCall(t, callback)
}
调用convert
将输入文件名替换为“-”。终端的等效用法如下所示:
my_process_that_outputs_pdf | convert - <convertion options here...> my_output.png
my_process_输出的_pdf | convert-my_output.png