Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 861
Jared Martin Avatar asked Sep 23 '14 13:09

Jared Martin


1 Answers

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",
})
like image 86
Firdaus Ramlan Avatar answered Oct 05 '22 12:10

Firdaus Ramlan