Specify styling (font, in particular) for a certain cell in Prawn - ruby-on-rails

I want to specify a font style for a certain cell. What I found in the documentation is the capacity to do it for all the cell, but not for one I need:
table data, :cell_style => { :font => "Times-Roman", :font_style => :italic }
How do I do that for only one cell?

Turn your table into a block to do more configuration and then you can find it by row and column. See line 2 for your font, I included a couple other examples to set styles by rows & columns:
table(invoice_header_data, width: 210) do
style(row(0).column(0), font: "Times-Roman")
style(rows(0..-1), :padding => [2, 10, 2, 10], :borders => [])
style(row(4), padding: [12, 10], :font_style => :bold)
style(columns(1..3), :align => :right)
end

Related

Prawn::Errors::CannotFit Rails Make new line table

I am working on a web app in ruby on rails that should list presentations that a set of users can sign up for. At the end, the admin should be able to send an email to all users with an automatically generated PDF. I am using prawn to create this pdf.
The controller:
def generate_pdf(teac,pres)
Prawn::Document.new(:page_size => "A4", :page_layout => :landscape) do
text "#{teac.name} #{teac.surname}", align: :center
text "Your Presentations"
table([
["Name","Surname","Title","Room","Time","Date"],
[pres.collect{ |r| [r.name] },
pres.collect{ |r| [r.surname] },
pres.collect{ |r| [r.title] },
pres.collect{ |r| [r.room] },
pres.collect{ |r| [Time.at(r.time.to_i).utc.strftime("%H:%M")] },
pres.collect{ |r| [r.date] }]
])
move_down 20
pres.each do |pres|
text "visitors '#{pres.title}':"
text "#{pres.visitors}"
move_down 20
end
end.render
end
The problem occurs when the title is so long that the table expands beyond the landscape A4 page. Is there a way to automatically add a new line when the title becomes to long?
If you are okay with setting static column widths, that should to the trick.
For example:
table(data, column_widths: [100, 100, 100, 100, 100, 100])

How to set minimum height of row in prawn Table cell

I can set height for row. But it is fixed height. if content increases height does not increase automatically. just clip extra content.
Here is my code. how can i set default height 20 not fixed height
pdf.table([applicant_data],
:cell_style => {
:inline_format => true,
:padding => [3, 3,3,3], :size => 9,
:border_widths => [0.5, 0.5, 0.5, 0.5],
:height => 20,
#:font_style => :bold
},
:column_widths => {0 => 30, 1 => 110, 2 => 50, 3 => 110, 4 => col_wid_bo })
In the table block, you can set the row height:
pdf.table data do
rows(0..-1).each do |r|
r.height = 25 if r.height < 25
end
end
I solved the problem by setting height dynamically based on string length. If string fits in one line the default height , if not not increase height based on string length. That's it.

Prawn table not creating in Rails 4.0 and Ruby 2.0

