Skip to content

Commit

Permalink
add optional line item columns width configuration (#35)
Browse files Browse the repository at this point in the history
Make line items table column widths configurable

Co-authored-by: whysthatso <git@whysthatso.net>
Co-authored-by: Chris Oliver <excid3@gmail.com>
  • Loading branch information
3 people authored Mar 5, 2024
1 parent 184806d commit 6c7e9be
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ gem 'receipts'

And then execute:

$ bundle
```sh
$ bundle
```

Or install it yourself as:

$ gem install receipts
```sh
$ gem install receipts
```

## Usage

Expand Down Expand Up @@ -124,14 +128,32 @@ Here's an example of where each option is displayed.
![options](examples/images/options.jpg)
#### Line Items Table - Column Widths
You may set an option to configure the line items table's columns width in order to accommodate shortcomings of Prawn's width guessing ability to render header and content reasonably sized.
The configuration depends on your line item column count and follows the prawn/table configuration as documented [here](https://prawnpdf.org/prawn-table-manual.pdf):
This will size the second column to 400 and the fourth column to 50.
```ruby
column_widths: {1 => 400,3 => 50 }
```
This will set all column widths, considering your table has 4 columns.
```ruby
column_widths: [100, 200, 240]
```
If not set, it will fall back to Prawn's default behavior.

### Formatting

`details` and `line_items` allow inline formatting with Prawn. This allows you to use HTML tags to format text: `<b>` `<i>` `<u>` `<strikethrough>` `<sub>` `<sup>` `<font>` `<color>` `<link>`

See [the Prawn docs](https://prawnpdf.org/api-docs/2.3.0/Prawn/Text.html#text-instance_method) for more information.

##### Page Size
#### Page Size

You can specify a different page size by passing in the `page_size` keyword argument:

Expand Down Expand Up @@ -216,7 +238,7 @@ class ChargesController < ApplicationController
@charge = current_user.charges.find(params[:id])
end
def send_pdf
def send_pdf
# Render the PDF in memory and send as the response
send_data @charge.receipt.render,
filename: "#{@charge.created_at.strftime("%Y-%m-%d")}-gorails-receipt.pdf",
Expand Down Expand Up @@ -312,9 +334,8 @@ Receipts::Statement.new(

## Contributing

1. Fork it ( https://github.com/excid3/receipts/fork )
1. Fork it [https://github.com/excid3/receipts/fork](https://github.com/excid3/receipts/fork)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
18 changes: 14 additions & 4 deletions lib/receipts/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class << self
end

def initialize(attributes = {})
super page_size: attributes.delete(:page_size) || "LETTER"
super(page_size: attributes.delete(:page_size) || "LETTER")
setup_fonts attributes.fetch(:font, Receipts.default_font)

@title = attributes.fetch(:title, self.class.title)
Expand All @@ -22,7 +22,10 @@ def generate_from(attributes)
header company: company, height: attributes.fetch(:logo_height, 16)
render_details attributes.fetch(:details)
render_billing_details company: company, recipient: attributes.fetch(:recipient)
render_line_items attributes.fetch(:line_items)
render_line_items(
line_items: attributes.fetch(:line_items),
column_widths: attributes[:column_widths]
)
render_footer attributes.fetch(:footer, default_message(company: company))
end

Expand Down Expand Up @@ -79,11 +82,18 @@ def render_billing_details(company:, recipient:, margin_top: 16)
table(line_items, width: bounds.width, cell_style: {borders: [], inline_format: true, overflow: :expand})
end

def render_line_items(line_items, margin_top: 30)
def render_line_items(line_items:, margin_top: 30, column_widths: nil)
move_down margin_top

borders = line_items.length - 2
table(line_items, width: bounds.width, cell_style: {border_color: "eeeeee", inline_format: true}) do

table_options = {
width: bounds.width,
cell_style: {border_color: "eeeeee", inline_format: true},
column_widths: column_widths
}.compact

table(line_items, table_options) do
cells.padding = 6
cells.borders = []
row(0..borders).borders = [:bottom]
Expand Down

0 comments on commit 6c7e9be

Please sign in to comment.