breezy_pdf 0.0.17 → 0.0.18

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: 22152e31ae48f2787e5860af3c1fbd2f27b1108b
4
- data.tar.gz: 461a8f053a29e9a761f4ca645f1545a120a28ae7
3
+ metadata.gz: f96e0707087a6ec855ab37b985ee3ff85462cd0f
4
+ data.tar.gz: a194a4f380f659fa12f695ccd83047649a858d17
5
5
  SHA512:
6
- metadata.gz: dde4a034799d9408d8b7a6206b30532a834cdd35b45f7acdbe2dfc09e9a5f0bf8602157d41b6f6f2c6a773b1049a54ff3bd21fc36b959cecc86c15e4ba1ac054
7
- data.tar.gz: cb25cf71e8379d207c5f7811541d47708e12fec3e5f6df173dae8692042d1decf4d7ab3f04a68ec5f153e4b65ed32a1ac63d93e980614bc08c52c20efcf05088
6
+ metadata.gz: 880c5ba65853fb17df0f3be7893b7e75a2b3542d4b2874fa2df27bd8b61020d1976aeee5469f2b5c6aefac1ce78a9a26a6a2c6a8c19dd545ed65b3d553cc1354
7
+ data.tar.gz: b747791a6b0c9418e91b027b2244235b86be9337022e279a674cd33fbfbdd6b1d792d3886076c3abfecbd7d136bc99d8904310b9e122d1feab6f27f1816ff06c
data/README.md CHANGED
@@ -140,7 +140,7 @@ BreezyPDF.setup do |config|
140
140
  #
141
141
  # Cache asset URL's to prevent re-uploading of assets. Assets are cached based on asset_path,
142
142
  # so fingerprinting or digests are recommended before turning on. The default cache store is
143
- # a null store, which won't actuall store anything. An in-memory store is also provided, but
143
+ # a null store, which won't actually store anything. An in-memory store is also provided, but
144
144
  # this store won't share values across threads. Alternatively, use an external store which
145
145
  # implements a `fetch(key, opts={}, &blk) API, such as the Rails.cache.
146
146
  #
@@ -161,7 +161,7 @@ BreezyPDF.setup do |config|
161
161
  # Extract Metadata
162
162
  #
163
163
  # BreezyPDF supports specifying how a page should be rendered through meta tags within
164
- # the HTML to be rendered. Contact support@breezypdf.com for more details. Default is
164
+ # the HTML to be rendered. Visit https://docs.breezypdf.com for metadata information. Default is
165
165
  # true.
166
166
  #
167
167
  # Only applicable when `treat_urls_as_private == true`
@@ -207,6 +207,16 @@ BreezyPDF.setup do |config|
207
207
  # Configure the logger, if you're into that sort of thing.
208
208
  #
209
209
  # config.logger = Logger.new(STDOUT).tap { |logger| logger.level = Logger::FATAL }
210
+
211
+ # Default Meta Data
212
+ #
213
+ # Define default meta data which will be included with every render. Extracted metadata
214
+ # will override these values. Visit https://docs.breezypdf.com for metadata information.
215
+ #
216
+ # config.default_metadata = {
217
+ # width: 23.4,
218
+ # height: 33.1
219
+ # }
210
220
  end
211
221
  ```
212
222
 
@@ -219,48 +229,28 @@ If you want to show a loading animation on the current page, you can simply load
219
229
  Here is a contrived example:
220
230
 
221
231
  ```html
222
- <a href="/this/path.pdf" class="breezy-pdf-download">Download as PDF</a>
232
+ <a href="/this/path.pdf" class="pdf-link">Download as PDF</a>
223
233
 
224
234
  <script type="text/javascript">
