princely 1.2.5 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,8 +16,9 @@ respond_to blocks.
16
16
  format.pdf do
17
17
  render :pdf => "file_name",
18
18
  :template => "controller/action.pdf.erb",
19
- :stylesheets => ["application","prince"]
20
- :layout => "pdf"
19
+ :stylesheets => ["application","prince"],
20
+ :layout => "pdf",
21
+ :disposition => "inline" # PDF will be sent inline, means you can load it inside an iFrame or Embed
21
22
  end
22
23
  end
23
24
  end
@@ -37,6 +38,7 @@ The defaults for the render options are as follows:
37
38
  layout: false
38
39
  template: the template for the current controller/action
39
40
  stylesheets: none
41
+ disposition: attachment (created PDF file will be sent as download)
40
42
 
41
43
  == Contributors
42
44
 
data/Rakefile CHANGED
@@ -1,42 +1 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "princely"
8
- gem.summary = %Q{A simple Rails wrapper for the PrinceXML PDF generation library.}
9
- gem.description = %Q{A wrapper for the PrinceXML PDF generation library.}
10
- gem.email = "michael@intridea.com"
11
- gem.homepage = "http://github.com/mbleigh/princely"
12
- gem.authors = ["Michael Bleigh"]
13
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
- end
15
-
16
- rescue LoadError
17
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
- end
19
-
20
- require 'rake/testtask'
21
- Rake::TestTask.new(:test) do |test|
22
- test.libs << 'lib' << 'test'
23
- test.pattern = 'test/**/*_test.rb'
24
- test.verbose = true
25
- end
26
-
27
- task :default => :test
28
-
29
- require 'rake/rdoctask'
30
- Rake::RDocTask.new do |rdoc|
31
- if File.exist?('VERSION')
32
- version = File.read('VERSION')
33
- else
34
- version = ""
35
- end
36
-
37
- rdoc.rdoc_dir = 'rdoc'
38
- rdoc.title = "princely #{version}"
39
- rdoc.options << '--line-numbers' << '--inline-source'
40
- rdoc.rdoc_files.include('README*')
41
- rdoc.rdoc_files.include('lib/**/*.rb')
42
- end
1
+ require "bundler/gem_tasks"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.5
1
+ 1.2.6
@@ -16,21 +16,43 @@
16
16
  #
17
17
  $:.unshift(File.dirname(__FILE__))
18
18
  require 'logger'
19
+ require 'princely/rails' if defined?(Rails)
19
20
 
20
21
  class Princely
21
- VERSION = "1.0.0" unless const_defined?("VERSION")
22
-
23
22
  attr_accessor :exe_path, :style_sheets, :log_file, :logger
24
23
 
25
24
  # Initialize method
26
25
  #
27
- def initialize()
26
+ def initialize(options={})
28
27
  # Finds where the application lives, so we can call it.
29
- @exe_path = `which prince`.chomp
28
+ @exe_path = options[:path] || find_prince_executable
30
29
  raise "Cannot find prince command-line app in $PATH" if @exe_path.length == 0
31
- @style_sheets = ''
32
- @log_file = "#{RAILS_ROOT}/log/prince.log"
33
- @logger = RAILS_DEFAULT_LOGGER
30
+ raise "Cannot find prince command-line app at #{@exe_path}" if @exe_path && !File.executable?(@exe_path)
31
+ @style_sheets = ''
32
+ @log_file = options[:log_file]
33
+ @logger = options[:logger]
34
+ end
35
+
36
+ def logger
37
+ @logger ||= defined?(Rails) ? Rails.logger : StdoutLogger
38
+ end
39
+
40
+ def log_file
41
+ @log_file ||= defined?(Rails) ?
42
+ Rails.root.join("log/prince.log") :
43
+ File.expand_path(File.dirname(__FILE__) + "/log/prince.log")
44
+ end
45
+
46
+ def ruby_platform
47
+ RUBY_PLATFORM
48
+ end
49
+
50
+ def find_prince_executable
51
+ if ruby_platform =~ /mswin32/
52
+ "C:/Program Files/Prince/Engine/bin/prince"
53
+ else
54
+ `which prince`.chomp
55
+ end
34
56
  end
35
57
 
36
58
  # Sets stylesheets...
@@ -47,7 +69,7 @@ class Princely
47
69
  #
48
70
  def exe_path
49
71
  # Add any standard cmd line arguments we need to pass
50
- @exe_path << " --input=html --server --log=#{@log_file} "
72
+ @exe_path << " --input=html --server --log=#{log_file} "
51
73
  @exe_path << @style_sheets
