From 6c7e9be60693b4cf20e0768d3385c3a4ded5cd96 Mon Sep 17 00:00:00 2001 From: Andreas Wagner Date: Tue, 5 Mar 2024 17:14:41 +0200 Subject: [PATCH] add optional line item columns width configuration (#35) Make line items table column widths configurable Co-authored-by: whysthatso Co-authored-by: Chris Oliver --- README.md | 35 ++++++++++++++++++++++++++++------- lib/receipts/base.rb | 18 ++++++++++++++---- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5b2e9e6..d5ca860 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: `` `` `` `` `` `` `` `` `` 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: @@ -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", @@ -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 - diff --git a/lib/receipts/base.rb b/lib/receipts/base.rb index d67141d..444ae45 100644 --- a/lib/receipts/base.rb +++ b/lib/receipts/base.rb @@ -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) @@ -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 @@ -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]