breezy_pdf 0.0.10 → 0.0.11

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b91b8eac90c1bf86deddc2e966c2d50606fcc4fd
4
- data.tar.gz: 29095dcc755d6233c8a78cd06339e4364485f289
3
+ metadata.gz: bab096cd639ab31ef3b100733473563c2f2d5892
4
+ data.tar.gz: e8fc7df02cc0f40311a136eca9416ba5d48076e0
5
5
  SHA512:
6
- metadata.gz: 032c38d3690ecce7ced0de58cbf51c16c7c9c28e083e627ea3edd7dd466e9d7f1546b710cfb8c33c24363409c85506f22214b4dba8f55f483fb88cf7427287c7
7
- data.tar.gz: d960c94bb564f598bb8b3e5712d527f18cd5d402b308262177e2d3d945dd2a4d66fafbdb2ccc651da0053083c37fb9507bb0db25d7d9b1e117222df86d7bbbe4
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BreezyPDF
4
- VERSION = "0.0.10"
4
+ VERSION = "0.0.11"
5
5
  end
data/lib/breezy_pdf.rb CHANGED
@@ -29,6 +29,7 @@ module BreezyPDF
29
29
  autoload :Intercept, "breezy_pdf/intercept"
30
30
  autoload :PrivateAssets, "breezy_pdf/private_assets"
31
31
  autoload :HTML, "breezy_pdf/html"
32
+ autoload :HTML2PDF, "breezy_pdf/html_2_pdf"
32
33
 
33
34
  BreezyPDFError = Class.new(StandardError)
34
35
 
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.10
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-27 00:00:00.000000000 Z
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