在线填写并更新上载的PDF表单,然后将其保存回服务器-Ruby on Rails [英] Fill up and update an uploaded PDF form online and save it back to the server - Ruby on Rails

查看:98
本文介绍了在线填写并更新上载的PDF表单,然后将其保存回服务器-Ruby on Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是要求:

在我用Ruby on Rails开发的Web应用程序中,我们需要有一个选项可以将PDF form template上传到系统,将其重新带回到浏览器中,用户应该能够在线填写PDF表单最后将其保存回服务器.

In my web-app developed in Ruby on Rails, we require to have an option to upload a PDF form template to the system, take it back in the browser itself and the user should be able to fill up the PDF form online and finally save it back to the server.

然后,用户将来从应用程序下载更新的PDF表单.我进行了很多搜索,但找不到合适的解决方案.请提出建议.

Then, user will come and download the updated PDF form from the application. I've searched a lot but couldn't find a proper solution to it. Please suggest.

推荐答案

正如我对已嵌入表单字段的预构建PDF所述,我使用pdtk 在此处可用.这是我使用的标准流程,但您可能会有所不同:

As I stated for prebuilt PDF's with form fields already embedded I use pdtk Available Here and the active_pdftk gem Available Here. This is the standard process I use but yours may differ:

 class Form
   def populate(obj)
    #Stream the PDF form into a TempFile in the tmp directory
    template = stream
    #turn the streamed file into a pdftk Form
    #pdftk_path should be the path to the executable for pdftk
    populated_form =  ActivePdftk::Form.new(template,path: pdftk_path)
    #This will generate the form_data Hash based on the fields in the form
    #each form field is specified as a method with or without arguments
    #fields with arguments are specified as method_name*args for splitting purposes
    form_data = populated_form.fields.each_with_object({}) do |field,obj|
      meth,args = field.name.split("*")
      #set the Hash key to the value of the method with or without args
      obj[field.name] = args ? obj.send(meth,args) : obj.send(meth)
    end     
    fill(template,form_data)
  end
  private 
  def fdf(waiver_data,path)
    @fdf ||= ActivePdftk::Fdf.new(waiver_data)
    @fdf.save_to path
  end
  def fill(template,waiver_data)
    rand_path = generate_tmp_file('.fdf')
    initialize_pdftk.fill_form(template,
                               fdf(waiver_data,rand_path),
                               output:"#{rand_path.gsub(/fdf/,'pdf')}",
                               options:{flatten:true})
  end
  def initialize_pdftk
    @pdftk ||= ActivePdftk::Wrapper.new(:path =>pdftk_path)
  end 
end

基本上,这是将表单流式传输到临时文件.然后将其转换为ActivePdftk::Form.然后,它读取所有字段并构建field_name => value结构的Hash.由此,它生成一个fdf文件,并使用该文件来填充实际的PDF文件,然后将其输出到展平的另一个临时文件中,以从最终结果中删除字段.

Basically what this does is it streams the form to a tempfile. Then it converts it to a ActivePdftk::Form. Then it reads all the fields and builds a Hash of field_name => value structure. From this it generates an fdf file and uses that to populate the actual PDF file and then outputs it to another tempfile flattened to remove the fields from the final result.

您的用例可能有所不同,但希望该示例对帮助您实现目标很有用.我没有包括所使用的所有方法,因为我假设您知道如何做诸如读取文件之类的事情.此外,我的表单还需要更多动态特性,例如带有参数的方法.显然,如果您仅填写原始固定数据,则也可以对此部分进行一些更改.

Your use case might differ but hopefully this example will be useful in helping you achieve your goal. I have not included every method used as I am assuming you know how to do things like read a file. Also my forms require a bit more dynamics like methods with arguments. Obviously if you are just filling in raw fixed data this portion could be changed a bit too.

给定类的用法示例称为Form,您还有其他一些对象可以用来填写表单.

An example of usage given your class is called Form and you have some other object to fill the form with.

 class SomeController < ApplicationController
   def download_form
     @form = Form.find(params[:form_id])
     @object = MyObject.find(params[:my_object_id]) 
     send_file(@form.populate(@object), type: :pdf, layout:false, disposition: 'attachment')
   end
 end

此示例将从@object中获取@formpopulate,然后将其作为已填充和展平的PDF呈现给最终用户.如果您只需要将其保存回数据库中,我相信您可以使用某种类型的上传器来解决这个问题.

This example will take @form and populate it from @object then present it to the end user as a filled and flattened PDF. If you just needed to save it back into the database I am sure you can figure this out using an uploader of some kind.

这篇关于在线填写并更新上载的PDF表单,然后将其保存回服务器-Ruby on Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