2

I would download a PDF File in a web server. I use the Net::HTTP Ruby class.

def open_file(url)
  uri = URI.parse(url)

  http         = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Get.new(uri.path)
  request.basic_auth(self.class.user, self.class.password)

  http.request(request)
end

It works, I retrieve my PDF file, it's a string like : %PDF-1.3\n%\ ...

I have a method who return the result :

def file
  result = open_file(self.file_url)
  times  = 0

  if result.code == 404 && times <= 5
    sleep(1)
    times += 1
    file
  else
    result.body
  end
end

(It's a recursive method because that possible the file doesn't exist again on the server)

But when I would save this file with Paperclip, I have a error : Paperclip::AdapterRegistry::NoHandlerError (No handler found for "%PDF-1.3\n% ...

I tried manipulate the file with StringIO... without success :(.

Anyone have a idea ?

hypee
  • 718
  • 5
  • 20

1 Answers1

4

Assuming the PDF object you're getting is okay (I'm not 100% sure it is), then you could do this:

file = StringIO.new(attachment) #mimic a real upload file
file.class.class_eval { attr_accessor :original_filename, :content_type } #add attr's that paperclip needs
file.original_filename = "your_report.pdf"
file.content_type = "application/pdf"

then save the file with Paperclip.

(from "Save a Prawn PDF as a Paperclip attachment?")

Community
  • 1
  • 1
benjaminjosephw
  • 4,369
  • 3
  • 21
  • 40
  • I'm so confused ! I have already try with StringIO, I felt that it doesn't work, but now everything is ok. – hypee Dec 10 '13 at 13:47