Permalink
Browse files

Initial version

  • Loading branch information...
0 parents commit cbb9f09183274567dc6fe4337b6c32a9a3f284ab @msaraiva msaraiva committed May 16, 2012
@@ -0,0 +1,5 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
+.DS_Store
@@ -0,0 +1,8 @@
+source "http://rubygems.org"
+
+ gem 'rspec', '>= 2.8.0'
+ gem 'rspec-rails', '>= 2.8.0'
+ gem 'rjb', '>= 1.4.0'
+ gem 'pdf-inspector', '>= 1.0.1', :require => "pdf/inspector"
+
+gemspec
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "jasper_rails/version"
+
+Gem::Specification.new do |s|
+ s.name = "jasper_rails"
+ s.version = JasperRails::VERSION
+ s.authors = ["Marlus Saraiva", "Rodrigo Maia"]
+ s.summary = %q{Rails and JasperReports integration}
+ s.description = %q{Generate pdf reports on Rails using Jasper Reports reporting tool}
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency('rspec', '>= 2.8.0')
+ s.add_dependency('rspec-rails', '>= 2.8.0')
+ s.add_dependency('rjb', '>= 1.4.0')
+ s.add_dependency('pdf-inspector', '>= 1.0.1')
+end
@@ -0,0 +1,105 @@
+require "jasper_rails/version"
+require "rails"
+require "rjb"
+require "rspec"
+require "pdf/inspector"
+
+module JasperRails
+
+ classpath = '.'
+ Dir["#{File.dirname(__FILE__)}/java/*.jar"].each do |jar|
+ classpath << File::PATH_SEPARATOR + File.expand_path(jar)
+ end
+
+ Rjb::load( classpath, ['-Djava.awt.headless=true','-Xms128M', '-Xmx256M'] )
+
+ JRException = Rjb::import 'net.sf.jasperreports.engine.JRException'
+ JasperCompileManager = Rjb::import 'net.sf.jasperreports.engine.JasperCompileManager'
+ JasperExportManager = Rjb::import 'net.sf.jasperreports.engine.JasperExportManager'
+ JasperFillManager = Rjb::import 'net.sf.jasperreports.engine.JasperFillManager'
+ JasperPrint = Rjb::import 'net.sf.jasperreports.engine.JasperPrint'
+ JRXmlUtils = Rjb::import 'net.sf.jasperreports.engine.util.JRXmlUtils'
+ JRXPathQueryExecuterFactory = Rjb::import 'net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory'
+ InputSource = Rjb::import 'org.xml.sax.InputSource'
+ StringReader = Rjb::import 'java.io.StringReader'
+ HashMap = Rjb::import 'java.util.HashMap'
+ ByteArrayInputStream = Rjb::import 'java.io.ByteArrayInputStream'
+ JavaString = Rjb::import 'java.lang.String'
+ JFreeChart = Rjb::import 'org.jfree.chart.JFreeChart'
+
+ module Jasper
+ module Rails
+ def self.render_pdf(jasper_file, datasource, parameters, options)
+ options ||= {}
+ parameters ||= {}
+ jrxml_file = jasper_file.sub(/\.jasper$/, ".jrxml")
+
+ begin
+ # Convert the ruby parameters' hash to a java HashMap.
+ # Pay attention that, for now, all parameters are converted to string!
+ jasper_params = HashMap.new
+ parameters.each do |k,v|
+ jasper_params.put(JavaString.new(k.to_s), JavaString.new(v.to_s))
+ end
+
+ # Compile it, if needed
+ if !File.exist?(jasper_file) || (File.exist?(jrxml_file) && File.mtime(jrxml_file) > File.mtime(jasper_file))
+ JasperCompileManager.compileReportToFile(jrxml_file, jasper_file)
+ end
+
+ # Fill the report
+ input_source = InputSource.new
+ input_source.setCharacterStream(StringReader.new(datasource.to_xml(options).to_s))
+ data_document = silence_warnings do
+ # This is here to avoid the "already initialized constant DOCUMENT_POSITION_*" warnings.
+ # It's harmless. But pretty annoying.
+ JRXmlUtils._invoke('parse', 'Lorg.xml.sax.InputSource;', input_source)
+ end
+
+ jasper_params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, data_document)
+ jasper_print = JasperFillManager.fillReport(jasper_file, jasper_params)
+
+ # Export it!
+ JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', jasper_print)
+ rescue Exception=>e
+ if e.respond_to? 'printStackTrace'
+ puts e.message
+ e.printStackTrace
+ else
+ puts e.message
+ puts e.backtrace
+ end
+ end
+ end
+ end
+ end
+
+ class ActionController::Responder
+ def to_pdf
+ jasper_file = "app/views/#{controller.controller_path}/#{controller.action_name}.jasper"
+
+ params = {}
+ controller.instance_variables.each do |v|
+ params[v.to_s[1..-1]] = controller.instance_variable_get(v)
+ end
+
+ controller.send_data Jasper::Rails::render_pdf(jasper_file, resource, params, options), :type => Mime::PDF
+ end
+ end
+
+end
+
+RSpec::Matchers.define :contain do |string|
+ match do |response|
+ @pdf = PDF::Inspector::Text::analyze(response.body)
+ @pdf.strings.include? string
+ end
+
+ failure_message_for_should do |response|
+ "expected that the PDF text would contain #{expected}. It only contains: #{@pdf.strings.to_s}"
+ end
+
+ failure_message_for_should_not do |response|
+ "expected that the PDF text would not contain #{expected}. But it does: #{@pdf.strings.to_s}"
+ end
+end
@@ -0,0 +1,4 @@
+# -*- encoding: utf-8 -*-
+module JasperRails
+ VERSION = "0.1.0"
+end
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit cbb9f09

Please sign in to comment.