postdoc 0.3.1 → 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 +4 -4
- data/lib/postdoc.rb +12 -62
- data/lib/postdoc/chrome_process.rb +33 -0
- data/lib/postdoc/client.rb +46 -0
- data/lib/postdoc/html_document.rb +24 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 866cf4c6e0cfe521087c96c5aea85b003a139d55467ccd77e757c6c45084cb17
|
4
|
+
data.tar.gz: c2789db75c33ede4a402e06c815d7e9a33689960d53773940fca96f350ec1f2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8fedb4b38ebc3fbb45577423c478211c7d801bb917742b2493c1f57869b51aaba1877efac94383e1fbdfb77bd07d3c0ec5eb9edb2a8e6f732453045e865a797
|
7
|
+
data.tar.gz: af3070ea0e915b3f44349f926e5fe08d1c2270f4bc7143a1352145a679e0882392b5f1e02800e6a7240612c16fa182fa1282424262498e171d89a6b7726d656b
|
data/lib/postdoc.rb
CHANGED
@@ -1,72 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'action_controller'
|
4
|
+
require 'postdoc/chrome_process'
|
5
|
+
require 'postdoc/client'
|
6
|
+
require 'postdoc/html_document'
|
4
7
|
|
5
8
|
module Postdoc
|
6
9
|
ActionController::Renderers.add :pdf do |_filename, options|
|
7
10
|
Postdoc.render_from_string render_to_string(options), options
|
8
11
|
end
|
9
12
|
|
10
|
-
def self.render_from_string(
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
random_port = 1024 + Random.rand(65_535 - 1024)
|
19
|
-
pid = Process.spawn "chrome --remote-debugging-port=#{random_port} --headless"
|
20
|
-
end
|
21
|
-
|
22
|
-
success = false
|
23
|
-
10.times do
|
24
|
-
begin
|
25
|
-
TCPSocket.new('localhost', random_port)
|
26
|
-
success = true
|
27
|
-
break
|
28
|
-
rescue
|
29
|
-
end
|
30
|
-
sleep 1
|
31
|
-
end
|
32
|
-
|
33
|
-
return unless success
|
34
|
-
|
35
|
-
begin
|
36
|
-
chrome = options[:client].nil? ? ChromeRemote.client(port: random_port) : options[:client]
|
37
|
-
|
38
|
-
chrome.send_cmd 'Page.enable'
|
39
|
-
chrome.send_cmd 'Page.navigate', url: "file://#{htmlfile.path}"
|
40
|
-
chrome.wait_for 'Page.loadEventFired'
|
41
|
-
|
42
|
-
if options[:header_template].present? || options[:footer_template].present?
|
43
|
-
displayHeaderFooter = true
|
44
|
-
else
|
45
|
-
displayHeaderFooter = false
|
46
|
-
end
|
47
|
-
|
48
|
-
response = chrome.send_cmd 'Page.printToPDF', {
|
49
|
-
landscape: options[:landscape] || false,
|
50
|
-
printBackground: true,
|
51
|
-
marginTop: options[:margin_top] || 1,
|
52
|
-
marginBottom: options[:margin_bottom] || 1,
|
53
|
-
displayHeaderFooter: displayHeaderFooter,
|
54
|
-
headerTemplate: options[:header_template] || '',
|
55
|
-
footerTemplate: options[:footer_template] || ''
|
56
|
-
}
|
57
|
-
result = Base64.decode64 response['data']
|
58
|
-
ensure
|
59
|
-
if options[:client].nil?
|
60
|
-
Process.kill 'KILL', pid
|
61
|
-
Process.wait pid
|
62
|
-
else
|
63
|
-
chrome.send_cmd 'Page.close'
|
64
|
-
end
|
65
|
-
|
66
|
-
htmlfile.close
|
67
|
-
htmlfile.unlink
|
68
|
-
end
|
69
|
-
|
70
|
-
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
|
71
21
|
end
|
72
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,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postdoc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frank Groeneveld
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chrome_remote
|
@@ -44,6 +44,20 @@ dependencies:
|
|
44
44
|
- - "<"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '6.1'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: mocha
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
47
61
|
description: Description of Postdoc.
|
48
62
|
email:
|
49
63
|
- frank@ivaldi.nl
|
@@ -54,6 +68,9 @@ 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
|