简体   繁体   English

PDF 到节点中的图像

[英]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:也许你可以试试这个兄弟:

  1. Create an uploads folder & copy the sample.pdf file which you want to convert as png.创建一个上传文件夹并复制要转换为 png 的 sample.pdf 文件。 since here I am only going to show you how to convert the pdf to png I am using the pdf as a static path you need to upload that pdf file via REST API.因为在这里我只会向您展示如何将 pdf 转换为 png 我正在使用 pdf 作为 static 路径您需要通过 REST API 上传该 pdf 文件。
  2. Now open your app.js OR index.js file & paste the below code we are using the pdf2pic packages to covert the pdf pages to png images.现在打开你的 app.js 或 index.js 文件并粘贴下面的代码,我们使用 pdf2pic 包将 pdf 页面转换为 png 图像。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM