postdoc 0.3.7 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/postdoc/batch.rb +34 -0
- data/lib/postdoc/chrome_process.rb +1 -0
- data/lib/postdoc/client.rb +21 -16
- data/lib/postdoc/html_document.rb +1 -1
- data/lib/postdoc/job.rb +18 -0
- data/lib/postdoc/print_settings.rb +48 -0
- data/lib/postdoc.rb +34 -2
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2562e31f953afcf27300148e00217e1599424efb39bbb1242e0d29f264260c28
|
4
|
+
data.tar.gz: 6b9271a82c70cbe57efd534b1b2d80b8f6be5e952ebdfa167f3cfabb944bbd77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f71a3cf459f62e022b0f13853084ca2796fb642a6a9c681173b341f961dd257486a141459ec42fe7b3f4caf2c6c6c86aafec2dc7f14e23b7c8c775c8c356ebd
|
7
|
+
data.tar.gz: dec210aedf9353c0e96bf25e5b503088508fbe7a79661b75702ce5c372039f762be6d79f417b55313a83e5857ebbb39bd310b5a4304157b4dcbc072ec5853e01
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'postdoc/print_settings'
|
2
|
+
require 'postdoc/job'
|
3
|
+
|
4
|
+
module Postdoc
|
5
|
+
# This module represents a batch. At times we want to render multiple PDF's in
|
6
|
+
# series and batching saves a lot of overhead by not restarting chrome over
|
7
|
+
# and over. Practical applications are "stitching" PDF's together to get
|
8
|
+
# around markup constraints in chrome or rendering a batch of reports all at
|
9
|
+
# once.
|
10
|
+
class Batch
|
11
|
+
def initialize(jobs: [])
|
12
|
+
@jobs = jobs
|
13
|
+
end
|
14
|
+
|
15
|
+
# Creates a job from a document and add to the jobs.
|
16
|
+
def add_document(document, settings: {})
|
17
|
+
settings = PrintSettings.new(**settings)
|
18
|
+
@jobs << Job.new(document, settings: settings)
|
19
|
+
end
|
20
|
+
|
21
|
+
def add_job(job)
|
22
|
+
@jobs << job
|
23
|
+
end
|
24
|
+
|
25
|
+
# returns the output of the config. Requires a {Postdoc::Client Client}
|
26
|
+
def result(client)
|
27
|
+
@jobs.map { |job| job.result(client) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def cleanup
|
31
|
+
@jobs.each(&:cleanup)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/postdoc/client.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require 'chrome_remote'
|
4
4
|
|
5
5
|
module Postdoc
|
6
|
+
|
6
7
|
class Client
|
7
8
|
attr_accessor :client
|
8
9
|
|
@@ -12,28 +13,32 @@ module Postdoc
|
|
12
13
|
raise 'ChromeClient couldn\'t launch' if @client.blank?
|
13
14
|
end
|
14
15
|
|
16
|
+
# We should move away from passing options like this and collect them in
|
17
|
+
# the prinbt settings.
|
15
18
|
def print_pdf_from_html(file_path,
|
16
|
-
|
17
|
-
footer_template: false,
|
18
|
-
**options)
|
19
|
+
settings: PrintSettings.new)
|
19
20
|
|
20
21
|
client.send_cmd 'Page.enable'
|
21
22
|
client.send_cmd 'Page.navigate', url: "file://#{file_path}"
|
22
23
|
client.wait_for 'Page.loadEventFired'
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
}
|
35
|
-
|
36
|
-
|
25
|
+
# prevent race condition
|
26
|
+
sleep 0.1 if settings.slow_pc
|
27
|
+
|
28
|
+
response = client.send_cmd 'Page.printToPDF', settings.to_cmd
|
29
|
+
|
30
|
+
Base64.decode64 response['data']
|
31
|
+
end
|
32
|
+
|
33
|
+
def print_document(file_path, settings: PrintSettings.new)
|
34
|
+
client.send_cmd 'Page.enable'
|
35
|
+
client.send_cmd 'Page.navigate', url: "file://#{file_path}"
|
36
|
+
client.wait_for 'Page.loadEventFired'
|
37
|
+
|
38
|
+
# prevent race condition
|
39
|
+
sleep 0.1 if settings.slow_pc
|
40
|
+
|
41
|
+
response = client.send_cmd 'Page.printToPDF', settings.to_cmd
|
37
42
|
|
38
43
|
Base64.decode64 response['data']
|
39
44
|
end
|
data/lib/postdoc/job.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Postdoc
|
2
|
+
# A job is a single "print" in a batch and binds a document to a setting.
|
3
|
+
class Job
|
4
|
+
def initialize(document, settings: PrintSettings.new)
|
5
|
+
@settings = settings
|
6
|
+
@document = HTMLDocument.new(document)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Return the result of the job.
|
10
|
+
def result(client)
|
11
|
+
client.print_document(@document.path, settings: @settings)
|
12
|
+
end
|
13
|
+
|
14
|
+
def cleanup
|
15
|
+
@document.cleanup
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Postdoc
|
2
|
+
# Different prints require different settings. This class is used instead of
|
3
|
+
# passing options arguments throughout all of the code.
|
4
|
+
class PrintSettings
|
5
|
+
|
6
|
+
attr_accessor :slow_pc
|
7
|
+
|
8
|
+
def initialize(
|
9
|
+
header_template: '',
|
10
|
+
footer_template: '',
|
11
|
+
landscape: false,
|
12
|
+
print_background: true,
|
13
|
+
margin_top: 1,
|
14
|
+
margin_bottom: 1,
|
15
|
+
margin_left: 1,
|
16
|
+
margin_right: 1,
|
17
|
+
slow_pc: false
|
18
|
+
)
|
19
|
+
@header_template = header_template
|
20
|
+
@footer_template = footer_template
|
21
|
+
@landscape = landscape
|
22
|
+
@print_background = print_background
|
23
|
+
@margin_top = margin_top
|
24
|
+
@margin_bottom = margin_bottom
|
25
|
+
@margin_left = margin_left
|
26
|
+
@margin_right = margin_right
|
27
|
+
@slow_pc = slow_pc
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_cmd
|
31
|
+
{ landscape: @landscape,
|
32
|
+
printBackground: true,
|
33
|
+
marginTop: @margin_top,
|
34
|
+
marginBottom: @margin_bottom,
|
35
|
+
marginLeft: @margin_left,
|
36
|
+
marginRight: @margin_right,
|
37
|
+
displayHeaderFooter: display_header_and_footer?,
|
38
|
+
headerTemplate: @header_template,
|
39
|
+
footerTemplate: @footer_template }
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def display_header_and_footer?
|
45
|
+
@header_template.present? || @footer_template.present?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/postdoc.rb
CHANGED
@@ -4,6 +4,8 @@ require 'action_controller'
|
|
4
4
|
require 'postdoc/chrome_process'
|
5
5
|
require 'postdoc/client'
|
6
6
|
require 'postdoc/html_document'
|
7
|
+
require 'postdoc/print_settings'
|
8
|
+
require 'postdoc/batch'
|
7
9
|
|
8
10
|
module Postdoc
|
9
11
|
ActionController::Renderers.add :pdf do |_filename, options|
|
@@ -11,12 +13,42 @@ module Postdoc
|
|
11
13
|
end
|
12
14
|
|
13
15
|
def self.render_from_string(content, **options)
|
16
|
+
print_settings = PrintSettings.new(**options)
|
14
17
|
server = ChromeProcess.new(**options)
|
15
|
-
html_file = HTMLDocument.new(content
|
18
|
+
html_file = HTMLDocument.new(content)
|
16
19
|
server.client
|
17
|
-
.print_pdf_from_html(html_file.path,
|
20
|
+
.print_pdf_from_html(html_file.path,
|
21
|
+
settings: print_settings)
|
18
22
|
ensure
|
23
|
+
if server.client.client.present?
|
24
|
+
server.client.client.send_cmd 'Browser.close'
|
25
|
+
end
|
19
26
|
server.kill
|
20
27
|
html_file.cleanup
|
21
28
|
end
|
29
|
+
|
30
|
+
# A clean and easy way to render a batch:
|
31
|
+
# ```
|
32
|
+
# html_string_1
|
33
|
+
# html_string_2
|
34
|
+
# with_footer = Postdoc::PrintSettings.new(footer_template: footer_template)
|
35
|
+
|
36
|
+
# result = Postdoc.batch do |batch|
|
37
|
+
# batch.add_document doc1
|
38
|
+
# batch.add_document doc2, settings: with_footer
|
39
|
+
# end
|
40
|
+
# ```
|
41
|
+
def self.batch(batch: Batch.new)
|
42
|
+
yield(batch)
|
43
|
+
begin
|
44
|
+
server = ChromeProcess.new
|
45
|
+
batch.result(server.client)
|
46
|
+
ensure
|
47
|
+
if server.client.client.present?
|
48
|
+
server.client.client.send_cmd 'Browser.close'
|
49
|
+
end
|
50
|
+
server.kill
|
51
|
+
batch.cleanup
|
52
|
+
end
|
53
|
+
end
|
22
54
|
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.
|
4
|
+
version: 0.4.1
|
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-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chrome_remote
|
@@ -33,7 +33,7 @@ dependencies:
|
|
33
33
|
version: 4.0.0
|
34
34
|
- - "<"
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: '
|
36
|
+
version: '7.0'
|
37
37
|
type: :runtime
|
38
38
|
prerelease: false
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 4.0.0
|
44
44
|
- - "<"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
46
|
+
version: '7.0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: mocha
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -68,9 +68,12 @@ files:
|
|
68
68
|
- LICENSE
|
69
69
|
- Rakefile
|
70
70
|
- lib/postdoc.rb
|
71
|
+
- lib/postdoc/batch.rb
|
71
72
|
- lib/postdoc/chrome_process.rb
|
72
73
|
- lib/postdoc/client.rb
|
73
74
|
- lib/postdoc/html_document.rb
|
75
|
+
- lib/postdoc/job.rb
|
76
|
+
- lib/postdoc/print_settings.rb
|
74
77
|
homepage: https://github.com/ivaldi/postdoc
|
75
78
|
licenses:
|
76
79
|
- BSD-2-Clause
|