Convert PDF to Images using Ruby / JRuby

I'm looking for an easy way to generate previews for shortcuts created as PDF files. It would be great if I could convert these pdfs to images and show them to the user before actually printing / downloading.

The application is currently running in Jruby on rails, but it is possible that we are moving on to two applications, one running in native ruby ​​(main application) and one in jruby (pdf tool). So the choice is not limited to one of these worlds.

So far, I see the following options:

  • RMagick: seems mature, ImageMagick as backend may not work on jruby
  • RMagick4J: Like RMagick, but available for JRuby, looks inactive, only supports a subset.
  • Jmagick: JNI interface for Java, only works in JRuby

The PDF generation itself will always be in the Jruby environment, so JMagick sounds like a good idea (expect a possible JNI issue). On the other hand, it would be great if the solution was not limited to one plate.

Any ideas? Suggestions? Bad experience? Perhaps a completely different idea?

Thanks!

+1


a source to share


3 answers


Here's a very thin clean Ruby wrapper for ImageMagick. You can easily send any commands you want to use ImageMagick with some of the module methods:

www.misuse.org/science/2008/01/30/mojomagick-ruby-image-library-for-imagemagick/



It should work fine on jruby too, as there is no DLL magic or anything under the hood.

+1


a source


Are you on Linux?

sudo apt-get install imagemagick

      

In your ruby ​​code, do



system "convert -colorspace rgb -density 150 ../path/whatever.pdf ../path/whatever.jpg"

      

-density is the DPI of the final image

-colorspace fixes problems I had with colors looking

+2


a source


I am using IText for PDF generation / manipulation. This is Java jar but very easy to call from JRuby. If you combine it with the FlyingSaucer project , you can also create images. Both are very easy to use.

+1


a source







All Articles