matthewmacleod-princely 1.2.5
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/pdf_helper.rb +59 -0
- data/lib/princely/rails.rb +6 -0
- data/lib/princely.rb +96 -0
- data/matthewmacleod-princely.gemspec +47 -0
- data/rails/init.rb +1 -0
- metadata +65 -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 = "matthewmacleod-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/matthewmacleod/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
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/rails/init'
|
@@ -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_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 + '"' }
|
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(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR,"#{stylesheet}.css")
|
58
|
+
end
|
59
|
+
end
|
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_DEFAULT_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,47 @@
|
|
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{matthewmacleod-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-12-14}
|
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
|
+
"matthewmacleod-princely.gemspec",
|
29
|
+
"rails/init.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/matthewmacleod/princely}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.5}
|
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
|
47
|
+
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../lib/princely/rails"
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: matthewmacleod-princely
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Bleigh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-14 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A wrapper for the PrinceXML PDF generation library.
|
17
|
+
email: michael@intridea.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- .gitignore
|
26
|
+
- MIT-LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- VERSION
|
30
|
+
- init.rb
|
31
|
+
- lib/princely.rb
|
32
|
+
- lib/princely/pdf_helper.rb
|
33
|
+
- lib/princely/rails.rb
|
34
|
+
- matthewmacleod-princely.gemspec
|
35
|
+
- rails/init.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/matthewmacleod/princely
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- 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:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: A simple Rails wrapper for the PrinceXML PDF generation library.
|
64
|
+
test_files: []
|
65
|
+
|