bidi2pdf-rails 0.0.1.alpha.1

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.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.idea/.gitignore +8 -0
  3. data/.idea/bidi2pdf-rails.iml +449 -0
  4. data/.idea/inspectionProfiles/profiles_settings.xml +5 -0
  5. data/.idea/misc.xml +4 -0
  6. data/.idea/modules.xml +8 -0
  7. data/.idea/vcs.xml +6 -0
  8. data/.rspec +3 -0
  9. data/.rubocop.yml +93 -0
  10. data/.ruby-gemset +1 -0
  11. data/.ruby-version +1 -0
  12. data/MIT-LICENSE +20 -0
  13. data/README.md +102 -0
  14. data/Rakefile +11 -0
  15. data/lib/bidi2pdf-rails.rb +4 -0
  16. data/lib/bidi2pdf_rails/browser_console_log_subscriber.rb +24 -0
  17. data/lib/bidi2pdf_rails/chromedriver_manager_singleton.rb +69 -0
  18. data/lib/bidi2pdf_rails/main_log_subscriber.rb +33 -0
  19. data/lib/bidi2pdf_rails/network_log_subscriber.rb +20 -0
  20. data/lib/bidi2pdf_rails/railtie.rb +44 -0
  21. data/lib/bidi2pdf_rails/services/html_renderer.rb +33 -0
  22. data/lib/bidi2pdf_rails/services/html_to_pdf_converter.rb +28 -0
  23. data/lib/bidi2pdf_rails/services/pdf_browser_session.rb +39 -0
  24. data/lib/bidi2pdf_rails/services/pdf_renderer.rb +81 -0
  25. data/lib/bidi2pdf_rails/services/url_to_pdf_converter.rb +82 -0
  26. data/lib/bidi2pdf_rails/version.rb +3 -0
  27. data/lib/bidi2pdf_rails.rb +141 -0
  28. data/lib/generators/bidi2pdf_rails/USAGE +8 -0
  29. data/lib/generators/bidi2pdf_rails/initializer_generator.rb +74 -0
  30. data/lib/generators/bidi2pdf_rails/templates/bidi2pdf_rails.rb.tt +121 -0
  31. data/spec/bidi2pdf_rails/bidi2pdf_rails_spec.rb +9 -0
  32. data/spec/dummy/Rakefile +6 -0
  33. data/spec/dummy/app/assets/images/img.jpg +0 -0
  34. data/spec/dummy/app/assets/javascripts/paged.polyfill-4.3.js +33251 -0
  35. data/spec/dummy/app/assets/stylesheets/application.css +1 -0
  36. data/spec/dummy/app/assets/stylesheets/pdf.css +121 -0
  37. data/spec/dummy/app/assets/stylesheets/reset.css +449 -0
  38. data/spec/dummy/app/assets/stylesheets/screen.css +281 -0
  39. data/spec/dummy/app/controllers/application_controller.rb +4 -0
  40. data/spec/dummy/app/controllers/reports_controller.rb +12 -0
  41. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  42. data/spec/dummy/app/helpers/reports_helper.rb +2 -0
  43. data/spec/dummy/app/views/layouts/application.html.erb +67 -0
  44. data/spec/dummy/app/views/layouts/pdf.html.erb +67 -0
  45. data/spec/dummy/app/views/pwa/manifest.json.erb +22 -0
  46. data/spec/dummy/app/views/pwa/service-worker.js +26 -0
  47. data/spec/dummy/app/views/reports/show.html.erb +153 -0
  48. data/spec/dummy/bin/rails +4 -0
  49. data/spec/dummy/bin/rake +4 -0
  50. data/spec/dummy/bin/setup +29 -0
  51. data/spec/dummy/config/application.rb +40 -0
  52. data/spec/dummy/config/boot.rb +5 -0
  53. data/spec/dummy/config/environment.rb +5 -0
  54. data/spec/dummy/config/environments/development.rb +57 -0
  55. data/spec/dummy/config/environments/production.rb +73 -0
  56. data/spec/dummy/config/environments/test.rb +51 -0
  57. data/spec/dummy/config/initializers/bidi2pdf_rails.rb +68 -0
  58. data/spec/dummy/config/initializers/content_security_policy.rb +25 -0
  59. data/spec/dummy/config/initializers/cors.rb +9 -0
  60. data/spec/dummy/config/initializers/filter_parameter_logging.rb +8 -0
  61. data/spec/dummy/config/initializers/inflections.rb +16 -0
  62. data/spec/dummy/config/initializers/permissions_policy.rb +13 -0
  63. data/spec/dummy/config/locales/en.yml +31 -0
  64. data/spec/dummy/config/puma.rb +34 -0
  65. data/spec/dummy/config/routes.rb +18 -0
  66. data/spec/dummy/config.ru +6 -0
  67. data/spec/dummy/log/development.log +12781 -0
  68. data/spec/dummy/public/404.html +67 -0
  69. data/spec/dummy/public/406-unsupported-browser.html +66 -0
  70. data/spec/dummy/public/422.html +67 -0
  71. data/spec/dummy/public/500.html +66 -0
  72. data/spec/dummy/public/icon.png +0 -0
  73. data/spec/dummy/public/icon.svg +3 -0
  74. data/spec/dummy/spec/helpers/reports_helper_spec.rb +15 -0
  75. data/spec/dummy/spec/requests/reports_spec.rb +10 -0
  76. data/spec/dummy/spec/views/reports/show.html.erb_spec.rb +5 -0
  77. data/spec/dummy/storage/test.sqlite3 +0 -0
  78. data/spec/dummy/tmp/README.md +69 -0
  79. data/spec/dummy/tmp/local_secret.txt +1 -0
  80. data/spec/dummy/tmp/pids/server.pid +1 -0
  81. data/spec/dummy/tmp/restart.txt +0 -0
  82. data/spec/generator/bidie2pdf_rails_initializer_generator_spec.rb +5 -0
  83. data/spec/generator/initializer_generator_spec.rb +5 -0
  84. data/spec/rails_helper.rb +60 -0
  85. data/spec/requests/reports_spec.rb +17 -0
  86. data/spec/spec_helper.rb +94 -0
  87. data/tasks/bidi2pdf_rails_tasks.rake +4 -0
  88. metadata +310 -0
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bidi2pdf"
4
+ require "bidi2pdf/bidi/commands/print_parameters_validator"
5
+ require_relative "bidi2pdf_rails/version"
6
+ require_relative "bidi2pdf_rails/railtie"
7
+ require_relative "bidi2pdf_rails/chromedriver_manager_singleton"
8
+ require_relative "bidi2pdf_rails/main_log_subscriber"
9
+ require_relative "bidi2pdf_rails/browser_console_log_subscriber"
10
+ require_relative "bidi2pdf_rails/network_log_subscriber"
11
+
12
+ module Bidi2pdfRails
13
+ class << self
14
+ attr_accessor :logger, :notification_service, :verbosity, :default_timeout, :remote_browser_url, :cookies,
15
+ :headers, :auth, :headless, :chromedriver_port, :chrome_session_args,
16
+ :proxy_addr, :proxy_port, :proxy_user, :proxy_pass, :install_dir,
17
+ :pdf_page_width, :pdf_page_height,
18
+ :pdf_margin_top, :pdf_margin_bottom, :pdf_margin_left, :pdf_margin_right,
19
+ :pdf_scale, :pdf_shrink_to_fit, :pdf_print_background, :pdf_page_format, :pdf_orientation, :verbosity,
20
+ :wait_for_network_idle, :wait_for_page_loaded, :wait_for_page_check_script
21
+
22
+ # Allow configuration through a block
23
+ def configure
24
+ yield self if block_given?
25
+
26
+ validate_print_options! print_options
27
+ apply_bidi2pdf_config
28
+ end
29
+
30
+ def validate_print_options!(opts)
31
+ validator = Bidi2pdf::Bidi::Commands::PrintParametersValidator.new(opts)
32
+
33
+ validator.validate!
34
+ end
35
+
36
+ def print_options(overrides = {})
37
+ overrides ||= {}
38
+ opts = {}
39
+
40
+ opts[:background] = pdf_print_background unless pdf_print_background.nil?
41
+ opts[:orientation] = pdf_orientation unless pdf_orientation.nil?
42
+ opts[:scale] = pdf_scale unless pdf_scale.nil?
43
+ opts[:shrinkToFit] = pdf_shrink_to_fit unless pdf_shrink_to_fit.nil?
44
+
45
+ # Margins
46
+ margins = {
47
+ top: @pdf_margin_top,
48
+ bottom: @pdf_margin_bottom,
49
+ left: @pdf_margin_left,
50
+ right: @pdf_margin_right
51
+ }.compact
52
+
53
+ opts[:margin] = margins unless margins.empty?
54
+
55
+ # Page size
56
+ page = {
57
+ width: @pdf_page_width,
58
+ height: @pdf_page_height,
59
+ format: @pdf_page_format
60
+ }.compact
61
+
62
+ page = overrides[:page].compact if overrides[:page]&.compact.present?
63
+
64
+ opts[:page] = page unless page.empty?
65
+
66
+ opts.deep_merge(overrides)
67
+ end
68
+
69
+ def apply_bidi2pdf_config
70
+ # Allow configuration through a block
71
+ Bidi2pdf.configure do |config|
72
+ config.notification_service = notification_service
73
+
74
+ config.logger = logger.tagged("bidi2pdf")
75
+ config.default_timeout = default_timeout
76
+
77
+ config.network_events_logger = Logger.new(nil)
78
+ config.browser_console_logger = Logger.new(nil) # Disable browser console logging
79
+
80
+ config.enable_default_logging_subscriber = false
81
+ end
82
+
83
+ Chromedriver::Binary.configure do |config|
84
+ config.logger = logger
85
+
86
+ config.proxy_addr = proxy_addr
87
+ config.proxy_port = proxy_port
88
+ config.proxy_user = proxy_user
89
+ config.proxy_pass = proxy_pass
90
+
91
+ config.install_dir = install_dir
92
+ end
93
+ end
94
+
95
+ def use_local_chromedriver?
96
+ !use_remote_browser?
97
+ end
98
+
99
+ def use_remote_browser?
100
+ remote_browser_url.present?
101
+ end
102
+
103
+ def reset_to_defaults!
104
+ @logger = nil
105
+ @default_timeout = 10
106
+ @verbosity = 0
107
+ @remote_browser_url = nil
108
+ @cookies = {}
109
+ @headers = {}
110
+ @auth = nil
111
+ @headless = !Rails.env.development?
112
+ @chromedriver_port = 0
113
+ @chrome_session_args = Bidi2pdf::Bidi::Session::DEFAULT_CHROME_ARGS
114
+
115
+ @proxy_addr = nil
116
+ @proxy_port = nil
117
+ @proxy_user = nil
118
+ @proxy_pass = nil
119
+ @install_dir = nil
120
+
121
+ @wait_for_network_idle = true
122
+ @wait_for_page_loaded = false
123
+ @wait_for_page_check_script = nil
124
+
125
+ @pdf_print_background = true
126
+ @pdf_orientation = "portrait"
127
+ @pdf_page_width = Bidi2pdf::PAPER_FORMATS_CM[:a4][:width]
128
+ @pdf_page_height = Bidi2pdf::PAPER_FORMATS_CM[:a4][:height]
129
+ @pdf_page_format = nil
130
+
131
+ @pdf_shrink_to_fit = nil
132
+ @pdf_scale = 1.0
133
+ end
134
+
135
+ def logger
136
+ @logger ||= Rails.logger # lazy load the logger, after it exists
137
+ end
138
+ end
139
+
140
+ self.reset_to_defaults!
141
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ bin/rails generate initializer Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,74 @@
1
+ module Bidi2pdfRails
2
+ class InitializerGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("templates", __dir__)
4
+ desc "Generates a config/initializers/bidi2pdf.rb config file"
5
+
6
+ class_option :force, type: :boolean, default: false, desc: "Overwrite existing file"
7
+ class_option :quiet, type: :boolean, default: false, desc: "Skip prompts"
8
+
9
+ def ask_questions
10
+ return if options[:quiet]
11
+
12
+ @log_network_events = yes?("Log network traffic? (y/n)", :green)
13
+ @log_browser_console = yes?("Log browser console output? (y/n)", :green)
14
+ @headless = yes?("Run Chrome in headless mode? (y/n)", :green)
15
+ @proxy = yes?("Use a proxy server? (y/n)", :green)
16
+
17
+ if @proxy
18
+ @proxy_addr = ask("Proxy address (e.g., 127.0.0.1):", :yellow)
19
+ @proxy_port = ask("Proxy port (e.g., 8080):", :yellow)
20
+ end
21
+
22
+ @configure_pdf = yes?("Configure custom PDF settings? (y/n)", :green)
23
+ if @configure_pdf
24
+ @pdf_orientation = ask("PDF orientation (portrait/landscape):", :yellow)
25
+ @pdf_margins = yes?("Configure PDF margins? (y/n)", :green)
26
+ if @pdf_margins
27
+ @pdf_margin_top = ask("PDF margin top (mm):", :yellow)
28
+ @pdf_margin_bottom = ask("PDF margin bottom (mm):", :yellow)
29
+ @pdf_margin_left = ask("PDF margin left (mm):", :yellow)
30
+ @pdf_margin_right = ask("PDF margin right (mm):", :yellow)
31
+ end
32
+ @pdf_page = yes?("Configure PDF page size? (y/n)", :green)
33
+ if @pdf_page
34
+ @pdf_page_width = ask("PDF page width (mm):", :yellow)
35
+ @pdf_page_height = ask("PDF page height (mm):", :yellow)
36
+ end
37
+
38
+ @pdf_print_background = yes?("Print background graphics? (y/n)", :green)
39
+ @pdf_scale = ask("PDF scale (e.g., 1.0):", :yellow)
40
+ @shrink_to_fit = ask("Shrink to fit? (y/n)", :green)
41
+ end
42
+ end
43
+
44
+ def generate_config
45
+ @config_path = "config/initializers/bidi2pdf_rails.rb"
46
+
47
+ template "bidi2pdf_rails.rb.tt", @config_path
48
+ end
49
+
50
+ def inject_environment_config
51
+ env_file = "config/environments/development.rb"
52
+
53
+ if File.exist?(env_file)
54
+ if File.read(env_file).include?("config.x.bidi2pdf_rails")
55
+ say_status :skipped, "Bidi2PDF settings already present in #{env_file}", :yellow
56
+ else
57
+ content = <<~RUBY.gsub(/^(?=[\w#])/, " ")
58
+ # Custom Bidi2PDF settings, check config/initializers/bidi2pdf_rails.rb for hints
59
+ config.x.bidi2pdf_rails.headless = false
60
+ config.x.bidi2pdf_rails.verbosity = :high
61
+ # config.x.bidi2pdf_rails.log_browser_console = true
62
+ # config.x.bidi2pdf_rails.default_timeout = 60
63
+ RUBY
64
+
65
+ inject_into_file env_file, content, before: /^end\s*$/
66
+
67
+ say_status :modified, env_file, :green
68
+ end
69
+ else
70
+ say_status :error, "Could not find #{env_file}", :red
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bidi2pdfRails.configure do |config|
4
+ overrides = Rails.application.config.x.bidi2pdf_rails
5
+
6
+ config.notification_service = ActiveSupport::Notifications
7
+
8
+ config.logger = Rails.logger
9
+ config.verbosity = overrides.verbosity.nil? ? :none : overrides.verbosity
10
+ config.default_timeout = 10
11
+
12
+ # Chrome & BiDi
13
+ # config.remote_browser_url = nil
14
+ config.headless = overrides.headless.nil? ? <%= @headless ? "true" : "false" %> : overrides.headless
15
+ #
16
+ # alternative to remote_browser_url
17
+ #
18
+ # config.chromedriver_port = 0
19
+ # config.chrome_session_args = [
20
+ # "--disable-gpu",
21
+ # "--no-sandbox"
22
+ # ]
23
+
24
+ <% if @proxy %>
25
+ config.proxy_addr = "<%= @proxy_addr %>"
26
+ config.proxy_port = <%= @proxy_port %>
27
+ config.proxy_user = nil
28
+ config.proxy_pass = nil
29
+ <% else %>
30
+ # config.proxy_addr = nil
31
+ # config.proxy_port = nil
32
+ # config.proxy_user = nil
33
+ # config.proxy_pass = nil
34
+ <% end %>
35
+
36
+ # PDF settings (css media settings take precedence over these)
37
+ <% if @configure_pdf %>
38
+
39
+ config.pdf_orientation = "<%= @pdf_orientation || "portrait" %>"
40
+ <% if @pdf_margins %>
41
+ config.pdf_margin_top = <%= @pdf_margin_top || 2.5 %>
42
+ config.pdf_margin_bottom = <%= @pdf_margin_bottom || 2 %>
43
+ config.pdf_margin_left = <%= @pdf_margin_left || 2 %>
44
+ config.pdf_margin_right = <%= @pdf_margin_right || 2 %>
45
+ <% else %>
46
+ # config.pdf_margin_top = 2.5
47
+ # config.pdf_margin_bottom = 2
48
+ # config.pdf_margin_left = 2
49
+ # config.pdf_margin_right = 2
50
+ <% end %>
51
+
52
+ <% if @pdf_page %>
53
+ # Page settings
54
+ config.pdf_page_width = <%= @pdf_page_width || Bidi2pdf::PAPER_FORMATS_CM[:a4][:width] %>
55
+ config.pdf_page_height = <%= @pdf_page_height || Bidi2pdf::PAPER_FORMATS_CM[:a4][:height] %>
56
+ <% else %>
57
+ # Page settings
58
+ # config.pdf_page_format = "A4"
59
+ #
60
+ # or custom page size
61
+ #
62
+ # config.pdf_page_width = <%= Bidi2pdf::PAPER_FORMATS_CM[:a4][:width] %>
63
+ # config.pdf_page_height = <%= Bidi2pdf::PAPER_FORMATS_CM[:a4][:height] %>
64
+ <% end %>
65
+
66
+ config.pdf_print_background = <%= @pdf_print_background ? "true" : "false" %>
67
+ config.pdf_scale = <%= @pdf_scale || 1.0 %>
68
+ config.shrink_to_fit = <%= @shrink_to_fit ? "true" : "false" %>
69
+ <% else %>
70
+ # config.pdf_orientation = "portrait"
71
+ # config.pdf_margin_top = 2.5
72
+ # config.pdf_margin_bottom = 2
73
+ # config.pdf_margin_left = 2
74
+ # config.pdf_margin_right = 2
75
+ # config.pdf_print_background = true
76
+ # config.pdf_scale = 1.0
77
+ # config.shrink_to_fit = true
78
+
79
+ # Page settings
80
+ # config.pdf_page_format = "A4"
81
+ #
82
+ # or custom page size
83
+ #
84
+ # config.pdf_page_width = <%= Bidi2pdf::PAPER_FORMATS_CM[:a4][:width] %>
85
+ # config.pdf_page_height = <%= Bidi2pdf::PAPER_FORMATS_CM[:a4][:height] %>
86
+ <% end %>
87
+
88
+ # config.cookies = [
89
+ # { name: "session", value: "abc123", domain: "example.com" }
90
+ # ]
91
+
92
+ # config.headers = {
93
+ # "X-API-KEY" => "topsecret"
94
+ # }
95
+
96
+ # config.auth = {
97
+ # username: "admin",
98
+ # password: "secret"
99
+ # }
100
+
101
+ # chromedriver install dir
102
+ # config.install_dir = Rails.root.join("tmp", "bidi2pdf").to_s
103
+
104
+ # wait for the page to load settings
105
+ # config.wait_for_network_idle = true
106
+ # config.wait_for_page_loaded = false
107
+ # this script can be any valid JS script that returns a Promise
108
+ # it will wait for the Promise to resolve
109
+ # config.wait_for_page_check_script = nil
110
+ end
111
+
112
+ Rails.application.config.after_initialize do
113
+ Bidi2pdfRails::MainLogSubscriber.attach_to "bidi2pdf", inherit_all: true # needed for imported methods
114
+ Bidi2pdfRails::MainLogSubscriber.attach_to "bidi2pdf_rails", inherit_all: true # needed for imported methods
115
+
116
+ Bidi2pdfRails::BrowserConsoleLogSubscriber.attach_to "bidi2pdf"
117
+
118
+ Bidi2pdfRails::MainLogSubscriber.silence /network_event_.*\.bidi2pdf/
119
+
120
+ Bidi2pdfRails::NetworkLogSubscriber.attach_to "bidi2pdf"
121
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../rails_helper"
4
+
5
+ RSpec.describe Bidi2pdfRails do
6
+ it "has a version number" do
7
+ expect(Bidi2pdfRails::VERSION).not_to be_nil
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative "config/application"
5
+
6
+ Rails.application.load_tasks