postdoc 0.2.2 → 0.3.3

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
  SHA256:
3
- metadata.gz: a85e6b50205b46d1127b53214c2bad5e53aef5765081e17a1998eafb7c1bae4b
4
- data.tar.gz: 6d00e103be5c33ce38e20e0bfa3cf8939ee2a8824a9a13e32f15662fea31c0b6
3
+ metadata.gz: 866cf4c6e0cfe521087c96c5aea85b003a139d55467ccd77e757c6c45084cb17
4
+ data.tar.gz: c2789db75c33ede4a402e06c815d7e9a33689960d53773940fca96f350ec1f2c
5
5
  SHA512:
6
- metadata.gz: 416f6e0522cbd000347afb73c1e66fc7ad1b054db46d0bb4ae2a3b21dfc24720b8bbdd1eca7fbe7c13860815a3916aa30e1d7d009e1f2675aca5f8c599755f7d
7
- data.tar.gz: a5b397c41b09ddf29125eb3ddd4e967971abcabefcf3a7b57222888c827b783f6978fb14316d51170baf8c7c04b0e701c5054fdaac151438452b943552ebe5fc
6
+ metadata.gz: d8fedb4b38ebc3fbb45577423c478211c7d801bb917742b2493c1f57869b51aaba1877efac94383e1fbdfb77bd07d3c0ec5eb9edb2a8e6f732453045e865a797
7
+ data.tar.gz: af3070ea0e915b3f44349f926e5fe08d1c2270f4bc7143a1352145a679e0882392b5f1e02800e6a7240612c16fa182fa1282424262498e171d89a6b7726d656b
data/lib/postdoc.rb CHANGED
@@ -1,55 +1,22 @@
1
- require 'chrome_remote'
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_controller'
4
+ require 'postdoc/chrome_process'
5
+ require 'postdoc/client'
6
+ require 'postdoc/html_document'
2
7
 
3
8
  module Postdoc
4
- ActionController::Renderers.add :pdf do |filename, options|
5
- render_from_string_to_pdf render_to_string(options), options
9
+ ActionController::Renderers.add :pdf do |_filename, options|
10
+ Postdoc.render_from_string render_to_string(options), options
6
11
  end
7
12
 
8
- def self.render_from_string(string, options)
9
- htmlfile = Tempfile.new ['input', '.html']
10
-
11
- htmlfile.write string
12
- htmlfile.flush
13
-
14
- # random port at 1025 or higher
15
- random_port = 1024 + Random.rand(65535 - 1024)
16
-
17
- pid = Process.spawn "chrome --remote-debugging-port=#{random_port} --headless"
18
-
19
- # FIXME
20
- sleep 1
21
-
22
- begin
23
- chrome = ChromeRemote.client port: random_port
24
- chrome.send_cmd 'Page.enable'
25
-
26
- chrome.send_cmd 'Page.navigate', url: "file://#{htmlfile.path}"
27
- chrome.wait_for 'Page.loadEventFired'
28
-
29
- if options[:header_template].present? || options[:footer_template].present?
30
- displayHeaderFooter = true
31
- else
32
- displayHeaderFooter = false
33
- end
34
-
35
- response = chrome.send_cmd 'Page.printToPDF', {
36
- landscape: options[:landscape] || false,
37
- printBackground: true,
38
- marginTop: options[:margin_top] || 1,
39
- marginBottom: options[:margin_bottom] || 1,
40
- displayHeaderFooter: displayHeaderFooter,
41
- headerTemplate: options[:header_template],
42
- footerTemplate: options[:footer_template]
43
- }
44
- result = Base64.decode64 response['data']
45
- ensure
46
- Process.kill 'KILL', pid
47
- Process.wait pid
48
-
49
- htmlfile.close
50
- htmlfile.unlink
51
- end
52
-
53
- result
13
+ def self.render_from_string(content, **options)
14
+ server = ChromeProcess.new(**options)
15
+ html_file = HTMLDocument.new(content, **options)
16
+ server.client
17
+ .print_pdf_from_html(html_file.path, **options)
18
+ ensure
19
+ server.kill
20
+ html_file.cleanup
54
21
  end
