postdoc 0.3.7 → 0.3.8

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: 4a8f25b2dcb78fb58ec199d021985eef4e9892c55cba5ad9b33b8a1aea14baba
4
- data.tar.gz: f2ef8f9083917b3efbd212cdbc7a25c7f574db1a1a963bacf0a194151f2f8fbe
3
+ metadata.gz: 54aee40fdabd27f1565648b9f80a992a1799214472a7639019ca4385f5d2bd38
4
+ data.tar.gz: 8d4870650fd4c27a6ebad35af0995a345c70b8edb77fef36c4a1a8d1cc41a82e
5
5
  SHA512:
6
- metadata.gz: 166ca4c618a7740e02edda59711e1b23a5555e2c4d479ac00b71d6d9dda9c613fdbbd125fe0053f75405881c21e7b31f9494ef5df997cd3afe32b1486b403997
7
- data.tar.gz: 963c118d472a7d244fc4841bae66151f211804e796a1a1d8be3f3d1dae25a9f5274122c867abe99cd565a3bfc457a4516b94ce8065866fb929918226331816ce
6
+ metadata.gz: 8edd792288b47e25ee4c1b76bc7ee80c5533aca499751f0d119acf1b978b0cbf80e7dc10cb2f4eb51db0d86241cb88440c78ff8a07e7604d0ddf67f688870281
7
+ data.tar.gz: 2f83ad865f53d224044d1a9dd6a1abe758bbfe778ec4fccd9cb59904a2653e565b26e1650c05e29eb0242233f01de130e96e6a36a6d478acb27dea825986ac7c
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,36 @@ 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
19
23
  server.kill
20
24
  html_file.cleanup
21
25
  end
26
+
27
+ # A clean and easy way to render a batch:
28
+ # ```
29
+ # html_string_1
30
+ # html_string_2
31
+ # with_footer = Postdoc::PrintSettings.new(footer_template: footer_template)
32
+
33
+ # result = Postdoc.batch do |batch|
34
+ # batch.add_document doc1
35
+ # batch.add_document doc2, settings: with_footer
36
+ # end
37
+ # ```
38
+ def self.batch(batch: Batch.new)
39
+ yield(batch)
40
+ begin
41
+ server = ChromeProcess.new
42
+ batch.result(server.client)
43
+ ensure
44
+ server.kill
45
+ batch.cleanup
46
+ end
47
+ end
22
48
  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
 
@@ -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,27 @@ 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
+ client.send_cmd 'Browser.close'
28
+
29
+ Base64.decode64 response['data']
30
+ end
31
+
32
+ def print_document(file_path, settings: PrintSettings.new)
33
+ client.send_cmd 'Page.enable'
34
+ client.send_cmd 'Page.navigate', url: "file://#{file_path}"
35
+ client.wait_for 'Page.loadEventFired'
36
+ response = client.send_cmd 'Page.printToPDF', settings.to_cmd
35
37
 
36
38
  client.send_cmd 'Browser.close'
37
39
 
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postdoc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank Groeneveld
@@ -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