rprince 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ tmp
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1 @@
1
+ WARNING: This assumes you have Prince 7.0 and considerably modifies the API found in Princely.
data/Rakefile ADDED
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rprince"
8
+ gem.summary = %Q{A minimal Rails/gem wrapper for the PrinceXML PDF generation library.}
9
+ gem.description = %Q{A minimal wrapper for the PrinceXML PDF generation library.}
10
+ gem.email = "timon.karnezos@gmail.com"
11
+ gem.homepage = "http://github.com/timonk/rprince"
12
+ gem.authors = ["Timon Karnezos"]
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ task :default => :test
27
+
28
+ require 'rake/rdoctask'
29
+ Rake::RDocTask.new do |rdoc|
30
+ if File.exist?('VERSION')
31
+ version = File.read('VERSION')
32
+ else
33
+ version = ""
34
+ end
35
+
36
+ rdoc.rdoc_dir = 'rdoc'
37
+ rdoc.title = "princely #{version}"
38
+ rdoc.options << '--line-numbers' << '--inline-source'
39
+ rdoc.rdoc_files.include('README*')
40
+ rdoc.rdoc_files.include('lib/**/*.rb')
41
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/rails/init'
@@ -0,0 +1,50 @@
1
+ module PdfHelper
2
+ require 'rprince'
3
+
4
+ def rprince_pdf(options = {})
5
+ options[:stylesheets] ||= []
6
+ options[:layout] ||= false
7
+ options[:template] ||= File.join(controller_path, action_name)
8
+
9
+ unless options.has_key?(:normalize_sources)
10
+ options[:normalize_sources] = true
11
+ end
12
+
13
+ prince = RPrince.new()
14
+
15
+ # Sets style sheets on PDF renderer
16
+ prince.style_sheets = options[:stylesheets].map{|style| stylesheet_file_path(style) }
17
+ html_string = render_to_string(:template => options[:template], :layout => options[:layout])
18
+
19
+ if options[:normalize_sources]
20
+ # Make all paths relative, on disk paths...
21
+ html_string.gsub!( /src=["']+([^:]+?)["']/i ) { |m| %{src="#{RAILS_ROOT}/public/#{$1}"} }
22
+ end
23
+
24
+ # Remove asset ids
25
+ html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| %{src="#{$1.split('?').first}"} }
26
+
27
+
28
+ # Send the generated PDF file from our html string.
29
+ if options[:file]
30
+ prince.html_string_to_pdf_file(html_string, options[:file])
31
+ else
32
+ prince.html_string_to_pdf_string(html_string)
33
+ end
34
+ end
35
+
36
+ def send_rprince_pdf(pdf_name, options = {})
37
+ options.delete(:file)
38
+ send_data(rprince_pdf(options),
39
+ :filename => "#{pdf_name}.pdf",
40
+ :type => 'application/pdf'
41
+ )
42
+ end
43
+
44
+ private
45
+
46
+ def stylesheet_file_path(stylesheet)
47
+ stylesheet = stylesheet.to_s.gsub(".css","")
48
+ File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, "#{stylesheet}.css")
49
+ end
50
+ end
@@ -0,0 +1,6 @@
1
+ require 'rprince'
2
+ require 'rprince/pdf_helper'
3
+
4
+ Mime::Type.register 'application/pdf', :pdf
5
+
6
+ ActionController::Base.send(:include, PdfHelper)
data/lib/rprince.rb ADDED
@@ -0,0 +1,93 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+ require 'logging'
3
+
4
+ class RPrince
5
+ attr_accessor :style_sheets
6
+
7
+ class << self
8
+ attr_reader :executable_path
9
+ attr_reader :executable_log_path
10
+ attr_accessor :allow_network_access
11
+ attr_accessor :verbose_executable_logging
12
+
13
+ def executable_log_path=(path)
14
+ @executable_log_path = File.expand_path(path)
15
+ end
16
+
17
+ def executable_path=(path)
18
+ @executable_path = File.expand_path(path)
19
+ end
20
+
21
+ def detect_executable_path
22
+ IO.popen('which prince') { |pipe| @executable_path = pipe.gets }
23
+ raise "Cannot find prince command-line app in $PATH" unless @executable_path
24
+ @executable_path.strip!
25
+ end
26
+ end
27
+
28
+ def initialize()
29
+ @logger = Logging::Logger[RPrince]
30
+ @style_sheets = []
31
+ end
32
+
33
+ # Since the prince CLI has trouble with long input file paths, we create this method to buffer
34
+ # them through Ruby and to the stdin of our prince process.
35
+ def html_files_to_pdf_file(files, output_file)
36
+ command = build_command(output_file)
37
+ logger.info("Calling Prince executable with: `#{command}`")
38
+ pdf = IO.popen(command, "w+")
39
+ buffer = ""
40
+ files.each do |path|
41
+ File.open(File.expand_path(path), "r") do |f|
42
+ while ( f.read(1024, buffer) )
43
+ pdf.write(buffer)
44
+ end
45
+ end
46
+ end
47
+ pdf.close
48
+ end
49
+
50
+ def html_string_to_pdf_string(html_string_input)
51
+ command = build_command
52
+ logger.info("Calling Prince executable with: `#{command}`")
53
+ pdf = IO.popen(command, "w+")
54
+ pdf.puts(html_string_input)
55
+ pdf.close_write
56
+ result = pdf.gets(nil)
57
+ pdf.close_read
58
+
59
+ result
60
+ end
61
+
62
+ def html_string_to_pdf_file(html_string_input, output_file)
63
+ command = build_command(output_file)
64
+ logger.info("Calling Prince executable with: `#{command}`")
65
+ pdf = IO.popen(command, "w+")
66
+ pdf.puts(html_string_input)
67
+ pdf.close
68
+ end
69
+
70
+ private
71
+
72
+ def build_flags
73
+ flags = style_sheets.map{|s| "--style='#{File.expand_path(s)}'" }
74
+ flags << "--input=html"
75
+ if RPrince.verbose_executable_logging
76
+ flags << '--verbose'
77
+ else
78
+ flags << '--silent'
79
+ end
80
+ flags << "--no-network" unless RPrince.allow_network_access
81
+ flags << "--log='#{RPrince.executable_log_path}'" if RPrince.executable_log_path
82
+ flags.join(' ')
83
+ end
84
+
85
+ def build_command(output_file='-')
86
+ # we're always passing the input on stdin, so we use a dash for that arg, per Prince's CLI spec
87
+ [RPrince.executable_path, build_flags, "-", "-o '#{output_file}'"].join(' ')
88
+ end
89
+
90
+ def logger
91
+ @logger
92
+ end
93
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/../lib/rprince/rails"
data/rprince.gemspec ADDED
@@ -0,0 +1,49 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rprince}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Timon Karnezos"]
12
+ s.date = %q{2010-04-09}
13
+ s.description = %q{A minimal wrapper for the PrinceXML PDF generation library.}
14
+ s.email = %q{timon.karnezos@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.markdown",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "init.rb",
25
+ "lib/rprince.rb",
26
+ "lib/rprince/pdf_helper.rb",
27
+ "lib/rprince/rails.rb",
28
+ "rails/init.rb",
29
+ "rprince.gemspec",
30
+ "sample/sample.html",
31
+ "sample/sample.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/timonk/rprince}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{A minimal Rails/gem wrapper for the PrinceXML PDF generation library.}
38
+
39
+ if s.respond_to? :specification_version then
40
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
+ else
45
+ end
46
+ else
47
+ end
48
+ end
49
+
@@ -0,0 +1,5 @@
1
+ <html>
2
+ <body>
3
+ The content of the body element is displayed in your pdf.
4
+ </body>
5
+ </html>
data/sample/sample.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ this_dir = File.dirname(__FILE__)
3
+ require File.join(this_dir, "..", "lib", "rprince")
4
+
5
+ Logging::Logger.root.clear_appenders
6
+ Logging::Logger.root.add_appenders(Logging::Appenders::Stdout.new)
7
+
8
+ RPrince.detect_executable_path
9
+ RPrince.executable_log_path = 'prince_executable.log'
10
+ RPrince.verbose_executable_logging = true
11
+
12
+ p = RPrince.new
13
+
14
+ example_html_file = File.join(this_dir, "sample.html")
15
+ example_html = File.read(example_html_file)
16
+
17
+ to_file_outfile = File.join(this_dir, "to_file.pdf")
18
+ p.html_string_to_pdf_file(example_html, to_file_outfile)
19
+
20
+ to_string_outfile = File.join(this_dir, "to_string.pdf")
21
+ pdf_string = p.html_string_to_pdf_string(example_html)
22
+ File.open(to_string_outfile, "w+") {|f| f.write(pdf_string) }
23
+
24
+ files_to_file_outfile = File.join(this_dir, "fs2f.pdf")
25
+ p.html_files_to_pdf_file([example_html_file, example_html_file], files_to_file_outfile)
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rprince
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Timon Karnezos
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-09 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A minimal wrapper for the PrinceXML PDF generation library.
22
+ email: timon.karnezos@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.markdown
29
+ files:
30
+ - .gitignore
31
+ - MIT-LICENSE
32
+ - README.markdown
33
+ - Rakefile
34
+ - VERSION
35
+ - init.rb
36
+ - lib/rprince.rb
37
+ - lib/rprince/pdf_helper.rb
38
+ - lib/rprince/rails.rb
39
+ - rails/init.rb
40
+ - rprince.gemspec
41
+ - sample/sample.html
42
+ - sample/sample.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/timonk/rprince
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.6
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: A minimal Rails/gem wrapper for the PrinceXML PDF generation library.
73
+ test_files: []
74
+