5

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.

Serghei
  • 412
  • 6
  • 19

3 Answers3

5

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
alexandre-rousseau
  • 2,321
  • 26
  • 33
4
// for only first pdf page use:
gm(file, 'pdf.pdf[0]').toBuffer(...)
// for only second pdf page use:
gm(file, 'pdf.pdf[1]').toBuffer(...)
alexandre-rousseau
  • 2,321
  • 26
  • 33
Igor Vujović
  • 41
  • 1
  • 2
-2

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.

Or Guz
  • 1,018
  • 6
  • 13
  • I tried this library, but how to create this "spindrift" from buffer? – Serghei Feb 11 '15 at 12:53
  • Have you tried using your "file" variable with spindrift before using toBuffer? – Or Guz Feb 11 '15 at 12:58
  • Hm , I tried just without buffering (just opened and tried to save new file) and got error `spawn: pdftk /var/www//temp/tt.pdf output -` `events.js:72` `throw er; // Unhandled 'error' event`` ` ^` `Error: spawn ENOENT` ` at errnoException (child_process.js:1011:11)` ` at Process.ChildProcess._handle.onexit (child_process.js:802:34)` – Serghei Feb 11 '15 at 13:11
  • the double slash here are intended? /var/www"//"temp/tt.pdf – Or Guz Feb 11 '15 at 13:19
  • no i just removed the whole path from there, actually is like /var/www/temp/tt.pdf – Serghei Feb 11 '15 at 13:39
  • Have you tried var pdf = spindrift('/var/www/temp/tt.pdf')? – Or Guz Feb 11 '15 at 13:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70714/discussion-between-or-guz-and-user755669). – Or Guz Feb 11 '15 at 14:35