Sha256: 2fa0c5b2e54213e7c103b0349741871b625b6a1d14d120c6b48bd52cba8db08e

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'action_controller'
require 'postdoc/chrome_process'
require 'postdoc/client'
require 'postdoc/html_document'
require 'postdoc/print_settings'
require 'postdoc/batch'

module Postdoc
  ActionController::Renderers.add :pdf do |_filename, options|
    Postdoc.render_from_string render_to_string(options), &options
  end

  def self.render_from_string(content, **options)
    print_settings = PrintSettings.new(**options)
    server = ChromeProcess.new(**options)
    html_file = HTMLDocument.new(content)
    server.client
        .print_pdf_from_html(html_file.path,
            settings: print_settings)
  ensure
    if server.client.client.present?
      server.client.client.send_cmd 'Browser.close'
    end
    server.kill
    html_file.cleanup
  end

  # A clean and easy way to render a batch:
  # ```
  # html_string_1
  # html_string_2
  # with_footer = Postdoc::PrintSettings.new(footer_template: footer_template)

  # result = Postdoc.batch do |batch|
  #   batch.add_document doc1
  #   batch.add_document doc2, settings: with_footer
  # end
  # ```
  def self.batch(batch: Batch.new)
    yield(batch)
    begin
      server = ChromeProcess.new
      batch.result(server.client)
    ensure
      if server.client.client.present?
        server.client.client.send_cmd 'Browser.close'
      end
      server.kill
      batch.cleanup
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
postdoc-0.5.0 lib/postdoc.rb
postdoc-0.4.5 lib/postdoc.rb
postdoc-0.4.4 lib/postdoc.rb
postdoc-0.4.3 lib/postdoc.rb
postdoc-0.4.2 lib/postdoc.rb