bidi2pdf-rails 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d0aa5d0b7d83ed5324b0a8669672f08d7a3e7b22dc7f9d51994f59e4d503df5
4
- data.tar.gz: 928af8b8e8646b13c027cf2fb88ca216f53d37abc808c325733e18ee1011e1b8
3
+ metadata.gz: bdc355a4443f528f48ebd21191833657a5460ee9bbbcc0ad0b9d78d33a8893c0
4
+ data.tar.gz: 7a51735017e0f853b64eac9de4174e42f5bdc087e85ab37e2b82f205492e556b
5
5
  SHA512:
6
- metadata.gz: 01abea76bf14ed10a93abe22394621df080e864024ad828de8a7edcf36eed62b12f008e02db06cf6e0b2449408132467df90415918473eabb55bbeb1ca6c91f8
7
- data.tar.gz: 0b99cbcb01e48f2577e0da652b5a20f6720bbd11feb6e0dc06bea684e4d5c40e8d94b4c72c83862e498ade59050c1ef72bb28902f1f334c0f250821854391fd3
6
+ metadata.gz: eb4fae40d50dff4a550b50402511245f700a219d850db16d3d717da6243dc554d0aca260a0e2b37ba2f1f83c5235aac000f527fdd0834a989a19fc7d14f099e0
7
+ data.tar.gz: 6c1191ec15930b910f0b5558099996cdecf4b14bf613c0f9c979aee30b554a1a010ec9a98a27f405881b9f54595acf0ef60462534d7e3ad3bd3c3fde200a5581
@@ -28,80 +28,6 @@
28
28
  </content>
29
29
  <orderEntry type="jdk" jdkName="RVM: ruby-3.3.4 [bidi2pdf-rails]" jdkType="RUBY_SDK" />
30
30
  <orderEntry type="sourceFolder" forTests="false" />
31
- <orderEntry type="module-library">
32
- <library name="bidi2pdf (v0.1.6) [path][gem]" type="rubylib">
33
- <properties>
34
- <option name="additionalInfo">
35
- <AdditionalInfo>
36
- <option name="authors" value="N/A" />
37
- <option name="email" value="N/A" />
38
- <option name="homepage" value="N/A" />
39
- <option name="summary" value="N/A" />
40
- </AdditionalInfo>
41
- </option>
42
- <option name="fromPath" value="true" />
43
- <option name="git" value="true" />
44
- <option name="name" value="bidi2pdf" />
45
- <option name="requirePaths">
46
- <list>
47
- <option value="lib" />
48
- </list>
49
- </option>
50
- <option name="url" value="file://$MODULE_DIR$/../bidi2pdf" />
51
- <option name="version" value="0.1.6" />
52
- </properties>
53
- <CLASSES>
54
- <root url="file://$MODULE_DIR$/../bidi2pdf/bin" />
55
- <root url="file://$MODULE_DIR$/../bidi2pdf/exe" />
56
- <root url="file://$MODULE_DIR$/../bidi2pdf/lib" />
57
- <root url="file://$MODULE_DIR$/../bidi2pdf/pkg" />
58
- <root url="file://$MODULE_DIR$/../bidi2pdf/sig" />
59
- <root url="file://$MODULE_DIR$/../bidi2pdf/tmp" />
60
- <root url="file://$MODULE_DIR$/../bidi2pdf/.git" />
61
- <root url="file://$MODULE_DIR$/../bidi2pdf/spec" />
62
- <root url="file://$MODULE_DIR$/../bidi2pdf/.idea" />
63
- <root url="file://$MODULE_DIR$/../bidi2pdf/tasks" />
64
- <root url="file://$MODULE_DIR$/../bidi2pdf/docker" />
65
- <root url="file://$MODULE_DIR$/../bidi2pdf/output" />
66
- <root url="file://$MODULE_DIR$/../bidi2pdf/.bundle" />
67
- <root url="file://$MODULE_DIR$/../bidi2pdf/.github" />
68
- <root url="file://$MODULE_DIR$/../bidi2pdf/coverage" />
69
- </CLASSES>
70
- <JAVADOC />
71
- <SOURCES>
72
- <root url="file://$MODULE_DIR$/../bidi2pdf/bin" />
73
- <root url="file://$MODULE_DIR$/../bidi2pdf/exe" />
74
- <root url="file://$MODULE_DIR$/../bidi2pdf/lib" />
75
- <root url="file://$MODULE_DIR$/../bidi2pdf/pkg" />
76
- <root url="file://$MODULE_DIR$/../bidi2pdf/sig" />
77
- <root url="file://$MODULE_DIR$/../bidi2pdf/tmp" />
78
- <root url="file://$MODULE_DIR$/../bidi2pdf/.git" />
79
- <root url="file://$MODULE_DIR$/../bidi2pdf/spec" />
80
- <root url="file://$MODULE_DIR$/../bidi2pdf/.idea" />
81
- <root url="file://$MODULE_DIR$/../bidi2pdf/tasks" />
82
- <root url="file://$MODULE_DIR$/../bidi2pdf/docker" />
83
- <root url="file://$MODULE_DIR$/../bidi2pdf/output" />
84
- <root url="file://$MODULE_DIR$/../bidi2pdf/.bundle" />
85
- <root url="file://$MODULE_DIR$/../bidi2pdf/.github" />
86
- <root url="file://$MODULE_DIR$/../bidi2pdf/coverage" />
87
- </SOURCES>
88
- <excluded>
89
- <root url="file://$MODULE_DIR$/../bidi2pdf/bin" />
90
- <root url="file://$MODULE_DIR$/../bidi2pdf/exe" />
91
- <root url="file://$MODULE_DIR$/../bidi2pdf/pkg" />
92
- <root url="file://$MODULE_DIR$/../bidi2pdf/tmp" />
93
- <root url="file://$MODULE_DIR$/../bidi2pdf/.git" />
94
- <root url="file://$MODULE_DIR$/../bidi2pdf/spec" />
95
- <root url="file://$MODULE_DIR$/../bidi2pdf/.idea" />
96
- <root url="file://$MODULE_DIR$/../bidi2pdf/tasks" />
97
- <root url="file://$MODULE_DIR$/../bidi2pdf/docker" />
98
- <root url="file://$MODULE_DIR$/../bidi2pdf/output" />
99
- <root url="file://$MODULE_DIR$/../bidi2pdf/.bundle" />
100
- <root url="file://$MODULE_DIR$/../bidi2pdf/.github" />
101
- <root url="file://$MODULE_DIR$/../bidi2pdf/coverage" />
102
- </excluded>
103
- </library>
104
- </orderEntry>
105
31
  <orderEntry type="library" scope="PROVIDED" name="Ascii85 (v2.0.1, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
