[英]PDF to Image in node
I am using node-express and need to convert pdf into image in the server side.我正在使用 node-express,需要在服务器端将 pdf 转换为图像。
Pdf-poppler is not working in linux and pdf-image happens to be not working as well. pdf-poppler 在 linux 中不工作,而 pdf-image 恰好也不工作。 Any alternate method how we can convert and save pdf to image in the backend.
我们可以使用任何替代方法将 pdf 转换并保存为后端图像。
may be you can try this bro:也许你可以试试这个兄弟:
or you visit this link: https://codinghub.medium.com/how-to-convert-pdf-file-pages-to-png-images-node.js-dccec010bf13或者您访问此链接: https://codinghub.medium.com/how-to-convert-pdf-file-pages-to-png-images-node.js-dccec010bf13
Pdf-poppler use pdftocairo
command provided by the poppler project. pdf-poppler 使用 poppler 项目提供的
pdftocairo
命令。 Since poppler did support Linux, you can install it by yourself.由于poppler确实支持Linux,所以可以自行安装。
For example, on ubuntu例如,在 ubuntu
apt-get install poppler-utils
then call pdftocairo command using the code below from Pdf-poppler.然后使用 Pdf-poppler 中的以下代码调用 pdftocairo 命令。
const path = require('path');
const {execFile} = require('child_process');
const pdftocairoBin = '/usr/bin/pdftocairo';
const FORMATS = ['png', 'jpeg', 'tiff', 'pdf', 'ps', 'eps', 'svg'];
let defaultOptions = {
format: 'jpeg',
scale: 1024,
out_dir: null,
out_prefix: null,
page: null
};
function pdf2image(file, opts) {
return new Promise((resolve, reject) => {
opts.format = FORMATS.includes(opts.format) ? opts.format : defaultOptions.format;
opts.scale = opts.scale || defaultOptions.scale;
opts.out_dir = opts.out_dir || defaultOptions.out_dir;
opts.out_prefix = opts.out_prefix || path.dirname(file);
opts.out_prefix = opts.out_prefix || path.basename(file, path.extname(file));
opts.page = opts.page || defaultOptions.page;
let args = [];
args.push([`-${opts.format}`]);
if (opts.page) {
args.push(['-f']);
args.push([parseInt(opts.page)]);
args.push(['-l']);
args.push([parseInt(opts.page)]);
}
if (opts.scale) {
args.push(['-scale-to']);
args.push([parseInt(opts.scale)]);
}
args.push(`${file}`);
args.push(`${path.join(opts.out_dir, opts.out_prefix)}`);
execFile(pdftocairoBin, args, {
encoding: 'utf8',
maxBuffer: 5000*1024,
shell: false
}, (err, stdout, stderr) => {
if (err) {
reject(err);
}
else {
resolve(stdout);
}
});
});
};
Usage of pdf2image function pdf2image function的使用
pdf2image('./input.pdf', {out_dir:'./', out_prefix:'ppp'});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.