如何在Jasper报告中自动打印?

时间:2016-01-27 02:49:53

标签: ruby-on-rails jasper-reports ruby-on-rails-4.2

我们正在使用jasper gem在rails中使用jasper报告。

这是我们的代码,它的工作非常好:

jasper_rest_client.rb

def jasper_jidfile(report_dir, file)
    RestClient.get "#{jasper_url}/rest/resource/reports/#{report_dir}/#{file}", {:cookies => {"JSESSIONID" => jasper_login.cookies["JSESSIONID"]}}
end

def jasper_connect(args)
    format = args[:format]
    new_name = args[:new_name]
    file=args[:file]
    report_dir=args[:report]
    parameters=args[:parameters]

    response_login = jasper_login
    response_resource_doc = response_resource(report_dir, file, parameters)

    response_resources = response_resource_doc.to_s
    response_put_report = RestClient.put "#{jasper_url}/rest/report/reports/#{report_dir}/#{file}?RUN_OUTPUT_FORMAT=#{format.upcase}", response_resources, {:cookies => {"#{JID}" => response_login.cookies["#{JID}"]}}
    response_doc = REXML::Document.new response_put_report
    uuid = ''
    response_doc.elements.each("report/uuid") {|element| uuid = element.text}
    response_get_report = RestClient.get "#{jasper_url}/rest/report/#{uuid}?file=report", {:cookies => {"#{JID}" => response_login.cookies["#{JID}"]}}
    response.headers["Content-Type"] = "application/pdf"
    send_data response_get_report, :filename => "#{new_name}.#{format}", :type => :html, :disposition => "inline"
end


def response_resource(report_dir, file, parameters)
    responsed = REXML::Document.new jasper_jidfile(report_dir, file)
    responsed.elements.each("resourceDescriptor") do |el|
        parameters.each do |params, text|
            atr = REXML::Element.new "parameter"
            atr.attributes['name'] = "#{params}"
            atr.text = "#{text}"
            el.insert_after "label", atr
        end
    end
    return responsed
end

def response_resource_attributes(attributes)
    attributes      = attributes
    attributes_keys = attributes.keys
    raise "Invalid hash" if attributes_keys[0] != :report
    raise "Invalid hash" if attributes_keys[1] != :file
    report = attributes[:report]
    file   = attributes[:file]
    count  = 4
    response_doc = response_resource(report, file)
    response_doc.elements.each("resourceDescriptor") do |atr|
        atr.insert_after "label", response_parameter(attributes[attributes_keys[count]].first, attributes[attributes_keys[count]].last)
        count += 1
    end
    return response_doc
end

def response_parameter(param, text)
    atr = REXML::Element.new "parameter"
    atr.attributes['name'] = "#{param}"
    atr.text = text
    atr
end

sample_controller.rb

def print
    jasper_connect(:report => "sample_rpt", :file => "transaction_type_msts", :format => "pdf", :new_name => "transaction", :parameters => [["user_name", "#{current_user.first_name}"], ["report_name", "Transaction Type Masterlist"]])
end

我们计划在生成报告后直接打印报告。 谁知道怎么做?

0 个答案:

没有答案