2013-12-10 2 views
2

웹 서버에 PDF 파일을 다운로드합니다. Net :: HTTP Ruby 클래스를 사용합니다. 그것은 작동Net :: HTTP PDF 파일을 가져와 클립으로 저장

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 

, 내 PDF 파일을 검색, 그건 캐릭터처럼 :

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 

(그것은 가능성이 있기 때문에 재귀 방법이다 : 결과를 반환 누가 내가하는 방법이 %PDF-1.3\n%\ ...

파일이 서버에 다시 존재하지 않음)

하지만이 파일을 Paperclip으로 저장할 때 오류가 있습니다. Paperclip::AdapterRegistry::NoHandlerError (No handler found for "%PDF-1.3\n% ...

StringIO ... 성공없이 파일을 조작하려고했습니다.

누구나 아이디어가 있으십니까?

답변

4

, 당신은이 작업을 수행 할 수있다 (나는 그것이 100 % 확실하지 않다) 당신이 받고있는 PDF 객체가 괜찮 가정 :

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" 

다음 종이 클립으로 파일을 저장합니다.

(부터 "Save a Prawn PDF as a Paperclip attachment?")

+0

나는 너무 혼란스러워! 이미 StringIO로 시도했지만 작동하지 않는다고 느꼈지만 이제는 모든 것이 정상입니다. – hypee

+1

듣기 좋다! – benjaminjosephw

관련 문제