PDF report seems to be cached in rails

46 views
Skip to first unread message

Joshua Moore

unread,
Jun 24, 2010, 12:13:54 PM6/24/10
to ruby-r...@googlegroups.com
Hi,

I am working to generate some PDF reports in our Rails application with Ruport. I created an initial prototype outside of Rails that works fine. The problem happened when I moved it into Rails, now whenever I generate the report it will run the formatter another time. For example if I have generated the report 3 times my complete report will appear three times in the PDF. In addition it is not a cache of the previously generated reports being kept around. If I change the data that I am generating the report off of then all of the repeated reports will be changed. I have pasted the report below, does anybody have any ideas of what might be going own? Thanks in advance.

Josh


Code:
class Reports::NewPatientController < ApplicationController

filter_access_to :all, :context => :payments

layout false

def show
table = nil
table = Ruport::Data::Table.load('public/rancho new patients by clinic.csv')
data = nil
data = FormattedController.render_pdf(:data => table, :header_text => 'Rancho Physical Therapy')
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
send_data(data,
{:type => 'application/pdf',
:filename => 'rancho new patients by clinic.pdf',
:disposition => 'inline'})
end

end


class FormattedController < Ruport::Controller::Table
stage :header, :body

def setup
table = nil
table = self.data
data_column_names = table.column_names - [table.column_names[0]]

table.add_column('Total') {|r|
t = 0
data_column_names.each {|n| t += r[n].to_i}
t
}
table << ['','','','','']
column_totals = ['Total']
data_column_names = table.column_names - [table.column_names[0]]

data_column_names.each do |name|
t = 0
table.column(name).collect{|e| t += e.to_i}
column_totals << t
end
table << column_totals
table.add_column('', :default => ' ', :position => 1)
table.add_column(' ', :default => ' ', :position => table.column_names.count - 1)

self.data= table
end
end

class FormattedPDF < Ruport::Formatter::PDF
renders :pdf, :for => FormattedController

def build_header
pdf_writer().select_font "Helvetica-Bold"

add_text options.header_text, :font_size => 20, :justification => :center

pdf_writer.select_font "Helvetica"

add_text "New Patients - By Clinic\n",:font_size => 16, :justification => :center
add_text "Jan 1, 2010 to Apr 30, 2010\n",:font_size => 16, :justification => :center
add_text "\n"
end

def build_body
build_table_body
end
end

Bob N

unread,
Jun 25, 2010, 5:19:42 AM6/25/10
to Ruby Reports
Hi,

Let me see if I understand correctly. When you generate a PDF, it will
create the file. When you hit the show action again it will append the
data to the PDF that was already generated?

Out of curiosity, did you try to remove the lines where you're setting
the response headers and see if you get the same behavior? I have a
project where I'm almost doing the same thing as you, but I'm not
setting the headers and it works fine.

Also, which versions of Ruport & Rails are you using? I'll try to re-
produce the error.

Regards,
Bob

Joshua Moore

unread,
Jun 25, 2010, 1:01:26 PM6/25/10
to ruby-r...@googlegroups.com
Hi,

Actually this happens with any type of formatter that I am creating custom. I am using rails 2.3.5 and ruport 1.6.3. I have done some debugging and what I have found is that the build methods in the formatter are being called the number of times that I have generated the report since Loading the rails application. I found a solution. Once I moved the code out of the rails controller I worked fine. I think something with the fact that Rails reloads the controllers for every request. Now that it is only being loaded once with all the stuff from the lib directory it is working great. I am not sure if that is a bug or is just how it works.

Josh

> --
> You received this message because you are subscribed to the Google Groups "Ruby Reports" group.
> To post to this group, send email to ruby-r...@googlegroups.com.
> To unsubscribe from this group, send email to ruby-reports...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ruby-reports?hl=en.
>

Bob Nadler

unread,
Jun 25, 2010, 1:25:11 PM6/25/10
to ruby-r...@googlegroups.com
Ahh. Didn't think of that. I think what happens is that when Rails is
in development mode it reloads for each request by design. That way
you can make changes to controllers, routes, etc. without restarting
the server. If you set the Rails ENV to production it shouldn't
happen. You're right though, you should put the report generation into
/lib or a /reports folder and just call it from the controller.

--Bob

Reply all
Reply to author
Forward
0 new messages