【发布时间】:2017-07-31 18:06:12
【问题描述】:
在我的 Rails 应用程序中,我希望有一个特殊的路径来下载自定义 PDF。
此 PDF 应通过 PDFKit 从我的应用程序中的 ERB 模板生成。与其描述我想要实现的目标,不如粘贴一些不可执行但带有注释的代码:
class MyController < ApplicationController
def download_my_list_as_pdf
# The template uses the instance variables below
@user_id = params[:user_id]
@items = ['first_item', 'second_item']
# This line describes what I'd like to do but behaves not like I want ;)
# Render the ERB template and save the rendered html in a variable
# I'd also use another layout
rendered_html = render :download_my_list_as_pdf
kit = PDFKit.new(rendered_html, page_size: 'A4')
kit.to_pdf
pdf_file_path = "#{Rails.root}/public/my_list.pdf"
kit.to_file(pdf_file_path)
send_file pdf_file_path, type: 'application/pdf'
# This is the message I'd like to show at the end
# But using 'render' more than once is not allowed
render plain: 'Download complete'
end
end
我还没有找到这个问题的答案,任何帮助将不胜感激!
【问题讨论】:
标签: ruby-on-rails ruby pdfkit