52
74
  return @exe_path
53
75
  end
@@ -74,6 +96,7 @@ class Princely
74
96
  pdf.close_write
75
97
  result = pdf.gets(nil)
76
98
  pdf.close_read
99
+ result.force_encoding('BINARY') if RUBY_VERSION >= "1.9"
77
100
  return result
78
101
  end
79
102
 
@@ -81,7 +104,7 @@ class Princely
81
104
  path = self.exe_path()
82
105
  # Don't spew errors to the standard out...and set up to take IO
83
106
  # as input and output
84
- path << " --silent - -o #{output_file} >> #{@log_file} 2>> #{@log_file}"
107
+ path << " --silent - -o '#{output_file}' >> '#{log_file}' 2>> '#{log_file}'"
85
108
 
86
109
  # Show the command used...
87
110
  logger.info "\n\nPRINCE XML PDF COMMAND"
@@ -93,4 +116,10 @@ class Princely
93
116
  pdf.puts(string)
94
117
  pdf.close
95
118
  end
96
- end
119
+
120
+ class StdoutLogger
121
+ def self.info(msg)
122
+ puts msg
123
+ end
124
+ end
125
+ end
@@ -25,16 +25,11 @@ module PdfHelper
25
25
 
26
26
  prince = Princely.new()
27
27
  # Sets style sheets on PDF renderer
28
- prince.add_style_sheets(*options[:stylesheets].collect{|style| stylesheet_file_path(style)})
28
+ prince.add_style_sheets(*options[:stylesheets].collect{|style| asset_file_path(style)})
29
29
 
30
30
  html_string = render_to_string(:template => options[:template], :layout => options[:layout])
31
31
 
