node-pdf-image icon indicating copy to clipboard operation
node-pdf-image copied to clipboard

Split every page in PDF?

Open kcollignon opened this issue 9 years ago • 8 comments

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.

kcollignon avatar Oct 28 '15 03:10 kcollignon

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");
        });
    }
});

tarantindigital avatar Nov 24 '15 03:11 tarantindigital

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?

dustinc avatar Feb 23 '16 21:02 dustinc

Would like a convertAllPages method too!

giiska avatar Jul 26 '17 03:07 giiska

I too would like a convertAllPages method!

rbar2 avatar Aug 16 '17 17:08 rbar2

I too would like a convertAllPages method!

nareshtank001 avatar Aug 23 '17 15:08 nareshtank001

+1

elipeters avatar Mar 09 '18 03:03 elipeters

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 ]
});

roest01 avatar May 07 '18 14:05 roest01

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
	});
});

rainabba avatar Feb 07 '19 23:02 rainabba