RailsUsageExamples

tarc edited this page Sep 18, 2012 · 6 revisions
Clone this wiki locally

Here’s how you can use JasperServer-Client in your Rails app to generate reports:

Basic Controller Action

Request a report and output it to the user’s browser in PDF format:

ReportsController < ApplicationController
  def example_report
    output_format = "PDF"
    report_unit = "/example/my-report"
    report_params = params[:report_params] # this should be a Hash!

    client = JasperServer::Client.new("http://example.com/jasperserver/services/repository",
                                      "jasperadmin", "secret!")
    request = JasperServer::ReportRequest.new(report_unit, output_format, report_params)
    pdf_data = client.request_report(request)

    filename = params[:action]+" on "+Time.now.iso8601+"."+format.downcase
    send_data(pdf_data, :type => Mime::Type.lookup_by_extension(output_format.downcase), 
              :disposition => 'inline', :filename => filename)
  end
end

Generating Flash Reports

JasperServer 3.0 supports reports in Flash (SWF) format. This is a really nifty way to view reports online, since the Jasper Flash report viewer can be embedded directly into web pages.

Making this work is a bit complicated:

  1. First you’ll have to get a copy of the JasperReports flash viewer — a small .swf file that takes XML output form JasperServer and displays it as an Flash applet in the user’s browser. The file is bundled with JasperServer, but you can grab it directly from here: http://rubyforge.org/frs/download.php/43989/jasperreports-flash-3.swf
  2. Put the jasperreports-flash-3.swf in your Rails app’s /public directory.
  3. We will now need to set up two controller actions — one to render the page with the Flash applet, and another that the Flash applet will request its XML report data from:
ReportsController < ApplicationController
  def flash_report
    @data_url = url_for(:action => :flash_report_data)

    # since this template is very simple, we will render it inline...
    render :inline => %{
      <object width="500" height="600">
        <param name="movie" value="/jasperreports-flash-3.0.0.swf">
        <embed src="/jasperreports-flash-3.swf" flashvars="jrpxml=<%= CGI::escape @data_url %>" width="500" height="600">
      </object>
    }
  end

  # This is exactly like the 'example_report' we defined in the other example above;
  # the only difference is that our desired output format is "XML"
  def flash_report_data
    output_format = "XML"
    report_unit = "/example/my-report"
    report_params = params[:report_params] # this should be a Hash!

    client = JasperServer::Client.new("http://example.com/jasperserver/services/repository",
                                      "jasperadmin", "secret!")
    request = JasperServer::ReportRequest.new(report_unit, output_format, report_params)
    pdf_data = client.request_report(request)

    filename = params[:action]+" on "+Time.now.iso8601+"."+format.downcase
    send_data(data, :type => Mime::Type.lookup_by_extension(format.downcase), 
              :disposition => 'inline', :filename => filename)
  end
end