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.
@@ -0,0 +1,158 @@
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 js 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_js = <<~JS
28
+ # // insert styles into the page with javascript
29
+ # document.head.insertAdjacentHTML('beforeend', `
30
+ # <style>
31
+ # p {
32
+ # page-break-after: always;
33
+ # }
34
+ # </style>
35
+ # `);
36
+ # document.body.insertAdjacentHTML('beforeend', `
37
+ # <p>7</p>
38
+ # <p>8</p>
39
+ # `);
40
+ # JS
41
+ #
42
+ # 2. Inline within controller action:
43
+ # js = <<~JS
44
+ # // insert styles into the page with javascript
45
+ # document.head.insertAdjacentHTML('beforeend', `
46
+ # <style>
47
+ # p {
48
+ # page-break-after: always;
49
+ # }
50
+ # </style>
51
+ # `);
52
+ # document.body.insertAdjacentHTML('beforeend', `
53
+ # <p>7</p>
54
+ # <p>8</p>
55
+ # `);
56
+ # JS
57
+ # render pdf: 'inject-js-raw',
58
+ # custom_js: js,
59
+ # layout: 'simple',
60
+ # template: 'reports/simple',
61
+ # wait_for_page_loaded: false,
62
+ # print_options: { page: { format: :A4 } }
63
+ #
64
+
65
+ when_ "I request a PDF from a page and inject raw js" do
66
+ before do
67
+ with_pdf_settings :custom_js, <<-JS
68
+ // insert styles into the page with javascript
69
+ document.head.insertAdjacentHTML('beforeend', `
70
+ <style>
71
+ p {
72
+ page-break-after: always;
73
+ }
74
+ </style>
75
+ `);
76
+ document.body.insertAdjacentHTML('beforeend', `
77
+ <p>7</p>
78
+ <p>8</p>
79
+ `);
80
+ JS
81
+
82
+ @response = get_pdf_response "/inject/raw-js"
83
+ end
84
+
85
+ then_ "I receive a successful HTTP response" do
86
+ expect(@response.code).to eq("200")
87
+ end
88
+
89
+ and_ "I receive a PDF file in response" do
90
+ expect(@response['Content-Type']).to eq("application/pdf")
91
+ end
92
+
93
+ and_ "the PDF contains the expected number of pages" do
94
+ expected_page_count = 8
95
+ expect(@response.body).to have_pdf_page_count(expected_page_count)
96
+ end
97
+
98
+ and_ "the disposition header is set to attachment" do
99
+ expect(@response['Content-Disposition']).to start_with('inline; filename="inject-raw-js.pdf"')
100
+ end
101
+
102
+ and_ 'the last page contains the expected content ("6")' do
103
+ expect(@response.body).to contains_pdf_text("6").at_page(6)
104
+ end
105
+ end
106
+ end
107
+
108
+ scenario "Using an external javascript file" do
109
+ # Controller setup:
110
+ #
111
+ # You can configure cookies in two ways:
112
+ #
113
+ # 1. In an initializer (global config):
114
+ #
115
+ # Bidi2pdfRails.configure do |config|
116
+ # config.pdf_settings.custom_js_url = ->(controller) { controller.view_context.asset_url 'javascripts/simple.js' }
117
+ # end
118
+ #
119
+ # 2. Inline within controller action:
120
+ #
121
+ # render pdf: 'inject-js-url',
122
+ # custom_js_url: view_context.asset_url('javascripts/simple.js'),
123
+ # layout: 'simple',
124
+ # template: 'reports/simple',
125
+ # wait_for_page_loaded: false,
126
+ # print_options: { page: { format: :A4 } }
127
+ #
128
+
129
+ when_ "I request a PDF from a page and inject an external stylesheet" do
130
+ before do
131
+ with_pdf_settings :custom_js_url, ->(controller) { controller.view_context.asset_url 'javascripts/simple.js' }
132
+
133
+ @response = get_pdf_response "/inject/url-js"
134
+ end
135
+
136
+ then_ "I receive a successful HTTP response" do
137
+ expect(@response.code).to eq("200")
138
+ end
139
+
140
+ and_ "I receive a PDF file in response" do
141
+ expect(@response['Content-Type']).to eq("application/pdf")
142
+ end
143
+
144
+ and_ "the PDF contains the expected number of pages" do
145
+ expected_page_count = 8
146
+ expect(@response.body).to have_pdf_page_count(expected_page_count)
147
+ end
148
+
149
+ and_ "the disposition header is set to attachment" do
150
+ expect(@response['Content-Disposition']).to start_with('inline; filename="inject-url-js.pdf"')
151
+ end
152
+
153
+ and_ 'the last page contains the expected content ("6")' do
154
+ expect(@response.body).to contains_pdf_text(6).at_page(6)
155
+ end
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,12 @@
1
+ // insert styles into the page with javascript
2
+ document.head.insertAdjacentHTML('beforeend', `
3
+ <style>
4
+ p {
5
+ page-break-after: always;
6
+ }
7
+ </style>
8
+ `);
9
+ document.body.insertAdjacentHTML('beforeend', `
10
+ <p>7</p>
11
+ <p>8</p>
12
+ `);
@@ -0,0 +1,3 @@
1
+ p {
2
+ page-break-after: always;
3
+ }
@@ -25,6 +25,10 @@ class ReportsController < ApplicationController
25
25
  render pdf: 'inline-html', inline: html, wait_for_page_loaded: false, print_options: { page: { format: :A4 } }
