Improve formatting of rich text in pdf

This commit is contained in:
david 2024-07-21 02:15:06 +02:00
parent 879670983d
commit 1e1ae1ce3b
4 changed files with 24 additions and 6 deletions

View file

@ -52,6 +52,7 @@ gem 'caxlsx'
gem 'caxlsx_rails'
gem 'image_processing', '~> 1.2'
gem 'pagy', '~> 9.0'
gem 'prawn-markup'
gem 'prawn-rails'
gem 'sablon'

View file

@ -188,6 +188,10 @@ GEM
matrix (~> 0.4)
pdf-core (~> 0.10.0)
ttfunk (~> 1.8)
prawn-markup (1.0.0)
nokogiri
prawn
prawn-table
prawn-rails (1.4.2)
actionview (>= 3.1.0)
prawn
@ -352,6 +356,7 @@ DEPENDENCIES
jbuilder
jsbundling-rails
pagy (~> 9.0)
prawn-markup
prawn-rails
puma (>= 5.0)
rails (~> 7.1.3, >= 7.1.3.4)

View file

@ -1,8 +1,17 @@
module PdfHelper
def prepare_rich_text(rich_text)
return rich_text
{ del: 'strikethrough' }.each do |tag, replacement|
rich_text = rich_text.to_s.gsub("<#{tag}", "<#{replacement}")
rich_text = rich_text.to_s.gsub("</#{tag}>", "</#{replacement}>")
end
%w[div p del blockquote pre code].each do |tag|
rich_text = rich_text.to_s.gsub(%r{<#{tag}.*?>|</#{tag}>}, '')
end
rich_text.gsub!(/<h1.*?>/, '')
rich_text.gsub!(%r{</h1>}, '<br><br>')
rich_text
end
end

View file

@ -1,14 +1,17 @@
prawn_document do |pdf|
pdf.text @report.name
pdf.markup_options = {
heading1: { style: :bold, size: 14, margin_bottom: 10, margin_top: 20 }
}
pdf.text @report.name, size: 22, style: :bold, align: :center
@report.elements.each do |element|
pdf.text element.title
pdf.text element.title, size: 16, style: :bold
pdf.text element.path
pdf.text prepare_rich_text(element.description_html), inline_format: true
pdf.markup prepare_rich_text(element.description_html.to_s), inline_format: true
element.success_criteria.each do |success_criterion|
pdf.text success_criterion.title
pdf.text prepare_rich_text(success_criterion.description_html), inline_format: true
pdf.text prepare_rich_text(success_criterion.comment_html), inline_format: true
pdf.text success_criterion.title, size: 14, style: :bold
pdf.markup prepare_rich_text(success_criterion.description_html.to_s), inline_format: true
pdf.markup prepare_rich_text(success_criterion.comment_html.to_s), inline_format: true
end
end
end