Permalink
Browse files

Fix hanging spec, use local PDF for /fill specs, add spec for testing…

… SS-5 PDF (but it fails).
  • Loading branch information...
1 parent 2c3bef0 commit 6dc9c42b51ca7945f2f8a432bb47d3410e10e99b @greggersh greggersh committed Oct 21, 2012
Showing with 28 additions and 23 deletions.
  1. +8 −11 lib/pdf_filler.rb
  2. +20 −12 spec/pdf_filler_spec.rb
  3. BIN spec/ss-5.pdf
View
@@ -29,14 +29,11 @@ def initialize
# Given a PDF an array of fields -> values
# return a PDF with the given fields filled out
def fill( url, data )
-
source_pdf = open( URI.escape( url ) )
step_1_result = Tempfile.new( ['pdf', '.pdf'] )
filled_pdf = Tempfile.new( ['pdf', '.pdf'] )
data = urldecode_keys data
- puts data.inspect
-
#Fill fillable fields (step 1)
@pdftk.fill_form source_pdf.path, step_1_result.path, data.find_all{ |key, value| !key[KEY_REGEX] }
@@ -61,14 +58,14 @@ def get_fields(url)
fields = fields.split("---")
@output = []
fields.each do |field|
- @hash = {}
- field.split("\n").each() do |line|
- next if line == ""
- key, value = line.split(": ")
- @hash[key] = value
- end
- next if @hash.empty?
- @output.push @hash
+ @hash = {}
+ field.split("\n").each() do |line|
+ next if line == ""
+ key, value = line.split(": ")
+ @hash[key] = value
+ end
+ next if @hash.empty?
+ @output.push @hash
end
@output
end
@@ -1,7 +1,6 @@
require 'spec_helper'
describe 'PdfFiller' do
-
TEST_PDF = 'http://help.adobe.com/en_US/Acrobat/9.0/Samples/interactiveform_enabled.pdf'
def app
@@ -16,23 +15,20 @@ def app
it "should show the README" do
get '/'
- last_response.should =~ /PDF Form Filler/
- last_response.should =~ /RESTful service to fill both fillable and unfillable forms/
+ last_response.body.should contain(/PDF Filler/)
+ last_response.body.should =~ /PDF Filler is a RESTful service \(API\) to aid in the completion of existing PDF-based forms/
end
-
end
describe "POST /fill" do
-
it "should return the PDF" do
- post "/fill", :pdf => TEST_PDF
+ post "/fill", :pdf => "./spec/sample.pdf"
last_response.should be_ok
last_response.headers['Content-Type'].should eq( 'application/pdf' )
end
it "should fill fields" do
-
- post "/fill", :pdf => TEST_PDF, :Name_Last => "_MYGOV_FILLABLE_", :"100,100,1" => "_MYGOV_NON_FILLABLE_"
+ post "/fill", :pdf => "./spec/sample.pdf", :Name_Last => "_MYGOV_FILLABLE_", :"100,100,1" => "_MYGOV_NON_FILLABLE_"
compressed = Tempfile.new( ['pdf', '.pdf'], nil , :encoding => 'ASCII-8BIT' )
uncompressed = Tempfile.new( ['pdf', '.pdf'], nil , :encoding => 'ASCII-8BIT' )
@@ -44,10 +40,22 @@ def app
file = File.open( uncompressed.path, 'rb' )
contents = file.read
contents.include?('_MYGOV_FILLABLE_').should be_true
- #contents.include?('_MYGOV_NON_FILLABLE_').should be_true
-
+ #contents.include?('_MYGOV_NON_FILLABLE_').should be_true
+ end
+
+ context "when the PDF file has weird field names" do
+ it "should still fill the fields properly" do
+ post "/fill", :pdf => "./spec/ss-5.pdf", "topmostSubform[0].Page5[0].firstname[0]".to_sym => "_MYGOV_FILLABLE_"
+ compressed = Tempfile.new(['pdf', '.pdf'], nil , :encoding => 'ASCII-8BIT')
+ uncompressed = Tempfile.new( ['pdf', '.pdf'], nil , :encoding => 'ASCII-8BIT' )
+ compressed << last_response.body
+ pdftk = PdfForms.new('/usr/local/bin/pdftk')
+ pdftk.call_pdftk compressed.path, 'output', uncompressed.path, 'uncompress'
+ file = File.open( uncompressed.path, 'rb' )
+ contents = file.read
+ contents.should =~ /MYGOV/
+ end
end
-
end
describe "GET /fields" do
@@ -67,7 +75,7 @@ def app
end
describe "GET /form" do
- it "should create a form for the pdf base don fields" do
+ it "should create a form for the pdf based on fields" do
get "/form", :pdf => './spec/sample.pdf'
last_response.should be_ok
last_response.body.should =~ /PHD/
View
Binary file not shown.

0 comments on commit 6dc9c42

Please sign in to comment.