6

Does anyone know how to upload a multi-page pdf with Paperclip and convert each page into a Jpeg?

So far, every time I upload a PDF, it only allows me to see the first page of the PDF as a JPEG. But I would like to be able to upload and convert every page from the PDF into a JPEG.

Is there any gem or plug-in that can help me upload a 10-pg PDF and Convert/Store in the Database as 10 JPEG files?

I have looked at docsplit-images gem, but I am not sure if that is the solution best solution or how it works.

Post.rb

class Post < ActiveRecord::Base
  belongs_to :Blogs

  attr_accessible :content, :title, :pdf

  has_attached_file :pdf,
                    :url  => "/assets/products/:id/:style/:basename.:extension",
                    :path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"

  validates_attachment_content_type :pdf,
      :content_type => [ 'application/pdf' ],
      :message => "only pdf files are allowed"
end

_form.html.erb

<%= form_for ([@post]), :html => { :multipart => true } do |f| %>

    <%= f.file_field :pdf %>

<% end %>

show.html.erb

  <%= image_tag @post.pdf.url(:original) %>
Serge Pedroza
  • 2,160
  • 3
  • 28
  • 41
  • When you say it only lets you see the first page, that means on re-downloading the PDF from the server, right? That behavior would surprise me, since I don't think Paperclip parses the PDF at all — it just directly copies the binary data to your server — so it'd be really strange for someone to have parsed the PDF and truncated it to one page somewhere during that process. – Matchu Jul 22 '12 at 03:41
  • 1
    @Matchu basically, i can see an image of the first page of the PDF in the browser. I want to be able to scroll through all ten pages. is there anyway to do this. – Serge Pedroza Jul 22 '12 at 03:45
  • What is the url: portion for? How is it different from :path? – Jwan622 Apr 24 '15 at 06:47

1 Answers1

6

Using an image tag for this makes no sense. Change your image_tag to a regular link and you'll be able to download and view all the pages.

<p>
  <%= link_to 'My PDF', @post.pdf.url %>
</p>
Peter Brown
  • 50,956
  • 18
  • 113
  • 146
  • I would like to be able to loop through either each pdf page to show in a gallery format. Or on upload, be able to convert the PDF into multiple Jpegs so I could then view them in a gallery format. – Serge Pedroza Dec 17 '14 at 22:14
  • 1
    @SurgePedroza this question is over 2 years old, you should open a new one rather than change it if you have new requirements. Otherwise it's less likely to be seen and answered. – Peter Brown Dec 18 '14 at 12:41