1

我们遇到的 PDF 文件是包含用户提供的数据的可填写 PDF 表单。我们想提取用户填写到表单中的数据,但不知道有任何具有此功能的 gem。因此,例如,PDF 表单中有一个“名字”字段,用户用“大卫”完成了该字段——我们希望获得诸如“名字=>大卫”之类的数据

查看示例文件的属性告诉我:

  • PDF 生成器:Adobe LiveCycle Designer ES 8.2
  • PDF 版本:1.7,Adobe Extension Level 3 (Acrobat 9.x)

建议和想法表示赞赏!

谢谢

4

1 回答 1

1

由于该dump_data_fields方法具有非常标准化的结构,因此该方法应该可以满足您的需要,它将输出一个数组,其中每个字段都是一个哈希对象。

def parse_pdf_dump(file)
  file = open(file,&:read)
  fields = file.split("---").delete_if{|f| f.empty?}
  #Create an Array of the fields 
  fields.map do |field|
    #Create a have of attribute => value for each field attribute
    Hash[
       field.split("\n").map do |line|
            split_line = line.split(":")
            #grab the name of the attribute
            name = split_line.shift
            #grab the value of the attribute
            #join is used in the case that the data originally had a : in it
            val = split_line.join(":")
            unless f_name.nil?
             [name.downcase, val.strip]
            end
       end
    ]
  end
end

使用 active_pdftk 调用如下

require 'active_pdftk'
output_path = '/data_fields.txt'
pdftk = ActivePdftk::Wrapper.new(:path => [YOUR PATH TO PDFTK BINARY OR EXE])
pdftk.dump_data_fields([YOUR PDF], :output => output_path)
fields_array = parse_pdf_dump(output_path)
%x( rm output_path)

因此,您将使用 pdftk 将数据字段转储到一个数组fields_array中,然后删除文本文件。

于 2013-11-11T14:23:19.307 回答