Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress a pdf with node?

I have found:

  • scissors
  • pdfspin

They both rely on PDFTk, which costs around $1,000.

Where can I learn more about pdf compression with node, or find the already made library ?

like image 243
bigopon Avatar asked May 02 '17 00:05

bigopon


3 Answers

Using ghostscript4js:

gs.executeSync('gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=out.pdf in.pdf')

See this answer for various options.

Needs to have ghostscript installed on the host system though.

like image 190
phil294 Avatar answered Oct 26 '22 23:10

phil294


Try using shrinkpdf to compress your pdf file

And this post: How to run shell script file using nodejs?

like image 41
Chuong Tran Avatar answered Oct 27 '22 00:10

Chuong Tran


This can be easily achieved with ConvertAPI NodeJS library:

var convertapi = require('convertapi')('<YOUR SECRET HERE>');
convertapi.convert('compress', {
File: '/path/to/my_file.pdf'
}, 'pdf').then(function(result) {
     result.saveFiles('/path/to/dir');
});

The compression level can be adjusted by specifying the image compression algorithms and quality. You can also remove redundant objects from the PDF including fonts, bookmarks, annotations, forms, page labels, article threads, tagged information, page thumbnails, layers, duplicates, piece information dictionaries like Adobe Illustrator or Photoshop private data, etc.

You can find a complete list of compression parameters here: https://www.convertapi.com/pdf-to-compress and the auto-generated code snippet at the bottom of the page.

Disclaimer: I work for the vendor of the library.

like image 21
Kostelis Avatar answered Oct 27 '22 01:10

Kostelis