gpt4 book ai didi

ruby-on-rails - 在 S3 上存储系统生成的 PDF

转载 作者:行者123 更新时间:2023-12-04 20:53:42 24 4
gpt4 key购买 nike

已解决,请参阅底部的编辑。

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

最佳答案

哦,我的错,你可以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

关于ruby-on-rails - 在 S3 上存储系统生成的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7269651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com