Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 333
Satchel Avatar asked Feb 08 '11 03:02

Satchel


2 Answers

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!

like image 145
Xavier Holt Avatar answered Nov 07 '22 00:11

Xavier Holt


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;
like image 38
user596916 Avatar answered Nov 07 '22 00:11

user596916