Skip to content

Commit

Permalink
Replace prawn-format by prawn-markup
Browse files Browse the repository at this point in the history
TODO:

- [] Make sure that the pdf is generated correctly
- [] Make sure all styles are correct
  • Loading branch information
hallelujah committed Dec 21, 2021
1 parent 48ead26 commit 59359bd
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ gem 'httpclient', github: 'mikz/httpclient', branch: 'ssl-env-cert'
gem 'json-schema', git: 'https://github.com/3scale/json-schema.git'
gem 'paperclip', '~> 6.0'
gem 'prawn'
gem 'prawn-format'
gem 'prawn-markup'
gem 'prawn-table'
gem 'rails_event_store', '~> 0.9.0'
gem 'ratelimit'
Expand Down
9 changes: 5 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1598,9 +1598,10 @@ GEM
prawn (2.4.0)
pdf-core (~> 0.9.0)
ttfunk (~> 1.7)
prawn-core (0.8.4)
prawn-format (0.2.3)
prawn-core
prawn-markup (0.3.3)
nokogiri
prawn
prawn-table
prawn-table (0.2.2)
prawn (>= 1.3.0, < 3.0.0)
prometheus-client-mmap (0.11.0)
Expand Down Expand Up @@ -2051,7 +2052,7 @@ DEPENDENCIES
pg (~> 0.21.0)
pisoni (~> 1.29)
prawn
prawn-format
prawn-markup
prawn-table
protected_attributes_continued (~> 1.3.0)
pry-byebug (>= 3.7.0)
Expand Down
13 changes: 4 additions & 9 deletions app/lib/pdf/finance/invoice_generator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# frozen_string_literal: true

require 'prawn'
require 'prawn/format'
require "prawn/measurement_extensions"
require 'gruff'
require "open-uri"

module Pdf
module Finance
Expand All @@ -25,7 +20,7 @@ def initialize(invoice_data)
@pdf = Prawn::Document.new(page_size: 'A4',
page_layout: :portrait)

@pdf.tags(@style.tags)
@pdf.markup_options = @style.tags
@pdf.font(@style.font)
end

Expand Down Expand Up @@ -88,7 +83,7 @@ def print_address_columns

def print_address(person, name = nil)
subtitle("<b>#{name}</b>") if name
@pdf.table(person, @style.table_style.merge(width: TABLE_HALF_WIDTH))
@pdf.table(person, @style.table_style.deep_merge(width: TABLE_HALF_WIDTH))
end

def print_details
Expand All @@ -103,8 +98,8 @@ def print_details

def print_line_items
subtitle('<b>Line items</b>')
opts = { width: TABLE_FULL_WIDTH, headers: InvoiceReportData::LINE_ITEMS_HEADING }
@pdf.table(@data.line_items, @style.table_style.merge(opts))
opts = { width: TABLE_FULL_WIDTH, header: true }
@pdf.table([InvoiceReportData::LINE_ITEMS_HEADING] + @data.line_items, @style.table_style.deep_merge(opts))
move_down
@pdf.text(@data.vat_zero_text) if @data.vat_rate&.zero?
end
Expand Down
4 changes: 2 additions & 2 deletions app/lib/pdf/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ module Pdf::Printer
#
def two_columns(at = [0.mm, @pdf.cursor], options = {}, &block)
@pdf.bounding_box(at, { :width => PAGE_WIDTH }.merge(options)) do
@pdf.column_box [0.mm, @pdf.bounds.top], :width => TABLE_HALF_WIDTH do
@pdf.column_box [0.mm, @pdf.bounds.top], :width => TABLE_HALF_WIDTH, columns: 1 do
yield(:left)
end

@pdf.column_box [97.mm, @pdf.bounds.top], :width => TABLE_HALF_WIDTH do
@pdf.column_box [97.mm, @pdf.bounds.top], :width => TABLE_HALF_WIDTH, columns: 1 do
yield(:right)
end
end
Expand Down
12 changes: 2 additions & 10 deletions app/lib/pdf/report.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
# frozen_string_literal: true

require 'prawn'
require 'prawn/format'
require "prawn/measurement_extensions"
require 'gruff'
require 'pdf/format'
require 'pdf/data'
require 'pdf/styles/colored'

# REFACTOR: extract abstract Report class, and DRY functionality with InvoiceReporter
module Pdf
class Report
Expand Down Expand Up @@ -158,8 +150,8 @@ def three_scale_logo

