Search code examples
javascriptnode.jsgraphicsmagick

Unable to create PDF with Graphics Magick in Node


I have a Node application, within which I am using Graphics Magick to do some image/pdf manipulation.

I have the following code which calls mosaic() to combine a pdf and png. If I export the result as a png then the process is successful. However if I try to export the result as a pdf then the resulting pdf file does have a size, but opening it shows that there is nothing to see, it appears to be blank. No errors are thrown.

var newFileName = "result.pdf";
gm()
  .in('-page', '+0+0')
  .in('C:\\Code\\ProjectName\\src\\api\\test\\TestTemplatePDF.pdf')
  .in('-page', '+103+70')
  .in('C:\\Code\\ProjectName\\src\\api\\test\\pic1.png')
  .mosaic()
  .stream('pdf', (err, stdout, stderr) => {
    if (err) console.log('stream error', err);
    console.log('stream');

    var writeStream = fs.createWriteStream('./etc/' + newFileName);
    stdout.pipe(writeStream);

    stderr.on('end', () => {
      fs.readFile('./etc/streamError.txt', (err, data) => {
        console.log('reading errorStream');
        // if (err) console.error(err);
        if (err) {
          console.log('We found an error reading streamError.txt', err);
          res.send(err);
        } else if (data.length !== 0) {
          console.log('streamError.txt should contain a detailed error message', data);
          res.send(data);
        } else {
          console.log('streamError.txt contains no errors');
        }
      });
    });

    stdout.on('end', () => {
      fs.readFile('./etc/' + newFileName, (err, data) => {
        if (err) {
          console.log("stdout error: " + err);
          res.end();
        } else {
          console.log('Successfully read our new image file');         
        }
      })
    })
  })

Output/console shows:

stream
reading errorStream
streamError.txt contains no errors
successfully read our new file

Solution

  • In the end this problem went away when I converted the pdf to a png before editing. Presumably the conclusion is that when using mosaic() that they need to be the same type.