0

For a rails app that works a lot with uploaded image heavy pdf files I'm looking for a way to optimize the file size of uploaded pdf's.

Adobe Acrobat has a 'save as reduced file size pdf' option which often halves the filesize when images are included.

I would like to do a similar action that is triggered after a file upload in my rails app.

Any ideas?

Michael Torfs
  • 828
  • 2
  • 9
  • 24
  • You cant solve this - PDF files can be "locked". No way of modifying them. This wont work for all the cases. – lzap Jul 20 '11 at 08:59

1 Answers1

0

While @lzap's comment may be true, if you still want to give it a shot, you might look at pdftk (PDF Toolkit). Its a library for manipulating and creating PDF files that looks like it offers the ability to compress a given pdf file.

The library can be installed on most major operating systems, so if you have the ability to install it on your host, then simply call:

system("pdftk uncompressed-input.pdf output compressed-outpu.pdf compress")

inside your rails app whenever you want to compress a particular PDF file. I have no idea how long this would take, and if you are compressing many PDF's at the same time, you may want to consider handing off to a background job (without this, Rails will wait until the compression is done before returning anything to the browser, probably causing a timeout error for long running groups of compression calls).

Also, if your file names come from user input, be extra careful to avoid injection attacks.

Community
  • 1
  • 1
Dave W.
  • 1,576
  • 2
  • 18
  • 29