我可以使用初始化方法将大虾生成的pdf文件保存到profect文件夹吗?

Can I save a generated by prawn pdf file to the profect folder, using initialize method?

我有一个 OrderPdf class 继承自 Prawn::Document 这是 class 的内容:

class OrderPdf < Prawn::Document
  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::TagHelper

  def initialize(order)
    super(top_margin: 50, size: 12)
    @order = order
    logo
    # Other methods that generate pdf's content
    footer
  end

  def logo
    # Some stuff here
  end

  def footer
    #Some stuff here
  end
end

我这样称呼它:OrderPdf.new(@order)。 Pdf 文件已生成,但我需要的是将其保存到我的项目中,名称如下:"#{Rails.root}/public/uploads/orders/order-#{order.order_number}.pdf" 我知道,我可以这样做:Prawn::Document.generate("#{Rails.root}/public/uploads/orders/order-#{order.order_number}.pdf"),但我能做点什么吗与现有代码相似?先谢谢了。

您可以在初始化程序中调用render_file方法。

def initialize(order)
  super(top_margin: 50, size: 12)
  @order = order
  logo
  # Other methods that generate pdf's content
  footer
  render_file "#{Rails.root}/public/uploads/orders/order-#{order.order_number}.pdf"
end