go_static 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -208
- metadata +3 -3
data/README.md
CHANGED
@@ -1,27 +1,26 @@
|
|
1
|
-
#
|
1
|
+
# GoStatic
|
2
2
|
|
3
|
-
##
|
3
|
+
## An image generation plugin for Ruby on Rails
|
4
4
|
|
5
|
-
|
5
|
+
GoStatic uses the shell utility [wkhtmltoimage](http://code.google.com/p/wkhtmltopdf/) to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let Wicked take care of the hard stuff.
|
6
6
|
|
7
|
-
|
7
|
+
_GoStatic has been verified to work on Ruby 1.8.7 and 1.9.2; Rails 2 and Rails 3_
|
8
8
|
|
9
9
|
### Installation
|
10
10
|
|
11
|
-
First, be sure to install [
|
12
|
-
|
13
|
-
This plugin relies on streams to communicate with wkhtmltopdf.
|
11
|
+
First, be sure to install [wkhtmltoimage](http://code.google.com/p/wkhtmltopdf/) version 0.10.0.rc2.
|
12
|
+
This plugin relies on streams to communicate with wkhtmltoimage.
|
14
13
|
|
15
|
-
More information about [
|
14
|
+
More information about [wkhtmltoimage](http://code.google.com/p/wkhtmltopdf/) could be found [here](http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltoimage-0.10.0_rc2-doc.html).
|
16
15
|
|
17
16
|
Next:
|
18
17
|
|
19
|
-
script/plugin install git://github.com/mileszs/
|
20
|
-
script/generate
|
18
|
+
script/plugin install git://github.com/mileszs/go_static.git
|
19
|
+
script/generate go_static
|
21
20
|
|
22
21
|
or add this to your Gemfile:
|
23
22
|
|
24
|
-
gem '
|
23
|
+
gem 'go_static'
|
25
24
|
|
26
25
|
### Basic Usage
|
27
26
|
|
@@ -29,8 +28,8 @@ or add this to your Gemfile:
|
|
29
28
|
def show
|
30
29
|
respond_to do |format|
|
31
30
|
format.html
|
32
|
-
format.
|
33
|
-
render :
|
31
|
+
format.png do
|
32
|
+
render :png => "file_name"
|
34
33
|
end
|
35
34
|
end
|
36
35
|
end
|
@@ -42,204 +41,12 @@ or add this to your Gemfile:
|
|
42
41
|
def show
|
43
42
|
respond_to do |format|
|
44
43
|
format.html
|
45
|
-
format.
|
46
|
-
render :
|
47
|
-
:disposition => 'attachment'
|
48
|
-
:template => 'things/show.pdf.erb',
|
49
|
-
:layout => 'pdf.html', # use 'pdf.html' for a pdf.html.erb file
|
50
|
-
:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf', # path to binary
|
51
|
-
:show_as_html => params[:debug].present?, # allow debuging based on url param
|
52
|
-
:orientation => 'Landscape', # default Portrait
|
53
|
-
:page_size => 'A4, Letter, ...', # default A4
|
54
|
-
:save_to_file => Rails.root.join('pdfs', "#{filename}.pdf"),
|
55
|
-
:save_only => false, # depends on :save_to_file being set first
|
56
|
-
:proxy => 'TEXT',
|
57
|
-
:basic_auth => false # when true username & password are automatically sent from session
|
58
|
-
:username => 'TEXT',
|
59
|
-
:password => 'TEXT',
|
60
|
-
:cover => 'URL',
|
61
|
-
:dpi => 'dpi',
|
62
|
-
:encoding => 'TEXT',
|
63
|
-
:user_style_sheet => 'URL',
|
64
|
-
:cookie => ['_session_id SESSION_ID'], # could be an array or a single string in a 'name value' format
|
65
|
-
:post => ['query QUERY_PARAM'], # could be an array or a single string in a 'name value' format
|
66
|
-
:redirect_delay => NUMBER,
|
67
|
-
:zoom => FLOAT,
|
68
|
-
:page_offset => NUMBER,
|
69
|
-
:book => true,
|
70
|
-
:default_header => true,
|
71
|
-
:disable_javascript => false,
|
72
|
-
:grayscale => true,
|
73
|
-
:lowquality => true,
|
74
|
-
:enable_plugins => true,
|
75
|
-
:disable_internal_links => true,
|
76
|
-
:disable_external_links => true,
|
77
|
-
:print_media_type => true,
|
78
|
-
:disable_smart_shrinking => true,
|
79
|
-
:use_xserver => true,
|
80
|
-
:no_background => true,
|
81
|
-
:extra => '' # directly inserted into the command to wkhtmltopdf
|
82
|
-
:margin => {:top => SIZE, # default 10 (mm)
|
83
|
-
:bottom => SIZE,
|
84
|
-
:left => SIZE,
|
85
|
-
:right => SIZE},
|
86
|
-
:header => {:html => { :template => 'users/header.pdf.erb', # use :template OR :url
|
87
|
-
:layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
|
88
|
-
:url => 'www.example.com',
|
89
|
-
:locals => { :foo => @bar }},
|
90
|
-
:center => 'TEXT',
|
91
|
-
:font_name => 'NAME',
|
92
|
-
:font_size => SIZE,
|
93
|
-
:left => 'TEXT',
|
94
|
-
:right => 'TEXT',
|
95
|
-
:spacing => REAL,
|
96
|
-
:line => true,
|
97
|
-
:content => 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string)
|
98
|
-
:footer => {:html => { :template => 'shared/footer.pdf.erb', # use :template OR :url
|
99
|
-
:layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
|
100
|
-
:url => 'www.example.com',
|
101
|
-
:locals => { :foo => @bar }},
|
102
|
-
:center => 'TEXT',
|
103
|
-
:font_name => 'NAME',
|
104
|
-
:font_size => SIZE,
|
105
|
-
:left => 'TEXT',
|
106
|
-
:right => 'TEXT',
|
107
|
-
:spacing => REAL,
|
108
|
-
:line => true,
|
109
|
-
:content => 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string)
|
110
|
-
:toc => {:font_name => "NAME",
|
111
|
-
:depth => LEVEL,
|
112
|
-
:header_text => "TEXT",
|
113
|
-
:header_fs => SIZE,
|
114
|
-
:l1_font_size => SIZE,
|
115
|
-
:l2_font_size => SIZE,
|
116
|
-
:l3_font_size => SIZE,
|
117
|
-
:l4_font_size => SIZE,
|
118
|
-
:l5_font_size => SIZE,
|
119
|
-
:l6_font_size => SIZE,
|
120
|
-
:l7_font_size => SIZE,
|
121
|
-
:l1_indentation => NUM,
|
122
|
-
:l2_indentation => NUM,
|
123
|
-
:l3_indentation => NUM,
|
124
|
-
:l4_indentation => NUM,
|
125
|
-
:l5_indentation => NUM,
|
126
|
-
:l6_indentation => NUM,
|
127
|
-
:l7_indentation => NUM,
|
128
|
-
:no_dots => true,
|
129
|
-
:disable_links => true,
|
130
|
-
:disable_back_links => true},
|
131
|
-
:outline => {:outline => true,
|
132
|
-
:outline_depth => LEVEL}
|
44
|
+
format.png do
|
45
|
+
render :png => 'file_name',
|
46
|
+
:disposition => 'attachment' # default 'inline'
|
133
47
|
end
|
134
48
|
end
|
135
49
|
end
|
136
50
|
end
|
137
51
|
|
138
52
|
By default, it will render without a layout (:layout => false) and the template for the current controller and action.
|
139
|
-
|
140
|
-
### Super Advanced Usage ###
|
141
|
-
|
142
|
-
If you need to just create a pdf and not display it:
|
143
|
-
|
144
|
-
# create a pdf from a string
|
145
|
-
pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')
|
146
|
-
|
147
|
-
# create a pdf from string using templates, layouts and content option for header or footer
|
148
|
-
WickedPdf.new.pdf_from_string(
|
149
|
-
render_to_string(:pdf => "pdf_file.pdf", :template => 'templates/pdf.html.erb', :layout => 'pdfs/layout_pdf'),
|
150
|
-
:footer => {:content => render_to_string({:template => 'templates/pdf_footer.html.erb', :layout => 'pdfs/layout_pdf'})}
|
151
|
-
|
152
|
-
# or from your controller, using views & templates and all wicked_pdf options as normal
|
153
|
-
pdf = render_to_string :pdf => "some_file_name"
|
154
|
-
|
155
|
-
# then save to a file
|
156
|
-
save_path = Rails.root.join('pdfs','filename.pdf')
|
157
|
-
File.open(save_path, 'wb') do |file|
|
158
|
-
file << pdf
|
159
|
-
end
|
160
|
-
|
161
|
-
If you need to display utf encoded characters, add this to your pdf views or layouts:
|
162
|
-
|
163
|
-
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
164
|
-
|
165
|
-
### Styles
|
166
|
-
|
167
|
-
You must define absolute paths to CSS files, images, and javascripts; the best option is to use the *wicked_pdf_stylesheet_link_tag*, *wicked_pdf_image_tag*, and *wicked_pdf_javascript_include_tag* helpers.
|
168
|
-
|
169
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
170
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
171
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
172
|
-
<head>
|
173
|
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
174
|
-
<%= wicked_pdf_stylesheet_link_tag "pdf" -%>
|
175
|
-
<%= wicked_pdf_javascript_include_tag "number_pages" %>
|
176
|
-
</head>
|
177
|
-
<body onload='number_pages'>
|
178
|
-
<div id="header">
|
179
|
-
<%= wicked_pdf_image_tag 'mysite.jpg' %>
|
180
|
-
</div>
|
181
|
-
<div id="content">
|
182
|
-
<%= yield %>
|
183
|
-
</div>
|
184
|
-
</body>
|
185
|
-
</html>
|
186
|
-
|
187
|
-
### Page Numbering
|
188
|
-
|
189
|
-
A bit of javascript can help you number your pages. Create a template or header/footer file with this:
|
190
|
-
|
191
|
-
<html>
|
192
|
-
<head>
|
193
|
-
<script>
|
194
|
-
function number_pages() {
|
195
|
-
var vars={};
|
196
|
-
var x=document.location.search.substring(1).split('&');
|
197
|
-
for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
|
198
|
-
var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
|
199
|
-
for(var i in x) {
|
200
|
-
var y = document.getElementsByClassName(x[i]);
|
201
|
-
for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
|
202
|
-
}
|
203
|
-
}
|
204
|
-
</script>
|
205
|
-
</head>
|
206
|
-
<body onload="number_pages()">
|
207
|
-
Page <span class="page"></span> of <span class="topage"></span>
|
208
|
-
</body>
|
209
|
-
</html>
|
210
|
-
|
211
|
-
Anything with a class listed in "var x" above will be auto-filled at render time.
|
212
|
-
|
213
|
-
If you do not have explicit page breaks (and therefore do not have any "page" class), you can also use wkhtmltopdf's built in page number generation by setting one of the headers to "[page]":
|
214
|
-
|
215
|
-
render :pdf => 'filename', :header => { :right => '[page] of [topage]' }
|
216
|
-
|
217
|
-
### Configuration
|
218
|
-
|
219
|
-
You can put your default configuration, applied to all pdf's at "wicked_pdf.rb" initializer.
|
220
|
-
|
221
|
-
### Further Reading
|
222
|
-
|
223
|
-
Andreas Happe's post [Generating PDFs from Ruby on Rails](http://snikt.net/rails/2012/04/26/wicked-pdf/)
|
224
|
-
|
225
|
-
StackOverflow [questions with the tag "wicked-pdf"](http://stackoverflow.com/questions/tagged/wicked-pdf)
|
226
|
-
|
227
|
-
### Debugging
|
228
|
-
|
229
|
-
Now you can use a debug param on the URL that shows you the content of the pdf in plain html to design it faster.
|
230
|
-
|
231
|
-
First of all you must configure the render parameter ":show_as_html => params[:debug]" and then just use it like normally but adding "debug=1" as a param:
|
232
|
-
|
233
|
-
http://localhost:3001/CONTROLLER/X.pdf?debug=1
|
234
|
-
|
235
|
-
However, the wicked_pdf_* helpers will use file:// paths for assets when using :show_as_html, and your browser's cross-domain safety feature will kick in, and not render them. To get around this, you can load your assets like so in your templates:
|
236
|
-
|
237
|
-
<%= params[:debug].present? ? image_tag('foo') : wicked_pdf_image_tag('foo') %>
|
238
|
-
|
239
|
-
### Inspiration
|
240
|
-
|
241
|
-
You may have noticed: this plugin is heavily inspired by the PrinceXML plugin [princely](http://github.com/mbleigh/princely/tree/master). PrinceXML's cost was prohibitive for me. So, with a little help from some friends (thanks [jqr](http://github.com/jqr)), I tracked down wkhtmltopdf, and here we are.
|
242
|
-
|
243
|
-
### Awesome Peoples
|
244
|
-
|
245
|
-
Also, thanks to [unixmonkey](https://github.com/Unixmonkey), [galdomedia](http://github.com/galdomedia), [jcrisp](http://github.com/jcrisp), [lleirborras](http://github.com/lleirborras), [tiennou](http://github.com/tiennou), and everyone else for all their hard work and patience with my delays in merging in their enhancements.
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: go_static
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lleir Borras Metje
|