postdoc 0.2.3 → 0.3.4

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: d21422293d756a0a95e7a697efcf2d35e9d8fc558124d8a665c05c5e3eef2e3d
4
- data.tar.gz: bbfda4ff89ce4066f8ea2ea1da1190ce30c21990508db035c05aa733dc1e9014
3
+ metadata.gz: a6a52e2638a2925d5834959e3a716295aaf5a66e8be797452c507b72f4fa7e0a
4
+ data.tar.gz: cca67e571e577b9ac1b770a4b9049b66953beab03ab13d6046d90684af0daf91
5
5
  SHA512:
6
- metadata.gz: 32451b802853cbe1e0383ae26e7c264956cf136c0703c7e3cbdbd3ff4d5d8364db2c5647aa010d196c70b9759dfeff7668d0caff1750ee8e3600a688e13ad56b
7
- data.tar.gz: 7ee471438ee718694588e0b1827e601e6a4b68b22685c937e76a97448d9fc64a50f3010d02fc0c0a50d0679da894290b9db77fffab9a39458c021c583d5abb57
6
+ metadata.gz: e7ccb76057f1cf0112dc9150fba8de4b9aef0261ed0f229a15af3030a4e4b10bb80fb8929dddf40aa16af10bf0133778da3cb7d5ed99f81a93b73267cb4e3b21
7
+ data.tar.gz: 7e5b5c6eca2c4020b4e8c27078aed985bcfb4e1b4f0e4bd0c3a7f8585e094069a8eb723619045f1fe2971c9cc9d3c4bff92205b6225dacdfc9e1849f10dee3be
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|
9
+ ActionController::Renderers.add :pdf do |_filename, options|
5
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,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Postdoc
4
+ class ChromeProcess
5
+ attr_reader :pid, :port
6
+
7
+ def initialize(port: Random.rand(65_535 - 1024), **_options)
8
+ @port = port
9
+ @pid = Process.spawn "chrome --remote-debugging-port=#{port} --headless",
10
+ out: File::NULL, err: File::NULL
11
+ end
12
+
13
+ def alive?
14
+ @alive ||= test_socket!
15
+ rescue Errno::ECONNREFUSED
16
+ false
17
+ end
18
+
19
+ def kill
20
+ Process.kill 'KILL', pid
21
+ Process.wait pid
22
+ end
23
+
24
+ def client
25
+ Client.new port
26
+ end
27
+
28
+ private
29
+
30
+ def test_socket!
31
+ TCPSocket.new('localhost', port)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,49 @@
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
+ raise 'ChromeClient couldn\'t launch' if @client.blank?
13
+ end
14
+
15
+ def print_pdf_from_html(file_path,
16
+ header_template: false,
17
+ footer_template: false,
18
+ **options)
19
+
20
+ client.send_cmd 'Page.enable'
21
+ client.send_cmd 'Page.navigate', url: "file://#{file_path}"
22
+ client.wait_for 'Page.loadEventFired'
23
+
24
+ response = client.send_cmd 'Page.printToPDF', {
25
+ landscape: options[:landscape] || false,
26
+ printBackground: true,
27
+ marginTop: options[:margin_top] || 1,
28
+ marginBottom: options[:margin_bottom] || 1,
29
+ marginLeft: options[:margin_left] || 1,
30
+ marginRight: options[:margin_right] || 1,
31
+ displayHeaderFooter: !!(header_template || footer_template),
32
+ headerTemplate: header_template || '',
33
+ footerTemplate: footer_template || ''
34
+ }
35
+
36
+ Base64.decode64 response['data']
37
+ end
38
+
39
+ private
40
+
41
+ def setup_connection_or_wait
42
+ @client = ChromeRemote.client(port: @port)
43
+ true
44
+ rescue Errno::ECONNREFUSED
45
+ sleep(0.1)
46
+ false
47
+ end
48
+ end
49
+ 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.3
4
+ version: 0.3.4
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-13 00:00:00.000000000 Z
11
+ date: 2021-03-16 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: []