postdoc 0.3.5 → 0.3.9
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 +34 -2
- data/lib/postdoc/batch.rb +34 -0
- data/lib/postdoc/chrome_process.rb +4 -1
- data/lib/postdoc/client.rb +15 -15
- data/lib/postdoc/html_document.rb +1 -1
- data/lib/postdoc/job.rb +18 -0
- data/lib/postdoc/print_settings.rb +43 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8805f0d630a0bb6aacb1370839faec84c781d556c0f2f21a62f82cfb0127513
|
4
|
+
data.tar.gz: b0767fa2999a50cab5aa58204e4c43c16e38c0ee941c1389660c35cf6062fb15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dbb3519424aef59b75c1951599be4a91e1b9fc722c5ef75e0dd9197cd58f8f455f0db5cc0e0bcfe9efb076c8797fc8fab48d6d745cd91a5b31bf7ea97c26769
|
7
|
+
data.tar.gz: 8c33f0a51a2127c1289095c6e43031e7fa988b410da1c94aa14d7caa1b33185398553000ef5e2ca4836a7cdcc77be5339aa9a71f2f70991f36228050df190870
|
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
|
@@ -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
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Postdoc
|
4
|
+
# Spins up a Chrome process.
|
4
5
|
class ChromeProcess
|
5
6
|
attr_reader :pid, :port
|
6
7
|
|
@@ -17,8 +18,10 @@ module Postdoc
|
|
17
18
|
end
|
18
19
|
|
19
20
|
def kill
|
20
|
-
Process.kill '
|
21
|
+
Process.kill 'INT', pid
|
21
22
|
Process.wait pid
|
23
|
+
rescue
|
24
|
+
true
|
22
25
|
end
|
23
26
|
|
24
27
|
def client
|
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,26 +13,25 @@ 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
|
-
response = client.send_cmd 'Page.printToPDF',
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
}
|
25
|
+
response = client.send_cmd 'Page.printToPDF', settings.to_cmd
|
26
|
+
|
27
|
+
Base64.decode64 response['data']
|
28
|
+
end
|
29
|
+
|
30
|
+
def print_document(file_path, settings: PrintSettings.new)
|
31
|
+
client.send_cmd 'Page.enable'
|
32
|
+
client.send_cmd 'Page.navigate', url: "file://#{file_path}"
|
33
|
+
client.wait_for 'Page.loadEventFired'
|
34
|
+
response = client.send_cmd 'Page.printToPDF', settings.to_cmd
|
35
35
|
|
36
36
|
Base64.decode64 response['data']
|
37
37
|
end
|
@@ -41,7 +41,7 @@ module Postdoc
|
|
41
41
|
def setup_connection_or_wait
|
42
42
|
@client = ChromeRemote.client(port: @port)
|
43
43
|
true
|
44
|
-
rescue
|
44
|
+
rescue
|
45
45
|
sleep(0.1)
|
46
46
|
false
|
47
47
|
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,43 @@
|
|
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
|
+
def initialize(
|
6
|
+
header_template: '',
|
7
|
+
footer_template: '',
|
8
|
+
landscape: false,
|
9
|
+
print_background: true,
|
10
|
+
margin_top: 1,
|
11
|
+
margin_bottom: 1,
|
12
|
+
margin_left: 1,
|
13
|
+
margin_right: 1
|
14
|
+
)
|
15
|
+
@header_template = header_template
|
16
|
+
@footer_template = footer_template
|
17
|
+
@landscape = landscape
|
18
|
+
@print_background = print_background
|
19
|
+
@margin_top = margin_top
|
20
|
+
@margin_bottom = margin_bottom
|
21
|
+
@margin_left = margin_left
|
22
|
+
@margin_right = margin_right
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_cmd
|
26
|
+
{ landscape: @landscape,
|
27
|
+
printBackground: true,
|
28
|
+
marginTop: @margin_top,
|
29
|
+
marginBottom: @margin_bottom,
|
30
|
+
marginLeft: @margin_left,
|
31
|
+
marginRight: @margin_right,
|
32
|
+
displayHeaderFooter: display_header_and_footer?,
|
33
|
+
headerTemplate: @header_template,
|
34
|
+
footerTemplate: @footer_template }
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def display_header_and_footer?
|
40
|
+
@header_template.present? || @footer_template.present?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
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.9
|
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-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chrome_remote
|
@@ -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
|