drnic-princely 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +50 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/init.rb +6 -0
- data/lib/pdf_helper.rb +55 -0
- data/lib/prince.rb +77 -0
- metadata +61 -0
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
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Princely
|
2
|
+
========
|
3
|
+
|
4
|
+
Princely is a simple wrapper for the Prince XML PDF generation library
|
5
|
+
(http://www.princexml.com). It is almost entirely based on the SubImage
|
6
|
+
Prince library found on this blog post:
|
7
|
+
|
8
|
+
http://sublog.subimage.com/articles/2007/05/29/html-css-to-pdf-using-ruby-on-rails
|
9
|
+
|
10
|
+
I have taken the helpers and made them a little bit more generalized and
|
11
|
+
reusable, and created a render option set for pdf generation. The plugin
|
12
|
+
will also automatically register the PDF MimeType so that you can use
|
13
|
+
pdf in controller respond_to blocks.
|
14
|
+
|
15
|
+
Example
|
16
|
+
=======
|
17
|
+
|
18
|
+
class Provider::EstimatesController < Provider::BaseController
|
19
|
+
def show
|
20
|
+
respond_to do |format|
|
21
|
+
format.html
|
22
|
+
format.pdf do
|
23
|
+
render :pdf => "file_name",
|
24
|
+
:template => "controller/action.pdf.erb",
|
25
|
+
:stylesheets => ["application","prince"]
|
26
|
+
:layout => "pdf"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def pdf
|
32
|
+
make_and_send_pdf("file_name")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Render Defaults
|
37
|
+
===============
|
38
|
+
|
39
|
+
The defaults for the render options are as follows:
|
40
|
+
|
41
|
+
layout: false
|
42
|
+
template: the template for the current controller/action
|
43
|
+
stylesheets: none
|
44
|
+
|
45
|
+
Resources
|
46
|
+
=========
|
47
|
+
|
48
|
+
Trac: http://trac.intridea.com/trac/public/
|
49
|
+
|
50
|
+
Copyright (c) 2007 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 = "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/drnic/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.0.0
|
data/init.rb
ADDED
data/lib/pdf_helper.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module PdfHelper
|
2
|
+
require 'prince'
|
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 = Prince.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
|
+
return prince.pdf_from_string(html_string)
|
41
|
+
end
|
42
|
+
|
43
|
+
def make_and_send_pdf(pdf_name, options = {})
|
44
|
+
send_data(
|
45
|
+
make_pdf(options),
|
46
|
+
:filename => pdf_name + ".pdf",
|
47
|
+
:type => 'application/pdf'
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
def stylesheet_file_path(stylesheet)
|
52
|
+
stylesheet = stylesheet.to_s.gsub(".css","")
|
53
|
+
File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR,"#{stylesheet}.css")
|
54
|
+
end
|
55
|
+
end
|
data/lib/prince.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Prince XML Ruby interface.
|
2
|
+
# http://www.princexml.com
|
3
|
+
#
|
4
|
+
# Library by Subimage Interactive - http://www.subimage.com
|
5
|
+
#
|
6
|
+
#
|
7
|
+
# USAGE
|
8
|
+
# -----------------------------------------------------------------------------
|
9
|
+
# prince = Prince.new()
|
10
|
+
# html_string = render_to_string(:template => 'some_document')
|
11
|
+
# send_data(
|
12
|
+
# prince.pdf_from_string(html_string),
|
13
|
+
# :filename => 'some_document.pdf'
|
14
|
+
# :type => 'application/pdf'
|
15
|
+
# )
|
16
|
+
#
|
17
|
+
require 'logger'
|
18
|
+
|
19
|
+
class Prince
|
20
|
+
VERSION = "1.0.0"
|
21
|
+
|
22
|
+
attr_accessor :exe_path, :style_sheets, :log_file, :logger
|
23
|
+
|
24
|
+
# Initialize method
|
25
|
+
#
|
26
|
+
def initialize()
|
27
|
+
# Finds where the application lives, so we can call it.
|
28
|
+
@exe_path = `which prince`.chomp
|
29
|
+
@style_sheets = ''
|
30
|
+
@log_file = "#{RAILS_ROOT}/log/prince.log"
|
31
|
+
@logger = RAILS_DEFAULT_LOGGER
|
32
|
+
end
|
33
|
+
|
34
|
+
# Sets stylesheets...
|
35
|
+
# Can pass in multiple paths for css files.
|
36
|
+
#
|
37
|
+
def add_style_sheets(*sheets)
|
38
|
+
for sheet in sheets do
|
39
|
+
@style_sheets << " -s #{sheet} "
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns fully formed executable path with any command line switches
|
44
|
+
# we've set based on our variables.
|
45
|
+
#
|
46
|
+
def exe_path
|
47
|
+
# Add any standard cmd line arguments we need to pass
|
48
|
+
@exe_path << " --input=html --server --log=#{@log_file} "
|
49
|
+
@exe_path << @style_sheets
|
50
|
+
return @exe_path
|
51
|
+
end
|
52
|
+
|
53
|
+
# Makes a pdf from a passed in string.
|
54
|
+
#
|
55
|
+
# Returns PDF as a stream, so we can use send_data to shoot
|
56
|
+
# it down the pipe using Rails.
|
57
|
+
#
|
58
|
+
def pdf_from_string(string)
|
59
|
+
path = self.exe_path()
|
60
|
+
# Don't spew errors to the standard out...and set up to take IO
|
61
|
+
# as input and output
|
62
|
+
path << ' --silent - -o -'
|
63
|
+
|
64
|
+
# Show the command used...
|
65
|
+
logger.info "\n\nPRINCE XML PDF COMMAND"
|
66
|
+
logger.info path
|
67
|
+
logger.info ''
|
68
|
+
|
69
|
+
# Actually call the prince command, and pass the entire data stream back.
|
70
|
+
pdf = IO.popen(path, "w+")
|
71
|
+
pdf.puts(string)
|
72
|
+
pdf.close_write
|
73
|
+
result = pdf.gets(nil)
|
74
|
+
pdf.close_read
|
75
|
+
return result
|
76
|
+
end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: drnic-princely
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Bleigh
|
8
|
+
- Seth from Subimage Interactive
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-08-21 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
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
|
18
|
+
email: michael@intridea.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README
|
25
|
+
files:
|
26
|
+
- MIT-LICENSE
|
27
|
+
- README
|
28
|
+
- Rakefile
|
29
|
+
- VERSION
|
30
|
+
- init.rb
|
31
|
+
- lib/pdf_helper.rb
|
32
|
+
- lib/prince.rb
|
33
|
+
has_rdoc: false
|
34
|
+
homepage: http://github.com/drnic/princely
|
35
|
+
licenses:
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --charset=UTF-8
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.3.5
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: A simple Rails wrapper for the PrinceXML PDF generation library.
|
60
|
+
test_files: []
|
61
|
+
|