jasper-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rspec', '>= 2.8.0'
4
+ gem 'rspec-rails', '>= 2.8.0'
5
+ gem 'rjb', '>= 1.4.0'
6
+ gem 'pdf-inspector', '>= 1.0.1', :require => "pdf/inspector"
7
+
8
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jasper-rails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jasper-rails"
7
+ s.version = JasperRails::VERSION
8
+ s.authors = ["Marlus Saraiva", "Rodrigo Maia"]
9
+ s.summary = %q{Rails and JasperReports integration}
10
+ s.description = %q{Generate pdf reports on Rails using Jasper Reports reporting tool}
11
+ s.email = "rodrigomaia@grupofortes.com.br"
12
+ s.homepage = "https://github.com/fortesinformatica/jasper-rails"
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_dependency('rspec', '>= 2.8.0')
20
+ s.add_dependency('rspec-rails', '>= 2.8.0')
21
+ s.add_dependency('rjb', '>= 1.4.0')
22
+ s.add_dependency('pdf-inspector', '>= 1.0.1')
23
+ end
@@ -0,0 +1,105 @@
1
+ require "jasper-rails/version"
2
+ require "rails"
3
+ require "rjb"
4
+ require "rspec"
5
+ require "pdf/inspector"
6
+
7
+ module JasperRails
8
+
9
+ classpath = '.'
10
+ Dir["#{File.dirname(__FILE__)}/java/*.jar"].each do |jar|
11
+ classpath << File::PATH_SEPARATOR + File.expand_path(jar)
12
+ end
13
+
14
+ Rjb::load( classpath, ['-Djava.awt.headless=true','-Xms128M', '-Xmx256M'] )
15
+
16
+ JRException = Rjb::import 'net.sf.jasperreports.engine.JRException'
17
+ JasperCompileManager = Rjb::import 'net.sf.jasperreports.engine.JasperCompileManager'
18
+ JasperExportManager = Rjb::import 'net.sf.jasperreports.engine.JasperExportManager'
19
+ JasperFillManager = Rjb::import 'net.sf.jasperreports.engine.JasperFillManager'
20
+ JasperPrint = Rjb::import 'net.sf.jasperreports.engine.JasperPrint'
21
+ JRXmlUtils = Rjb::import 'net.sf.jasperreports.engine.util.JRXmlUtils'
22
+ JRXPathQueryExecuterFactory = Rjb::import 'net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory'
23
+ InputSource = Rjb::import 'org.xml.sax.InputSource'
24
+ StringReader = Rjb::import 'java.io.StringReader'
25
+ HashMap = Rjb::import 'java.util.HashMap'
26
+ ByteArrayInputStream = Rjb::import 'java.io.ByteArrayInputStream'
27
+ JavaString = Rjb::import 'java.lang.String'
28
+ JFreeChart = Rjb::import 'org.jfree.chart.JFreeChart'
29
+
30
+ module Jasper
31
+ module Rails
32
+ def self.render_pdf(jasper_file, datasource, parameters, options)
33
+ options ||= {}
34
+ parameters ||= {}
35
+ jrxml_file = jasper_file.sub(/\.jasper$/, ".jrxml")
36
+
37
+ begin
38
+ # Convert the ruby parameters' hash to a java HashMap.
39
+ # Pay attention that, for now, all parameters are converted to string!
40
+ jasper_params = HashMap.new
41
+ parameters.each do |k,v|
42
+ jasper_params.put(JavaString.new(k.to_s), JavaString.new(v.to_s))
43
+ end
44
+
45
+ # Compile it, if needed
46
+ if !File.exist?(jasper_file) || (File.exist?(jrxml_file) && File.mtime(jrxml_file) > File.mtime(jasper_file))
47
+ JasperCompileManager.compileReportToFile(jrxml_file, jasper_file)
48
+ end
49
+
50
+ # Fill the report
51
+ input_source = InputSource.new
52
+ input_source.setCharacterStream(StringReader.new(datasource.to_xml(options).to_s))
53
+ data_document = silence_warnings do
54
+ # This is here to avoid the "already initialized constant DOCUMENT_POSITION_*" warnings.
55
+ # It's harmless. But pretty annoying.
56
+ JRXmlUtils._invoke('parse', 'Lorg.xml.sax.InputSource;', input_source)
57
+ end
58
+
59
+ jasper_params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, data_document)
60
+ jasper_print = JasperFillManager.fillReport(jasper_file, jasper_params)
61
+
62
+ # Export it!
63
+ JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', jasper_print)
64
+ rescue Exception=>e
65
+ if e.respond_to? 'printStackTrace'
66
+ puts e.message
67
+ e.printStackTrace
68
+ else
69
+ puts e.message
70
+ puts e.backtrace
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ class ActionController::Responder
78
+ def to_pdf
79
+ jasper_file = "app/views/#{controller.controller_path}/#{controller.action_name}.jasper"
80
+
81
+ params = {}
82
+ controller.instance_variables.each do |v|
83
+ params[v.to_s[1..-1]] = controller.instance_variable_get(v)
84
+ end
85
+
86
+ controller.send_data Jasper::Rails::render_pdf(jasper_file, resource, params, options), :type => Mime::PDF
87
+ end
88
+ end
89
+
90
+ end
91
+
92
+ RSpec::Matchers.define :contain do |string|
93
+ match do |response|
94
+ @pdf = PDF::Inspector::Text::analyze(response.body)
95
+ @pdf.strings.include? string
96
+ end
97
+
98
+ failure_message_for_should do |response|
99
+ "expected that the PDF text would contain #{expected}. It only contains: #{@pdf.strings.to_s}"
100
+ end
101
+
102
+ failure_message_for_should_not do |response|
103
+ "expected that the PDF text would not contain #{expected}. But it does: #{@pdf.strings.to_s}"
104
+ end
105
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module JasperRails
3
+ VERSION = "0.1.0"
4
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jasper-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marlus Saraiva
9
+ - Rodrigo Maia
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-05-16 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &70277456604960 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 2.8.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70277456604960
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec-rails
28
+ requirement: &70277456604480 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.8.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70277456604480
37
+ - !ruby/object:Gem::Dependency
38
+ name: rjb
39
+ requirement: &70277456603940 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: 1.4.0
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *70277456603940
48
+ - !ruby/object:Gem::Dependency
49
+ name: pdf-inspector
50
+ requirement: &70277456603480 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 1.0.1
56
+ type: :runtime
57
+ prerelease: false
58
+ version_requirements: *70277456603480
59
+ description: Generate pdf reports on Rails using Jasper Reports reporting tool
60
+ email: rodrigomaia@grupofortes.com.br
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - Gemfile
67
+ - Rakefile
68
+ - jasper-rails.gemspec
69
+ - lib/jasper-rails.rb
70
+ - lib/jasper-rails/version.rb
71
+ - lib/java/commons-beanutils-1.8.2.jar
72
+ - lib/java/commons-collections-3.2.1.jar
73
+ - lib/java/commons-digester-2.1.jar
74
+ - lib/java/commons-logging-1.1.jar
75
+ - lib/java/groovy-all-1.7.5.jar
76
+ - lib/java/iText-2.1.7.jar
77
+ - lib/java/jasperreports-4.5.1.jar
78
+ - lib/java/jcommon-1.0.15.jar
79
+ - lib/java/jfreechart-1.0.12.jar
80
+ - lib/java/xalan.jar
81
+ homepage: https://github.com/fortesinformatica/jasper-rails
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.17
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Rails and JasperReports integration
105
+ test_files: []