def print_table(data, width, headings)
if data.present?
options = { headers: headings, width: width }
@pdf.table data, @style.table_style.merge(options)
options = { header: true, width: width }
@pdf.table [headings] + data, @style.table_style.deep_merge(options)
else
@pdf.text "<small>No current data</small>"
end
Expand Down
37 changes: 21 additions & 16 deletions app/lib/pdf/styles/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@
module Pdf::Styles
class Base
def tags
{ :domain => { :color => "#999", :font_weight => :bold, :font_size => 14},
:period => {:font_size => 6.mm},
:date => {:font_size => 5.mm, :color => "999999", :font_weight => :bold},
:subtitle => {:font_size => 4.mm, :font_weight => :bold, :color => "555"},
:td => {:font_size => 3.mm, :font_weight => :bold, :color => "222222"},
:th => {:font_weight => :bold, :font_size => 3.5.mm, :color => "ffffff"},
:small => {:font_size => 3.5.mm, :color => "555"},
:red => {:color => 'red'},
:green => {:color => 'green'},
:b => { :style => :bold }
{ domain: { color: "#999", font_weight: :bold, font_size: 14 },
period: { font_size: 6.mm },
date: { font_size: 5.mm, color: "999999", font_weight: :bold },
subtitle: { font_size: 4.mm, font_weight: :bold, color: "555" },
table: {
header: { style: :bold, size: 3.5.mm, color: "ffffff" },
cell: { size: 3.mm, style: :bold, color: "222222" }
},
small: { font_size: 3.5.mm, color: "555" },
red: { color: 'red' },
green: { color: 'green' },
b: { style: :bold }
}
end

def table_style
{ :position => :left,
:width => 187.5.mm,
:vertical_padding => 1.2.mm,
:horizontal_padding => 1.2.mm,
:border_style => :grid,
:border_width => 0.2.mm }
{
position: :left,
width: 187.5.mm,
cell_style: {
padding: 1.2.mm,
borders: [:left, :right, :top, :bottom],
border_width: 0.2.mm
}
}
end

def font
Expand Down
31 changes: 16 additions & 15 deletions app/lib/pdf/styles/black_and_white.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# frozen_string_literal: true

require 'prawn/format'

class Pdf::Styles::BlackAndWhite < Pdf::Styles::Base

def tags
super.merge(
{ :domain => { :font_weight => :bold, :font_size => 14},
:period => { :font_size => 6.mm},
:date => { :font_size => 5.mm, :font_weight => :bold},
{ :domain => { :font_weight => :bold, :font_size => 14 },
:period => { :font_size => 6.mm },
:date => { :font_size => 5.mm, :font_weight => :bold },
:subtitle => { :font_size => 4.mm, :font_weight => :bold },
:td => { :font_size => 3.mm, :font_weight => :bold },
:th => { :font_weight => :bold, :font_size => 3.5.mm, :color => "ffffff"},
:small => { :font_size => 3.5.mm, :color => "555"},
:red => { :color => 'black'},
:green => { :color => 'b0b0b0'},
:b => { :style => :bold }
table: {
header: { style: :bold, size: 3.mm, color: "ffffff" },
cell: { size: 3.mm, style: :bold}
},
:small => { :font_size => 3.5.mm, :color => "555" },
:red => { :color => 'black' },
:green => { :color => 'b0b0b0' },
:b => { :style => :bold }
})
end

def table_style
super.merge(:border_color => 'a7a7a7',
:header_color => 'd6d6d6',
# :row_colors => [ "d6d6d6d", "efefef" ],
:row_colors => %w[ffffff ffffff])
super.deep_merge(
cell_style: { :border_color => 'a7a7a7' },
:header_color => 'd6d6d6',
# :row_colors => [ "d6d6d6d", "efefef" ],
:row_colors => %w[ffffff ffffff])
end
end
28 changes: 16 additions & 12 deletions app/lib/pdf/styles/colored.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
class Pdf::Styles::Colored < Pdf::Styles::Base

def tags
super.merge(:domain => { :color => "#999", :font_weight => :bold, :font_size => 14},
:date => {:font_size => 5.mm, :color => "999999", :font_weight => :bold},
:subtitle => {:font_size => 4.mm, :font_weight => :bold, :color => "555"},
:td => {:font_size => 3.mm, :font_weight => :bold, :color => "222222"},
:th => {:font_weight => :bold, :font_size => 3.5.mm, :color => "ffffff"},
:small => {:font_size => 3.5.mm, :color => "555"},
:red => {:color => 'red'},
:green => {:color => 'green'})
super.merge(:domain => { :color => "#999", :font_weight => :bold, :font_size => 14 },
:date => { :font_size => 5.mm, :color => "999999", :font_weight => :bold },
:subtitle => { :font_size => 4.mm, :font_weight => :bold, :color => "555" },
:td => { :font_size => 3.mm, :font_weight => :bold, :color => "222222" },
:th => { :font_weight => :bold, :font_size => 3.5.mm, :color => "ffffff" },
:small => { :font_size => 3.5.mm, :color => "555" },
:red => { :color => 'red' },
:green => { :color => 'green' })
end

def table_style
super.merge( :header_text_color => 'ffffff',
:row_colors => %w[d9e8f8 e6f0fb],
:border_color => 'ffffff',
:header_color => '7e7e7f' )
super.deep_merge(
header_text_color: 'ffffff',
row_colors: %w[d9e8f8 e6f0fb],
cell_style: {
border_color: 'ffffff',
header_color: '7e7e7f'
}
)
end

end
2 changes: 1 addition & 1 deletion app/lib/three_scale/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ThreeScale

# This class represents some amount of money with currency. It behaves more or
# less like Numeric type, so it supports basic arithemtic operations,
# less like Numeric type, so it supports basic arithmetic operations,
# comparisons, and so on. All operators convert both money objects to the
# same currency (the currency of the first operand) before performing
# calculations. The conversion is done using exchange rates that are fetched
Expand Down
24 changes: 24 additions & 0 deletions config/initializers/prawn.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# require 'prawn'
# require 'prawn/format'
require "prawn/measurement_extensions"
require 'gruff'
require "open-uri"

module Prawn
class Table
def header_color=(color)
row(0).background_color = color
end

class Cell
class << self
prepend(Module.new do
def make(pdf, content, options = {})
content = content.to_s if content.is_a?(ThreeScale::Money)
super(pdf, content, options)
end
end)
end
end
end
end

0 comments on commit 59359bd

Please sign in to comment.