106
32
  <orderEntry type="library" scope="PROVIDED" name="actioncable (v7.2.2.1, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
107
33
  <orderEntry type="library" scope="PROVIDED" name="actionmailbox (v7.2.2.1, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
@@ -118,6 +44,7 @@
118
44
  <orderEntry type="library" scope="PROVIDED" name="ammeter (v1.1.7, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
119
45
  <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.3, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
120
46
  <orderEntry type="library" scope="PROVIDED" name="benchmark (v0.4.0, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
47
+ <orderEntry type="library" scope="PROVIDED" name="bidi2pdf (v0.1.8, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
121
48
  <orderEntry type="library" scope="PROVIDED" name="bigdecimal (v3.1.9, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
122
49
  <orderEntry type="library" scope="PROVIDED" name="builder (v3.3.0, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
123
50
  <orderEntry type="library" scope="PROVIDED" name="bundler (v2.5.11, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
@@ -180,7 +107,7 @@
180
107
  <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.13.2, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
181
108
  <orderEntry type="library" scope="PROVIDED" name="rspec-rails (v7.1.1, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
182
109
  <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.13.2, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
183
- <orderEntry type="library" scope="PROVIDED" name="rubocop (v1.75.2, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
110
+ <orderEntry type="library" scope="PROVIDED" name="rubocop (v1.75.3, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
184
111
  <orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.44.1, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
185
112
  <orderEntry type="library" scope="PROVIDED" name="rubocop-performance (v1.25.0, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
186
113
  <orderEntry type="library" scope="PROVIDED" name="rubocop-rails (v2.31.0, RVM: ruby-3.3.4 [bidi2pdf-rails]) [gem]" level="application" />
@@ -418,21 +345,32 @@
418
345
  <option name="myRootTask">
419
346
  <RakeTaskImpl id="rake">
420
347
  <subtasks>