I have the following table code:
member_id_and_first_name_and_nps_score_and_comments = [["14169021-00",
"Chris", 9, "YMCA in the Tampa, FL area is fantastic. I would like to
see the level of support for other sports also provided to
racquetball."], ["1660592-00", "Nayrone", 9, "There are so many
offerings for activities and there are many convenient locations
throughout the area."], ["16183029-00", "Mary", 9, "I like the staff
but the showers are gross"], ["16257833-00", "Phyllis", 10, "A
comfortable environment, a place where you can meet people and most
importantly the staff is always available."], ["1629846-00", "Joan",
10, "Cost, Distance , Family Friendly"], ["16204559-00", "Lisa-Marie",
5, "The facilities are fairly crowded which makes working out on
equipment or participating in classes cramped and difficult. "],
["16258576-00", "Michael", 7, "yhe cost way to much\r\n"],
["16161644-00", "April", 10, ""], ["1663157-00", "Mariela", 10, "Great
customer service, clean facilities and professional staff "],
["16114120-00", "Alison", 10, "The Y offers great programs and is
active in the local community. "], ["16177159-00", "Sheri", 10, "The Y
is a place for everything. It is a place for fitness, fellowship,
socialization, community, upward progretion of the youth, employment
opps..."]]
table([
["Member ID", "First Name", "NPS Score", "Comments"],
[member_id_and_first_name_and_nps_score_and_comments]
.transpose.reject{ |x| x[2].nil? }
],
:position => :center, :column_widths => {0 => 50, 1 => 60, 2 => 45, 3 => 285}) do
row(0).style :background_color => 'C0C0C0'
end
The table is generating with only the header row (Member ID, First Name, etc.). The actual member_id_and_first_name_and_nps_score_and_comments array is not populating in the column.
Any thoughts as to why this is?
Try:
table([
["Member ID", "First Name", "NPS Score", "Comments"],
*member_id_and_first_name_and_nps_score_and_comments.reject{ |x| x[2].nil? }
],
:position => :center, :column_widths => {0 => 50, 1 => 60, 2 => 45, 3 => 285}) do
row(0).style :background_color => 'C0C0C0'
end

Can't see the labels on Pie HighCharts

I'm using Ruby on Rails and HighCharts:
So here is the print-screen that I see when setting my highchart:
Here is the code:
#chart = LazyHighCharts::HighChart.new('pie') do |f|
f.chart({:defaultSeriesType=>"pie" , :margin=> [50, 200, 60, 170]} )
series = {
:type => 'pie',
:name => 'Browser share',
:data => [['say', 100],['may', 500]]
}
f.series(series)
f.options[:title][:text] = "Total spend on services"
f.legend(:layout=> 'vertical',:style=> {:left=> 'auto', :bottom=> 'auto',:right=> '50px',:top=> '100px'})
f.plot_options(:pie=>{
:allowPointSelect=>true,
:cursor=>"pointer" ,
:dataLabels=>{
:enabled=>true,
:color=>"white",
:style=>{
:font=>"13px Trebuchet MS, Verdana, sans-serif"
}
}
})
end
All is good, apart from I don't see the labels on the pie... It seems there is a small string below the chart, however the label is not initialized.
Anyone know how to solve this issue?
Is it because you have it set to white?
:color=>"white",

header and footer in Prawn PDF

I have read through all relevant posts on Prawn but found no mentioning (even in Prawn's own documentation) of headers and footers.
However, I did see a demo on Prawnto's own website about headers and footers. I copied the entire source of that demo just to see if it works but an error of undefined method "header" is complained about. Am I to understand that Prawn took out header and footer recently in the gem or is there something else I need to do first to use header and footer?
The demo page:
http://cracklabs.com/prawnto/code/prawn_demos/source/text/flowing_text_with_header_and_footer
the part of code of concern:
Prawn::Document.generate("flow_with_headers_and_footers.pdf") do
header margin_box.top_left do
text "Here's My Fancy Header", :size => 25, :align => :center
end
text "hello world!"
end
And by header, just in case, I mean the snippets of words that appear usually at a corner of every page of a document. Like your account number in your bills pages.
thanks!
The sample you are refering to, from the prawnto plugin, is using an older version of prawn.
Since i also needed header and footer i looked a bit more into this. It seems that that version of prawn had header and footer methods, which were implemented using lazy bounding box. (found by checking the code on github)
In the new prawn version you can do the same thing using repeaters.
Here is the full sample rewritten using the new version:
require "#{File.dirname(__FILE__)}/../example_helper.rb"
Prawn::Document.generate("test.pdf") do
repeat :all do
# header
bounding_box [bounds.left, bounds.top], :width => bounds.width do
font "Helvetica"
text "Here's My Fancy Header", :align => :center, :size => 25
stroke_horizontal_rule
end
# footer
bounding_box [bounds.left, bounds.bottom + 25], :width => bounds.width do
font "Helvetica"
stroke_horizontal_rule
move_down(5)
text "And here's a sexy footer", :size => 16
end
end
bounding_box([bounds.left, bounds.top - 50], :width => bounds.width, :height => bounds.height - 100) do
text "this is some flowing text " * 200
move_down(20)
font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf"
table [["ὕαλον ϕαγεῖν", "baaar", "1" ],
["This is","a sample", "2" ],
["Table", "dont\ncha\nknow?", "3" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules\nwith an iron fist", "x" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ],
[ "It", "Rules", "4" ]],
:font_size => 24,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :center,
:headers => ["Column A","Column B","#"]
end
end
you can check the documentation page of repeat for other options which allow you to exactly specify where you want the repeaters.
#GrantSayer thx for the example, but this will only let you show the current page number, not the total number of pages.
You can also use the number_pages function for the footer:
Prawn::Document.generate("page_with_numbering.pdf") do
text "Hai"
start_new_page
text "bai"
start_new_page
text "-- Hai again"
number_pages "<page> in a total of <total>", [bounds.right - 50, 0]
end
However, in my case I also need to format/style and right align the page numbers to match company style guides. I used go_to_page(k) to create my own header and footer functions, which add the header and footer to each page after all the pages are created. This gives me both styling options and the total number of pages:
Prawn::Document.generate("footer_example.pdf", :skip_page_creation => true) do
10.times do
start_new_page
text "Some filler text for the page"
end
# footer
page_count.times do |i|
go_to_page(i+1)
lazy_bounding_box([bounds.right-50, bounds.bottom + 25], :width => 50) {
text "#{i+1} / #{page_count}"
}.draw
end
end
It's little bit different with latest version of Prawn you must passe an hash
Prawn::Document.generate("page_with_numbering.pdf") do
text "Hai"
start_new_page
text "bai"
start_new_page
text "-- Hai again"
number_pages "<page> in a total of <total>", { :start_count_at => 0, :page_filter => :all, :at => [bounds.right - 50, 0], :align => :right, :size => 14 }
end
If you want a footer that do not write stuff over your text, you have to create the bounding_box below the margin of the document using bounds.bottom.
require 'prawn'
file_name = 'hello.pdf'
random_table = (0..50).map{|i|[*('a'..'z')]} # generate a 2D array for example (~2 pages)
Prawn::Document::generate(file_name) do |pdf|
pdf.table random_table
pdf.page_count.times do |i|
pdf.bounding_box([pdf.bounds.left, pdf.bounds.bottom], :width => pdf.bounds.width, :height => 30) {
# for each page, count the page number and write it
pdf.go_to_page i+1
pdf.move_down 5 # move below the document margin
pdf.text "#{i+1}/#{pdf.page_count}", :align => :center # write the page number and the total page count
}
end
end
It should look like that, you can see that the footer is outside the margin bottom :
Hope it help someone
START EDIT
This works in prawn >= 0.12
END EDIT
Here is my solution using repeat, canvas and cell. Essentially I'm drawing my bounding boxes at the absolute top and bottom of every page. I'm using cell to have better styling control over it. Hope this is going to be helpful to someone. ( I used slightly annoying colors to better illustrate how you can control styling of header and footer)
Prawn::Document.generate("headers_and_footers_with_background.pdf") do
repeat :all do
# header
canvas do
bounding_box([bounds.left, bounds.top], :width => bounds.width) do
cell :content => 'Header',
:background_color => 'EEEEEE',
:width => bounds.width,
:height => 50,
:align => :center,
:text_color => "001B76",
:borders => [:bottom],
:border_width => 2,
:border_color => '00FF00',
:padding => 12
end
end
# footer
canvas do
bounding_box [bounds.left, bounds.bottom + 50], :width => bounds.width do
cell :content => 'Footer',
:background_color => '333333',
:width => bounds.width,
:height => 50,
:align => :center,
:text_color => "FFFFFF",
:borders => [:top],
:border_width => 2,
:border_color => 'FF0000',
:padding => 12
end
end
end
# body
bounding_box([bounds.left, bounds.top - 25], :width => bounds.width, :height => bounds.height - 50) do
100.times do
text "Some filler text for the page"
end
end
end
here's the problem when using the bounding_box for creating the custom footer contents... it is still rendering within the bounds of a margin.
I was looking for something that will write the contents in the margin area together with number_pages.(because a footer usually is set in the bottom margin area)... and it seems that there were none.
so instead, I used text_box and place the coordinates outside my main bounding box like so:
repeat :all do
text_box "My custom footer", size: 7, align: :center, :at => [bounds.left, bounds.bottom], :height => 100, :width => bounds.width
end
take note that the repeat :all , will render this footer text to every page.
The only way I've found to get a repeating item on a page is to use the Prawn::Document::LazyBoundingBox method. Basically this allows you to define a bounding box that is only rendered once you call it. So the usual pseudo-code steps are
Define lazy bounding box element assign to somevar
On each new page call the element.
The example from the source shows how
file = "lazy_bounding_boxes.pdf"
Prawn::Document.generate(file, :skip_page_creation => true) do
point = [bounds.right-50, bounds.bottom + 25]
page_counter = lazy_bounding_box(point, :width => 50) do
text "Page: #{page_count}"
end
10.times do
start_new_page
text "Some filler text for the page"
page_counter.draw
end
end
This gives you a Page-count text output on each new page. What would be ideal is if this could be applied in the setup of a page template that is reused without the manual call to render the element. Combined with text flow this would give the traditional solution of headers and footers.

Resources