aheckmann / gm

GraphicsMagick for node
http://aheckmann.github.com/gm/
6.95k stars 616 forks source link

Is it possible to flatten a pdf (and all its pages) without writing to image files? #736

Closed changavila closed 5 years ago

changavila commented 6 years ago

So i have a AWS lambda that gets the PDF buffer of the file from s3, and flattens the pdf to images, and write it back to the pdf the uploads it to S3. But with this, i can only process one page at a time. Is it possible to process all the pages at once then retrieve the all pages flattened in one PDF buffer?

This is my sample code snippet:

gm(material, 'material1.pdf')
    .density(216,154)
    .define('pdf:use-cropbox=true')
    .flatten()
    .toBuffer('PDF',function (err, buffer) {
        if (err) return handle(err);
        console.log('done!');

        s3Helper.upload(buffer, options);
    });
demsey2 commented 5 years ago

hi @changavila I have the same requirement, did you find any solution?

changavila commented 5 years ago

Hi @demsey2, This is possible by using .command('convert') then from this convert it toBuffer('PDF' , function(err,buffer). Then from buffer you can write this to file, or upload buffer to s3 in my case. You will have to use imagemagick here rather than using the graphicsmagick. To do this you must initialize graphicsmagick to: const gm = require('gm').subClass({ imageMagick: true });

demsey2 commented 5 years ago

thanks a lot

puneeth8994 commented 5 years ago

@changavila Off topic but can you help me understand how you got gm to work on lambda? I have added the binaries and pointed the appPath to it but I get the error message that "binaries can't be found"

changavila commented 5 years ago

Sorry for the very delayed reply.

@puneeth8994 I think you don't need to add the binaries, since the AWS lambda has already imageMagick in it.