htmlToPdf 3.0.1 → 4.0.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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +78 -0
- data/Gemfile.lock +36 -2
- data/README.md +46 -1
- data/docs/background-jobs.md +229 -0
- data/htmlToPdf.gemspec +4 -2
- data/lib/htmlToPdf/browser_pool.rb +15 -12
- data/lib/htmlToPdf/concurrency_limiter.rb +40 -0
- data/lib/htmlToPdf/configuration.rb +61 -0
- data/lib/htmlToPdf/converter.rb +41 -0
- data/lib/htmlToPdf/generate_job.rb +44 -0
- data/lib/htmlToPdf/job_status/cache_store.rb +31 -0
- data/lib/htmlToPdf/job_status.rb +78 -0
- data/lib/htmlToPdf/renderer.rb +54 -0
- data/lib/htmlToPdf/storage/active_storage_adapter.rb +35 -0
- data/lib/htmlToPdf/storage/s3_adapter.rb +48 -0
- data/lib/htmlToPdf/storage.rb +41 -0
- data/lib/htmlToPdf/version.rb +1 -1
- data/lib/htmlToPdf.rb +65 -17
- data/spec/htmlToPdf/concurrency_limiter_spec.rb +83 -0
- data/spec/htmlToPdf/converter_spec.rb +71 -0
- data/spec/htmlToPdf/generate_job_spec.rb +111 -0
- data/spec/htmlToPdf/job_status_spec.rb +78 -0
- data/spec/htmlToPdf/renderer_spec.rb +67 -0
- data/spec/htmlToPdf/storage/active_storage_adapter_spec.rb +37 -0
- data/spec/htmlToPdf/storage/s3_adapter_spec.rb +45 -0
- data/spec/internal/app/models/document.rb +3 -0
- data/spec/internal/config/database.yml +3 -0
- data/spec/internal/config/storage.yml +3 -0
- data/spec/internal/db/schema.rb +40 -0
- data/spec/spec_helper.rb +7 -1
- metadata +66 -3
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
RSpec.describe HtmlToPdf::Converter do
|
|
4
|
+
let(:fake_browser) { instance_double(Ferrum::Browser, quit: nil) }
|
|
5
|
+
let(:fake_page) { instance_double(Ferrum::Page, go_to: nil, pdf: "%PDF-fake-bytes") }
|
|
6
|
+
let(:written_path) { [] }
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
allow(Ferrum::Browser).to receive(:new).and_return(fake_browser)
|
|
10
|
+
allow(fake_browser).to receive(:create_page) do |&block|
|
|
11
|
+
block.call(fake_page)
|
|
12
|
+
end
|
|
13
|
+
allow(fake_page).to receive(:go_to) { |url| written_path << url.delete_prefix("file://") }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
after { HtmlToPdf::BrowserPool.reset! }
|
|
17
|
+
|
|
18
|
+
describe ".convert" do
|
|
19
|
+
it "writes the HTML to a temp file under config.tmp_dir, converts it, then deletes the temp file" do
|
|
20
|
+
Dir.mktmpdir do |dir|
|
|
21
|
+
HtmlToPdf.configure { |c| c.tmp_dir = dir }
|
|
22
|
+
|
|
23
|
+
result = described_class.convert("<h1>hi</h1>", print_options: {}, javascript_delay: nil)
|
|
24
|
+
|
|
25
|
+
expect(result).to eq("%PDF-fake-bytes")
|
|
26
|
+
expect(written_path.first).to start_with(dir)
|
|
27
|
+
expect(File.exist?(written_path.first)).to be false
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "strips the \\" artifact the same way the synchronous path always has" do
|
|
32
|
+
allow(File).to receive(:write).and_call_original
|
|
33
|
+
|
|
34
|
+
described_class.convert('<p>say \\"hi\\"</p>', print_options: {}, javascript_delay: nil)
|
|
35
|
+
|
|
36
|
+
expect(File).to have_received(:write).with(anything, "<p>say hi</p>")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "deletes the temp file even if the conversion raises" do
|
|
40
|
+
allow(fake_browser).to receive(:create_page).and_raise(ArgumentError, "boom")
|
|
41
|
+
written = []
|
|
42
|
+
allow(File).to receive(:write).and_wrap_original { |m, path, content| written << path; m.call(path, content) }
|
|
43
|
+
|
|
44
|
+
expect { described_class.convert("<p>x</p>", print_options: {}, javascript_delay: nil) }
|
|
45
|
+
.to raise_error(ArgumentError)
|
|
46
|
+
expect(File.exist?(written.first)).to be false
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe ".render_pdf" do
|
|
51
|
+
it "sleeps for javascript_delay/1000 seconds before capturing, when positive" do
|
|
52
|
+
allow(described_class).to receive(:sleep)
|
|
53
|
+
|
|
54
|
+
Tempfile.create(["x", ".html"]) do |f|
|
|
55
|
+
described_class.render_pdf(f.path, print_options: {}, javascript_delay: 500)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
expect(described_class).to have_received(:sleep).with(0.5)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "does not sleep when javascript_delay is nil or zero" do
|
|
62
|
+
allow(described_class).to receive(:sleep)
|
|
63
|
+
|
|
64
|
+
Tempfile.create(["x", ".html"]) do |f|
|
|
65
|
+
described_class.render_pdf(f.path, print_options: {}, javascript_delay: nil)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
expect(described_class).not_to have_received(:sleep)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
# Loaded eagerly by lib/htmlToPdf.rb itself (guarded on ActiveJob::Base
|
|
4
|
+
# already being defined, true here) — required explicitly anyway so this
|
|
5
|
+
# file doesn't depend on load order against other specs.
|
|
6
|
+
require "htmlToPdf/generate_job"
|
|
7
|
+
|
|
8
|
+
RSpec.describe HtmlToPdf::GenerateJob do
|
|
9
|
+
let(:fake_browser) { instance_double(Ferrum::Browser, quit: nil) }
|
|
10
|
+
let(:fake_page) { instance_double(Ferrum::Page, go_to: nil, pdf: "%PDF-fake-bytes") }
|
|
11
|
+
|
|
12
|
+
it "is defined as soon as the gem loads, not only after HtmlToPdf.generate_later is first called — a separate Sidekiq/Solid Queue worker process that never calls generate_later itself still needs the class available to perform a job the web process enqueued" do
|
|
13
|
+
expect(defined?(HtmlToPdf::GenerateJob)).to be_truthy
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
before do
|
|
17
|
+
HtmlToPdf.configure { |c| c.default_url_options = { http_host: "example.test" } }
|
|
18
|
+
allow(Ferrum::Browser).to receive(:new).and_return(fake_browser)
|
|
19
|
+
allow(fake_browser).to receive(:create_page).and_yield(fake_page)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
after { HtmlToPdf::BrowserPool.reset! }
|
|
23
|
+
|
|
24
|
+
describe "HtmlToPdf.generate_later" do
|
|
25
|
+
it "enqueues a GenerateJob and immediately writes a queued JobStatus" do
|
|
26
|
+
job = nil
|
|
27
|
+
expect do
|
|
28
|
+
job = HtmlToPdf.generate_later(
|
|
29
|
+
controller: WidgetsController, action: :show, assigns: { widget: "Widget #1" }
|
|
30
|
+
)
|
|
31
|
+
end.to have_enqueued_job(described_class)
|
|
32
|
+
|
|
33
|
+
expect(HtmlToPdf::JobStatus.find(job.job_id).state).to eq("queued")
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe "#perform" do
|
|
38
|
+
it "renders the PDF, stores it, and marks the status completed, calling on_job_success" do
|
|
39
|
+
on_success = nil
|
|
40
|
+
HtmlToPdf.configure { |c| c.on_job_success = ->(status) { on_success = status } }
|
|
41
|
+
|
|
42
|
+
perform_enqueued_jobs do
|
|
43
|
+
HtmlToPdf.generate_later(controller: WidgetsController, action: :show, assigns: { widget: "W" })
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
status = on_success
|
|
47
|
+
expect(status.state).to eq("completed")
|
|
48
|
+
expect(status.result).to be_nil # no `store:` was given
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "stores the PDF via the given adapter when store: is passed" do
|
|
52
|
+
document = Document.create!
|
|
53
|
+
perform_enqueued_jobs do
|
|
54
|
+
HtmlToPdf.generate_later(
|
|
55
|
+
controller: WidgetsController, action: :show, assigns: { widget: "W" },
|
|
56
|
+
store: { adapter: :active_storage, record: document, attachment: :pdf }
|
|
57
|
+
)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
document.reload
|
|
61
|
+
expect(document.pdf).to be_attached
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "retries on a recoverable Ferrum error, then succeeds" do
|
|
65
|
+
call_count = 0
|
|
66
|
+
allow(fake_browser).to receive(:create_page) do |&block|
|
|
67
|
+
call_count += 1
|
|
68
|
+
raise Ferrum::DeadBrowserError if call_count == 1
|
|
69
|
+
|
|
70
|
+
block.call(fake_page)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
perform_enqueued_jobs do
|
|
74
|
+
HtmlToPdf.generate_later(controller: WidgetsController, action: :show, assigns: { widget: "W" })
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
expect(call_count).to eq(2)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it "marks the status failed and calls on_job_failure once retries are exhausted" do
|
|
81
|
+
allow(fake_browser).to receive(:create_page).and_raise(Ferrum::DeadBrowserError)
|
|
82
|
+
on_failure_args = nil
|
|
83
|
+
HtmlToPdf.configure { |c| c.on_job_failure = ->(status, error) { on_failure_args = [status, error] } }
|
|
84
|
+
|
|
85
|
+
job = nil
|
|
86
|
+
perform_enqueued_jobs do
|
|
87
|
+
job = HtmlToPdf.generate_later(controller: WidgetsController, action: :show, assigns: { widget: "W" })
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
status, error = on_failure_args
|
|
91
|
+
expect(error).to be_a(Ferrum::DeadBrowserError)
|
|
92
|
+
expect(status.job_id).to eq(job.job_id)
|
|
93
|
+
expect(HtmlToPdf::JobStatus.find(job.job_id).state).to eq("failed")
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "discards (no retry) on ArgumentError — a bad print_options can't be fixed by retrying" do
|
|
97
|
+
on_failure_args = nil
|
|
98
|
+
HtmlToPdf.configure { |c| c.on_job_failure = ->(status, error) { on_failure_args = [status, error] } }
|
|
99
|
+
|
|
100
|
+
perform_enqueued_jobs do
|
|
101
|
+
HtmlToPdf.generate_later(
|
|
102
|
+
controller: WidgetsController, action: :show,
|
|
103
|
+
pdf_options: { print_options: { format: "not-a-real-format" } }
|
|
104
|
+
)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
expect(fake_browser).not_to have_received(:create_page)
|
|
108
|
+
expect(on_failure_args.last).to be_a(ArgumentError)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
RSpec.describe HtmlToPdf::JobStatus do
|
|
4
|
+
describe "lifecycle" do
|
|
5
|
+
it "moves through queued -> processing -> completed, persisted via config.job_status_store" do
|
|
6
|
+
status = described_class.new("job-1", metadata: { invoice_id: 42 })
|
|
7
|
+
|
|
8
|
+
status.queued!
|
|
9
|
+
expect(status.state).to eq("queued")
|
|
10
|
+
expect(status.queued_at).to be_a(Time)
|
|
11
|
+
|
|
12
|
+
status.processing!
|
|
13
|
+
expect(status.state).to eq("processing")
|
|
14
|
+
expect(status.started_at).to be_a(Time)
|
|
15
|
+
|
|
16
|
+
status.completed!(result: { adapter: :s3, key: "invoices/42.pdf" })
|
|
17
|
+
expect(status.state).to eq("completed")
|
|
18
|
+
expect(status.result).to eq(adapter: :s3, key: "invoices/42.pdf")
|
|
19
|
+
expect(status.finished_at).to be_a(Time)
|
|
20
|
+
expect(status.metadata).to eq(invoice_id: 42)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "records an exception's class and message on failed!, not the raw exception" do
|
|
24
|
+
status = described_class.new("job-2")
|
|
25
|
+
|
|
26
|
+
status.failed!(ArgumentError.new("bad print_options"))
|
|
27
|
+
|
|
28
|
+
expect(status.state).to eq("failed")
|
|
29
|
+
expect(status.error).to eq(class: "ArgumentError", message: "bad print_options")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "#queued?/#processing?/#completed?/#failed? reflect the current state" do
|
|
33
|
+
status = described_class.new("job-4")
|
|
34
|
+
status.queued!
|
|
35
|
+
expect(status.queued?).to be true
|
|
36
|
+
expect(status.processing?).to be false
|
|
37
|
+
|
|
38
|
+
status.processing!
|
|
39
|
+
expect(status.processing?).to be true
|
|
40
|
+
expect(status.queued?).to be false
|
|
41
|
+
|
|
42
|
+
status.completed!(result: nil)
|
|
43
|
+
expect(status.completed?).to be true
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
describe ".find" do
|
|
48
|
+
it "reads back a status written by a different instance (e.g. a worker process)" do
|
|
49
|
+
described_class.new("job-5", metadata: { a: 1 }).processing!
|
|
50
|
+
|
|
51
|
+
found = described_class.find("job-5")
|
|
52
|
+
|
|
53
|
+
expect(found.state).to eq("processing")
|
|
54
|
+
expect(found.metadata).to eq(a: 1)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "returns a status with a nil state for a job_id nothing has written yet" do
|
|
58
|
+
found = described_class.find("never-seen")
|
|
59
|
+
|
|
60
|
+
expect(found.state).to be_nil
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
describe "the default store (CacheStore)" do
|
|
65
|
+
it "is namespaced under htmlToPdf:job_status: so it can't collide with the host app's own cache keys" do
|
|
66
|
+
described_class.new("job-6").queued!
|
|
67
|
+
|
|
68
|
+
expect(Rails.cache.read("htmlToPdf:job_status:job-6")).to include(state: "queued")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "honors config.job_status_ttl as the cache expiry" do
|
|
72
|
+
HtmlToPdf.configure { |c| c.job_status_ttl = 60 }
|
|
73
|
+
expect(Rails.cache).to receive(:write).with(anything, anything, hash_including(expires_in: 60))
|
|
74
|
+
|
|
75
|
+
described_class.new("job-7").queued!
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
RSpec.describe HtmlToPdf::Renderer do
|
|
4
|
+
before do
|
|
5
|
+
HtmlToPdf.configure { |c| c.default_url_options = { http_host: "example.test" } }
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
describe "without config.default_url_options set" do
|
|
9
|
+
it "raises a clear ArgumentError instead of silently rendering broken asset/route URLs" do
|
|
10
|
+
HtmlToPdf.configure { |c| c.default_url_options = nil }
|
|
11
|
+
|
|
12
|
+
expect { described_class.render(controller: WidgetsController, action: :show) }
|
|
13
|
+
.to raise_error(ArgumentError, /default_url_options/)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe "rendering outside of a request (stubbed Ferrum, no real Chrome)" do
|
|
18
|
+
let(:fake_browser) { instance_double(Ferrum::Browser, quit: nil) }
|
|
19
|
+
let(:fake_page) { instance_double(Ferrum::Page, go_to: nil, pdf: "%PDF-fake-bytes") }
|
|
20
|
+
|
|
21
|
+
before do
|
|
22
|
+
allow(Ferrum::Browser).to receive(:new).and_return(fake_browser)
|
|
23
|
+
allow(fake_browser).to receive(:create_page).and_yield(fake_page)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
after { HtmlToPdf::BrowserPool.reset! }
|
|
27
|
+
|
|
28
|
+
it "renders the given controller/action via ActionController::Renderer and converts it" do
|
|
29
|
+
result = described_class.render(
|
|
30
|
+
controller: WidgetsController,
|
|
31
|
+
action: :show,
|
|
32
|
+
assigns: { widget: "Widget #1" }
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
expect(result).to eq("%PDF-fake-bytes")
|
|
36
|
+
expect(fake_page).to have_received(:pdf).with(hash_including(encoding: :binary))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "accepts the controller as a String, matching the synchronous path's flexibility" do
|
|
40
|
+
result = described_class.render(controller: "WidgetsController", action: :show)
|
|
41
|
+
|
|
42
|
+
expect(result).to eq("%PDF-fake-bytes")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "resolves print_options/layout through the same HtmlToPdf::PrintOptions as the synchronous path" do
|
|
46
|
+
described_class.render(
|
|
47
|
+
controller: WidgetsController,
|
|
48
|
+
action: :show,
|
|
49
|
+
pdf_options: { print_options: { landscape: true, format: "A4" } }
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
expect(fake_page).to have_received(:pdf).with(hash_including(landscape: true, paper_width: 8.27))
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "strips header_template/footer_template from pdf_options by default, same as the synchronous path" do
|
|
56
|
+
described_class.render(
|
|
57
|
+
controller: WidgetsController,
|
|
58
|
+
action: :show,
|
|
59
|
+
pdf_options: { print_options: { header_template: "<script>evil</script>" } }
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
expect(fake_page).to have_received(:pdf) do |**opts|
|
|
63
|
+
expect(opts).not_to have_key(:header_template)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
RSpec.describe HtmlToPdf::Storage::ActiveStorageAdapter do
|
|
4
|
+
it "attaches the PDF bytes to the given record/attachment and returns blob metadata" do
|
|
5
|
+
document = Document.create!
|
|
6
|
+
|
|
7
|
+
result = described_class.new(record: document, attachment: :pdf, filename: "invoice.pdf")
|
|
8
|
+
.store("%PDF-1.4 fake bytes")
|
|
9
|
+
|
|
10
|
+
document.reload
|
|
11
|
+
expect(document.pdf).to be_attached
|
|
12
|
+
expect(document.pdf.blob.filename.to_s).to eq("invoice.pdf")
|
|
13
|
+
expect(document.pdf.blob.content_type).to eq("application/pdf")
|
|
14
|
+
expect(result).to eq(
|
|
15
|
+
adapter: :active_storage,
|
|
16
|
+
blob_id: document.pdf.blob.id,
|
|
17
|
+
key: document.pdf.blob.key,
|
|
18
|
+
filename: "invoice.pdf",
|
|
19
|
+
byte_size: "%PDF-1.4 fake bytes".bytesize
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "dispatches through HtmlToPdf::Storage.store(adapter: :active_storage, ...)" do
|
|
24
|
+
document = Document.create!
|
|
25
|
+
|
|
26
|
+
HtmlToPdf::Storage.store("%PDF-bytes", adapter: :active_storage, record: document, attachment: :pdf)
|
|
27
|
+
|
|
28
|
+
expect(document.reload.pdf).to be_attached
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "raises a clear LoadError if ActiveStorage isn't defined" do
|
|
32
|
+
hide_const("ActiveStorage")
|
|
33
|
+
|
|
34
|
+
expect { described_class.new(record: Document.new, attachment: :pdf).store("x") }
|
|
35
|
+
.to raise_error(LoadError, /ActiveStorage isn't loaded/)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
require "aws-sdk-s3"
|
|
3
|
+
|
|
4
|
+
RSpec.describe HtmlToPdf::Storage::S3Adapter do
|
|
5
|
+
# Aws::S3::Client's own built-in test mode — no real network calls, no
|
|
6
|
+
# WebMock needed.
|
|
7
|
+
let(:client_options) { { stub_responses: true, region: "us-east-1" } }
|
|
8
|
+
|
|
9
|
+
it "uploads the PDF bytes to the given bucket/key and returns where it landed" do
|
|
10
|
+
captured_client = nil
|
|
11
|
+
allow(Aws::S3::Client).to receive(:new).and_wrap_original { |original, *args| captured_client = original.call(*args) }
|
|
12
|
+
|
|
13
|
+
result = described_class.new(key: "invoices/1.pdf", bucket: "my-bucket", client_options: client_options)
|
|
14
|
+
.store("%PDF-fake-bytes")
|
|
15
|
+
|
|
16
|
+
put_object_call = captured_client.api_requests.find { |r| r[:operation_name] == :put_object }
|
|
17
|
+
expect(put_object_call[:params]).to include(
|
|
18
|
+
bucket: "my-bucket", key: "invoices/1.pdf", body: "%PDF-fake-bytes", content_type: "application/pdf"
|
|
19
|
+
)
|
|
20
|
+
expect(result).to include(adapter: :s3, bucket: "my-bucket", key: "invoices/1.pdf")
|
|
21
|
+
expect(result[:url]).to include("my-bucket").and include("invoices/1.pdf")
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "falls back to config.s3_bucket/s3_client_options when not given per call" do
|
|
25
|
+
HtmlToPdf.configure do |c|
|
|
26
|
+
c.s3_bucket = "configured-bucket"
|
|
27
|
+
c.s3_client_options = client_options
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
result = described_class.new(key: "x.pdf").store("bytes")
|
|
31
|
+
|
|
32
|
+
expect(result[:bucket]).to eq("configured-bucket")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "raises ArgumentError when no bucket is given or configured" do
|
|
36
|
+
expect { described_class.new(key: "x.pdf") }.to raise_error(ArgumentError, /bucket/)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "raises a clear LoadError if aws-sdk-s3 can't be required" do
|
|
40
|
+
adapter = described_class.new(key: "x.pdf", bucket: "b", client_options: client_options)
|
|
41
|
+
allow(adapter).to receive(:require).with("aws-sdk-s3").and_raise(LoadError)
|
|
42
|
+
|
|
43
|
+
expect { adapter.store("bytes") }.to raise_error(LoadError, /add `gem "aws-sdk-s3"`/)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Only what this gem's own specs exercise: ActiveStorage's own core tables
|
|
2
|
+
# (so storage/active_storage_adapter_spec.rb can attach a real blob) plus one
|
|
3
|
+
# minimal owning model, `documents`.
|
|
4
|
+
ActiveRecord::Schema[8.1].define do
|
|
5
|
+
create_table :documents, force: true
|
|
6
|
+
|
|
7
|
+
create_table :active_storage_blobs, force: true do |t|
|
|
8
|
+
t.string :key, null: false
|
|
9
|
+
t.string :filename, null: false
|
|
10
|
+
t.string :content_type
|
|
11
|
+
t.text :metadata
|
|
12
|
+
t.string :service_name, null: false
|
|
13
|
+
t.bigint :byte_size, null: false
|
|
14
|
+
t.string :checksum
|
|
15
|
+
t.datetime :created_at, null: false, precision: 6
|
|
16
|
+
|
|
17
|
+
t.index [:key], unique: true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
create_table :active_storage_attachments, force: true do |t|
|
|
21
|
+
t.string :name, null: false
|
|
22
|
+
t.references :record, null: false, polymorphic: true, index: false
|
|
23
|
+
t.references :blob, null: false
|
|
24
|
+
|
|
25
|
+
t.datetime :created_at, null: false, precision: 6
|
|
26
|
+
|
|
27
|
+
t.index [:record_type, :record_id, :name, :blob_id],
|
|
28
|
+
name: "index_active_storage_attachments_uniqueness", unique: true
|
|
29
|
+
t.foreign_key :active_storage_blobs, column: :blob_id
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
create_table :active_storage_variant_records, force: true do |t|
|
|
33
|
+
t.belongs_to :blob, null: false, index: false
|
|
34
|
+
t.string :variation_digest, null: false
|
|
35
|
+
|
|
36
|
+
t.index [:blob_id, :variation_digest],
|
|
37
|
+
name: "index_active_storage_variant_records_uniqueness", unique: true
|
|
38
|
+
t.foreign_key :active_storage_blobs, column: :blob_id
|
|
39
|
+
end
|
|
40
|
+
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
require "combustion"
|
|
2
|
-
Combustion.initialize! :action_controller, :action_view
|
|
2
|
+
Combustion.initialize! :action_controller, :action_view, :active_record, :active_job, :active_storage
|
|
3
3
|
|
|
4
4
|
require "rspec/rails"
|
|
5
|
+
require "active_job/test_helper"
|
|
5
6
|
require "htmlToPdf"
|
|
6
7
|
|
|
8
|
+
ActiveJob::Base.queue_adapter = :test
|
|
9
|
+
|
|
7
10
|
RSpec.configure do |config|
|
|
8
11
|
config.expect_with :rspec do |c|
|
|
9
12
|
c.syntax = :expect
|
|
10
13
|
end
|
|
11
14
|
|
|
15
|
+
config.include ActiveJob::TestHelper
|
|
16
|
+
|
|
12
17
|
config.before do
|
|
13
18
|
HtmlToPdf.reset_configuration!
|
|
19
|
+
HtmlToPdf::ConcurrencyLimiter.reset!
|
|
14
20
|
end
|
|
15
21
|
|
|
16
22
|
# Specs tagged :chrome exercise a real headless Chrome/Chromium process.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: htmlToPdf
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 4.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ram Laxman Yadav
|
|
@@ -165,10 +165,40 @@ dependencies:
|
|
|
165
165
|
- - "~>"
|
|
166
166
|
- !ruby/object:Gem::Version
|
|
167
167
|
version: '0.9'
|
|
168
|
+
- !ruby/object:Gem::Dependency
|
|
169
|
+
name: aws-sdk-s3
|
|
170
|
+
requirement: !ruby/object:Gem::Requirement
|
|
171
|
+
requirements:
|
|
172
|
+
- - "~>"
|
|
173
|
+
- !ruby/object:Gem::Version
|
|
174
|
+
version: '1.0'
|
|
175
|
+
type: :development
|
|
176
|
+
prerelease: false
|
|
177
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
178
|
+
requirements:
|
|
179
|
+
- - "~>"
|
|
180
|
+
- !ruby/object:Gem::Version
|
|
181
|
+
version: '1.0'
|
|
182
|
+
- !ruby/object:Gem::Dependency
|
|
183
|
+
name: sqlite3
|
|
184
|
+
requirement: !ruby/object:Gem::Requirement
|
|
185
|
+
requirements:
|
|
186
|
+
- - "~>"
|
|
187
|
+
- !ruby/object:Gem::Version
|
|
188
|
+
version: '2.0'
|
|
189
|
+
type: :development
|
|
190
|
+
prerelease: false
|
|
191
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
192
|
+
requirements:
|
|
193
|
+
- - "~>"
|
|
194
|
+
- !ruby/object:Gem::Version
|
|
195
|
+
version: '2.0'
|
|
168
196
|
description: HtmlToPdf lets any Rails controller action be downloaded as a PDF. Request
|
|
169
197
|
the action with a PDF format and the gem renders the view exactly as the browser
|
|
170
198
|
would, then converts it to PDF via a pooled headless Chrome instance (through the
|
|
171
|
-
ferrum gem)
|
|
199
|
+
ferrum gem) — synchronously, in-request, or in the background via HtmlToPdf.generate_later
|
|
200
|
+
(ActiveJob, so it runs under Sidekiq/Solid Queue/any adapter), with job status tracking
|
|
201
|
+
and S3/ActiveStorage storage. Supports Rails 7.x and 8.x.
|
|
172
202
|
email:
|
|
173
203
|
- yadavramlaxman@gmail.com
|
|
174
204
|
executables: []
|
|
@@ -187,26 +217,47 @@ files:
|
|
|
187
217
|
- Rakefile
|
|
188
218
|
- config.ru
|
|
189
219
|
- docs/architecture.md
|
|
220
|
+
- docs/background-jobs.md
|
|
190
221
|
- docs/contributing.md
|
|
191
222
|
- docs/rails-integration.md
|
|
192
223
|
- docs/security.md
|
|
193
224
|
- htmlToPdf.gemspec
|
|
194
225
|
- lib/htmlToPdf.rb
|
|
195
226
|
- lib/htmlToPdf/browser_pool.rb
|
|
227
|
+
- lib/htmlToPdf/concurrency_limiter.rb
|
|
196
228
|
- lib/htmlToPdf/configuration.rb
|
|
229
|
+
- lib/htmlToPdf/converter.rb
|
|
230
|
+
- lib/htmlToPdf/generate_job.rb
|
|
231
|
+
- lib/htmlToPdf/job_status.rb
|
|
232
|
+
- lib/htmlToPdf/job_status/cache_store.rb
|
|
197
233
|
- lib/htmlToPdf/print_options.rb
|
|
234
|
+
- lib/htmlToPdf/renderer.rb
|
|
235
|
+
- lib/htmlToPdf/storage.rb
|
|
236
|
+
- lib/htmlToPdf/storage/active_storage_adapter.rb
|
|
237
|
+
- lib/htmlToPdf/storage/s3_adapter.rb
|
|
198
238
|
- lib/htmlToPdf/version.rb
|
|
199
239
|
- spec/htmlToPdf/browser_pool_spec.rb
|
|
240
|
+
- spec/htmlToPdf/concurrency_limiter_spec.rb
|
|
200
241
|
- spec/htmlToPdf/configuration_spec.rb
|
|
242
|
+
- spec/htmlToPdf/converter_spec.rb
|
|
243
|
+
- spec/htmlToPdf/generate_job_spec.rb
|
|
244
|
+
- spec/htmlToPdf/job_status_spec.rb
|
|
201
245
|
- spec/htmlToPdf/print_options_spec.rb
|
|
246
|
+
- spec/htmlToPdf/renderer_spec.rb
|
|
247
|
+
- spec/htmlToPdf/storage/active_storage_adapter_spec.rb
|
|
248
|
+
- spec/htmlToPdf/storage/s3_adapter_spec.rb
|
|
202
249
|
- spec/integration/controller_spec.rb
|
|
203
250
|
- spec/internal/app/controllers/application_controller.rb
|
|
204
251
|
- spec/internal/app/controllers/booms_controller.rb
|
|
205
252
|
- spec/internal/app/controllers/widgets_controller.rb
|
|
253
|
+
- spec/internal/app/models/document.rb
|
|
206
254
|
- spec/internal/app/views/layouts/application.html.erb
|
|
207
255
|
- spec/internal/app/views/layouts/pdf.html.erb
|
|
208
256
|
- spec/internal/app/views/widgets/show.html.erb
|
|
257
|
+
- spec/internal/config/database.yml
|
|
209
258
|
- spec/internal/config/routes.rb
|
|
259
|
+
- spec/internal/config/storage.yml
|
|
260
|
+
- spec/internal/db/schema.rb
|
|
210
261
|
- spec/internal/log/.gitignore
|
|
211
262
|
- spec/internal/public/favicon.ico
|
|
212
263
|
- spec/spec_helper.rb
|
|
@@ -235,19 +286,31 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
235
286
|
requirements: []
|
|
236
287
|
rubygems_version: 3.6.9
|
|
237
288
|
specification_version: 4
|
|
238
|
-
summary: Render Rails controller actions to PDF
|
|
289
|
+
summary: Render Rails controller actions to PDF, synchronously or in the background,
|
|
290
|
+
using headless Chrome
|
|
239
291
|
test_files:
|
|
240
292
|
- spec/htmlToPdf/browser_pool_spec.rb
|
|
293
|
+
- spec/htmlToPdf/concurrency_limiter_spec.rb
|
|
241
294
|
- spec/htmlToPdf/configuration_spec.rb
|
|
295
|
+
- spec/htmlToPdf/converter_spec.rb
|
|
296
|
+
- spec/htmlToPdf/generate_job_spec.rb
|
|
297
|
+
- spec/htmlToPdf/job_status_spec.rb
|
|
242
298
|
- spec/htmlToPdf/print_options_spec.rb
|
|
299
|
+
- spec/htmlToPdf/renderer_spec.rb
|
|
300
|
+
- spec/htmlToPdf/storage/active_storage_adapter_spec.rb
|
|
301
|
+
- spec/htmlToPdf/storage/s3_adapter_spec.rb
|
|
243
302
|
- spec/integration/controller_spec.rb
|
|
244
303
|
- spec/internal/app/controllers/application_controller.rb
|
|
245
304
|
- spec/internal/app/controllers/booms_controller.rb
|
|
246
305
|
- spec/internal/app/controllers/widgets_controller.rb
|
|
306
|
+
- spec/internal/app/models/document.rb
|
|
247
307
|
- spec/internal/app/views/layouts/application.html.erb
|
|
248
308
|
- spec/internal/app/views/layouts/pdf.html.erb
|
|
249
309
|
- spec/internal/app/views/widgets/show.html.erb
|
|
310
|
+
- spec/internal/config/database.yml
|
|
250
311
|
- spec/internal/config/routes.rb
|
|
312
|
+
- spec/internal/config/storage.yml
|
|
313
|
+
- spec/internal/db/schema.rb
|
|
251
314
|
- spec/internal/log/.gitignore
|
|
252
315
|
- spec/internal/public/favicon.ico
|
|
253
316
|
- spec/spec_helper.rb
|