32
- # Make all paths relative, on disk paths...
33
- html_string.gsub!(".com:/",".com/") # strip out bad attachment_fu URLs
34
- html_string.gsub!( /src=["']+([^:]+?)["']/i ) { |m| "src=\"#{RAILS_ROOT}/public/" + $1 + '"' } # re-route absolute paths
35
-
36
- # Remove asset ids on images with a regex
37
- html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| 'src="' + $1.split('?').first + '"' }
32
+ html_string = localize_html_string(html_string)
38
33
 
39
34
  # Send the generated PDF file from our html string.
40
35
  if filename = options[:filename] || options[:file]
@@ -44,16 +39,46 @@ module PdfHelper
44
39
  end
45
40
  end
46
41
 
42
+ def localize_html_string(html_string)
43
+ # Make all paths relative, on disk paths...
44
+ html_string.gsub!(".com:/",".com/") # strip out bad attachment_fu URLs
45
+ html_string.gsub!( /src=["']+([^:]+?)["']/i ) { |m| "src=\"#{Rails.public_path}/#{$1}\"" } # re-route absolute paths
46
+
47
+ # Remove asset ids on images with a regex
48
+ html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| "src=\"#{$1.split('?').first}\"" }
49
+ html_string
50
+ end
51
+
52
+ def asset_file_path(asset)
53
+ stylesheet = stylesheet.to_s.gsub(".css","")
54
+ File.join(config.stylesheets_dir, "#{stylesheet}.css")
55
+ end
56
+ alias_method :stylesheet_file_path, :asset_file_path
57
+
47
58
  def make_and_send_pdf(pdf_name, options = {})
59
+ options[:disposition] ||= 'attachment'
48
60
  send_data(
49
61
  make_pdf(options),
50
62
  :filename => pdf_name + ".pdf",
51
- :type => 'application/pdf'
63
+ :type => 'application/pdf',
64
+ :disposition => options[:disposition]
52
65
  )
53
66
  end
54
-
55
- def stylesheet_file_path(stylesheet)
56
- stylesheet = stylesheet.to_s.gsub(".css","")
57
- File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR,"#{stylesheet}.css")
67
+
68
+ module AssetSupport
69
+ def localize_html_string(html_string)
70
+ # Make all paths relative, on disk paths...
71
+ html_string.gsub!(".com:/",".com/") # strip out bad attachment_fu URLs
72
+ html_string.gsub!( /src=["']+([^:]+?)["']/i ) { |m| "src=\"#{asset_file_path($1)}\"" } # re-route absolute paths
73
+
74
+ # Remove asset ids on images with a regex
75
+ html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| 'src="' + $1.split('?').first + '"' }
76
+ html_string
77
+ end
78
+
79
+ def asset_file_path(asset)
80
+ # Remove /assets/ from generated names and try and find a matching asset
81
+ Rails.application.assets.find_asset(asset.gsub(/\/assets\//, "")).try(:pathname) || asset
82
+ end
58
83
  end
59
84
  end
@@ -1,6 +1,8 @@
1
- require 'princely'
2
1
  require 'princely/pdf_helper'
3
2
 
4
- Mime::Type.register 'application/pdf', :pdf
3
+ if Mime::Type.lookup_by_extension(:pdf) != 'application/pdf'
4
+ Mime::Type.register 'application/pdf', :pdf
5
+ end
5
6
 
6
- ActionController::Base.send(:include, PdfHelper)
7
+ ActionController::Base.send(:include, PdfHelper)
8
+ ActionController::Base.send(:include, PdfHelper::AssetSupport) if Rails::VERSION::MINOR > 0
@@ -0,0 +1,3 @@
1
+ class Princely
2
+ VERSION = "1.3.0"
3
+ end
metadata CHANGED
@@ -1,65 +1,56 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: princely
3
- version: !ruby/object:Gem::Version
4
- version: 1.2.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.0
5
+ prerelease:
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - Michael Bleigh
9
+ - Jared Fraser
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
-
12
- date: 2009-09-19 00:00:00 -04:00
13
- default_executable:
13
+ date: 2012-01-25 00:00:00.000000000 Z
14
14
  dependencies: []
15
-
16
15
  description: A wrapper for the PrinceXML PDF generation library.
17
- email: michael@intridea.com
16
+ email:
17
+ - michael@intridea.com
18
+ - dev@jsf.io
18
19
  executables: []
19
-
20
20
  extensions: []
21
-
22
- extra_rdoc_files:
21
+ extra_rdoc_files:
23
22
  - README.rdoc
24
- files:
25
- - .gitignore
23
+ files:
26
24
  - MIT-LICENSE
27
25
  - README.rdoc
28
26
  - Rakefile
29
27
  - VERSION
30
- - init.rb
31
28
  - lib/princely.rb
32
29
  - lib/princely/pdf_helper.rb
33
30
  - lib/princely/rails.rb
34
- - princely.gemspec
35
- - rails/init.rb
36
- has_rdoc: true
31
+ - lib/princely/version.rb
37
32
  homepage: http://github.com/mbleigh/princely
38
33
  licenses: []
39
-
40
34
  post_install_message:
41
- rdoc_options:
42
- - --charset=UTF-8
43
- require_paths:
35
+ rdoc_options: []
36
+ require_paths:
44
37
  - lib
45
- required_ruby_version: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: "0"
50
- version:
51
- required_rubygems_version: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: "0"
56
- version:
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
57
50
  requirements: []
58
-
59
51
  rubyforge_project:
60
- rubygems_version: 1.3.3
52
+ rubygems_version: 1.8.24
61
53
  signing_key:
62
54
  specification_version: 3
63
55
  summary: A simple Rails wrapper for the PrinceXML PDF generation library.
64
56
  test_files: []
65
-
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- *.gem
2
- tmp
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require File.dirname(__FILE__) + '/rails/init'
@@ -1,46 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{princely}
8
- s.version = "1.2.5"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Michael Bleigh"]
12
- s.date = %q{2009-09-19}
13
- s.description = %q{A wrapper for the PrinceXML PDF generation library.}
14
- s.email = %q{michael@intridea.com}
15
- s.extra_rdoc_files = [
16
- "README.rdoc"
17
- ]
18
- s.files = [
19
- ".gitignore",
20
- "MIT-LICENSE",
21
- "README.rdoc",
22
- "Rakefile",
23
- "VERSION",
24
- "init.rb",
25
- "lib/princely.rb",
26
- "lib/princely/pdf_helper.rb",
27
- "lib/princely/rails.rb",
28
- "princely.gemspec",
29
- "rails/init.rb"
30
- ]
31
- s.homepage = %q{http://github.com/mbleigh/princely}
32
- s.rdoc_options = ["--charset=UTF-8"]
33
- s.require_paths = ["lib"]
34
- s.rubygems_version = %q{1.3.3}
35
- s.summary = %q{A simple Rails wrapper for the PrinceXML PDF generation library.}
36
-
37
- if s.respond_to? :specification_version then
38
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
- s.specification_version = 3
40
-
41
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
42
- else
43
- end
44
- else
45
- end
46
- end
@@ -1 +0,0 @@
1
- require File.dirname(__FILE__) + "/../lib/princely/rails"