The Unknown Dev
The Unknown Dev

Reputation: 3118

Graphicsmagick for Node not writing to the correct file when converting PDF

I'm creating a thumbnail from the first page of a PDF with the Node gm module.

var fs = require('fs');
var gm = require('gm');

var writeStream = fs.createWriteStream("cover.jpg");

// Create JPG from page 0 of the PDF
gm("file.pdf[0]").setFormat("jpg").write(writeStream, function(error){
    if (!error) {
        console.log("Finished saving JPG");
    }
});

There's two problems with the script.

  1. It creates a file cover.jpg, but that file is empty (size 0) and can't be opened by any viewer.
  2. It creates a file named [object Object] that is an image of the PDF's first page (this is what I want, but the wrong name).

Aside from doing some additional file system manipulation to rename the [object Object] file after generating it, is there something I can change in the way I am using gm and fs in this script to write the image directly to the cover.jpg file?

This question is similar to what I am asking, but there is no accepted working answer and I need to install yet another library to use it (undesirable).

Upvotes: 0

Views: 978

Answers (1)

Terseus
Terseus

Reputation: 2212

write receives the file path as the first argument, not a write stream, therefore the method is converting the stream object into its string representation, that's why it saves a file named [object Object].

You can just use .write("cover.jpg"), or if you want to use a write stream, you may use .stream().pipe(writeStream).

Take a look at the stream examples of gm.

Upvotes: 1

Related Questions