55
22
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+ module Postdoc
3
+ class ChromeProcess
4
+ attr_reader :pid, :port
5
+
6
+ def initialize(port: Random.rand(65_535 - 1024), **_options)
7
+ @port = port
8
+ @pid = Process.spawn "chrome --remote-debugging-port=#{port} --headless",
9
+ out: File::NULL, err: File::NULL
10
+ end
11
+
12
+ def alive?
13
+ @alive ||= test_socket!
14
+ rescue
15
+ false
16
+ end
17
+
18
+ def kill
19
+ Process.kill 'KILL', pid
20
+ Process.wait pid
21
+ end
22
+
23
+ def client
24
+ Client.new port
25
+ end
26
+
27
+ private
28
+
29
+ def test_socket!
30
+ TCPSocket.new('localhost', port)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'chrome_remote'
4
+
5
+ module Postdoc
6
+ class Client
7
+ attr_accessor :client
8
+
9
+ def initialize(port)
10
+ @port = port
11
+ 100.times { setup_connection_or_wait && break }
12
+ end
13
+
14
+ def print_pdf_from_html(file_path,
15
+ header_template: false,
16
+ footer_template: false,
17
+ **options)
18
+
19
+ client.send_cmd 'Page.enable'
20
+ client.send_cmd 'Page.navigate', url: "file://#{file_path}"
21
+ client.wait_for 'Page.loadEventFired'
22
+
23
+ response = client.send_cmd 'Page.printToPDF', {
24
+ landscape: options[:landscape] || false,
25
+ printBackground: true,
26
+ marginTop: options[:margin_top] || 1,
27
+ marginBottom: options[:margin_bottom] || 1,
28
+ displayHeaderFooter: !!(header_template || footer_template),
29
+ headerTemplate: header_template || '',
30
+ footerTemplate: footer_template || ''
31
+ }
32
+
33
+ Base64.decode64 response['data']
34
+ end
35
+
36
+ private
37
+
38
+ def setup_connection_or_wait
39
+ @client = ChromeRemote.client(port: @port)
40
+ true
41
+ rescue
42
+ sleep(0.1)
43
+ false
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+
5
+ module Postdoc
6
+ class HTMLDocument
7
+ attr_accessor :file
8
+
9
+ delegate :path, to: :file
10
+
11
+ def initialize(content, path: nil, **_options)
12
+ path ||= Rails.root&.join('tmp') || '/tmp'
13
+ @file = Tempfile.new ['input', '.html'], path
14
+
15
+ file.write content
16
+ file.flush
17
+ end
18
+
19
+ def cleanup
20
+ file.close
21
+ file.unlink
22
+ end
23
+ end
24
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postdoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank Groeneveld
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-11 00:00:00.000000000 Z
11
+ date: 2021-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chrome_remote
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rails
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -31,19 +45,19 @@ dependencies:
31
45
  - !ruby/object:Gem::Version
32
46
  version: '6.1'
33
47
  - !ruby/object:Gem::Dependency
34
- name: chrome_remote
48
+ name: mocha
35
49
  requirement: !ruby/object:Gem::Requirement
36
50
  requirements:
37
51
  - - ">="
38
52
  - !ruby/object:Gem::Version
39
- version: 0.2.0
40
- type: :runtime
53
+ version: '0'
54
+ type: :development
41
55
  prerelease: false
42
56
  version_requirements: !ruby/object:Gem::Requirement
43
57
  requirements:
44
58
  - - ">="
45
59
  - !ruby/object:Gem::Version
46
- version: 0.2.0
60
+ version: '0'
47
61
  description: Description of Postdoc.
48
62
  email:
49
63
  - frank@ivaldi.nl
@@ -54,11 +68,14 @@ files:
54
68
  - LICENSE
55
69
  - Rakefile
56
70
  - lib/postdoc.rb
71
+ - lib/postdoc/chrome_process.rb
72
+ - lib/postdoc/client.rb
73
+ - lib/postdoc/html_document.rb
57
74
  homepage: https://github.com/ivaldi/postdoc
58
75
  licenses:
59
76
  - BSD-2-Clause
60
77
  metadata: {}
61
- post_install_message:
78
+ post_install_message:
62
79
  rdoc_options: []
63
80
  require_paths:
64
81
  - lib
@@ -73,8 +90,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
90
  - !ruby/object:Gem::Version
74
91
  version: '0'
75
92
  requirements: []
76
- rubygems_version: 3.0.3
77
- signing_key:
93
+ rubygems_version: 3.1.4
94
+ signing_key:
78
95
  specification_version: 4
79
96
  summary: Summary of Postdoc.
80
97
  test_files: []