26
26
  end
27
27
 
28
+ def inject
29
+ render pdf: "inject-#{params[:kind]}", layout: 'simple', template: 'reports/simple', wait_for_page_loaded: false, print_options: { page: { format: :A4 } }
30
+ end
31
+
28
32
  def convert_remote_url_basic_auth
29
33
  render pdf: 'convert-remote-url-basic-auth',
30
34
  url: basic_auth_endpoint_url(only_path: false),
@@ -42,4 +46,10 @@ class ReportsController < ApplicationController
42
46
  url: api_endpoint_url(only_path: false),
43
47
  wait_for_page_loaded: false
44
48
  end
49
+
50
+ def convert_remote_url_error
51
+ render pdf: 'convert-remote-url-cookie',
52
+ url: "https://httpstat.us/#{params[:code]}",
53
+ wait_for_page_loaded: false
54
+ end
45
55
  end
@@ -0,0 +1,6 @@
1
+ <p>1</p>
2
+ <p>2</p>
3
+ <p>3</p>
4
+ <p>4</p>
5
+ <p>5</p>
6
+ <p>6</p>
@@ -54,6 +54,10 @@ Bidi2pdfRails.configure do |config|
54
54
  # config.pdf_settings.print_background = true # Print background graphics?
55
55
  # config.pdf_settings.scale = 1.0 # PDF scale (e.g., 1.0)
56
56
  # config.pdf_settings.shrink_to_fit = false # Shrink to fit?
57
+ # config.pdf_settings.custom_js = nil # Raw JavaScript code to inject before PDF generation (without <script> tags)
58
+ # config.pdf_settings.custom_css = nil # Raw CSS styles to inject before PDF generation (without <style> tags)
59
+ # config.pdf_settings.custom_js_url = nil # URL to JavaScript file to load before PDF generation (takes precedence over custom_js)
60
+ # config.pdf_settings.custom_css_url = nil # URL to CSS file to load before PDF generation (takes precedence over custom_css)
57
61
 
58
62
  #
59
63
  # Remote URL Settings
@@ -6,6 +6,10 @@ Rails.application.routes.draw do
6
6
  get "convert-remote-url-basic-auth" => "reports#convert_remote_url_basic_auth", as: :print_remote_basic_auth
7
7
  get "convert-remote-url-cookie" => "reports#convert_remote_url_cookie", as: :print_remote_url_cookie
8
8
  get "convert-remote-url-header" => "reports#convert_remote_url_header", as: :print_remote_url_header
9
+ get "convert-remote-url-error/:code" => "reports#convert_remote_url_error", as: :print_error
10
+ get "inject/:kind" => "reports#inject",
11
+ constraints: { kind: /(raw-css|raw-js|url-css|url-js)/ },
12
+ as: :inject_css
9
13
 
10
14
  get 'basic-auth', to: 'secure#basic_auth_endpoint', as: :basic_auth_endpoint
11
15
  get 'header-auth', to: 'secure#api_endpoint', as: :api_endpoint