【发布时间】:2018-12-04 16:29:08
【问题描述】:
我有一个模板要由用户填写。我的目标是使用 wicked_pdf 将模板转换为 pdf(包含用户提供的信息)。
此外,没有任何用户输入命中模型/数据库。我只需要将模板转换为包含用户数据的 pdf,仅此而已。
到目前为止,我可以将模板转换为 pdf,保存并重定向回我的索引视图。但是,用户填写的字段都不会被保存;包含用户输入的输入或值的所有字段都是空白的。它只是保存一个空白模板。如何使用 wicked_pdf 将 pdf 和用户提供的数据转换为 pdf?
# controller save method --->
def save_grade_sheet
@result = Result.find(params[:id])
if current_user.admin?
filename = "#{@result.user.last_name}_grades.pdf"
save_path = "#{Rails.root}/public/uploads/#{@result.user.last_name}/#{filename}"
respond_to do |format|
pdf = render_to_string pdf: filename,
template: "/results/grade_sheet.html.erb",
encoding: "UTF-8",
disposition: "attachment",
save_to_file: save_path,
save_only: true
File.open(save_path, "wb") do |file|
file << pdf
end
flash[:notice] = "#{filename} successfully saved"
format.html { redirect_to results_path }
end
else
head :unauthorized
end
end
# sample template code
<div>
<div>
<h2>PERSONNEL INFORMATION</h2>
<p>Examinee's Name: <%= @result.user.first_name %> <%= @result.user.last_name %></p>
<p>Examiner's Name: <input class="inline" type="text"></p>
</div>
<div>
<p>Exam Type: <%= @result.test.upcase %></p>
<p>Exam Version: <input class="inline" type="text"></p>
<p>Exam Date: <%= @result.created_at.strftime("%m-%d-%y") %></p>
</div>
<%= button_to "Create Grade Sheet", save_grade_sheet_result_path(@result), data: { method: :post }, class: "btn btn-primary btn-lg"%>
</div>
【问题讨论】:
标签: ruby ruby-on-rails-5 wkhtmltopdf wicked-pdf