Skip to content

Instantly share code, notes, and snippets.

@jfensign
Created February 7, 2014 05:15
Show Gist options
  • Save jfensign/8857639 to your computer and use it in GitHub Desktop.
Save jfensign/8857639 to your computer and use it in GitHub Desktop.
Simple document thumbailer script. Packages unoconv and gs (GhostScript) are required.
Thumbnailer = ->
Thumbnailer = this
fs = require "fs"
cp = require "child_process"
path = require "path"
exec = cp.exec
enctype = "binary"
###
doc_to_image opts, cb
opts =
file_path: "/location/of/file/to/convert" #required
write_path
###
@doc_to_image: (opts, cb) ->
filename = opts.file_path.replace path.extname opts.file_path, ".#{opts.convert_to}"
is_pdf = path.extname opts.file_path is "pdf"
commands =
un: "unoconv --stdout #{opts.file_path} | "
gs: "gs -q -dNOPAUSE -dBATCH \
-dPDFFitPage -sDEVICE=#{opts.convert_to} \
-r144 -sOutputFile=%stdout \
#{if is_pdf then opts.file_path else '-_'}"
writable = fs.createWriteStream path.resolve(opts.write_to, filename),
flags: "w"
encoding: enctype
mode: 0o0777
child_process = exec "#{commands.un}#{commands.gs}",
encoding: enctype
maxBuffer: 5000 * 1024
writable.on "close", -> cb null, filename, writable
child_process.stdout.on "data", (chunk) -> writable.write chunk, enctype
child_process.stdout.on "close", -> writable.close()
child_process.stderr.on "data", (d) -> console.log d
module.exports = new Thumbnailer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment