Skip to content

Instantly share code, notes, and snippets.

@jamilnyc
Created July 23, 2016 03:55
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamilnyc/71bb717c95835bcc1d848b5158e90abb to your computer and use it in GitHub Desktop.
Save jamilnyc/71bb717c95835bcc1d848b5158e90abb to your computer and use it in GitHub Desktop.
Create a thumbnail from a PDF in Node
var gm = require('gm');
// Create JPG from page 0 of the PDF
gm("file.pdf[0]") // The name of your pdf
.setFormat("jpg")
.resize(200) // Resize to fixed 200px width, maintaining aspect ratio
.quality(75) // Quality from 0 to 100
.write("/tmp/cover.jpg", function(error){
// Callback function executed when finished
if (!error) {
console.log("Finished saving JPG");
} else {
console.log("There was an error!", error);
}
});
// Create a PNG using thumbnail function
gm("file.pdf[0]")
.thumb(
200, // Width
200, // Height
'/tmp/thumbnail.png', // Output file name
80, // Quality from 0 to 100
function (error, stdout, stderr, command) {
if (!error) {
console.log(command);
} else {
console.log(error);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment