Rails Prawn PDF Header With Vertical Text - ruby-on-rails

I have a functioning prawn document generated in rails. I built the table using text_box() to wrap the text in the header in order to rotate the text_box, and thus the text, 90 degrees. It works great, until the header reproduces on the next page...in which case the table header is intact but the text (inside the text_box()) is not reproduced because of how each text_box is positioned. Im wondering if anyone has a better idea for rotation the text so that it is simply in the table head. I couldn't get anything other than this method to produce what I want.
You can view my functioning PDF here...
http://flightrecord.herokuapp.com/vfrlarge.pdf?rating=6
The table data is quit extensive, so Im only gonna include how Im generating the table header:
def flights_table
table **flight_data_rows**, :width => 1200 do
self.header = true
def **flight_data_rows**
y_pos = 518
height = 500
x = 155
size = 7
[[
text_box("Lesson", :at => [5, y_pos], :rotate => 90, :size => size),
text_box("Date", :at => [24, y_pos+6], :size => size),
text_box("CFI", :at => [66, y_pos+6], :size => size),
text_box("N# / Type", :at => [109, y_pos+6], :size => size),
text_box("Total Time", :at => [x, y_pos], :rotate => 90, :size => size, :width => 300),
]] +
#data.map do |data|
[
data.the_data,
data.the_data,
data.the_data,
data.the_data,
data.the_data
]

This doesn't seem to be a viable option in prawn as of 1.0.0rc1. I tried many approaches, but none worked. I finally just removed the header row completely...in hopes of finishing this project later. I'll be giving wicked_pdf a shot.

Related

Rails + Cloudinary Gem - How to add rounding step to cl_image_tag?

I'm using the cloudinary gem and cl_image_tag helper method to display my images.
<%= cl_image_tag(picture.image, :width => :auto, :dpr => :auto, :responsive_placeholder => "blank") %>
As you can see, I'm adding the :width => :auto option in the helper method. This helper will create a link of this sort
<img data-src="http://res.cloudinary.com/travelcrumbs/image/upload/dpr_auto,w_auto/v1470821928/hero%20image/footsteps.jpg" class="cld-responsive">
The :width => :auto option creates a w_auto request in the created URL.
This way an image with appropriate (depending on the container) width is being requested from the cloudinary CDN. But I would like to change the rounding step to 200px (so that it only requests a different image every 200px.) and need to get a w_auto:200 request in the URL.
How do I do that?
The width parameter should be set to "auto:200". This can be done by switching to something like:
<%= cl_image_tag(picture.image, :width => "auto:200", :dpr => :auto, :responsive_placeholder => "blank") %>

Image in asset pipeline not found

I have seen a number of close problems, but have not been able to find a solution to get solve my problem. I am using Ruby 1.9.3 with Rails 3.2.13.
I am using Prawn to generate a PDF, and I a logo to appear. The logo is found without a problem in the development environment, but fails in the production environment, which is on Heroku.
In my controller I have:
def generate_risk_pdf(risk)
Prawn::Document.new do
image "#{Rails.root}/app/assets/images/logo-print.png", :scale => 0.75 if Rails.env.development?
image "#{ActionController::Base.helpers.asset_path('logo-print.png')}", :scale => 0.75 if Rails.env.production?
move_down 2
formatted_text [{:text => Exercise Australia", :styles => [:bold], :size => 20}]
font_size 6
bounding_box([bounds.right - 150, bounds.bottom], :width => 240, :height => 20) do
text "Copyright: Exercise Australia, #{Date.today.year}"
end
end.render
end
The Heroku logs have
ArgumentError (/assets/logo-print-5b6da7bd4012d953bc1bc749cf934d91.png not found):
app/controllers/single_entries_controller.rb:309:in `block in generate_risk_pdf'
Which corresponds to the line above with Rails.env.production?
If I append the URL to /assets/logo-print-5b6da7bd4012d953bc1bc749cf934d91.png the image file exists.
If I remove that line, it works (minus the logo).
Please note this is not only failing for Prawn, but even when I tried to use an image inline for an email.
Please help, I'm tearing my hair out!
Added: Serving from public works- but it is not my preferred solution.
Try add this to:
rails.root/config/environments/produciton.rb
config.serve_static_assets = true
config.assets.compile = true
A combination of both methods worked for me :
image "#{Rails.root}/app/assets#{ActionController::Base.helpers.asset_path('images/logo-print.png')}"

What takes the time when doing a Rails CSV import to Postgres?

I have a CSV with 950 rows that I'm importing into Postgres via Rails using the following controller code:
io = params[:file]
trade_plans = []
CSV.foreach(io.tempfile, { :col_sep => "|" }) do |row|
if row.length == 16
trade_plans << create_trade_plan(row)
end
end
TradePlan.create(trade_plans)
def create_trade_plan(row)
number_of_shares = row[10].to_i.abs
return {
:user_id => current_user.id,
:symbol => row[2].upcase,
:symbol_long_name => row[3].upcase,
:direction => STYPE_MAP[row[5]][1],
:number_of_shares => number_of_shares,
:description => "Trade #{row[1]} on #{format_datetime(row[7], row[8])}",
:trade_status => 'closed',
:is_potential => false,
:planned_entry_price => 0,
:planned_target_price => 0,
:planned_stop_loss_price => 0,
:without_trade_plan => true
}
end
I'm using activerecord's bulk create method, though from this answer it would seem that bulk insert isn't supported for Postgres... anyway, what I'd like to know is that my Rails logs give me this:
Completed 200 OK in 315855ms (Views: 1280.1ms | ActiveRecord: 3341.2ms)
If there were only 3.3 seconds of AR, how did the import take 315 seconds for 950 rows? There's a few callbacks and some validations but that seems a lot to me. Is 0.33 seconds per record a reasonable expectation?
Ruby processing also takes time. Use benchmarking to see what part of the code takes time.

Ruby-Rails Spreadsheet "corrupted" under Microsoft Excel 2010

I am using Spreadsheet gem to allow users to export data with .xls format. The code I am showing is allowing users to open the file in Open Office and MS Excel for MAC, but not for Windows, which I get a "file is corrupted" error.
I have a method that do the XLS export like this:
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet(:name => "Expired Activity")
# Title
title_format = Spreadsheet::Format.new(:size => 12, :pattern => 1, :pattern_fg_color => :blue, :align => :merge)
for i in 0..5
sheet1.row(0).set_format(i, title_format)
end
sheet1.row(0).push "Activity"
# Header row
header_format = Spreadsheet::Format.new(:size => 11, :pattern => 1, :pattern_fg_color => :yellow, :align => :merge)
index_row = sheet1.row(1)
index_row.push("Date")
index_row.push("First Name")
index_row.push("Last Name")
for i in 0..2
index_row.set_format(i, header_format)
end
# Row values
row_iterator = 2
#expired.each do |expired|
sheet1.row(row_iterator).push(expired.earnedOn, expired.firstName, expired.lastName)
row_iterator = row_iterator + 1
end
file_io = StringIO.new
book.write file_io
file_io.string
And this method is being called like this:
format.xls {#expired = current_user.recent_activity(unlimited_size,params[:start],nil);
send_data(build_excel_expired_activity, :filename => "#{Time.now.strftime('%Y-%m-%d')}_Expired_Activity.xls", :type => "application/xls", :disposition => 'attachment')
}
Any thoughts, or ideas?
You might want to have a look at the axlsx gem and its rails counterpart acts_as_xlsx. It supports full schema validation so you don't have to fight with this kind of thing.
https://www.ruby-toolbox.com/projects/axlsx

prawnto displaying tables that don't break when new page

I have a variable number of tables with variable number of rows and I want to have them displaying one after the other but if a table doesn't fit on the current page put it on the next then continue on. I have put the table in a transaction so I can roll back then print it if the height will fit on curent page but how do I get the table height?
I have this code at the moment
pdf.transaction do
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
pdf.move_down 20
#pdf.rollback
end
Any help on this would be great. Or any other way to do this ?
Best Regards
Rick
4 years later... :)
As #m-x wrote it, rollback was disabled for security reason, like "group", and is still not implemented. So, here how I deal with break pages for tables :
Big and simple table (one row per data)
Just use header option
pdf.table #data,
header: true, # You can use 'header: 2' if your header take two rows
font_size: 12,
border_style: :grid,
horizontal_padding: 10,
vertical_padding: 3,
border_width: 2,
position: :left,
row_colors: ["FFFFFF","DDDDDD"]
Small table or complexe table
make table
check if you need break page
draw table
With your example :
t = pdf.make_table #data,
font_size: 12,
border_style: :grid,
horizontal_padding: 10,
vertical_padding: 3,
border_width: 2,
position: :left,
row_colors: ["FFFFFF","DDDDDD"]
if cursor - t.height < 0
start_new_page
end
t.draw
Hope that helps
#current_page = pdf.page_count
#roll = pdf.transaction do
pdf.move_down 20
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
pdf.rollback if pdf.page_count > #current_page
end
if #roll == false
pdf.start_new_page
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
end
I hope this works for you as for me :-)
I'm a Prawn beginner, so this might not be the best solution, but it should work.
You can get the table height if you consider the font size and vertical padding and the number of records you have in #data and you can get the current cursor position by calling Prawn::Document.cursor method.
Having these two numbers you should be able to check whether the table fits on this page or not. If not, just start a new one (by calling Prawn::Document.start_new_page method).
Otherwise the table will break automatically and will continue on the next page.
Thanks Igor
I am currently setting the current page and then in the transaction after the new table has been rendered and before the roll back setting new_page variable. Then i can roll back and chek if the new page var > current page var and if it is start new page and print the table. See code below.
The problem is now the pdf.start_new_page says error but if i just take the pdf.rollback line out it works. See error below.
Any ideas any one or any easier ways, there must be one!!
thanks
rick
#current_page = pdf.page_count
pdf.transaction do
pdf.move_down 20
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
#the_next_page = pdf.page_count
pdf.rollback
end
if #the_next_page > #current_page
pdf.start_new_page
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
end
The error
> You have a nil object when you didn't expect it!
The error occurred while evaluating nil.identifier
Extracted source (around line #158):
155: end
RAILS_ROOT: C:/InstantRails/rails_apps/Macrotec-Application
Application Trace | Framework Trace | Full Trace
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:302:in `go_to_page'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:128:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `each'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:344:in `render'
C:/InstantRails/rails_apps/Macrotec-Application/app/views/quotations/show.pdf.prawn:158:in `_run_prawn_app47views47quotations47show46pdf46prawn'

Resources