Serghei
Serghei

Reputation: 422

How to convert selected pdf page with gm

I am converting different images and pdf files with "gm" module for nodejs. Image types go successfully but when I want to convert PDF to image have problems. I need to covert only one selected page from pdf file to jpg/png. If I pass whole pdf file to "gm" it saves to image only first page, but I cannot find the way to save another page.

gm(file).toBuffer(format.toUpperCase(), 
       function (err, buffer) {
    // so in buffer now we have converted image
 }

Thank you.

Upvotes: 5

Views: 6082

Answers (3)

Igor Vujović
Igor Vujović

Reputation: 41

// for only first pdf page use:
gm(file, 'pdf.pdf[0]').toBuffer(...)
// for only second pdf page use:
gm(file, 'pdf.pdf[1]').toBuffer(...)

Upvotes: 4

Thomsheer Ahamed
Thomsheer Ahamed

Reputation: 183

You can use gm.selectFrame like this

gm(file).selectFrame(0).toBuffer() // To get first page
gm(file).selectFrame(1).toBuffer() // To get second page

Upvotes: 5

Or Guz
Or Guz

Reputation: 1018

There is spindrift for manipulating pdf (includes image conversion).

You can define your pdf using (You don't have you use all of the commands):

var pdf = spindrift('in.pdf')
   .pages(7, 24)
   .page(1)
   .even()
   .odd()
   .rotate(90)
   .compress()
   .uncompress()
   .crop(100, 100, 300, 200) // left, bottom, right, top 

Later on convert to image:

// Use the 'index' property of an image element to extract an image: 
pdf.extractImageStream(0)

If you have to use gm, you can do what @Ben Fortune suggested in his comment and split the pdf first.

Upvotes: -2

Related Questions