Convert a .Doc or .Pdf to an Image and Display a Thumbnail in Ruby

Convert a .doc or .pdf to an image and display a thumbnail in Ruby?

A simple RMagick example to convert a PDF to a PNG would be:

require 'RMagick'
pdf ="doc.pdf")
thumb = pdf.scale(300, 300)
thumb.write "doc.png"

To convert a MS Word document, it won't be as easy. Your best option may be to first convert it to a PDF before generating the thumbnail. Your options for generating the PDF depend heavily on the OS you're running on. One might be to use OpenOffice and the Python Open Document Converter. There are also online conversion services you could try, including

Display a converted PDF to a rails page

To display the image, the browser only need a URL to the image. If you don't want to store the image on your HDD, you can encode the image into a data URL.

scaled_page = first_page.scale(300, 450)

# Set the image format to png so that we can quantize it to reduce its size

# Quantize the image
scaled_page = scaled_page.quantize

# A blob is just a binary string
blob = scaled_page.to_blob

# Base64 encode the blob and remove all line feeds
base64 = Base64.encode64(blob).tr("\n", "")

data_url = "data:image/png;base64,#{base64}"

# You need to find a way to send the data URL to the browser,
# e.g. `<%= image_tag data_url %>`

But I highly recommend that you persist the thumbnails on your HDD or better on a CDN because such images are hard to generate but are frequently accessed by the browsers. If you decided to do so, you need a strategy to generate unique URLs to those thumbnails and a way to associate the URLs to your PDF files.

How to convert PDF files to images using RMagick and Ruby

ImageMagick can do that with PDFs. Presumably RMagick can do it too, but I'm not familiar with it.

The code from the post you linked to:

require 'RMagick'
pdf ="doc.pdf")

pdf is an ImageList object, which according to the documentation delegates many of its methods to Array. You should be able to iterate over pdf and call write to write the individual images to files.

Generate a thumbnail for the first page of a PDF within a rails App?

The first answer from this question looks like it'll do what you want.

Gem Paperclip::Errors::NotIdentifiedByImageMagickError

Resizing images i.e. :styles => { :medium => "300x300>", :thumb => "345x215#" } requires imagemagick. Installing imagemagick should resolve the issue.

has_attached_file :media,
:styles => {
:medium => "300x300>",
:thumb => "345x215#",
:pdf_thumbnail => ["", :jpg]
:default_url => ActionController::Base.helpers.asset_path('branch-default.jpg')

validates :media, :attachment_content_type => { :content_type => ['image/png', 'image/jpg', 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document']}

Also, specifying pdf_thumbnail for pdf uploads as it will generate a thumbnail image although I think this feature requires GhostScript: Here is an excerpt from the paperclip github page:

If you are dealing with pdf uploads or running the test suite, you'll also need GhostScript to be installed.

Another useful link for other content type thumbnails:

Custom thumbnails for file types with Paperclip

Debugging JavaScript in Internet Explorer and Safari

For Safari you need to enable the "Develop" menu via Preferences (in Safari 3.1; see the entry in Apple's Safari development FAQ) or via

$ defaults write IncludeDebugMenu 1

at the terminal in Mac OS X. Then from the Develop menu choose Show Web Inspector and click on the Console link. Your script can write to the console using window.console.log.

For Internet Explorer, Visual Studio is really the best script debugger but the Microsoft Script Debugger is okay if you don't have Visual Studio. This post on the IE team blog walks you through installing it and connecting to Internet Explorer.

Internet Explorer 8 looks like it will have a very fancy script debugger, so if you're feeling really adventurous you could install the Internet Explorer 8 beta and give that a whirl.

Related Topics

Leave a reply