mooz / node-pdf-image

Provides an interface to convert PDF's pages to png files in Node.js by using ImageMagick
MIT License
237 stars 87 forks source link

Split every page in PDF? #10

Open kcollignon opened 9 years ago

kcollignon commented 9 years ago

I see that you have to pass the page number that you would like as an image, but it would be helpful if there is an option to split every page of a PDF to an image, rather than having to know the exact page number.

tarantindigital commented 8 years ago

Hi!

Try something like this:

var pdfImage = new pdf("file.pdf");
pdfImage.getInfo().then(function (info) {
    for(var i = 1; i <= info["Pages"]; i++){
        pdfImage.convertPage(i).then(function (imagePath) {
             fs.existsSync(i + ".png");
        });
    }
});
dustinc commented 8 years ago

I, too, would like this cooked in, but would adding this functionality using the existing convertPage get too heavy with a Promise for each page?

giiska commented 7 years ago

Would like a convertAllPages method too!

rbar2 commented 7 years ago

I too would like a convertAllPages method!

nareshtank001 commented 7 years ago

I too would like a convertAllPages method!

elipeters commented 6 years ago

+1

roest01 commented 6 years ago

Please update to Version 2.0.0 to use this Feature:

var PDFImage = require("pdf-image").PDFImage;

var pdfImage = new PDFImage("/tmp/slide.pdf");
pdfImage.convertFile().then(function (imagePaths) {
  // [ /tmp/slide-0.png, /tmp/slide-1.png ]
});
rainabba commented 5 years ago

Can anyone confirm this works? I can do my own gm convert commands in my container, but when I try to use this library, the call to convertFile() doesn't exit and I'm not seeing any extra CPU/memory activity for more than 1-2 seconds.

Maybe I'm still doing something wrong?

My working gm command

gm convert -verbose -density 150 ./test.pdf -quality 100 -type TrueColorMatte +adjoin test1/test1-%04d.png

My test code ( with 10,000 sec timeout )

lab.test('convert PDF to images', { timeout: 10000000 }, () => {
    return new Promise( (resolve, reject) => {
        const PDFImage = require('pdf-image').PDFImage;

        console.log('about to convert');
        let pdfImage = new PDFImage( pdfPath,
            {
                graphicsMagick: true,
                convertOptions: {
                '-density': '150',
                '-quality': '100',
                    '-depth': '8',
                    '-background': 'white',
                    '-flatten': null
                }
            }
        ).convertFile()
        .then( imagePaths => {
            console.log('done converting'); // <--- THIS IS NEVER HIT
            console.dir( imagePaths )
            expect( imagePaths ).to.be.a.array();
            resolve();
        })
        .catch( err => {
            console.error( { errorConverting: err } );
            expect( err ).to.be.a.null();
        });
        console.log(' OUT! '); // This is logged immediately
    });
});