How can I do inline formatting (bold and underline) for PDF generated by Prawnto in Rails?

Satchel picture Satchel · Feb 8, 2011 · Viewed 13.9k times · Source

I generate a PDF file using Prawn and the Prawnto plugin in my rails application.

I create a standard form with a standard textarea, and submit that as the body of the PDF file.

However, I need to be able to format words and sentences with:

  • bold
  • underline
  • maybe different type sizes

I want to be able to do it from within the textarea input box. Right now, because I use prawnto, I basically am genering a view which outputs what is in the textarea.

But if I put, say, bold in the text area, it doesn't format, it just renders.

How do I do this?

Answer

Xavier Holt picture Xavier Holt · Feb 8, 2011

we may have similar apps...

Prawn can do basic inline formatting based on (simple) HTML - take a look at the text/inline_format.rb example on github. In fact, take a look at the whole Prawn Example Gallery if you haven't - it's one of the best I've seen.

To get the HTML you need you can either type HTML straight into the textarea (a bit ugly - might not be a good idea if anyone besides you will be entering text) or use something like Markdown to interpret more user-friendly "style codes" like StackOverflow does. I think BlueCloth is the best-known ruby implementation, but I've never used it myself.

Bold and underline? No problem. Font size might be harder - I imagine it would be tricky to get BlueCloth to emit something like the (deprecated) < font > tag they use in the Prawn example...

Hope this helps - cheers!