Jared Martin
Jared Martin

Reputation: 653

How can I create thumbnails of pdfs with node.js and gm

I'm using meteor(which is built on node) and CollectionCFS(which allows me to use gm[GraphicsMagick] for thumb-nailing).

I do the following to have it automaticly create a thumbnail of uploaded images:

new FS.Store.FileSystem("thumbs", {
      transformWrite: function(fileObj, readStream, writeStream) {
        gm(readStream, fileObj.name()).resize('100', '100').stream().pipe(writeStream);
      },
      path: "/Volumes/Public/Thumbs",
    })

The transformWrite function receives the readStream(the original image), modifies it and pipes the results to the writeStream. How could I have it create thumbnails of PDF's?

Upvotes: 7

Views: 8485

Answers (1)

Firdaus Ramlan
Firdaus Ramlan

Reputation: 1146

If you just want the pdf's first page as thumbnail. do the following:

new FS.Store.FileSystem("thumbs", {
  transformWrite: function(fileObj, readStream, writeStream) {
    gm(readStream, fileObj.name() + '[0]').resize('100', '100').stream('png').pipe(writeStream);
  },
  beforeWrite: function (fileObj) {
    return {
      extension: 'png',
      type: 'image/png'
    };
  },
  path: "/Volumes/Public/Thumbs",
})

Upvotes: 4

Related Questions