cmaggard-princely 1.2.6

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.
@@ -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.
@@ -0,0 +1,50 @@
1
+ Princely
2
+ ========
3
+ * updated to avoid deprecation errors in rails 3
4
+
5
+ Princely is a simple wrapper for the Prince XML PDF generation library
6
+ (http://www.princexml.com). It is almost entirely based on the SubImage
7
+ Prince library found on this blog post:
8
+
9
+ http://sublog.subimage.com/articles/2007/05/29/html-css-to-pdf-using-ruby-on-rails
10
+
11
+ I have taken the helpers and made them a little bit more generalized and
12
+ reusable, and created a render option set for pdf generation. The plugin
13
+ will also automatically register the PDF MimeType so that you can use
14
+ pdf in controller respond_to blocks.
15
+
16
+ Example
17
+ =======
18
+
19
+ class Provider::EstimatesController < Provider::BaseController
20
+ def show
21
+ respond_to do |format|
22
+ format.html
23
+ format.pdf do
24
+ render :pdf => "file_name",
25
+ :template => "controller/action.pdf.erb",
26
+ :stylesheets => ["application","prince"]
27
+ :layout => "pdf"
28
+ end
29
+ end
30
+ end
31
+
32
+ def pdf
33
+ make_and_send_pdf("file_name")
34
+ end
35
+ end
36
+
37
+ Render Defaults
38
+ ===============
39
+
40
+ The defaults for the render options are as follows:
41
+
42
+ layout: false
43
+ template: the template for the current controller/action
44
+ stylesheets: none
45
+
46
+ Resources
47
+ =========
48
+
49
+ * Copyright (c) 2007 Michael Bleigh and Intridea, Inc., released under the MIT license
50
+ * Copyright (c) 2007 Seth from Subimage from http://sublog.subimage.com/2007/05/29/html-css-to-pdf-using-ruby-on-rails
@@ -0,0 +1,42 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "cmaggard-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 based on article by Seth from Subimage Interactive at http://sublog.subimage.com/2007/05/29/html-css-to-pdf-using-ruby-on-rails}
10
+ gem.email = "michael@intridea.com"
11
+ gem.homepage = "http://github.com/ccollins/princely"
12
+ gem.authors = ["Michael Bleigh", "Seth from Subimage Interactive"]
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
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.2.6
@@ -0,0 +1,43 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
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 = "cmaggard-princely"
8
+ s.version = "1.2.6"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Bleigh", "Seth from Subimage Interactive"]
12
+ s.date = "2012-01-03"
13
+ s.description = "A wrapper for the PrinceXML PDF generation library based on article by Seth from Subimage Interactive at http://sublog.subimage.com/2007/05/29/html-css-to-pdf-using-ruby-on-rails"
14
+ s.email = "michael@intridea.com"
15
+ s.extra_rdoc_files = [
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README.md",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "init.rb",
24
+ "lib/princely.rb",
25
+ "lib/princely/pdf_helper.rb",
26
+ "lib/princely/rails.rb",
27
+ "princely.gemspec"
28
+ ]
29
+ s.homepage = "http://github.com/ccollins/princely"
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = "1.8.11"
32
+ s.summary = "A simple Rails wrapper for the PrinceXML PDF generation library."
33
+
34
+ if s.respond_to? :specification_version then
35
+ s.specification_version = 3
36
+
37
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
38
+ else
39
+ end
40
+ else
41
+ end
42
+ end
43
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/lib/princely/rails"
@@ -0,0 +1,96 @@
1
+ # PrinceXML Ruby interface.
2
+ # http://www.princexml.com
3
+ #
4
+ # Library by Subimage Interactive - http://www.subimage.com
5
+ #
6
+ #
7
+ # USAGE
8
+ # -----------------------------------------------------------------------------
9
+ # princely = Princely.new()
10
+ # html_string = render_to_string(:template => 'some_document')
11
+ # send_data(
12
+ # princely.pdf_from_string(html_string),
13
+ # :filename => 'some_document.pdf'
14
+ # :type => 'application/pdf'
15
+ # )
16
+ #
17
+ $:.unshift(File.dirname(__FILE__))
18
+ require 'logger'
19
+
20
+ class Princely
21
+ VERSION = "1.2.5" unless const_defined?("VERSION")
22
+
23
+ attr_accessor :exe_path, :style_sheets, :log_file, :logger, :additional_options
24
+
25
+ # Initialize method
26
+ #
27
+ def initialize()
28
+ # Finds where the application lives, so we can call it.
29
+ @exe_path = `which prince`.chomp
30
+ raise "Cannot find prince command-line app in $PATH" if @exe_path.length == 0
31
+ @style_sheets = ''
32
+ @log_file = "#{Rails.root.to_s}/log/prince.log"
33
+ @logger = Rails.logger
34
+ end
35
+
36
+ # Sets stylesheets...
37
+ # Can pass in multiple paths for css files.
38
+ #
39
+ def add_style_sheets(*sheets)
40
+ for sheet in sheets do
41
+ @style_sheets << " -s #{sheet} "
42
+ end
43
+ end
44
+
45
+ # Returns fully formed executable path with any command line switches
46
+ # we've set based on our variables.
47
+ #
48
+ def exe_path
49
+ # Add any standard cmd line arguments we need to pass
50
+ @exe_path << " --input=html --server --log=#{@log_file} #{@additional_options} "
51
+ @exe_path << @style_sheets
52
+ return @exe_path
53
+ end
54
+
55
+ # Makes a pdf from a passed in string.
56
+ #
57
+ # Returns PDF as a stream, so we can use send_data to shoot
58
+ # it down the pipe using Rails.
59
+ #
60
+ def pdf_from_string(string, output_file = '-')
61
+ path = self.exe_path()
62
+ # Don't spew errors to the standard out...and set up to take IO
63
+ # as input and output
64
+ path << ' --silent - -o -'
65
+
66
+ # Show the command used...
67
+ logger.info "\n\nPRINCE XML PDF COMMAND"
68
+ logger.info path
69
+ logger.info ''
70
+
71
+ # Actually call the prince command, and pass the entire data stream back.
72
+ pdf = IO.popen(path, "w+")
73
+ pdf.puts(string)
74
+ pdf.close_write
75
+ result = pdf.gets(nil)
76
+ pdf.close_read
77
+ return result
78
+ end
79
+
80
+ def pdf_from_string_to_file(string, output_file)
81
+ path = self.exe_path()
82
+ # Don't spew errors to the standard out...and set up to take IO
83
+ # as input and output
84
+ path << " --silent - -o '#{output_file}' >> '#{@log_file}' 2>> '#{@log_file}'"
85
+
86
+ # Show the command used...
87
+ logger.info "\n\nPRINCE XML PDF COMMAND"
88
+ logger.info path
89
+ logger.info ''
90
+
91
+ # Actually call the prince command, and pass the entire data stream back.
92
+ pdf = IO.popen(path, "w+")
93
+ pdf.puts(string)
94
+ pdf.close
95
+ end
96
+ end
@@ -0,0 +1,60 @@
1
+ module PdfHelper
2
+ require 'princely'
3
+
4
+ def self.included(base)
5
+ base.class_eval do
6
+ alias_method_chain :render, :princely
7
+ end
8
+ end
9
+
10
+ def render_with_princely(options = nil, *args, &block)
11
+ if options.is_a?(Hash) && options.has_key?(:pdf)
12
+ options[:name] ||= options.delete(:pdf)
13
+ make_and_send_pdf(options.delete(:name), options)
14
+ else
15
+ render_without_princely(options, *args, &block)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def make_pdf(options = {})
22
+ options[:stylesheets] ||= []
23
+ options[:layout] ||= false
24
+ options[:template] ||= File.join(controller_path,action_name)
25
+ options[:additional_options] ||= ''
26
+ prince = Princely.new()
27
+ # Sets style sheets on PDF renderer
28
+ prince.additional_options = options[:additional_options]
29
+ prince.add_style_sheets(*options[:stylesheets].collect{|style| stylesheet_file_path(style)})
30
+
31
+ html_string = render_to_string(:template => options[:template], :layout => options[:layout])
32
+
33
+ # Make all paths relative, on disk paths...
34
+ html_string.gsub!(".com:/",".com/") # strip out bad attachment_fu URLs
35
+ # html_string.gsub!( /src=["']+([^:]+?)["']/i ) { |m| "src=\"#{RAILS_ROOT}/public/" + $1 + '"' } # re-route absolute paths
36
+
37
+ # Remove asset ids on images with a regex
38
+ html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| 'src="' + $1.split('?').first + '"' }
39
+
40
+ # Send the generated PDF file from our html string.
41
+ if filename = options[:filename] || options[:file]
42
+ prince.pdf_from_string_to_file(html_string, filename)
43
+ else
44
+ prince.pdf_from_string(html_string)
45
+ end
46
+ end
47
+
48
+ def make_and_send_pdf(pdf_name, options = {})
49
+ send_data(
50
+ make_pdf(options),
51
+ :filename => pdf_name + ".pdf",
52
+ :type => 'application/pdf'
53
+ )
54
+ end
55
+
56
+ def stylesheet_file_path(stylesheet)
57
+ stylesheet = stylesheet.to_s.gsub(".css","")
58
+ File.join(Rails.application.paths["public/stylesheets"],"#{stylesheet}.css")
59
+ end
60
+ end
@@ -0,0 +1,6 @@
1
+ require File.dirname(__FILE__) + '/../princely'
2
+ require 'princely/pdf_helper'
3
+
4
+ Mime::Type.register 'application/pdf', :pdf
5
+
6
+ ActionController::Base.send(:include, PdfHelper)
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmaggard-princely
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Bleigh
9
+ - Seth from Subimage Interactive
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-01-03 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: A wrapper for the PrinceXML PDF generation library based on article by
16
+ Seth from Subimage Interactive at http://sublog.subimage.com/2007/05/29/html-css-to-pdf-using-ruby-on-rails
17
+ email: michael@intridea.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files:
21
+ - README.md
22
+ files:
23
+ - MIT-LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - VERSION
27
+ - cmaggard-princely.gemspec
28
+ - init.rb
29
+ - lib/princely.rb
30
+ - lib/princely/pdf_helper.rb
31
+ - lib/princely/rails.rb
32
+ homepage: http://github.com/ccollins/princely
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
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'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.11
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: A simple Rails wrapper for the PrinceXML PDF generation library.
56
+ test_files: []