breezy_pdf 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/lib/breezy_pdf/html_2_pdf.rb +50 -0
- data/lib/breezy_pdf/version.rb +1 -1
- data/lib/breezy_pdf.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bab096cd639ab31ef3b100733473563c2f2d5892
|
4
|
+
data.tar.gz: e8fc7df02cc0f40311a136eca9416ba5d48076e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cacf0a2a24d3fad7bc333da4088304a865f8d5cdd45ee3ed51ad6025526cfd44de02e319ececd51cc4370a71f859c8986699e038604efe97298b9d0c97c65c15
|
7
|
+
data.tar.gz: 1ca284b117fbb9a91ad8802bbbd34aa646f79105e1ede4eb5aec7cf2a1afd9542ceb83ec7294ec2af868bad363e4b174f8126e6bcf01caf883a1c4f0b5dd7dcd
|
data/README.md
CHANGED
@@ -237,6 +237,27 @@ Here is a contrived example:
|
|
237
237
|
</script>
|
238
238
|
```
|
239
239
|
|
240
|
+
## Pragmatic access
|
241
|
+
|
242
|
+
You may want to render a PDF out of the HTTP request cycle like in a background job or mailer. BreezyPDF supports rendering HTML as a PDF pragmatically. BreezyPDF::HTML2PDF expects two arguments, the `asset_host` for assets, and the html fragment. The `asset_host` should be the locally accessible host where external assets (js, css) can be downloaded from. In development, this may be `http://localhost:3000`, while in production it may be your configured `Rails.application.config.action_controller.asset_host`. If `BreezyPDF.upload_assets == false`, then this value has no impact.
|
243
|
+
|
244
|
+
Here is an example of how you might do that:
|
245
|
+
|
246
|
+
```
|
247
|
+
def invoice_mailer(user, invoice)
|
248
|
+
asset_host = Rails.env.production? ? Rails.application.config.action_controller.asset_host : "http://localhost:3000"
|
249
|
+
|
250
|
+
html = ActionController::Renderer.render(template "invoices/show", assigns: { invoice: invoice }, locals: { current_user: user })
|
251
|
+
pdf = BreezyPDF::HTML2PDF.new(, html)
|
252
|
+
|
253
|
+
attachments["invoice-#{invoice.id}.pdf"] = pdf.to_file
|
254
|
+
@pdf_url = pdf.to_url
|
255
|
+
end
|
256
|
+
```
|
257
|
+
|
258
|
+
This example uses [ActionController::Renderer](http://api.rubyonrails.org/classes/ActionController/Renderer.html) to render a controller template's html.
|
259
|
+
|
260
|
+
|
240
261
|
## Development
|
241
262
|
|
242
263
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BreezyPDF
|
4
|
+
# Transform an HTML slug to a PDF
|
5
|
+
# Access it's URL or download it locally and access it as a Tempfile
|
6
|
+
class HTML2PDF
|
7
|
+
def initialize(asset_host, html_string)
|
8
|
+
@asset_host = asset_host
|
9
|
+
@html_string = html_string
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_url
|
13
|
+
url
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_file
|
17
|
+
file
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def file
|
23
|
+
@file ||= if io_object.is_a?(StringIO)
|
24
|
+
Tempfile.new.tap do |f|
|
25
|
+
f.write io_object.to_s
|
26
|
+
end
|
27
|
+
else
|
28
|
+
io_object
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def url
|
33
|
+
@url ||= BreezyPDF::RenderRequest.new(public_url, html_private_asset.metadata).submit.download_url
|
34
|
+
end
|
35
|
+
|
36
|
+
def io_object
|
37
|
+
@io_object ||= open(url)
|
38
|
+
end
|
39
|
+
|
40
|
+
def html_private_asset
|
41
|
+
@html_private_asset ||= BreezyPDF::PrivateAssets::HTML.new(@asset_host, @html_string)
|
42
|
+
end
|
43
|
+
|
44
|
+
def public_url
|
45
|
+
@public_url ||= BreezyPDF::Uploads::Base.new(
|
46
|
+
html_private_asset.filename, html_private_asset.content_type, html_private_asset.file_path
|
47
|
+
).public_url
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/breezy_pdf/version.rb
CHANGED
data/lib/breezy_pdf.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: breezy_pdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Westendorf
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-03-
|
11
|
+
date: 2018-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -147,6 +147,7 @@ files:
|
|
147
147
|
- lib/breezy_pdf/html.rb
|
148
148
|
- lib/breezy_pdf/html/publicize.rb
|
149
149
|
- lib/breezy_pdf/html/strip.rb
|
150
|
+
- lib/breezy_pdf/html_2_pdf.rb
|
150
151
|
- lib/breezy_pdf/intercept.rb
|
151
152
|
- lib/breezy_pdf/intercept/base.rb
|
152
153
|
- lib/breezy_pdf/intercept/private_url.rb
|