views:

20

answers:

2

I have this in my controller. I need my view to generate some HTML, which I then convert to a pdf using Flying Saucer (Im using JRuby). This then gets returned to the client.

  def show    
    @cs_agreement = CsAgreement.find(params[:id])

    respond_to do |format|
      format.pdf do
        # TODO there must be a better way of getting the path to the view!
        report_template = ERB.new(File.new("app/views/agreement_document/client_agreement.erb"), nil, "%<")
        created_report = report_template.result(binding)

        send_data( FlyingSaucer::create_pdf(created_report), :filename => "agreement.pdf",
                   :type => "application/pdf",
                   :disposition => 'inline')
      end
    end

This is the best I can do... I can only work out how to use ERB to manually generate the html so I can retreive the data to send to FlyingSaucer before returning.

This way also seems to mean I cant access any of the Helper methods.

Is there no way I can get rails to generate the html for me and somehow intercept it to convert to pdf before returning? The render function seems to just generate and return all in one..

+1  A: 

You can use render_to_string, as in

report_template = render_to_string 'client_agreement'

Same options as render. More details here.

If you need to specify a different controller / action, you can do that too, eg via :action => :action_name.

Peter
Works beautifully! Thanks.
Mongus Pong
+1  A: 

Use render_to_string.

def show    
  @cs_agreement = CsAgreement.find(params[:id])

  respond_to do |format|
    format.pdf do
      result = render_to_string
      send_data( FlyingSaucer::create_pdf(result), :filename => "agreement.pdf",
               :type => "application/pdf",
               :disposition => 'inline')
    end
  end
end
KandadaBoggu