Skip to content

Commit

Permalink
Fix measurements pdf raport
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejb2k committed Nov 5, 2023
1 parent 71159e2 commit 5077026
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ gem "has_scope", "~> 0.8.1"

# PDF generation
gem "prawn", "~> 2.4.0"
gem "prawn-table", "~> 0.2.2"

group :development, :test do
gem "debug", "~> 1.8.0", platforms: %i[mri mingw x64_mingw]
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ GEM
prawn (2.4.0)
pdf-core (~> 0.9.0)
ttfunk (~> 1.7)
prawn-table (0.2.2)
prawn (>= 1.3.0, < 3.0.0)
public_suffix (5.0.1)
puma (5.6.7)
nio4r (~> 2.0)
Expand Down Expand Up @@ -478,6 +480,7 @@ DEPENDENCIES
pg (~> 1.1)
phonelib (~> 0.8.1)
prawn (~> 2.4.0)
prawn-table (~> 0.2.2)
puma (~> 5.6)
pundit (~> 2.3.1)
rack-mini-profiler (~> 3.1.0)
Expand Down
Binary file added app/assets/fonts/Roboto-Bold.ttf
Binary file not shown.
Binary file added app/assets/fonts/Roboto-Regular.ttf
Binary file not shown.
40 changes: 25 additions & 15 deletions app/services/measurements/generate_day_raport_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@ def call
.order(measurement_date: :asc)

# Generate PDF file with measurements
begin
pdf_file = Prawn::Document.new do |pdf|
measurements.each do |measurement|
type = I18n.t(
"activerecord.attributes.measurement_types.#{measurement.measurement_type.name}"
)
value = measurement.value
unit = measurement.measurement_type.unit.symbol
date = I18n.l(measurement.measurement_date, format: "%H:%M, %d %B %Y")

pdf.text "#{type}: #{value} #{unit} - #{date}"
end
pdf_file = Prawn::Document.new do |pdf|
pdf.font Rails.root.join("app/assets/fonts/Roboto-Regular.ttf"), style: :normal

pdf.text "#{@account.full_name} (#{@account.user.email})"
pdf.move_down 20

pdf.text "Raport pomiarów z dnia: #{I18n.l(@date, format: '%d %B %Y')}"
pdf.text "Wygenerowano: #{I18n.l(Time.zone.now, format: '%H:%M, %d %B %Y')}"
pdf.move_down 20

table_data = [["Typ pomiaru", "Wartość", "Jednostka", "Godzina"]]

measurements.each do |measurement|
type = I18n.t("activerecord.attributes.measurement_types.#{measurement.measurement_type.name}")
value = measurement.value
unit = measurement.measurement_type.unit.symbol
date = I18n.l(measurement.measurement_date, format: "%H:%M")

table_data << [type, value, unit, date]
end

pdf.table(table_data, header: true) do
columns(1..3).align = :right
end
pdf_string = StringIO.new(pdf_file.render)
rescue StandardError
raise PDFGenerationError
end

pdf_string = StringIO.new(pdf_file.render)

# Build a new measurement raport with the generated PDF file
raport_name = I18n.t(
"services.measurements.generate_day_raport_service.raport_name",
Expand Down

0 comments on commit 5077026

Please sign in to comment.