From 4abae91a5ad40926cefd43cd0fe60235a0706283 Mon Sep 17 00:00:00 2001 From: david Date: Sun, 24 Nov 2024 00:58:36 +0100 Subject: [PATCH] Improve pdf --- app/models/pdf_documents/base.rb | 11 +++-- app/models/pdf_documents/customer_report.rb | 47 ++++++++++++++++++--- config/initializers/prawn.rb | 10 +++++ 3 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 config/initializers/prawn.rb diff --git a/app/models/pdf_documents/base.rb b/app/models/pdf_documents/base.rb index a165182..32703da 100644 --- a/app/models/pdf_documents/base.rb +++ b/app/models/pdf_documents/base.rb @@ -71,7 +71,6 @@ module PdfDocuments def logo @prawn_document.image "app/assets/images/logo-apfelschule.png", width: 150 - @prawn_document.move_down 30 end def prepare_rich_text(rich_text) @@ -112,7 +111,11 @@ module PdfDocuments end def image(attachable, **args) - @prawn_document.image(ActiveStorage::Blob.service.path_for(attachable.key), **args) rescue StandardError do false end + begin + @prawn_document.image(ActiveStorage::Blob.service.path_for(attachable.key), **args) && raise("david") + rescue StandardError + false + end end def without_page_break(&block) @@ -140,8 +143,8 @@ module PdfDocuments @prawn_document.text("absolute right: #{Float(@prawn_document.bounds.absolute_right).round(2)}") end - def new_page - @prawn_document.start_new_page + def new_page(required_space: 0) + @prawn_document.start_new_page if @prawn_document.cursor <= required_space + 10 end end end diff --git a/app/models/pdf_documents/customer_report.rb b/app/models/pdf_documents/customer_report.rb index b3d6088..bb7311a 100644 --- a/app/models/pdf_documents/customer_report.rb +++ b/app/models/pdf_documents/customer_report.rb @@ -5,16 +5,31 @@ module PdfDocuments private def generate + @prawn_document.repeat(:all) do + # @prawn_document.bounding_box([ 0, 790 ], width: 200) do + # logo + # end + # @prawn_document.formatted_text_box([ { + # text: "Dieses Dokument wurd am #{Time.current.strftime('%d %B %Y')} um #{Time.current.strftime('%H:%M:%S')} erstellt.", + # size: 8, + # align: :right + # } ], + # at: [ 0, -15 ], + # width: 200 + # ) + @prawn_document.draw_text "#{I18n.l params.report.updated_at.to_date} #{params.report.name}", at: [ 0, 770 ] + @prawn_document.font_size(8) do + @prawn_document.draw_text("Dieses Dokument wurd am #{Time.current.strftime('%d %B %Y')} um #{Time.current.strftime('%H:%M:%S')} erstellt.", at: [ 0, -15 ]) + end + end logo - @prawn_document.formatted_text_box [ { - text: "Dieses Dokument wurd am #{Time.current.strftime('%d %B %Y')} um #{Time.current.strftime('%H:%M:%S')} erstellt.", size: 8, align: :right - } ], align: :right - + move_down 20 heading1 params.report.name rich_text params.report.comment + @pages = {} params.report.export[:elements].each.with_index(1) do |(element, success_criteria), element_index| - new_page if element_index > 1 + new_page(required_space: 200) if element_index > 1 without_page_break do heading2 "#{element_index} #{element.title}" bold("#{element.page.path}") @@ -24,9 +39,31 @@ module PdfDocuments move_down(10) success_criteria.each.with_index(1) do |success_criterion, sc_index| + @pages[success_criterion.id] = @prawn_document.page_number success_criterion_row(success_criterion, [ element_index, sc_index ]) end end + string = "Seite / " + # Green page numbers 1 to 7 + options = { + at: [ @prawn_document.bounds.right - 150, -10 ], + width: 150, + align: :right, + start_count_at: 1 + } + @prawn_document.number_pages string, options + + x = params + p = @pages + @prawn_document.outline.define do + x.report.export[:elements].each.with_index(1) do |(element, success_criteria), element_index| + section("#{element_index} #{element.title}") do + success_criteria.each.with_index(1) do |sc, sc_index| + page(title: "#{element_index}.#{sc_index} itle}", destination: p[sc.id]) + end + end + end + end end private diff --git a/config/initializers/prawn.rb b/config/initializers/prawn.rb new file mode 100644 index 0000000..41bf34b --- /dev/null +++ b/config/initializers/prawn.rb @@ -0,0 +1,10 @@ +PrawnRails.config do |config| + config.page_layout = :portrait + config.page_size = "A4" + config.skip_page_creation = false + config.left_margin = 48 + config.top_margin = 48 + config.right_margin = 48 + config.bottom_margin = 48 +end +