bidi2pdf-rails 0.0.1.alpha.1 → 0.1.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.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.idea/bidi2pdf-rails.iml +16 -8
  3. data/.rubocop.yml +11 -0
  4. data/CHANGELOG.md +33 -0
  5. data/README.md +104 -41
  6. data/Rakefile +2 -0
  7. data/cliff.toml +126 -0
  8. data/lib/bidi2pdf_rails/browser_console_log_subscriber.rb +1 -1
  9. data/lib/bidi2pdf_rails/chromedriver_manager_singleton.rb +10 -10
  10. data/lib/bidi2pdf_rails/config.rb +133 -0
  11. data/lib/bidi2pdf_rails/configurable.rb +106 -0
  12. data/lib/bidi2pdf_rails/main_log_subscriber.rb +1 -1
  13. data/lib/bidi2pdf_rails/network_log_subscriber.rb +1 -1
  14. data/lib/bidi2pdf_rails/railtie.rb +2 -0
  15. data/lib/bidi2pdf_rails/services/pdf_renderer.rb +9 -8
  16. data/lib/bidi2pdf_rails/version.rb +1 -1
  17. data/lib/bidi2pdf_rails.rb +28 -99
  18. data/lib/generators/bidi2pdf_rails/USAGE +12 -4
  19. data/lib/generators/bidi2pdf_rails/initializer_generator.rb +136 -31
  20. data/lib/generators/bidi2pdf_rails/templates/bidi2pdf_rails.rb.tt +15 -101
  21. data/spec/acceptance/user_can_download_report_pdf_spec.rb +133 -0
  22. data/spec/acceptance/user_can_generate_pdf_from_protected_remote_url_spec.rb +173 -0
  23. data/spec/dummy/app/controllers/reports_controller.rb +35 -2
  24. data/spec/dummy/app/controllers/secure_controller.rb +52 -0
  25. data/spec/dummy/app/views/layouts/simple.html.erb +17 -0
  26. data/spec/dummy/app/views/secure/show.html.erb +10 -0
  27. data/spec/dummy/config/initializers/bidi2pdf_rails.rb +64 -54
  28. data/spec/dummy/config/routes.rb +9 -1
  29. data/spec/dummy/log/development.log +3850 -0
  30. data/spec/dummy/log/test.log +53046 -0
  31. data/spec/dummy/tmp/pids/server.pid +1 -1
  32. data/spec/integration/generators/bidi2pdf_rails/initializer_generator_spec.rb +64 -0
  33. data/spec/rails_helper.rb +8 -1
  34. data/spec/spec_helper.rb +47 -5
  35. data/spec/support/default_dirs_helper.rb +32 -0
  36. data/spec/support/pdf_helper.rb +12 -0
  37. data/spec/support/render_setting_helpers.rb +28 -0
  38. data/spec/support/request_server_bootstrap.rb +44 -0
  39. data/spec/{bidi2pdf_rails → unit/bidi2pdf_rails}/bidi2pdf_rails_spec.rb +1 -1
  40. data/spec/unit/bidi2pdf_rails/configurable/base_nested_config_spec.rb +133 -0
  41. data/tasks/changelog.rake +29 -0
  42. data/tasks/coverage.rake +23 -0
  43. metadata +69 -20
  44. data/spec/dummy/spec/helpers/reports_helper_spec.rb +0 -15
  45. data/spec/dummy/spec/requests/reports_spec.rb +0 -10
  46. data/spec/dummy/spec/views/reports/show.html.erb_spec.rb +0 -5
  47. data/spec/generator/bidie2pdf_rails_initializer_generator_spec.rb +0 -5
  48. data/spec/generator/initializer_generator_spec.rb +0 -5
  49. data/spec/requests/reports_spec.rb +0 -17
