Skip to content

Instantly share code, notes, and snippets.

@aberba
Forked from jamilnyc/thumbnail.js
Created May 26, 2024 09:58
Show Gist options
  • Save aberba/4d860a8d55312a6ffc6320387332d8b4 to your computer and use it in GitHub Desktop.
Save aberba/4d860a8d55312a6ffc6320387332d8b4 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