postdoc 0.3.0 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/postdoc.rb +12 -62
- data/lib/postdoc/chrome_process.rb +34 -0
- data/lib/postdoc/client.rb +49 -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: 74295695d3dbf550fa41c48f13ed409547fc0e30559d4baa215c5d8875ec291d
|
4
|
+
data.tar.gz: f4e09afaa99866596a382fc7d4dea72c1cbbfe6df972ca336da97949f2c6f637
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7cc297db02152057e2bfa61042430e29d67a0d10e4bd109e70791ea5bf065324d0cb25df9c6fd305f8671fbe9583b3c7470c8a843744ed76a972777ce7e331b4
|
7
|
+
data.tar.gz: 19395b879f1a9837e11986eba31f832818f8765f05f13861c09f00141bc3d2da41712845984fef87e476a2d3daf09347124e6e63a11456ae8e9f65ec1bd2432b
|
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,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 Exception
|
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,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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Frank Groeneveld
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-26 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
|