在S3上存储系统生成的PDF

时间:2011-09-01 11:28:52

标签: ruby-on-rails heroku amazon-s3 pdfkit

解决,请参见底部的编辑。


在我的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

然后我试图通过使用Paperclip上传将生成的PDF存储在S3上:

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" from ASCII-8BIT to UTF-8

如何使用Paperclip将生成的pdf上传到S3?我正在使用Heroku所以我无法在服务器上保存临时文件然后上传它。

////解决

哦,我的坏,你可以 store files for the duration of the session at root

这样可行:

   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

1 个答案:

答案 0 :(得分:3)

哦,我的坏,你可以 store files for the duration of the session at root

这样可行:

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" 
  thepdf = kit.to_file("#{Rails.root}/tmp/name.pdf")

     roy = Royarchive.new(:client_id => @contributor.client_id) 
     roy.pdf = thepdf
     roy.save!
     redirect_to :action => index
end