421
- <RakeTaskImpl description="Build bidi2pdf-rails-0.0.1.alpha.1.gem into the pkg directory" fullCommand="build" id="build" />
348
+ <RakeTaskImpl description="Build bidi2pdf-rails-0.1.1.gem into the pkg directory" fullCommand="build" id="build" />
422
349
  <RakeTaskImpl id="build">
423
350
  <subtasks>
424
- <RakeTaskImpl description="Generate SHA512 checksum of bidi2pdf-rails-0.0.1.alpha.1.gem into the checksums directory" fullCommand="build:checksum" id="checksum" />
351
+ <RakeTaskImpl description="Generate SHA512 checksum of bidi2pdf-rails-0.1.1.gem into the checksums directory" fullCommand="build:checksum" id="checksum" />
352
+ </subtasks>
353
+ </RakeTaskImpl>
354
+ <RakeTaskImpl id="changelog">
355
+ <subtasks>
356
+ <RakeTaskImpl description="Generate unreleased section in CHANGELOG.md (requires git-cliff)" fullCommand="changelog:update_unreleased" id="update_unreleased" />
425
357
  </subtasks>
426
358
  </RakeTaskImpl>
427
359
  <RakeTaskImpl description="Remove any temporary products" fullCommand="clean" id="clean" />
428
360
  <RakeTaskImpl description="Remove any generated files" fullCommand="clobber" id="clobber" />
429
- <RakeTaskImpl description="Build and install bidi2pdf-rails-0.0.1.alpha.1.gem into system gems" fullCommand="install" id="install" />
361
+ <RakeTaskImpl id="coverage">
362
+ <subtasks>
363
+ <RakeTaskImpl description="Run tests with coverage" fullCommand="coverage:coverage" id="coverage" />
364
+ <RakeTaskImpl description="Merge simplecov coverage reports" fullCommand="coverage:merge_reports" id="merge_reports" />
365
+ </subtasks>
366
+ </RakeTaskImpl>
367
+ <RakeTaskImpl description="Build and install bidi2pdf-rails-0.1.1.gem into system gems" fullCommand="install" id="install" />
430
368
  <RakeTaskImpl id="install">
431
369
  <subtasks>
432
- <RakeTaskImpl description="Build and install bidi2pdf-rails-0.0.1.alpha.1.gem into system gems without network access" fullCommand="install:local" id="local" />
370
+ <RakeTaskImpl description="Build and install bidi2pdf-rails-0.1.1.gem into system gems without network access" fullCommand="install:local" id="local" />
433
371
  </subtasks>
434
372
  </RakeTaskImpl>
435
- <RakeTaskImpl description="Create tag v0.0.1.alpha.1 and build and push bidi2pdf-rails-0.0.1.alpha.1.gem to https://rubygems.org" fullCommand="release[remote]" id="release[remote]" />
373
+ <RakeTaskImpl description="Create tag v0.1.1 and build and push bidi2pdf-rails-0.1.1.gem to https://rubygems.org" fullCommand="release[remote]" id="release[remote]" />
436
374
  <RakeTaskImpl description="Run RuboCop" fullCommand="rubocop" id="rubocop" />
437
375
  <RakeTaskImpl id="rubocop">
438
376
  <subtasks>
data/CHANGELOG.md CHANGED
@@ -7,8 +7,26 @@ All notable changes to this project will be documented in this file.
7
7
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8
8
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
9
9
 
10
+ [unreleased]: https://github.com/dieter-medium/bidi2pdf-rails/compare/v0.1.1..HEAD
11
+
10
12
  <!-- generated by git-cliff end -->
11
13
 
14
+ ## [0.1.1] - 2025-04-22
15
+
16
+ ### 🎨 Refactored
17
+
18
+ - Streamline PDF generation in browser session by @dieter-medium
19
+
20
+ ### 📝 Docs
21
+
22
+ - Update CHANGELOG formatting for releases by @dieter-medium
23
+ - Enhance README with usage and config examples by @dieter-medium
24
+
25
+ ### 🚀 Added
26
+
27
+ - Add support for injecting custom JS into PDFs by @dieter-medium
28
+ - Add support for injecting custom CSS into PDFs by @dieter-medium
29
+
12
30
  ## [0.1.0] - 2025-04-21
13
31
 
14
32
  ### 🐛 Fixed
@@ -28,6 +46,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
28
46
  - Enhance configuration and logging for Bidi2pdfRails by @dieter-medium
