-
-
Save aberba/4d860a8d55312a6ffc6320387332d8b4 to your computer and use it in GitHub Desktop.
Create a thumbnail from a PDF in Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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