postdoc 0.3.6 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74295695d3dbf550fa41c48f13ed409547fc0e30559d4baa215c5d8875ec291d
4
- data.tar.gz: f4e09afaa99866596a382fc7d4dea72c1cbbfe6df972ca336da97949f2c6f637
3
+ metadata.gz: 99100709b8c080b91123f7196ad2da2fa499bdd35c63e300442191c585527104
4
+ data.tar.gz: 9958e7e9d1601f4c0e2c1141a8f733cdb29d121bcee3e68e0a65b5a4fb13ec37
5
5
  SHA512:
6
- metadata.gz: 7cc297db02152057e2bfa61042430e29d67a0d10e4bd109e70791ea5bf065324d0cb25df9c6fd305f8671fbe9583b3c7470c8a843744ed76a972777ce7e331b4
7
- data.tar.gz: 19395b879f1a9837e11986eba31f832818f8765f05f13861c09f00141bc3d2da41712845984fef87e476a2d3daf09347124e6e63a11456ae8e9f65ec1bd2432b
6
+ metadata.gz: beb118c9f143dfc9801770c0a34908a5f50640c1d11fa4d051a5a3d9f26c5573445342df95615dad76ff8ac8bcaf5ab357439535bea246b97f97227cfe7982c5
7
+ data.tar.gz: 2d17c30f6604bd94eeaea2c482bf6d7a7f228a3bfc50ae5853e8c783c9098bb2cc0326ab0bf4cd29a00b528841963745d03a1e3e1721f7ba6953228fcc53bd50
@@ -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 'KILL', pid
21
+ Process.kill 'INT', pid
21
22
  Process.wait pid
23
+ rescue
24
+ true
22
25
  end
23
26
 
24
27
  def client
@@ -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
- header_template: false,
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
- 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
- }
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 Exception
44
+ rescue
45
45
  sleep(0.1)
46
46
  false
47
47
  end
@@ -8,7 +8,7 @@ module Postdoc
8
8
 
9
9
  delegate :path, to: :file
10
10
 
11
- def initialize(content, path: nil, **_options)
11
+ def initialize(content, path: nil)
12
12
  path ||= Rails.root&.join('tmp') || '/tmp'
13
13
  @file = Tempfile.new ['input', '.html'], path
14
14
 
@@ -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
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, **options)
18
+ html_file = HTMLDocument.new(content)
16
19
  server.client
17
- .print_pdf_from_html(html_file.path, **options)
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.3.6
4
+ version: 0.4.0
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-03-26 00:00:00.000000000 Z
11
+ date: 2021-10-26 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: '6.1'
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: '6.1'
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