@@ -0,0 +1,52 @@
1
+ # app/controllers/secure_controller.rb
2
+ class SecureController < ApplicationController
3
+ # Basic Auth protection for the first action
4
+ http_basic_authenticate_with name: "admin", password: "secret", only: :basic_auth_endpoint
5
+
6
+ before_action :authenticate_with_api_key, only: :api_endpoint
7
+ before_action :authenticate_with_cookie, only: :cookie_endpoint
8
+
9
+ def basic_auth_endpoint
10
+ @auth_method = "HTTP Basic Authentication"
11
+ @auth_description = "This endpoint requires username and password authentication."
12
+ @auth_details = "Credentials: username: <code>admin</code>, password: <code>secret</code>"
13
+
14
+ render :show, layout: 'simple'
15
+ end
16
+
17
+ def api_endpoint
18
+ @auth_method = "API Key Authentication"
19
+ @auth_description = "This endpoint requires an API key in the header."
20
+ @auth_details = "Required header: <code>X-API-Key: your-secret-api-key</code>"
21
+
22
+ render :show, layout: 'simple'
23
+ end
24
+
25
+ def cookie_endpoint
26
+ @auth_method = "Cookie Authentication"
27
+ @auth_description = "This endpoint requires an authentication cookie."
28
+ @auth_details = "Required cookie: <code>auth_token</code> with value <code>valid-authentication-token</code>"
29
+
30
+ render :show, layout: 'simple'
31
+ end
32
+
33
+ private
34
+
35
+ def authenticate_with_api_key
36
+ api_key = request.headers["X-API-Key"]
37
+ valid_key = "your-secret-api-key"
38
+
39
+ if api_key.blank? || api_key != valid_key
40
+ render html: "<h1>Unauthorized</h1><p>Invalid or missing API key</p>".html_safe, status: :unauthorized
41
+ end
42
+ end
43
+
44
+ def authenticate_with_cookie
45
+ auth_token = cookies.signed[:auth_token]
46
+ valid_token = "valid-authentication-token"
47
+
48
+ if auth_token.blank? || auth_token != valid_token
49
+ render html: "<h1>Unauthorized</h1><p>Invalid or missing API key</p>".html_safe, status: :unauthorized
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title><%= content_for(:title) || "Dummy" %></title>
5
+ <%= csp_meta_tag %>
6
+ <meta name="viewport" content="width=device-width,initial-scale=1">
7
+ <meta name="apple-mobile-web-app-capable" content="yes">
8
+ <%= csrf_meta_tags %>
9
+ <%= csp_meta_tag %>
10
+
11
+ <%= yield :head %>
12
+ </head>
13
+
14
+ <body>
15
+ <%= yield %>
16
+ </body>
17
+ </html>
@@ -0,0 +1,10 @@
1
+ # app/views/secure/show.html.erb
2
+ <h1>Protected Resource</h1>
3
+ <p>This page is secured with: <strong><%= @auth_method %></strong></p>
4
+ <p><%= @auth_description %></p>
5
+
6
+ <div class="auth-details">
7
+ <%= @auth_details %>
8
+ </div>
9
+
10
+ <p>When authenticated, you can access this content.</p>
@@ -3,66 +3,76 @@
3
3
  Bidi2pdfRails.configure do |config|
4
4
  overrides = Rails.application.config.x.bidi2pdf_rails
