Convert several images to a multi-page pdf file with GraphicsMagick and Node.js

I'm using this function to convert 2 pages from a multi-page TIFF file to a multi-page PDF file.

var writeStream = fs.createWriteStream('./docs/multipage1.pdf');

gm().command('convert')
    .in('./docs/' + data.nomefile + '[1]')
    .in('./docs/' + data.nomefile + '[13]')
    .density(150, 150).compress('jpeg')
    .stream('pdf', function(err, stdout, stderr) {

    if (!err) {

        stdout.pipe(writeStream)

    } else {

        console.log(err)

    }
})

I would like to change it to make it work with a dynamic page selection:

var pages = req.query.pages;
pages = JSON.parse(pages)  // array of page number [0,1,5,18]
for (page in pages) {
 data.nomefile+'['+pages[page]+']'
 }
...............

Answers:

Answer

It's no problem to add input parameters in the loop:

var comm = gm().command('convert')

for (page in pages) {
  comm.in('./docs'+ data.nomefile+'['+pages[page]+']')
}

comm.density(150, 150).compress('jpeg')
    .stream('pdf', function(err, stdout, stderr) {

    if (!err) {

        stdout.pipe(writeStream)

    } else {

        console.log(err)

    }
})

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.