225
- var downloadLinkEls = document.querySelectorAll('.breezy-pdf-download');
226
- var loadingEl = document.createElement('div');
227
- loadingEl.innerHTML = "<h1>Loading!</h1><p>Loading icon here, maybe?</p><p id='breezy-progress'></p>";
228
- loadingEl.style = "position: absolute; width: 100%; height: 100%; text-align: center; background: #fff; top: 0; left: 0; z-index: 10000; display: none;"
229
-
230
- document.body.appendChild(loadingEl);
231
- var progressEl = document.getElementById('breezy-progress');
232
-
233
- for (var i = downloadLinkEls.length - 1; i >= 0; i--) {
234
- var linkEl = downloadLinkEls[i];
235
- var pdfUrl = linkEl.getAttribute('href');
236
-
237
- // Listen for a click on the link, then start handling the change of state
238
- linkEl.addEventListener('click', function(ev) {
239
- loadingEl.style.display = "block"; // Display ad-hoc loading element
240
- ev.preventDefault();
241
-
242
- var i = 1;
243
- var interval = setInterval(function() {
244
- progressEl.innerText = i / 10.0 + ' waiting seconds so far...';
245
- i++;
246
- }, 100);
247
-
248
- var ajaxRequest = new XMLHttpRequest();
249
-
250
- ajaxRequest.addEventListener('load', function(ev) {
251
- clearInterval(interval);
252
- console.log("Done waiting. We'd close modals or remove loading animations here before setting the location.")
253
- loadingEl.style.display = "none";
254
-
255
- // Redirect the eventual URL of the PDF
256
- // If the browser downloads the file, the current page's HTML will still be shown
257
- window.location = ev.currentTarget.responseURL;
258
- })
259
-
260
- ajaxRequest.open('GET', pdfUrl);
261
- ajaxRequest.send();
235
+ $('.pdf-link').on('click', function(clickEv) {
236
+ var startTime = new Date();
237
+ var targetEl = $(clickEv.target);
238
+ var modalEl = $('#pdf-loading'); // An existing bootstrap modal in this example
239
+ var ajaxRequest = new XMLHttpRequest();
240
+
241
+ clickEv.preventDefault();
242
+
243
+ ajaxRequest.addEventListener('load', function(ev) {
244
+ modalEl.modal('hide');
245
+
246
+ window.location = ev.currentTarget.responseURL;
262
247
  })
263
- }
248
+
249
+ modalEl.modal('show');
250
+
251
+ ajaxRequest.open('GET', targetEl.attr('href'));
252
+ ajaxRequest.send();
253
+ })
264
254
  </script>
265
255
  ```
266
256
 
@@ -274,8 +264,10 @@ Here is an example of how you might do that:
274
264
  def invoice_mailer(user, invoice)
275
265
  asset_host = Rails.env.production? ? Rails.application.config.action_controller.asset_host : "http://localhost:3000"
276
266
 
267
+ metadata = { width: 8.5, width: 11 }
268
+
277
269
  html = ActionController::Renderer.render(template "invoices/show", assigns: { invoice: invoice }, locals: { current_user: user })
278
- pdf = BreezyPDF::HTML2PDF.new(, html)
270
+ pdf = BreezyPDF::HTML2PDF.new(asset_host, html, metadata)
279
271
 
280
272
  attachments["invoice-#{invoice.id}.pdf"] = pdf.to_file.read
281
273
  @pdf_url = pdf.to_url
data/lib/breezy_pdf.rb CHANGED
@@ -75,6 +75,22 @@ module BreezyPDF
75
75
  mattr_accessor :filter_elements_selectors
76
76
  @@filtered_element_selectors = %w[.breezy-pdf-remove]
77
77
 
78
+ mattr_writer :default_metadata
79
+ @@default_metadata = {
80
+ width: 8.5,
81
+ height: 11,
82
+ cssPageSize: false,
83
+ marginTop: 0.04,
84
+ marginRight: 0.04,
85
+ marginBottom: 0.04,
86
+ marginLeft: 0.04,
87
+ landscape: false,
88
+ scale: 1,
89
+ displayBackground: false,
90
+ headerTemplate: "",
91
+ footerTemplate: ""
92
+ }
93
+
78
94
  mattr_accessor :logger
79
95
  @@logger = Logger.new(STDOUT)
80
96
  @@logger.level = Logger::FATAL
@@ -82,4 +98,9 @@ module BreezyPDF
82
98
  def self.setup
83
99
  yield self
84
100
  end
101
+
102
+ # Support proper merging of hash rocket and symbol keys
103
+ def self.default_metadata
104
+ @@jsonified_metadata ||= JSON.parse(@@default_metadata.to_json)
105
+ end
85
106
  end
@@ -14,7 +14,7 @@ module BreezyPDF::HTML
14
14
  def public_fragment
15
15
  @public_fragment ||= parsed_document.tap do
16
16
  publicize!
17
- BreezyPDF.logger.info("[BreezyPDF] Replaced assets in `#{timing} seconds`")
17
+ BreezyPDF.logger.info("[BreezyPDF] Replaced assets in #{timing} seconds")
18
18
  end.to_html
19
19
  end
20
20
 
@@ -4,9 +4,10 @@ module BreezyPDF
4
4
  # Transform an HTML slug to a PDF
5
5
  # Access it's URL or download it locally and access it as a Tempfile
6
6
  class HTML2PDF
7
- def initialize(asset_host, html_string)
7
+ def initialize(asset_host, html_string, metadata = {})
8
8
  @asset_host = asset_host
9
9
  @html_string = html_string
10
+ @metadata = metadata
10
11
  end
11
12
 
12
13
  def to_url
@@ -30,7 +31,11 @@ module BreezyPDF
30
31
  end
31
32
 
32
33
  def url
33
- @url ||= BreezyPDF::RenderRequest.new(public_url, html_private_asset.metadata).submit.download_url
34
+ @url ||= BreezyPDF::RenderRequest.new(public_url, combined_metadata).submit.download_url
35
+ end
36
+
37
+ def combined_metadata
38
+ @combined_metadata ||= BreezyPDF.default_metadata.merge(@metadata).merge(html_private_asset.metadata)
34
39
  end
35
40
 
36
41
  def io_object
@@ -18,9 +18,9 @@ module BreezyPDF::Intercept
18
18
  end
19
19
 
20
20
  def metadata
21
- {
21
+ BreezyPDF.default_metadata.merge(
22
22
  "requested_url" => requested_url, "rendered_url" => rendered_url
23
- }
23
+ )
24
24
  end
25
25
 
26
26
  def rendered_url
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BreezyPDF
4
- VERSION = "0.0.17"
4
+ VERSION = "0.0.18"
5
5
  end
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.17
4
+ version: 0.0.18
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-04-02 00:00:00.000000000 Z
11
+ date: 2018-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby