yellowlab-princely 1.2.5.yellowlab.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +47 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/princely.rb +96 -0
- data/lib/princely/pdf_helper.rb +59 -0
- data/lib/princely/rails.rb +6 -0
- metadata +79 -0
data/.gitignore
ADDED
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.rdoc
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
= Princely
|
2
|
+
|
3
|
+
Princely is a simple wrapper for the Prince XML PDF generation library
|
4
|
+
(http://www.princexml.com). The plugin will also automatically registers
|
5
|
+
the PDF MimeType so that you can use pdf as a format in controller
|
6
|
+
respond_to blocks.
|
7
|
+
|
8
|
+
== Example
|
9
|
+
|
10
|
+
class Provider::EstimatesController < Provider::BaseController
|
11
|
+
# You can render PDF templates simply by
|
12
|
+
# using the :pdf option on render templates.
|
13
|
+
def show
|
14
|
+
respond_to do |format|
|
15
|
+
format.html
|
16
|
+
format.pdf do
|
17
|
+
render :pdf => "file_name",
|
18
|
+
:template => "controller/action.pdf.erb",
|
19
|
+
:stylesheets => ["application","prince"]
|
20
|
+
:layout => "pdf"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Alternatively, you can use make_and_send_pdf to
|
26
|
+
# render out a PDF for the action without a
|
27
|
+
# respond_to block.
|
28
|
+
def pdf
|
29
|
+
make_and_send_pdf("file_name")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
== Render Defaults
|
34
|
+
|
35
|
+
The defaults for the render options are as follows:
|
36
|
+
|
37
|
+
layout: false
|
38
|
+
template: the template for the current controller/action
|
39
|
+
stylesheets: none
|
40
|
+
|
41
|
+
== Contributors
|
42
|
+
|
43
|
+
* Gemification and more: Nic Williams
|
44
|
+
|
45
|
+
== Resources
|
46
|
+
|
47
|
+
* Copyright (c) 2007-2009 Michael Bleigh and Intridea, Inc., released under the MIT license.
|
data/Rakefile
ADDED
@@ -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 = "yellowlab-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
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.2.5.yellowlab.1
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "princely/rails"
|
data/lib/princely.rb
ADDED
@@ -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.0.0" unless const_defined?("VERSION")
|
22
|
+
|
23
|
+
attr_accessor :exe_path, :style_sheets, :log_file, :logger
|
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}/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} "
|
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,59 @@
|
|
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
|
+
|
26
|
+
prince = Princely.new()
|
27
|
+
# Sets style sheets on PDF renderer
|
28
|
+
prince.add_style_sheets(*options[:stylesheets].collect{|style| stylesheet_file_path(style)})
|
29
|
+
|
30
|
+
html_string = render_to_string(:template => options[:template], :layout => options[:layout])
|
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.public_path}/" + $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 + '"' }
|
38
|
+
|
39
|
+
# Send the generated PDF file from our html string.
|
40
|
+
if filename = options[:filename] || options[:file]
|
41
|
+
prince.pdf_from_string_to_file(html_string, filename)
|
42
|
+
else
|
43
|
+
prince.pdf_from_string(html_string)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def make_and_send_pdf(pdf_name, options = {})
|
48
|
+
send_data(
|
49
|
+
make_pdf(options),
|
50
|
+
:filename => pdf_name + ".pdf",
|
51
|
+
:type => 'application/pdf'
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def stylesheet_file_path(stylesheet)
|
56
|
+
stylesheet = stylesheet.to_s.gsub(".css","")
|
57
|
+
File.join(config.stylesheets_dir, "#{stylesheet}.css")
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yellowlab-princely
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 958535915
|
5
|
+
prerelease: true
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
- 5
|
10
|
+
- yellowlab
|
11
|
+
- 1
|
12
|
+
version: 1.2.5.yellowlab.1
|
13
|
+
platform: ruby
|
14
|
+
authors:
|
15
|
+
- Michael Bleigh
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2010-09-08 00:00:00 +10:00
|
21
|
+
default_executable:
|
22
|
+
dependencies: []
|
23
|
+
|
24
|
+
description: A wrapper for the PrinceXML PDF generation library.
|
25
|
+
email: michael@intridea.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README.rdoc
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.rdoc
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- init.rb
|
39
|
+
- lib/princely.rb
|
40
|
+
- lib/princely/pdf_helper.rb
|
41
|
+
- lib/princely/rails.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/mbleigh/princely
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">"
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 25
|
66
|
+
segments:
|
67
|
+
- 1
|
68
|
+
- 3
|
69
|
+
- 1
|
70
|
+
version: 1.3.1
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.3.7
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: A simple Rails wrapper for the PrinceXML PDF generation library.
|
78
|
+
test_files: []
|
79
|
+
|