gpt4 book ai didi

ruby-on-rails - 如何在 Rails 中的服务器中保存生成的 pdf

转载 作者:太空宇宙 更新时间:2023-11-03 18:16:50 24 4
gpt4 key购买 nike

目前我正在使用 prawn gem of rails 生成 pdf。当用户点击此操作时生成 pdf

def print_pdf
@user = User.find(params[:id])
@details = @user.details_data
respond_to do |format|
format.pdf do
pdf = PrintDetailsPdf.new(@user, view_context, @details)

send_data pdf.render, filename: "#{@user.id}.pdf",
type: 'application/pdf',
disposition: 'inline'
end
end
end

在上面的操作中,我生成了 pdf 并在浏览器中显示它,它运行良好。但我想在浏览器中显示 pdf,并将 pdf 保存在服务器的 public/user_details 目录中。我怎样才能做到这一点?

最佳答案

有一些很好的 gem 可以帮助文件上传和保存。 Here是这些 gem 的列表。 Paperclip 和 Carrierwave 是最受欢迎的选项。

您也可以从头开始实现。 Rails 具有内置的帮助程序,可以轻松推出您自己的解决方案。

pdf_content = *Content you want to save in the file*
File.open(Rails.root.join('public', 'user_details', filename), 'wb') do |file|
file.write(pdf_content.read)
end

这取决于您想要实现的复杂程度,但这对于简单的文件保存任务来说已经足够了。去here查找更多信息。

关于ruby-on-rails - 如何在 Rails 中的服务器中保存生成的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24778119/

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