Question

I've got a PDF that's generated through Prawn with HTML code in it. For each <br> or pair of <br>s I want to insert a new paragraph with Prawn's text method. Given this string:

sharemarket is a minnow by world standards, at just 2.5 per cent of the global sharemarket value. <br><br>One approach for local investors might be via an international share fund, with the fund manager

I want to at the <br><br> to end the current paragraph which is made like this:

pdf.text "sharemarket is a minnow by world standards, at just 2.5 per cent of the global sharemarket value."

Then start a new text from this point:

 pdf.text "One approach for local investors might be via an international share fund, with the fund manager"

Any help would be hugely appreciated. Bit stuck here.

Was it helpful?

Solution

You can split the text into an array:

> str = 'sharemarket is a minnow by world standards, at just 2.5 per cent of the global sharemarket value. <br><br>One approach for local investors might be via an international share fund, with the fund manager'
=> "sharemarket is a minnow by world standards, at just 2.5 per cent of the global sharemarket value. <br><br>One approach for local investors might be via an international share fund, with the fund manager"
> rows = str.split('<br><br>')
=> ["sharemarket is a minnow by world standards, at just 2.5 per cent of the global sharemarket value. ", "One approach for local investors might be via an international share fund, with the fund manager"]

And then, write it to the pdf:

rows.each {|row| pdf.text row}

OTHER TIPS

You could also just add :inline_format => true to the element in the PDF document:

  text "#{@text_area}", {
    :inline_format => true
  }

This automatically allows for <br> tags and other HTML formatting tags

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top