29
47
  - Ensure custom HTML can be used in PDF rendering by @dieter-medium
30
48
 
31
- [unreleased](https://github.com/dieter-medium/bidi2pdf-rails/compare/v0.1.0..HEAD)
32
- [0.1.0](https://github.com/dieter-medium/bidi2pdf-rails/compare/v0.0.1.alpha.1..v0.1.0)
49
+ ### 🔄 Released
50
+
51
+ - [unreleased](https://github.com/dieter-medium/bidi2pdf-rails/compare/v0.1.0..HEAD)
52
+ - [0.1.1](https://github.com/dieter-medium/bidi2pdf-rails/compare/v0.1.0..v0.1.1)
53
+ - [0.1.0](https://github.com/dieter-medium/bidi2pdf-rails/compare/v0.0.1.alpha.1..v0.1.0)
33
54
 
data/README.md CHANGED
@@ -10,8 +10,6 @@
10
10
  modern, headless-browser-based PDF rendering engine.
11
11
  Generate high-fidelity PDFs directly from your Rails views or external URLs with minimal setup.
12
12
 
13
- > ⚠️ **Project status:** _Under active development_. Not yet recommended for production use.
14
-
15
13
  ---
16
14
 
17
15
  ## ✨ Features
@@ -29,9 +27,9 @@ Generate high-fidelity PDFs directly from your Rails views or external URLs with
29
27
  Add to your Gemfile:
30
28
 
31
29
  ```ruby
32
- # Until released, use the GitHub repo
33
- gem "bidi2pdf-rails", github: "dieter-medium/bidi2pdf-rails", branch: "main"
34
- gem "bidi2pdf", github: "dieter-medium/bidi2pdf", branch: "main"
30
+ gem "bidi2pdf-rails"
31
+ # for development only
32
+ # gem "bidi2pdf-rails", github: "dieter-medium/bidi2pdf-rails", branch: "main"
35
33
 
36
34
  # Optional for performance:
37
35
  # gem "websocket-native"
@@ -133,6 +131,8 @@ This repo includes **real integration tests** that serve as usage documentation:
133
131
 
134
132
  - [Download PDF with `.pdf` format](spec/acceptance/user_can_download_report_pdf_spec.rb)
135
133
  - [Render protected remote URLs using Basic Auth, cookies, and headers](spec/acceptance/user_can_generate_pdf_from_protected_remote_url_spec.rb)
134
+ - [Inject custom CSS into a Webpage before printing](spec/acceptance/user_can_inject_css_before_pdf_printing_spec.rb)
135
+ - [Inject custom JS into a Webpage before printing](spec/acceptance/user_can_inject_js_before_pdf_printing_spec.rb)
136
136
 
137
137
  ---
138
138
 
@@ -146,7 +146,7 @@ See full config options in:
146
146
  bin/rails generate bidi2pdf_rails:initializer
147
147
  ```
148
148
 
149
- Or explore `Bidi2pdfRails::Configuration` in the source.
149
+ Or explore [Bidi2pdfRails::Config::CONFIG_OPTIONS](lib/bidi2pdf_rails/config.rb) in the source.
150
150
 
151
151
  ---
152
152
 
@@ -66,7 +66,11 @@ module Bidi2pdfRails
66
66
  { name: :page_height, desc: "PDF page height (cm, not needed when format is specified)", default: Bidi2pdf::PAPER_FORMATS_CM[:a4][:height], ask: true, color: :yellow },
67
67
  { name: :print_background, desc: "Print background graphics?", default: true, ask: true, color: :green },
68
68
  { name: :scale, desc: "PDF scale (e.g., 1.0)", default: 1.0, ask: true, color: :yellow },
69
- { name: :shrink_to_fit, desc: "Shrink to fit?", default: false, ask: true, color: :green }
69
+ { name: :shrink_to_fit, desc: "Shrink to fit?", default: false, ask: true, color: :green },
70
+ { name: :custom_js, desc: "Raw JavaScript code to inject before PDF generation (without <script> tags)", default: nil, ask: false, color: :yellow },
71
+ { name: :custom_css, desc: "Raw CSS styles to inject before PDF generation (without <style> tags)", default: nil, ask: false, color: :yellow },
72
+ { name: :custom_js_url, desc: "URL to JavaScript file to load before PDF generation", default: nil, ask: false, color: :yellow },
73
+ { name: :custom_css_url, desc: "URL to CSS file to load before PDF generation", default: nil, ask: false, color: :yellow }
70
74
  ]
71
75
  },
72
76
 
@@ -5,6 +5,7 @@ require "rails/railtie"
5
5
  require_relative "services/html_renderer"
6
6
  require_relative "services/pdf_renderer"
7
7
  require_relative "services/pdf_browser_session"
8
+ require_relative "services/pdf_injection"
8
9
  require_relative "services/url_to_pdf_converter"
9
10
  require_relative "services/html_to_pdf_converter"
10
11
 
@@ -4,13 +4,18 @@ module Bidi2pdfRails
4
4
  module Services
5
5
  class HtmlToPdfConverter
6
6
  include PdfBrowserSession
7
+ include PdfInjection
7
8
 
8
- def initialize(html, print_options: {}, wait_for_network_idle: true, wait_for_page_loaded: true, wait_for_page_check_script: nil)
9
+ def initialize(html, print_options: {}, wait_for_network_idle: true, wait_for_page_loaded: true, wait_for_page_check_script: nil, custom_css: nil, custom_css_url: nil, custom_js: nil, custom_js_url: nil)
9
10
  @html = html
10
11
  @print_options = print_options
11
12
  @wait_for_network_idle = wait_for_network_idle
12
13
  @wait_for_page_loaded = wait_for_page_loaded
13
14
  @wait_for_page_check_script = wait_for_page_check_script
15
+ @custom_css = custom_css
16
+ @custom_css_url = custom_css_url
17
+ @custom_js = custom_js
18
+ @custom_js_url = custom_js_url
14
19
  end
15
20
 
16
21
  def generate
@@ -21,6 +26,10 @@ module Bidi2pdfRails
21
26
 
22
27
  def prepare_tab(tab)
23
28
  tab.render_html_content(@html)
29
+
30
+ inject_custom_css(tab, @custom_css, @custom_css_url)
31
+ inject_custom_js(tab, @custom_js, @custom_js_url)
32
+
24
33
  wait_for_tab(tab)
25
34
  end
26
35
  end
@@ -4,8 +4,6 @@ module Bidi2pdfRails
4
4
  module Services
5
5
  module PdfBrowserSession
6
6
  def run_browser_session
7
- pdf_data = nil
8
-
9
7
  thread = Thread.new do
10
8
  Rails.application.executor.wrap do
11
9
  browser = ChromedriverManagerSingleton.session.browser
@@ -15,7 +13,7 @@ module Bidi2pdfRails
15
13
 
16
14
  begin
17
15
  prepare_tab(tab)
18
- pdf_data = tab.print(print_options: @print_options)
16
+ tab.print(print_options: @print_options)
19
17
  ensure
20
18
  tab&.close
21
19
  window&.close
@@ -24,7 +22,8 @@ module Bidi2pdfRails
24
22
  end
25
23
  end
26
24
 
27
- thread.join
25
+ pdf_data = thread.value
26
+
28
27
  Base64.decode64(pdf_data)
29
28
  end
30
29
 
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bidi2pdfRails
4
+ module Services
5
+ module PdfInjection
6
+ def inject_custom_css(tab, custom_css, custom_css_url)
7
+ return unless custom_css || custom_css_url
8
+
9
+ if custom_css
10
+ tab.inject_style(url: nil, content: custom_css, id: nil)
11
+ end
12
+
13
+ if custom_css_url
14
+ tab.inject_style(url: custom_css_url, content: nil, id: nil)
15
+ end
16
+ end
17
+
18
+ def inject_custom_js(tab, custom_js, custom_js_url)
19
+ return unless custom_js || custom_js_url
20
+
21
+ if custom_js
22
+ tab.inject_script(url: nil, content: custom_js, id: nil)
23
+ end
24
+
25
+ if custom_js_url
26
+ tab.inject_script(url: custom_js_url, content: nil, id: nil)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -50,6 +50,10 @@ module Bidi2pdfRails
50
50
  wait_for_network_idle = @pdf_options.fetch(:wait_for_network_idle, Bidi2pdfRails.config.general_options.wait_for_network_idle_value)
51
51
  wait_for_page_loaded = @pdf_options.fetch(:wait_for_page_loaded, Bidi2pdfRails.config.general_options.wait_for_page_loaded_value)
52
52
  wait_for_page_check_script = @pdf_options.fetch(:wait_for_page_check_script, Bidi2pdfRails.config.general_options.wait_for_page_check_script_value)
53
+ custom_css = @pdf_options.fetch(:custom_css, Bidi2pdfRails.config.pdf_settings.custom_css_value(@controller))
54
+ custom_css_url = @pdf_options.fetch(:custom_css_url, Bidi2pdfRails.config.pdf_settings.custom_css_url_value(@controller))
55
+ custom_js = @pdf_options.fetch(:custom_js, Bidi2pdfRails.config.pdf_settings.custom_js_value(@controller))
56
+ custom_js_url = @pdf_options.fetch(:custom_js_url, Bidi2pdfRails.config.pdf_settings.custom_js_url_value(@controller))
53
57
 
54
58
  if @print_url_options[:url]
55
59
  headers = @print_url_options[:headers] || Bidi2pdfRails.config.render_remote_settings.headers_value(@controller)
@@ -63,7 +67,11 @@ module Bidi2pdfRails
63
67
  print_options: print_options,
64
68
  wait_for_network_idle: wait_for_network_idle,
65
69
  wait_for_page_loaded: wait_for_page_loaded,
66
- wait_for_page_check_script: wait_for_page_check_script
70
+ wait_for_page_check_script: wait_for_page_check_script,
71
+ custom_css: custom_css,
72
+ custom_css_url: custom_css_url,
73
+ custom_js: custom_js,
74
+ custom_js_url: custom_js_url,
67
75
  ).generate
68
76
  else
69
77
  html = @html_options.fetch(:inline, HtmlRenderer.new(@controller, @html_options).render)
@@ -72,7 +80,11 @@ module Bidi2pdfRails
72
80
  print_options: print_options,
73
81
  wait_for_network_idle: wait_for_network_idle,
74
82
  wait_for_page_loaded: wait_for_page_loaded,
75
- wait_for_page_check_script: wait_for_page_check_script
83
+ wait_for_page_check_script: wait_for_page_check_script,
84
+ custom_css: custom_css,
85
+ custom_css_url: custom_css_url,
86
+ custom_js: custom_js,
87
+ custom_js_url: custom_js_url,
76
88
  ).generate
77
89
  end
78
90
  end
@@ -4,8 +4,9 @@ module Bidi2pdfRails
4
4
  module Services
5
5
  class UrlToPdfConverter
6
6
  include PdfBrowserSession
7
+ include PdfInjection
7
8
 
8
- def initialize(url, headers: {}, cookies: {}, auth: {}, print_options: {}, wait_for_network_idle: true, wait_for_page_loaded: true, wait_for_page_check_script: nil)
9
+ def initialize(url, headers: {}, cookies: {}, auth: {}, print_options: {}, wait_for_network_idle: true, wait_for_page_loaded: true, wait_for_page_check_script: nil, custom_css: nil, custom_css_url: nil, custom_js: nil, custom_js_url: nil)
9
10
  @url = url
10
11
  @headers = headers || {}
11
12
  @cookies = cookies || {}
@@ -14,6 +15,10 @@ module Bidi2pdfRails
14
15
  @wait_for_network_idle = wait_for_network_idle
15
16
  @wait_for_page_loaded = wait_for_page_loaded
16
17
  @wait_for_page_check_script = wait_for_page_check_script
18
+ @custom_css = custom_css
19
+ @custom_css_url = custom_css_url
20
+ @custom_js = custom_js
21
+ @custom_js_url = custom_js_url
17
22
  end
18
23
 
19
24
  def generate
@@ -28,6 +33,10 @@ module Bidi2pdfRails
28
33
  add_cookies(tab)
29
34
 
30
35
  tab.navigate_to(@url)
36
+
37
+ inject_custom_css(tab, @custom_css, @custom_css_url)
38
+ inject_custom_js(tab, @custom_js, @custom_js_url)
39
+
31
40
  wait_for_tab(tab)
32
41
  end
33
42
 
@@ -1,3 +1,3 @@
1
1
  module Bidi2pdfRails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+ require "net/http"
5
+ require "rack/handler/puma"
6
+ require "socket"
7
+ require "base64"
8
+
9
+ RSpec.feature "As a user, I want to inject css into a website before printing a PDF", :pdf, type: :request do
10
+ before(:all) do
11
+ # Bidi2pdfRails.config.general_options.headless = false
12
+ Bidi2pdfRails::ChromedriverManagerSingleton.initialize_manager force: true
13
+ end
14
+
15
+ after(:all) do
16
+ Bidi2pdfRails::ChromedriverManagerSingleton.shutdown
17
+ end
18
+
19
+ scenario "Using raw CSS" do
20
+ # Controller setup:
21
+ #
22
+ # You can configure basic auth in two ways:
23
+ #
24
+ # 1. In an initializer (global config):
25
+ #
26
+ # Bidi2pdfRails.configure do |config|
27
+ # config.pdf_settings.custom_css = <<~CSS
28
+ # p {
29
+ # page-break-after: always;
30
+ # }
31
+ # CSS
32
+ # end
33
+ #
34
+ # 2. Inline within controller action:
35
+ # css = <<~CSS
36
+ # p {
37
+ # page-break-after: always;
38
+ # }
39
+ # CSS
40
+ # render pdf: 'inject-css-raw',
41
+ # custom_css: css,
42
+ # layout: 'simple',
43
+ # template: 'reports/simple',
44
+ # wait_for_page_loaded: false,
45
+ # print_options: { page: { format: :A4 } }
46
+ #
47
+
48
+ when_ "I request a PDF from a page and inject raw css" do
49
+ before do
50
+ with_pdf_settings :custom_css, <<-CSS
51
+ p {
52
+ page-break-after: always;
53
+ }
54
+ CSS
55
+
56
+ @response = get_pdf_response "/inject/raw-css"
57
+ end
58
+
59
+ then_ "I receive a successful HTTP response" do
60
+ expect(@response.code).to eq("200")
61
+ end
62
+
63
+ and_ "I receive a PDF file in response" do
64
+ expect(@response['Content-Type']).to eq("application/pdf")
65
+ end
66
+
67
+ and_ "the PDF contains the expected number of pages" do
68
+ expected_page_count = 6
69
+ expect(@response.body).to have_pdf_page_count(expected_page_count)
70
+ end
71
+
72
+ and_ "the disposition header is set to attachment" do
73
+ expect(@response['Content-Disposition']).to start_with('inline; filename="inject-raw-css.pdf"')
74
+ end
75
+
76
+ and_ 'the last page contains the expected content ("6")' do
77
+ expect(@response.body).to contains_pdf_text("6").at_page(6)
78
+ end
79
+ end
80
+ end
81
+
82
+ scenario "Using an external stylesheet" do
83
+ # Controller setup:
84
+ #
85
+ # You can configure cookies in two ways:
86
+ #
87
+ # 1. In an initializer (global config):
88
+ #
89
+ # Bidi2pdfRails.configure do |config|
90
+ # config.pdf_settings.custom_css_url = ->(controller) { controller.view_context.asset_url 'stylesheets/simple.css' }
91
+ # end
92
+ #
93
+ # 2. Inline within controller action:
94
+ #
95
+ # render pdf: 'inject-css-url',
96
+ # custom_css_url: view_context.asset_url('stylesheets/simple.css'),
97
+ # layout: 'simple',
98
+ # template: 'reports/simple',
99
+ # wait_for_page_loaded: false,
100
+ # print_options: { page: { format: :A4 } }
101
+ #
102
+
103
+ when_ "I request a PDF from a page and inject an external stylesheet" do
104
+ before do
105
+ with_pdf_settings :custom_css_url, ->(controller) { controller.view_context.asset_url 'stylesheets/simple.css' }
106
+
107
+ @response = get_pdf_response "/inject/url-css"
108
+ end
109
+
110
+ then_ "I receive a successful HTTP response" do
111
+ expect(@response.code).to eq("200")
112
+ end
113
+
114
+ and_ "I receive a PDF file in response" do
115
+ expect(@response['Content-Type']).to eq("application/pdf")
116
+ end
117
+
118
+ and_ "the PDF contains the expected number of pages" do
119
+ expected_page_count = 6
120
+ expect(@response.body).to have_pdf_page_count(expected_page_count)
121
+ end
122
+
123
+ and_ "the disposition header is set to attachment" do
124
+ expect(@response['Content-Disposition']).to start_with('inline; filename="inject-url-css.pdf"')
125
+ end
126
+
127
+ and_ 'the last page contains the expected content ("6")' do
128
+ expect(@response.body).to contains_pdf_text(6).at_page(6)
129
+ end
130
+ end
131
+ end
132
+ end