postdoc 0.2.0 → 0.3.0
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 +42 -20
- metadata +18 -19
- data/lib/postdoc/postdoc_view_helper.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 529772412289f1fe24ffeedd9644f2f8799ee955c3dea07f256a33e23ca1bc51
|
4
|
+
data.tar.gz: 2b27cdf8389e6c096c1dc64f3005396542c7808e04449f79d81f1f62990b1f0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 948c6386bc64c6ee7d5d9423a905dfec09dbbc2a5ed80e8032817929f13e5b682ded267a4751aa3fe66cd5d38f83b66abf4ee0989f1b2a4bf2e3fb0bdc4ea9b4
|
7
|
+
data.tar.gz: ce0193c8374d7c6fb44f8196829531156b79b4684ace11d172224bc1bdcf87f089a82757344e00edad9d377d76e037d83215d482247ce30e0b115af496d9d75b
|
data/lib/postdoc.rb
CHANGED
@@ -1,45 +1,67 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
require 'chrome_remote'
|
3
4
|
|
4
5
|
module Postdoc
|
5
|
-
|
6
|
-
|
7
|
-
initializer 'postdoc.register' do |_app|
|
8
|
-
ActionView::Base.send :include, PostdocViewHelper
|
9
|
-
end
|
6
|
+
ActionController::Renderers.add :pdf do |_filename, options|
|
7
|
+
Postdoc.render_from_string render_to_string(options), options
|
10
8
|
end
|
11
9
|
|
12
|
-
|
10
|
+
def self.render_from_string(string, options)
|
13
11
|
htmlfile = Tempfile.new ['input', '.html']
|
14
12
|
|
15
|
-
htmlfile.write
|
13
|
+
htmlfile.write string
|
16
14
|
htmlfile.flush
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
16
|
+
if options[:client].nil?
|
17
|
+
# random port at 1025 or higher
|
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
|
25
34
|
|
26
35
|
begin
|
27
|
-
chrome = ChromeRemote.client
|
28
|
-
chrome.send_cmd 'Page.enable'
|
36
|
+
chrome = options[:client].nil? ? ChromeRemote.client(port: random_port) : options[:client]
|
29
37
|
|
38
|
+
chrome.send_cmd 'Page.enable'
|
30
39
|
chrome.send_cmd 'Page.navigate', url: "file://#{htmlfile.path}"
|
31
40
|
chrome.wait_for 'Page.loadEventFired'
|
32
41
|
|
42
|
+
if options[:header_template].present? || options[:footer_template].present?
|
43
|
+
displayHeaderFooter = true
|
44
|
+
else
|
45
|
+
displayHeaderFooter = false
|
46
|
+
end
|
47
|
+
|
33
48
|
response = chrome.send_cmd 'Page.printToPDF', {
|
34
49
|
landscape: options[:landscape] || false,
|
35
50
|
printBackground: true,
|
51
|
+
marginTop: options[:margin_top] || 1,
|
52
|
+
marginBottom: options[:margin_bottom] || 1,
|
53
|
+
displayHeaderFooter: displayHeaderFooter,
|
36
54
|
headerTemplate: options[:header_template] || '',
|
37
|
-
footerTemplate: options[:footer_template] || ''
|
55
|
+
footerTemplate: options[:footer_template] || ''
|
38
56
|
}
|
39
57
|
result = Base64.decode64 response['data']
|
40
58
|
ensure
|
41
|
-
|
42
|
-
|
59
|
+
if options[:client].nil?
|
60
|
+
Process.kill 'KILL', pid
|
61
|
+
Process.wait pid
|
62
|
+
else
|
63
|
+
chrome.send_cmd 'Page.close'
|
64
|
+
end
|
43
65
|
|
44
66
|
htmlfile.close
|
45
67
|
htmlfile.unlink
|
metadata
CHANGED
@@ -1,49 +1,49 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postdoc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
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:
|
11
|
+
date: 2020-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: chrome_remote
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '6'
|
19
|
+
version: 0.2.0
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
30
|
-
- - "<"
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '6'
|
26
|
+
version: 0.2.0
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
28
|
+
name: rails
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
36
30
|
requirements:
|
37
31
|
- - ">="
|
38
32
|
- !ruby/object:Gem::Version
|
39
|
-
version: 0.
|
33
|
+
version: 4.0.0
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '6.1'
|
40
37
|
type: :runtime
|
41
38
|
prerelease: false
|
42
39
|
version_requirements: !ruby/object:Gem::Requirement
|
43
40
|
requirements:
|
44
41
|
- - ">="
|
45
42
|
- !ruby/object:Gem::Version
|
46
|
-
version: 0.
|
43
|
+
version: 4.0.0
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '6.1'
|
47
47
|
description: Description of Postdoc.
|
48
48
|
email:
|
49
49
|
- frank@ivaldi.nl
|
@@ -54,12 +54,11 @@ files:
|
|
54
54
|
- LICENSE
|
55
55
|
- Rakefile
|
56
56
|
- lib/postdoc.rb
|
57
|
-
- lib/postdoc/postdoc_view_helper.rb
|
58
57
|
homepage: https://github.com/ivaldi/postdoc
|
59
58
|
licenses:
|
60
59
|
- BSD-2-Clause
|
61
60
|
metadata: {}
|
62
|
-
post_install_message:
|
61
|
+
post_install_message:
|
63
62
|
rdoc_options: []
|
64
63
|
require_paths:
|
65
64
|
- lib
|
@@ -74,8 +73,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
73
|
- !ruby/object:Gem::Version
|
75
74
|
version: '0'
|
76
75
|
requirements: []
|
77
|
-
rubygems_version: 3.
|
78
|
-
signing_key:
|
76
|
+
rubygems_version: 3.1.4
|
77
|
+
signing_key:
|
79
78
|
specification_version: 4
|
80
79
|
summary: Summary of Postdoc.
|
81
80
|
test_files: []
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module PostdocViewHelper
|
2
|
-
def postdoc_stylesheet_link_tag(*path)
|
3
|
-
content_tag :style, File.read(
|
4
|
-
Rails.root.join('app', 'assets', 'stylesheets', *path)
|
5
|
-
).html_safe,
|
6
|
-
type: 'text/css'
|
7
|
-
end
|
8
|
-
|
9
|
-
protected
|
10
|
-
|
11
|
-
# include an assets from the gem app/assets folder
|
12
|
-
def postdoc_gem_asset(*path)
|
13
|
-
File.join(File.dirname(__FILE__), '..', '..', 'app', 'assets', *path)
|
14
|
-
end
|
15
|
-
end
|