NJF
NJF

Reputation: 435

How to use paperclip to upload files other than images ...like pdf,docs

I am using paperclip to upload resume in my applyforjobs.Its working fine,i mean i can get browse button to upload files.But when i show the view page its not showing the pdf file that i upload,instead its showing just the name of the file. I also checked that folder which gets generated by default and that folder contains the pdfs which i uploaded during create aaction,which means upload thing is working fine.but i am not able to show that pdf.


Following is the code in my applyforjob.rb model for paperclip attachment :

has_attached_file :resume,:styles => { :small => "150x150>" }

Following is the code of applyforjobs/form.html.haml for uploading file :

= f.label :resume, "Upload resume".html_safe
= f.file_field :resume

Following is the code of applyforjobs/show.html.haml for showing file :

= image_tag @appliedusers.resume.url(:small)

but its not showing that pdf file.What am i supposed to write to upload and show files like pdf or docs?

Upvotes: 2

Views: 3317

Answers (2)

Chitra
Chitra

Reputation: 1404

@ , After saving the pdf you need to create a image which convert pdf to image to show on browser I did this like -

**self.save ##save pdf in paperclip

create_preview_image ##call this method

def create_preview_image

file_name = File.join(Rails.root, 'public',   self.document.url.split('?')[0]) if self.document

return unless file_name.present?

pdf = Magick::ImageList.new(file_name) 

return unless pdf.present?

png_file = Tempfile.new(["#{rand(9999).to_s}",'.png'])

pdf[0].write(png_file.path) do

    self.quality = 100

end

self.preview = png_file

self.save

end**

Upvotes: 0

oldhomemovie
oldhomemovie

Reputation: 15129

Since paperclip is a general purpose attachment uploading gem, as stated in it's Readme, it natively supports uploading files of any kind.

I suggest that you:

  1. remove :styles => { :small => "150x150>" } parameter from the has_attached_file
  2. write a paperclip callback which will generate a thumbnail image from pdf file
  3. define a method in model, say resume_thumbnail, which will be returning a path to generated file
  4. call the following in your view

    = image_tag @appliedusers.resume_thumbnail
    

No wonder image_tag @appliedusers.resume.url(:small) doesn't work for you: paperclip knows nothing about PDFs. paperclip blindly sees PDF file as a common file, and thus unable to do any kind of processing applicable to images only.

Upvotes: 2

Related Questions