5
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? ? false : overrides.headless
15
- # config.chromedriver_port = 0
16
- # config.chrome_session_args = [
17
- # "--disable-gpu",
18
- # "--no-sandbox"
19
- # ]
20
-
21
- # config.proxy_addr = nil
22
- # config.proxy_port = nil
23
- # config.proxy_user = nil
24
- # config.proxy_pass = nil
25
-
26
- # Viewport settings
27
- # config.viewport_width = 1920
28
- # config.viewport_height = 1080
29
-
30
- # PDF settings
31
- config.pdf_orientation = "portrait"
32
- config.pdf_margin_top = 2.5
33
- config.pdf_margin_bottom = 2.5
34
- config.pdf_margin_left = 3
35
- config.pdf_margin_right = 2
36
- config.pdf_print_background = true
37
- # config.pdf_scale = 1.0
38
-
39
- # config.cookies = [
40
- # { name: "session", value: "abc123", domain: "example.com" }
41
- # ]
42
-
43
- # config.headers = {
44
- # "X-API-KEY" => "topsecret"
45
- # }
46
-
47
- # config.auth = {
48
- # username: "admin",
49
- # password: "secret"
50
- # }
51
-
52
- # chromedriver install dir
53
- # config.install_dir = Rails.root.join("tmp", "bidi2pdf").to_s
54
-
55
- # wait for the page to load settings
56
- config.wait_for_network_idle = true
57
- config.wait_for_page_loaded = true
58
- # config.wait_for_page_check_script = nil
6
+ #
7
+ # General Options
8
+ #
9
+
10
+ # config.general_options.logger = Rails.logger # The logger
11
+
12
+ # Allowed values: "none", "low", "medium", "high"
13
+ config.general_options.verbosity = "medium" # How verbose to be
14
+ # config.general_options.headless = !Rails.env.development? # Run Chrome in headless mode
15
+ # config.general_options.wait_for_network_idle = true # Wait for network idle
16
+ config.general_options.wait_for_page_loaded = true # Wait for page loaded
17
+ # config.general_options.wait_for_page_check_script = nil # Wait for page check script
18
+ # config.general_options.notification_service = -> { ActiveSupport::Notifications } # Notification service
19
+ # config.general_options.default_timeout = 10 # Default timeout for various Bidi commands
20
+ # config.general_options.chrome_session_args = ["--disable-gpu", "--disable-popup-blocking", "--disable-hang-monitor"] # Chrome session arguments
21
+
22
+ #
23
+ # Chromedriver Settings (when chromedriver run within your app)
24
+ #
25
+
26
+ # config.chromedriver_settings.install_dir = nil # Chromedriver install directory
27
+ # config.chromedriver_settings.port = 0 # Chromedriver port
28
+
29
+ #
30
+ # Proxy Settings
31
+ #
32
+
33
+ # config.proxy_settings.addr = nil # Proxy address (e.g., 127.0.0.1)
34
+ # config.proxy_settings.port = nil # Proxy port (e.g., 8080)
35
+ # config.proxy_settings.user = nil # Proxy user
36
+ # config.proxy_settings.pass = -> { Rails.application.credentials.dig('bidi2pdf_rails', 'proxy_pass') } # Proxy password
37
+
38
+ #
39
+ # PDF Settings
40
+ #
41
+
42
+ # Allowed values: "portrait", "landscape"
43
+ # config.pdf_settings.orientation = "portrait" # PDF orientation (portrait/landscape)
44
+ # config.pdf_settings.margins = false # Configure PDF margins?
45
+ # config.pdf_settings.margin_top = 2.5 # PDF margin top (cm)
46
+ # config.pdf_settings.margin_bottom = 2 # PDF margin bottom (cm)
47
+ # config.pdf_settings.margin_left = 2 # PDF margin left (cm)
48
+ # config.pdf_settings.margin_right = 2 # PDF margin right (cm)
49
+
50
+ # Allowed values: "letter", "legal", "tabloid", "ledger", "a0", "a1", "a2", "a3", "a4", "a5", "a6"
51
+ # config.pdf_settings.page_format = nil # PDF page format (e.g., A4)
52
+ # config.pdf_settings.page_width = 21.0 # PDF page width (cm, not needed when format is specified)
53
+ # config.pdf_settings.page_height = 29.7 # PDF page height (cm, not needed when format is specified)
54
+ # config.pdf_settings.print_background = true # Print background graphics?
55
+ # config.pdf_settings.scale = 1.0 # PDF scale (e.g., 1.0)
56
+ # config.pdf_settings.shrink_to_fit = false # Shrink to fit?
57
+
58
+ #
59
+ # Remote URL Settings
60
+ #
61
+
62
+ # config.render_remote_settings.browser_url = nil # Remote browser URL (e.g. http://localhost:3001/sesion)
63
+ # config.render_remote_settings.basic_auth_user = nil # Basic auth user
64
+ # config.render_remote_settings.basic_auth_pass = -> { Rails.application.credentials.dig('bidi2pdf_rails', 'basic_auth_pass') } # Basic auth password
65
+ # config.render_remote_settings.headers = {"X-API-INFO" => "my info"} # Headers to be send when allong an url
66
+ # config.render_remote_settings.cookies = {"session_id" => "my session"} # Cookies to be send when alling an url
59
67
  end
60
68
 
61
69
  Rails.application.config.after_initialize do
62
70
  Bidi2pdfRails::MainLogSubscriber.attach_to "bidi2pdf", inherit_all: true # needed for imported methods
63
71
  Bidi2pdfRails::MainLogSubscriber.attach_to "bidi2pdf_rails", inherit_all: true # needed for imported methods
72
+
64
73
  Bidi2pdfRails::BrowserConsoleLogSubscriber.attach_to "bidi2pdf"
65
- Bidi2pdfRails::NetworkLogSubscriber.attach_to "bidi2pdf"
66
74
 
67
75
  Bidi2pdfRails::MainLogSubscriber.silence /network_event_.*\.bidi2pdf/
76
+
77
+ Bidi2pdfRails::NetworkLogSubscriber.attach_to "bidi2pdf"
68
78
  end
@@ -1,7 +1,15 @@
1
1
  Rails.application.routes.draw do
2
2
  get "reports/:id" => "reports#show", as: :report
3
3
 
4
- get "print-remote" => "reports#print_remote", as: :print_remote
4
+ get "convert-remote-url" => "reports#convert_remote_url", as: :print_remote
5
+ get "inline-html" => "reports#inline_html", as: :print_inline
6
+ get "convert-remote-url-basic-auth" => "reports#convert_remote_url_basic_auth", as: :print_remote_basic_auth
7
+ get "convert-remote-url-cookie" => "reports#convert_remote_url_cookie", as: :print_remote_url_cookie
8
+ get "convert-remote-url-header" => "reports#convert_remote_url_header", as: :print_remote_url_header
9
+
10
+ get 'basic-auth', to: 'secure#basic_auth_endpoint', as: :basic_auth_endpoint
11
+ get 'header-auth', to: 'secure#api_endpoint', as: :api_endpoint
12
+ get 'cookie-auth', to: 'secure#cookie_endpoint', as: :cookie_endpoint
5
13
 
6
14
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
7
15