Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override field size #38

Open
mervynlee94 opened this issue Nov 19, 2021 · 4 comments
Open

Override field size #38

mervynlee94 opened this issue Nov 19, 2021 · 4 comments

Comments

@mervynlee94
Copy link

Any idea how to override the method below to increase the field size in my rails application?

 def make_field_box(field)
    stroke_bounds
    bounds.add_left_padding 2
    move_down 2
    text field.upcase, size: 14
  end

I tried this in config/initializers/prawn_rails.rb but it is not working as expected.

include PrawnRailsForms

DocumentExtensions.module_eval do
  private
  def make_field_box(field)
    stroke_bounds
    bounds.add_left_padding 2
    move_down 2
    text field.upcase, size: 14
  end
end


@Anbranin
Copy link
Member

This worked for me, in this initializer (underneath everything already there, leaving that untouched):

include PrawnRailsForms::DocumentExtensions
def make_field_box(field)
# your code with a custom size
end

Let me know if that works and I'll close out this issue.

@werebus
Copy link
Member

werebus commented Nov 23, 2021

Can you clarify what you mean by, "increase the field size"? It looks like what you're trying to do here is increase the font size of the label, this text:

2021-11-23_09-06

There are built-in ways to increase the row height (via the height: option to the field_row method), and the width, height, and font size (size:) of the text_field.


That said, monkey-patching our module does appear to work:

  PrawnRailsForms::DocumentExtensions.module_eval do
    def make_field_box(field)
      stroke_bounds
      bounds.add_left_padding 2
      move_down 2
      text field.upcase, size: 14
    end
  end

2021-11-23_09-45

Just be advised:

  1. It's not guaranteed to keep working, it is a private method, after all.
  2. Doing so can break all sorts of other parts of your PDF. In our example app, I see several instances of missing field values because they got bushed out of the bounding box, checkboxes floating outside of their box, etc.

@mervynlee94
Copy link
Author

This worked for me, in this initializer (underneath everything already there, leaving that untouched):

include PrawnRailsForms::DocumentExtensions
def make_field_box(field)
# your code with a custom size
end

Let me know if that works and I'll close out this issue.

I actually copied the whole DocumentExtensions to make it work, starting with

include PrawnRailsForms

module PrawnRailsForms
  module DocumentExtensions
    ...
  end
end

@mervynlee94
Copy link
Author

Can you clarify what you mean by, "increase the field size"? It looks like what you're trying to do here is increase the font size of the label, this text:

2021-11-23_09-06

There are built-in ways to increase the row height (via the height: option to the field_row method), and the width, height, and font size (size:) of the text_field.

That said, monkey-patching our module does appear to work:

  PrawnRailsForms::DocumentExtensions.module_eval do
    def make_field_box(field)
      stroke_bounds
      bounds.add_left_padding 2
      move_down 2
      text field.upcase, size: 14
    end
  end

2021-11-23_09-45

Just be advised:

  1. It's not guaranteed to keep working, it is a private method, after all.
  2. Doing so can break all sorts of other parts of your PDF. In our example app, I see several instances of missing field values because they got bushed out of the bounding box, checkboxes floating outside of their box, etc.

Yes you are right. The font size (size:) of the text_field only works on the value but not field. As I read from the source code, the field is hardcoded. I think it will be nice to include the customisation of value font size in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants