Search code examples
javascriptnode.jspdfmeteorgraphicsmagick

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?


Solution

  • 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",
    })