我正在尝试在 Rails 4 中使用 PDFTK 让用户通过点击浏览器中的链接下载 PDF。表单数据通过应用程序输入,并使用 PDFTK 发送到表单字段。但是,当我访问相应的 URL 时,我无法查看 filled-out PDF。以下是我的相关代码。
这是 app/controllers/form_controllers.rb:
class FormsController < ApplicationController
def show
@tables = Table.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = TestPdfForm.new(@tables)
send_data pdf.render, filename: "filled-form.pdf", type: 'application/pdf'
end
end
end
end
这是 app/pdfs/fillable_form_pdf.rb:
class FillablePdfForm
attr_writer :template_path
attr_reader :attributes
def initialize
fill_out
end
def export(output_file_path=nil)
output_path = output_file_path || "#{Rails.root}/tmp/pdfs/# {SecureRandom.uuid}.pdf"
pdftk.fill_form template_path, output_path, attributes
output_path
end
def get_field_names
pdftk.get_field_names template_path
end
def template_path
@template_path ||= "#{Rails.root}/lib/pdf_templates/test_form.pdf"
end
protected
def attributes
@attributes ||= {}
end
def fill(key, value)
attributes[key.to_s] = value
end
def pdftk
@pdftk ||= PdfForms.new(ENV['PDFTK_PATH'])
end
def fill_out
raise 'Must be overridden by child class'
end
end
class TestPdfForm < FillablePdfForm
def initialize(tables)
super()
@tables = tables
fill_out
end
protected
def fill_out
fill :date, Date.today.to_s
[:first_name, :last_name].each do |field|
fill field, @tables.send(field)
end
fill :comments, "Hello, World"
end
end
有没有人有任何想法我在这里做错了什么?
谢谢您的帮助!
编辑
我已经让我的应用程序实际运行与 fillable_form_pdf 相关的代码。但是,现在验证 Heroku 确切知道 PDFTK 二进制文件的位置存在问题。
我已将 PDFTK_PATH 环境变量设置为指向/vendor/pdftk/bin/pdftk,但我仍然收到一条错误消息:
“Cliver::Dependency::NotFound(在 path.)找不到可执行文件[5]”
我实际上已经完成了在我的 Windows 7 机器上安装 pdftk 依赖的步骤,并且在 Heroku 文件路径中肯定有一个 pdftk 文件。所以我不确定我在这里缺少什么,有人能把我推向正确的方向吗?