gpt4 book ai didi

ruby-on-rails - 将 Prawn 与 Rails 应用程序一起使用 : Send generated pdf file to user's downloads folder instead of app directory

转载 作者:行者123 更新时间:2023-12-04 02:17:47 25 4
gpt4 key购买 nike

在 Controller 中,我在其中一个操作中有以下行:

def some_action
@blog = Blog.find(11)
PdfSingleBlog.new(@blog).print
end

PDfSingleBlog 只是充当服务的 PORO 类。该服务的工作显然是生成此博客的 pdf,并且应该将该 pdf 发送到用户的下载文件夹。该代码位于此处:

#app/services/pdf_single_blog.rb
class PdfSingleBlog

def initialize(blog)
@blog = blog
end

def print
pdf = Prawn::Document.new
pdf.text "Blog Name: #{@blog.name}"
pdf.text "Description: #{@blog.description}"
pdf.render_file "blog_info.pdf"
end
end

pdf 已成功生成,但未将其发送到用户的下载文件夹。相反,它会将其保存到服务器上应用程序的目录中。

我如何实际将生成的 pdf 发送到用户的下载文件夹?

PDF of prawn manual found here


更新了以下答案:


def some_action
@blog = Blog.find(11)
blog_pdf = PdfSingleBlog.new(@blog).prepare_for_print
send_data blog_pdf.render, filename: "blog.pdf", type: "application/pdf"
end

#app/services/pdf_single_blog.rb
class PdfSingleBlog

attr_reader :blog
def initialize(blog)
@blog = blog
end

def prepare_for_print
pdf = Prawn::Document.new
pdf.text "Blog Name: #{@blog.name}"
pdf.text "Description: #{@blog.description}"
return pdf
end
end

最佳答案

虽然您现在正在生成文件,但实际上并没有将其发送到浏览器。虽然您可以使用 send_file 将本地生成的文件发送到浏览器,但这需要您生成文件并将其保存在本地。

因为这个文件是动态生成的,所以我建议使用 send_data 来避免保存文件的需要。

您可以通过进行以下修改来做到这一点

在你的 Controller 中:

def some_action
@blog = Blog.find(11)
send_data PdfSingleBlog.new(@blog).print_data, filename:'blog.pdf', type: "application/pdf", disposition: :attachment
end

在你的模型中:

class PdfSingleBlog

def print_data
pdf = Prawn::Document.new
pdf.text "Blog Name: #{@blog.name}"
pdf.text "Description: #{@blog.description}"
pdf.render
end
end

这会将 pdf 作为附件流式传输到浏览器,提示最终用户打开或下载文件。

关于ruby-on-rails - 将 Prawn 与 Rails 应用程序一起使用 : Send generated pdf file to user's downloads folder instead of app directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32954549/

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