在 S3 上存储系统生成的 PDF
已解决,请参阅底部编辑。
在我的 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 上:
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,因此无法在服务器上保存临时文件然后上传。
////已解决
哦,我的错,你可以在会话期间将文件存储在根目录。
所以这有效:
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
Solved, see edit at bottom.
In my 3.1 rails app I'm generating a pdf like this:
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
Then I'm trying to store that generated PDF on S3 by uploading with 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
But that's giving me "\x9C" from ASCII-8BIT to UTF-8
How can I use Paperclip to upload this generated pdf to S3? I'm using Heroku so I can't save a temp file on the server then upload it.
////solved
Oh, my bad, you can store files for the duration of the session at root.
So this works:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哦,我的错,你可以在会话期间将文件存储在根目录。
所以这有效:
Oh, my bad, you can store files for the duration of the session at root.
So this works: