to_pdf 0.1.0

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.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,98 @@
1
+ = ToPDF
2
+
3
+ A no frills PDF render for Rails (>= 3.0) and {PrinceXML}[http://princexml.com/].
4
+
5
+ == Installation
6
+
7
+ In your Gemfile:
8
+
9
+ gem 'to_pdf'
10
+
11
+ == Usage
12
+
13
+ Create your PDF template like so:
14
+
15
+ app/views/jobs/show.pdf.haml
16
+
17
+ Then in your controller it’s just like responding to HTML or JSON etc.:
18
+
19
+ class JobController < ApplicationController
20
+ respond_to :pdf
21
+
22
+ def show
23
+ @job = Job.find(params[:id])
24
+ respond_with(@job)
25
+ end
26
+ end
27
+
28
+ PrinceXML cares not for fluff that comes with HTML, treat it like XML and jump straight to the content:
29
+
30
+ .job
31
+ .name
32
+ = job.name
33
+
34
+ You can though define CSS at the top of the document:
35
+
36
+ %style
37
+ :sass
38
+ @page
39
+ margin: 2cm
40
+ body
41
+ color: #000
42
+ font-family: "DejaVu Sans"
43
+
44
+ Or you can link to a stylesheet:
45
+
46
+ = stylesheet_link_tag 'pdf', media: 'print'
47
+
48
+ .job
49
+ .name
50
+ = job.name
51
+
52
+ This will work with the Rails 3.1 asset pipeline in production, however for it to work in development mode you will need to add this line of configuration to development.rb:
53
+
54
+ MyProject::Application.configure do
55
+ ...
56
+
57
+ config.prince_xml_asset_domain = "http://myproject.dev"
58
+ end
59
+
60
+ Another configuration option available is the path to the PrinceXML executable. Again, this can optionally be set on a per environment basis:
61
+
62
+ MyProject::Application.configure do
63
+ ...
64
+
65
+ config.prince_xml_executable_path = '/opt/bin/prince'
66
+ end
67
+
68
+ == Caveats
69
+
70
+ * Only sends PDFs inline
71
+ * Does not use layouts
72
+
73
+ == Maintainers
74
+
75
+ * {Hugh Evans}[http://github.com/hughevans]
76
+
77
+ == License
78
+
79
+ Copyright 2011 Hugh Evans
80
+
81
+ Permission is hereby granted, free of charge, to any person obtaining
82
+ a copy of this software and associated documentation files (the
83
+ "Software"), to deal in the Software without restriction, including
84
+ without limitation the rights to use, copy, modify, merge, publish,
85
+ distribute, sublicense, and/or sell copies of the Software, and to
86
+ permit persons to whom the Software is furnished to do so, subject to
87
+ the following conditions:
88
+
89
+ The above copyright notice and this permission notice shall be
90
+ included in all copies or substantial portions of the Software.
91
+
92
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
93
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
94
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
95
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
96
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
97
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
98
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => :spec
5
+ desc "Run specs."
6
+ RSpec::Core::RakeTask.new do |t|
7
+ end
@@ -0,0 +1,48 @@
1
+ class PrinceXML
2
+ class << self
3
+ attr_writer :executable_path, :asset_domain
4
+
5
+ def asset_domain=(domain)
6
+ @asset_domain = domain
7
+ end
8
+
9
+ def executable_path=(path)
10
+ @executable_path = File.expand_path(path)
11
+ end
12
+
13
+ def detect_executable_path
14
+ IO.popen('which prince') { |pipe| @executable_path = pipe.gets }
15
+ raise 'Cannot find command `prince` in $PATH' unless @executable_path
16
+ @executable_path.strip!
17
+ end
18
+
19
+ def string_to_pdf(string)
20
+ detect_executable_path unless @executable_path
21
+
22
+ pdf = IO.popen(prince_command, 'w+')
23
+ pdf.puts(localise_paths(string))
24
+ pdf.close_write
25
+ result = pdf.gets(nil)
26
+ pdf.close_read
27
+
28
+ result
29
+ end
30
+
31
+ def prince_command
32
+ "#{@executable_path} --input=html --silent - -o '-'"
33
+ end
34
+
35
+ def localise_paths(string)
36
+ string.gsub!('src="/', "src=\"#{Rails.public_path}/")
37
+
38
+ if @asset_domain
39
+ string.gsub!('link href="/', "link href=\"#{@asset_domain}/")
40
+ elsif defined?(Rails)
41
+ string.gsub!('link href="/', "link src=\"#{Rails.public_path}/")
42
+ string.gsub!('url(/', "url(#{Rails.public_path}/")
43
+ end
44
+
45
+ string
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,32 @@
1
+ module ToPDF
2
+ class Railtie < Rails::Railtie
3
+ initializer 'to_pdf.configure_prince_xml' do
4
+ if Rails.application.config.prince_xml_asset_domain
5
+ PrinceXML.asset_domain = Rails.application.config.prince_xml_asset_domain
6
+ end
7
+
8
+ if Rails.application.config.prince_xml_executable_path
9
+ PrinceXML.executable_path = Rails.application.config.prince_xml_executable_path
10
+ end
11
+ end
12
+
13
+ initializer 'to_pdf.init_mime_types' do
14
+ Mime::Type.register 'application/pdf', :pdf
15
+ end
16
+
17
+ initializer 'to_pdf.insert_into_action_controller' do
18
+ ActiveSupport.on_load :action_controller do
19
+ ActionController::Renderers.add :pdf do |template, options|
20
+ string = render_to_string template, options
21
+ send_data PrinceXML.string_to_pdf(string), :type => :pdf, :disposition => 'inline'
22
+ end
23
+
24
+ class ActionController::Responder
25
+ def to_pdf
26
+ controller.render :pdf => controller.action_name
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module ToPDF
2
+ VERSION = "0.1.0"
3
+ end
data/lib/to_pdf.rb ADDED
@@ -0,0 +1,3 @@
1
+ autoload :PrinceXML, 'to_pdf/prince_xml'
2
+
3
+ require 'to_pdf/railtie' if defined?(Rails::Railtie)
data/to_pdf.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'to_pdf/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'to_pdf'
7
+ s.version = ToPDF::VERSION
8
+ s.authors = ['Hugh Evans']
9
+ s.email = 'hugh@artpop.com.au'
10
+ s.homepage = 'https://github.com/hughevans/to_pdf'
11
+ s.summary = %q{Render rails actions as a PDF using PrinceXML.}
12
+ s.description = %q{Render rails actions as a PDF using PrinceXML.}
13
+
14
+ s.rubyforge_project = 'to_pdf'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.require_path = 'lib'
18
+
19
+ s.add_dependency 'rails', '>= 3.0.0'
20
+ s.add_development_dependency 'rspec', '~> 2.6.0'
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hugh Evans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70266974647220 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70266974647220
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70266974646620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.6.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70266974646620
36
+ description: Render rails actions as a PDF using PrinceXML.
37
+ email: hugh@artpop.com.au
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - Gemfile
43
+ - README.rdoc
44
+ - Rakefile
45
+ - lib/to_pdf.rb
46
+ - lib/to_pdf/prince_xml.rb
47
+ - lib/to_pdf/railtie.rb
48
+ - lib/to_pdf/version.rb
49
+ - to_pdf.gemspec
50
+ homepage: https://github.com/hughevans/to_pdf
51
+ licenses: []
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubyforge_project: to_pdf
70
+ rubygems_version: 1.8.10
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Render rails actions as a PDF using PrinceXML.
74
+ test_files: []