ferrum_pdf 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ea0c6aba4254ef7d958dec7d9f51048ce82e106ab068a413c1e3b83ec470b01
4
- data.tar.gz: e34df1014f457e4ad42a41a5a29ab9a0d0ab42027cb8d8374aeacf6557a49833
3
+ metadata.gz: f7537d25768b2cf12df0322c63ad643c5a56dfbce85d812db0cab2f46e5f280a
4
+ data.tar.gz: b36728c8686eacf59543f756ff2c3daddc20a4d9eb806580dd87faa732471cf0
5
5
  SHA512:
6
- metadata.gz: 68e840f65f0c0821cb8dfe6f3e249e2b5b567f8d3655c1a71a59f143beb25241aedea29fe33d95b18763a32fd5eea88bec9f6a9f4b0b6181df082544cc2453c2
7
- data.tar.gz: 123f094697e37dcee391c157ca5ba379b1b9557e8a4e7c017cb7b45861936bf24e484bec74092793dd3e64e36ffaee0a98bc613f56ed9a5e4e2dfefdbf7f1ac1
6
+ metadata.gz: 8f1b5d33362700a1ed42ff99068b968dc696fdb6f573b5c59f1f7be0207dc891cad8dd13994d8881fcb8a49ed774ed7aa7312445ccc15955f243dea30a8b66ce
7
+ data.tar.gz: 702c650ee2f84c7eee9562cd989cb938dac2e7ca3dfe2d0e800b9315882e6309817743c639e1f8b9b47ecb2d775a1cb023a33cc68f4ef84f526aa62247358e3d
data/README.md CHANGED
@@ -28,10 +28,20 @@ end
28
28
  You can also customize which template is rendered:
29
29
 
30
30
  ```ruby
31
- render_pdf(name = action_name, formats: [ :html ])
31
+ render_pdf(name = action_name, formats: [ :html ], pdf_options: {})
32
32
  ```
33
33
 
34
- This will render the template to string, then pass it along to FerrumPdf.
34
+ This will render the template to string with `render_to_string` in Rails, then pass it along to FerrumPdf.
35
+
36
+ For example, you can add headers and footers using `pdf_options`
37
+
38
+ ```ruby
39
+ render_pdf(pdf_options: {
40
+ display_header_footer: true,
41
+ header_template: FerrumPdf::DEFAULT_HEADER_TEMPLATE,
42
+ footer_template: FerrumPdf::DEFAULT_FOOTER_TEMPLATE
43
+ })
44
+ ```
35
45
 
36
46
  ### Directly with HTML
37
47
 
@@ -47,10 +57,14 @@ You can also pass host and protocol to convert any relative paths to full URLs.
47
57
  FerrumPdf.render_pdf(
48
58
  html: content,
49
59
  host: request.host_with_port,
50
- protocol: request.protocol
60
+ protocol: request.protocol,
61
+ pdf_options: {}
51
62
  )
52
63
  ```
53
64
 
65
+ See Chrome DevTools Protocol docs: https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
66
+ And Ferrum's `#pdf` docs: https://github.com/rubycdp/ferrum?tab=readme-ov-file#pdfoptions--string--boolean
67
+
54
68
  ## Contributing
55
69
 
56
70
  If you have an issue you'd like to submit, please do so using the issue tracker in GitHub. In order for us to help you in the best way possible, please be as detailed as you can.
@@ -1,3 +1,3 @@
1
1
  module FerrumPdf
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/ferrum_pdf.rb CHANGED
@@ -4,22 +4,25 @@ require "ferrum_pdf/railtie"
4
4
  require "ferrum"
5
5
 
6
6
  module FerrumPdf
7
+ DEFAULT_HEADER_TEMPLATE = "<div class='date text left'></div><div class='title text center'></div>"
8
+ DEFAULT_FOOTER_TEMPLATE = <<~HTML
9
+ <div class='url text left grow'></div>
10
+ <div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>
11
+ HTML
12
+
7
13
  def self.browser(**options)
8
14
  @browser ||= Ferrum::Browser.new(options)
9
15
  end
10
16
 
11
- def self.render_pdf(host:, protocol:, html: nil, url: nil)
12
- Tempfile.create do |file|
13
- browser.create_page do |page|
14
- if html
15
- page.content = FerrumPdf::HTMLPreprocessor.process(html, host, protocol)
16
- page.network.wait_for_idle
17
- else
18
- page.go_to(url)
19
- end
20
- page.pdf(path: file.path)
21
- file.read
17
+ def self.render_pdf(host:, protocol:, html: nil, url: nil, pdf_options: {})
18
+ browser.create_page do |page|
19
+ if html
20
+ page.content = FerrumPdf::HTMLPreprocessor.process(html, host, protocol)
21
+ page.network.wait_for_idle
22
+ else
23
+ page.go_to(url)
22
24
  end
25
+ page.pdf(**pdf_options.with_defaults(encoding: :binary))
23
26
  end
24
27
  end
25
28
 
@@ -51,13 +54,14 @@ module FerrumPdf
51
54
  module Controller
52
55
  extend ActiveSupport::Concern
53
56
 
54
- def render_pdf(name = action_name, formats: [ :html ])
57
+ def render_pdf(name = action_name, formats: [ :html ], pdf_options: {})
55
58
  content = render_to_string(name, formats: formats)
56
59
 
57
60
  FerrumPdf.render_pdf(
58
61
  html: content,
59
62
  host: request.host_with_port,
60
- protocol: request.protocol
63
+ protocol: request.protocol,
64
+ pdf_options: pdf_options
61
65
  )
62
66
  end
63
67
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ferrum_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-20 00:00:00.000000000 Z
11
+ date: 2024-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  requirements: []
77
- rubygems_version: 3.5.18
77
+ rubygems_version: 3.5.16
78
78
  signing_key:
79
79
  specification_version: 4
80
80
  summary: PDFs for Rails using Ferrum & headless Chrome