ruby-on-railsrubypdfprawn

Prawn PDF change font size and line height from html_text


In my Rails 6 app with Ruby 2.7 I'm using gem prawn to generate a pdf file. So far everything has been rather straight forward. I'm having a problem defining line height in html_text. Like in a method below:

  def invoice_notes_section
    invoice_notes.each { |note| html_text(note) }
  end

  def invoice_notes
    @invoice_notes ||= begin
      notes = []
      notes << (invoice.display_service_period? ? invoice.service_period_text : I18n.t('pdf.no_service_period'))
      notes << invoice.discount_text if invoice.display_discount_text?
      notes << invoice.notes
      notes << I18n.t('pdf.small_entrepreneur_hint') if invoice.customer.company.small_entrepreneur?
      notes.compact
    end

I want to change font size to 7pt and line height to 9pt. How to do that since I can't simply add e.g.:

      notes << invoice.notes, size: 7, leading: 9

Which give me SyntaxError:

error, unexpected ',', expecting `end'
      notes << invoice.notes, size: 7, leading: 9
                            ^
):

In this section I'm trying to achieve a text block which will look like this:

Desired text block

EDIT:

  def html_text(text)
    return if strip_tags(text).strip.empty?

    if text.include?('>') && text.include?('</')
      styled_text
    else
      text text
    end
  rescue Prawn::Errors::UnknownFont
    text text, size: BODY_FONT_SIZE
  end

If I replace if block lines to be like:

if text.include?('>') && text.include?('</')
  styled_text text, size: 7, leading: 9
else
  text text, size: 7, leading: 9
end

I'm getting an error ArgumentError (wrong number of arguments (given 2, expected 1)): for line with styled_text text, size: 7, leading: 9. What is that styled_text? If I change it to text I will get:

Das Leistungsdatum entspricht dem Rechnungsdatum sofern nichts anderes erwähnt wird.
    
<p>Bitte überweisen Sie den Betrag bis zum oben angegebenen Fälligkeitsdatum auf das angegebene Bankkonto unter Angabe

der Rechnungsnummer</p>

instead of:

Das Leistungsdatum entspricht dem Rechnungsdatum sofern nichts anderes erwähnt wird.
    
Bitte überweisen Sie den Betrag bis zum oben angegebenen Fälligkeitsdatum auf das angegebene Bankkonto unter Angabe
der Rechnungsnummer

Solution

  • I think you have answered your question with respect to what styled_text is (in that it comes from the prawn-styled-text gem).

    Furthermore, the styled_text method only accepts a single argument, so you can't provide additional options like you did with size: 7, leading: 9.

    If you want to change the font size and leading, you will have to modify the HTML fragments themselves. I guess it should work if you do something like this instead of the whole if clause:

    styled_text("<div style='font-size: 7; line-height: 9'>#{text}</div>")