Решено, см. Редактирование внизу.
В моем приложении 3.1 Rails я создаю PDF, подобную этому:
def show
@contributor = Contributor.find(params[:id])
respond_to do |format|
format.pdf {
html = render_to_string(:action => "show.html.erb")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css"
thepdf = send_data kit.to_pdf, :filename => "blah.pdf", :type => 'application/pdf'
redirect_to :action => save_to_s3
}
end
, то я пытаюсь сохранить, что сгенерировал PDF на S3, загружая с помощью PaperClip:
def save_to_s3
html = render_to_string(:action => "show.html.erb")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css"
roy = Royarchive.new(:client_id => @contributor.client_id)
f = File.open("blah.pdf", 'w+')
f.write kit.to_pdf
roy.pdf = f
roy.save!
end
, но это дает мне »\ x9c «От Ascii-8bit до UTF-8
Как я могу использовать PaperClip для загрузки этого сгенерированного PDF в S3? Я использую Heroku, поэтому я не могу сохранить файл Temp на сервере, а затем загрузить его.
//// решено
О, мой плохой, вы могут хранить файлы в течение длительности сеанса в корне .
Так что это работает:
def show
@contributors = Contributor.where(:client_id => current_user.client_id).paginate(:page => params[:page])
@contributor = Contributor.where(:client_id => current_user.client_id).find(params[:id])
respond_to do |format|
format.html
format.pdf {
html = render_to_string(:action => "show.html.erb")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/unique/print.css"
send_data kit.to_pdf, :filename => "name.pdf", :type => 'application/pdf'
@thepdf = kit.to_file("#{Rails.root}/tmp/name.pdf")
roy = Royarchive.new(:client_id => @contributor.client_id)
roy.pdf = @thepdf
roy.save!
}
end
end