如何在使用表单提交时下载prawn pdf文件?

时间:2010-09-30 07:29:32

标签: ruby-on-rails prawn

def index
    @forms = Form.all
    respond_to do |format|
      format.html
    end
  end
    def submit
        respond_to do |format|
        format.pdf {render  }

    end
    end

submit.pdf.prawn file
pdf.text "successfully submitted"

2 个答案:

答案 0 :(得分:7)

Prawn可以生成并返回PDF而无需在本地保存...

def submit
  send_data(generate_pdf, :filename => "output.pdf", :type => "application/pdf") 
end

private 
def generate_pdf
    Prawn::Document.new do
        text "Hello Stackoverflow"
    end.render 
end

答案 1 :(得分:0)

使用prawn生成保存在/ public文件夹中某处的pdf,如:

Prawn::Document.generate("#{RAILS_ROOT}/public/pdfs/myfile.pdf")

然后只需使用您的控制器将用户重定向到该位置:

redirect_to '/pdfs/myfile.pdf'