Skip to content

Instantly share code, notes, and snippets.

@mhl
Last active December 20, 2015 19:28
Show Gist options
  • Save mhl/6182881 to your computer and use it in GitHub Desktop.
Save mhl/6182881 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'iconv'
require 'tempfile'
unless ARGV.length == 2
STDERR.puts "Usage: <INPUT-PDF> <OUTPUT-PDF>"
exit 1
end
field_mapping = {
'Text1' => 'departmental-reference-number',
'Text2' => 'departmental-request-received-by',
'Text3' => 'departmental-request-received-date',
'Text4' => 'departmental-request-received-place',
'Text5a' => 'request-fee',
'Text5b' => 'deposit',
'Text5c' => 'access-fee',
'Text7' => 'full-names-and-surname-a',
'Text7a' => 'id-a-01',
'Text7b' => 'id-a-02',
'Text7c' => 'id-a-03',
'Text7d' => 'id-a-04',
'Text7e' => 'id-a-05',
'Text7f' => 'id-a-06',
'Text7g' => 'id-a-07',
'Text7h' => 'id-a-08',
'Text7i' => 'id-a-09',
'Text7j' => 'id-a-10',
'Text7k' => 'id-a-11',
'Text7l' => 'id-a-12',
'Text7m' => 'id-a-13',
'Text8' => 'full-names-and-surname-b',
'Text8a' => 'id-b-01',
'Text8b' => 'id-b-02',
'Text8c' => 'id-b-03',
'Text8d' => 'id-b-04',
'Text8e' => 'id-b-05',
'Text8f' => 'id-b-06',
'Text8g' => 'id-b-07',
'Text8h' => 'id-b-08',
'Text8i' => 'id-b-09',
'Text8j' => 'id-b-10',
'Text8k' => 'id-b-11',
'Text8l' => 'id-b-12',
'Text8m' => 'id-b-13',
'Text9' => 'description-of-record-1',
'Text9a' => 'description-of-record-2',
# TODO: add mappings for the other keys...
}
values_to_fill_in = {
'description-of-record-1' => 'just like to say',
'description-of-record-2' => 'helloooo',
}
template_pdf_filename, output_pdf_filename = ARGV
fdf_tempfile = Tempfile.new(['paia-', '.fdf'])
fdf_tempfile.close
fdf_filename = fdf_tempfile.path
$utf_16_to_utf_8 = Iconv.new "UTF-8", "UTF-16BE"
$utf_8_to_utf_16 = Iconv.new "UTF-16BE", "UTF-8"
def decode_key(s)
without_bom = s.gsub(/^\xFE\xFF/, '')
$utf_16_to_utf_8.iconv without_bom
end
def encode_value(s)
without_bom = $utf_8_to_utf_16.iconv s
"\xFE\xFF#{without_bom}"
end
# Generate the FDF file from the PDF:
unless system('pdftk',
template_pdf_filename,
'generate_fdf',
'output',
fdf_filename)
STDERR.puts "Generating FDF from #{template_pdf_filename} failed"
exit(1)
end
lines = File.open(fdf_filename, 'rb') { |f|
f.readlines
}
puts "fdf_filename is: #{fdf_filename}"
line_number_to_replacement_line = {}
lines.each_with_index do |line, i|
# We assume that the keys and values come in pairs of lines, with
# the value before the key, like:
#
# /V (<FE><FF>)
# /T (<FE><FF>^@T^@e^@x^@t^@9^@a)
if line =~ /T \((.*)\)$/
key = decode_key $1
puts "found key: #{key}"
useful_key_name = field_mapping[key]
if useful_key_name
# FIXME: just for figuring out IDs, fill in the key even if there
# isn't a value for it...
# if values_to_fill_in.has_key?(useful_key_name)
encoded_value = encode_value values_to_fill_in[useful_key_name] || useful_key_name
line_number_to_replacement_line[i - 1] = "/V (#{encoded_value})"
# end
end
end
end
File.open(fdf_filename, 'w') { |f|
lines.each_with_index do |line, i|
replacment_line = line_number_to_replacement_line[i]
f.puts replacment_line || line
end
}
# Output a PDF with those fields filled in:
unless system('pdftk',
template_pdf_filename,
'fill_form',
fdf_filename,
'output',
output_pdf_filename)
STDERR.puts "Generating completed PDF #{output_pdf_filename} failed"
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment