Skip to content

Commit

Permalink
Added x_offset and y_offset capabilities at a Class and per fill_form…
Browse files Browse the repository at this point in the history
…_with call
  • Loading branch information
netinlet committed Mar 5, 2012
1 parent 7a1d209 commit d50c1f2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source "http://rubygems.org"

# Specify your gem's dependencies in prawn-fillform.gemspec
#
gem 'prawn', :require => 'prawn'
gemspec
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,50 @@ end

Take a look in `examples` folder

## Per Fork netinlet/prawn-fillform

I was having issue with the form field placement (see https://github.com/moessimple/prawn-fillform/issues/1)
Scribus and Adobe Acrobat don't open pdf's in the same way so the formatting comes out differently. Much like
opening a Word document in OpenOffice can some render with funny formatting.

This fork added the ability to set :x_offset and :y_offset at the class level and on a per form basis.

#Class Methods
```ruby
Prawn::Document.set_fillform_xy_offset(x_offset, y_offset)

Prawn::Document.use_adobe_xy_offsets! # Your mileage may vary! Defaults to x_offset:2, y_offset:-40

Prawn::Document.fillform_x_offset

Prawn::Document.fillform_y_offset
```

#And on a per-form basis

See the :options param below

```ruby
require 'prawn-fillform'

data = {}
data[:page_1] = {}
data[:page_1][:firstname] = { :value => "Max", :options => {:x_offset => 2, :y_offset => -40} }
data[:page_1][:lastname] = { :value => "Mustermann" }
data[:page_1][:photo] = { :value => "test.jpg" }

# Create a PDF file with predefined data Fields
Prawn::Document.generate "output.pdf", :template => "template.pdf" do |pdf|
pdf.fill_form_with(data)
end
```










27 changes: 24 additions & 3 deletions lib/prawn-fillform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module Prawn

module Fillform

class Field
include Prawn::Document::Internals

Expand Down Expand Up @@ -152,6 +152,25 @@ def collect!
end
end
end

module XYOffsets
def fillform_x_offset
@fillform_x_offset ||= 2
end

def fillform_y_offset
@fillform_y_offset ||= -1
end

def set_fillform_xy_offset(x_offset, y_offset)
@fillform_x_offset = x_offset
@fillform_y_offset = y_offset
end

def use_adobe_xy_offsets!
set_fillform_xy_offset(2, -40)
end
end

def acroform_field_names
result = []
Expand Down Expand Up @@ -190,7 +209,6 @@ def acroform_fields
end

def fill_form_with(data={})

acroform_fields.each do |page, fields|
fields.each do |field|
number = page.to_s.split("_").last.to_i
Expand All @@ -203,8 +221,10 @@ def fill_form_with(data={})
value = value.to_s
if field.type == :text
fill_color options[:font_color] || field.font_color
x_offset = options[:x_offset] || self.class.fillform_x_offset
y_offset = options[:y_offset] || self.class.fillform_y_offset

text_box value, :at => [field.x + 2, field.y - 1],
text_box value, :at => [field.x + x_offset, field.y + y_offset],
:align => options[:align] || field.align,
:width => options[:width] || field.width,
:height => options[:height] || field.height,
Expand Down Expand Up @@ -238,5 +258,6 @@ def fill_form_with(data={})
end

require 'prawn/document'
Prawn::Document.extend Prawn::Fillform::XYOffsets
Prawn::Document.send(:include, Prawn::Fillform)

2 changes: 2 additions & 0 deletions prawn-fillform.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency "prawn"

# specify any dependencies here; for example:
# s.add_development_dependency "rspec"
Expand Down

0 comments on commit d50c1f2

Please sign in to comment.