11

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?

Satchel
  • 16,414
  • 23
  • 106
  • 192

2 Answers2

11

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!

Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
  • 2
    so if I just add the inline_format => true it will take the HTML in the text area? – Satchel Feb 08 '11 at 18:02
  • I am using the prawnto plugin, I forgot, and so not accessing the PDF directly, just passing a view with a render pdf and prawnto is taking care of it...how can I passformatting that way? – Satchel Feb 08 '11 at 18:25
  • @Angela I hadn't heard of prawnto. I just checked it out, though, and it looks promising. And it looks like it still uses the original Prawn methods, just in a view, so you should be calling `pdf.text @textarea_text` somewhere in your view. Adding the `:inline_format => true` option like you said _should_ be all it takes. But no promises - I haven't tested it! – Xavier Holt Feb 08 '11 at 20:29
3

In response to bold text...

In controller:

respond_to do |format|
  format.pdf  {render :layout => false}
  prawnto :filename => @current_project+".pdf", :prawn => {:font => 'Times-Roman'}, :inline=>false     
end

Then in pdf.prawn file you can use:

in a text box:

pdf.text_box "Document Revisions", :size => 16, :style => :bold, :at => [0.mm,10.mm], :width => 100.mm, :height => 15.mm;

or in a line of text on its own:

pdf.text "Document Contents", :size => 16, :style => :bold;

As I understand it - but not tried it - to underline you need to do:

:styles => [:bold, :underline];

reference this link for more

This is not a feature of version 0.8.4 but version 0.10.2 - not sure how you would do underline in 0.8.4. I am not currently using 0.10.2 so can not confirm that this is works.

Based on what you have said I think this is what you want to for bold:

pdf.text_box "#{@yourtext.text}", :size => 16, :style => :bold, :at => [0.mm,10.mm], :width => 100.mm, :height => 15.mm;
Dorian
  • 7,749
  • 4
  • 38
  • 57
user596916
  • 116
  • 4
  • :styles => [:bold, :underline]; does not work in prwan 0.8.4 but is in latest version on github. so not sure which version you are using. I am using 0.8.4 and while :style => :bold works the others do not. Have edited answer to cover this and further question. – user596916 Feb 09 '11 at 20:58