From 3fed862a4b34416f97176f1773277bbf3f67c0e6 Mon Sep 17 00:00:00 2001 From: Luis Porras Date: Tue, 30 May 2023 10:59:08 -0500 Subject: [PATCH 1/7] adding example of usage with wkhtmltopdf command line tool --- .../workflows/examples-wkhtmltopdf_rspec.yml | 28 +++++++++++++ .gitignore | 2 + Gemfile.lock | 1 + examples/pdfs/.gitkeep | 0 examples/wkhtmltopdf_rspec.rb | 39 +++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 .github/workflows/examples-wkhtmltopdf_rspec.yml create mode 100644 examples/pdfs/.gitkeep create mode 100644 examples/wkhtmltopdf_rspec.rb diff --git a/.github/workflows/examples-wkhtmltopdf_rspec.yml b/.github/workflows/examples-wkhtmltopdf_rspec.yml new file mode 100644 index 0000000..8c57c6a --- /dev/null +++ b/.github/workflows/examples-wkhtmltopdf_rspec.yml @@ -0,0 +1,28 @@ +name: "Examples: wkhtmltopdf module (RSpec)" + +on: + push: + branches: + - main + + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + name: Ruby ${{ matrix.ruby }} + strategy: + matrix: + ruby: + - '3.2.0' + + steps: + - uses: actions/checkout@v3 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Run the example + working-directory: ./examples + run: ruby wkhtmltopdf_rspec.rb \ No newline at end of file diff --git a/.gitignore b/.gitignore index f6216f2..dde5b6c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ /pkg/ /spec/reports/ /tmp/ +/examples/pdfs/*.pdf *.gem +.DS_Store diff --git a/Gemfile.lock b/Gemfile.lock index b86a79e..0a4960d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -140,6 +140,7 @@ GEM yard (0.9.34) PLATFORMS + -darwin-21 arm64-darwin-21 x86_64-darwin-22 x86_64-linux diff --git a/examples/pdfs/.gitkeep b/examples/pdfs/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/examples/wkhtmltopdf_rspec.rb b/examples/wkhtmltopdf_rspec.rb new file mode 100644 index 0000000..96f15d5 --- /dev/null +++ b/examples/wkhtmltopdf_rspec.rb @@ -0,0 +1,39 @@ +require 'bundler/inline' + +gemfile do + source 'https://rubygems.org' + gem 'rspec' + gem 'wkhtmltopdf-binary' + gem 'testcontainers-core', path: '../core', require: 'testcontainers' +end + +require 'rspec' +require 'rspec/autorun' + +RSpec.configure do |config| +end + +describe 'Wkhtmltopdf Example' do + let(:url) { 'https://google.com' } + let(:pdfs_path) { "#{__dir__}/pdfs:/pdfs:rw" } + let(:command) { [url, file_name] } + let(:file_name) { '/pdfs/google.pdf' } + let(:container) do + Testcontainers::DockerContainer.new('surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small') + end + + before do + container.with_filesystem_binds([pdfs_path]) + .with_command(command) + .start + end + + after do + container&.stop + container&.remove + end + + it 'generates a PDF page from URL' do + expect(File.file?(__dir__ + file_name)).to be_truthy + end +end From 0d75bdfe98d8f6aea70106082975331b11cbdc8b Mon Sep 17 00:00:00 2001 From: Luis Porras Date: Tue, 30 May 2023 12:49:39 -0500 Subject: [PATCH 2/7] change matcher in test --- examples/wkhtmltopdf_rspec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/wkhtmltopdf_rspec.rb b/examples/wkhtmltopdf_rspec.rb index 96f15d5..0dee74d 100644 --- a/examples/wkhtmltopdf_rspec.rb +++ b/examples/wkhtmltopdf_rspec.rb @@ -21,6 +21,7 @@ let(:container) do Testcontainers::DockerContainer.new('surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small') end + let(:file_path) { __dir__ + file_name } before do container.with_filesystem_binds([pdfs_path]) @@ -34,6 +35,6 @@ end it 'generates a PDF page from URL' do - expect(File.file?(__dir__ + file_name)).to be_truthy + expect(File).to exist(file_path) end end From c67e8bc0193adc5474239a62cea8a0acafe8f425 Mon Sep 17 00:00:00 2001 From: Luis Porras Date: Tue, 30 May 2023 16:17:44 -0500 Subject: [PATCH 3/7] standard fixes --- examples/wkhtmltopdf_rspec.rb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/wkhtmltopdf_rspec.rb b/examples/wkhtmltopdf_rspec.rb index 0dee74d..25805ce 100644 --- a/examples/wkhtmltopdf_rspec.rb +++ b/examples/wkhtmltopdf_rspec.rb @@ -1,32 +1,32 @@ -require 'bundler/inline' +require "bundler/inline" gemfile do - source 'https://rubygems.org' - gem 'rspec' - gem 'wkhtmltopdf-binary' - gem 'testcontainers-core', path: '../core', require: 'testcontainers' + source "https://rubygems.org" + gem "rspec" + gem "wkhtmltopdf-binary" + gem "testcontainers-core", path: "../core", require: "testcontainers" end -require 'rspec' -require 'rspec/autorun' +require "rspec" +require "rspec/autorun" RSpec.configure do |config| end -describe 'Wkhtmltopdf Example' do - let(:url) { 'https://google.com' } +describe "Wkhtmltopdf Example" do + let(:url) { "https://google.com" } let(:pdfs_path) { "#{__dir__}/pdfs:/pdfs:rw" } let(:command) { [url, file_name] } - let(:file_name) { '/pdfs/google.pdf' } + let(:file_name) { "/pdfs/google.pdf" } let(:container) do - Testcontainers::DockerContainer.new('surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small') + Testcontainers::DockerContainer.new("surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small") end let(:file_path) { __dir__ + file_name } before do container.with_filesystem_binds([pdfs_path]) - .with_command(command) - .start + .with_command(command) + .start end after do @@ -34,7 +34,7 @@ container&.remove end - it 'generates a PDF page from URL' do + it "generates a PDF page from URL" do expect(File).to exist(file_path) end end From 9771f0ece59ad78bb122e735bb6333b760df2b2f Mon Sep 17 00:00:00 2001 From: Luis Porras Date: Tue, 30 May 2023 20:01:46 -0500 Subject: [PATCH 4/7] adding web page fixture instead of real web URL --- .gitignore | 1 + Gemfile.lock | 1 + examples/fixtures/web_page/index.html | 168 ++++++++++++++++++++++++++ examples/fixtures/web_page/styles.css | 9 ++ examples/wkhtmltopdf_rspec.rb | 56 ++++++--- 5 files changed, 218 insertions(+), 17 deletions(-) create mode 100644 examples/fixtures/web_page/index.html create mode 100644 examples/fixtures/web_page/styles.css diff --git a/.gitignore b/.gitignore index dde5b6c..96c9ae5 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ /examples/pdfs/*.pdf *.gem .DS_Store +.tool-versions \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 0a4960d..9533ff9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -142,6 +142,7 @@ GEM PLATFORMS -darwin-21 arm64-darwin-21 + arm64-darwin-22 x86_64-darwin-22 x86_64-linux diff --git a/examples/fixtures/web_page/index.html b/examples/fixtures/web_page/index.html new file mode 100644 index 0000000..791fe91 --- /dev/null +++ b/examples/fixtures/web_page/index.html @@ -0,0 +1,168 @@ + + + + + + + + + + Sticky Footer Template ยท Bootstrap v5.3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+

Sticky footer

+

Pin a footer to the bottom of the viewport in desktop browsers with this custom HTML and CSS.

+

Use the sticky footer with a fixed navbar if need be, too.

+
+
+ +
+
+ Place sticky footer content here. +
+
+ + + + diff --git a/examples/fixtures/web_page/styles.css b/examples/fixtures/web_page/styles.css new file mode 100644 index 0000000..f8be437 --- /dev/null +++ b/examples/fixtures/web_page/styles.css @@ -0,0 +1,9 @@ +/* Custom page CSS +-------------------------------------------------- */ +/* Not required for template or sticky footer method. */ + +.container { + width: auto; + max-width: 680px; + padding: 0 15px; +} diff --git a/examples/wkhtmltopdf_rspec.rb b/examples/wkhtmltopdf_rspec.rb index 25805ce..37d9656 100644 --- a/examples/wkhtmltopdf_rspec.rb +++ b/examples/wkhtmltopdf_rspec.rb @@ -1,40 +1,62 @@ -require "bundler/inline" +require 'bundler/inline' gemfile do - source "https://rubygems.org" - gem "rspec" - gem "wkhtmltopdf-binary" - gem "testcontainers-core", path: "../core", require: "testcontainers" + source 'https://rubygems.org' + gem 'rspec' + gem 'wkhtmltopdf-binary' + gem 'testcontainers-core', path: '../core', require: 'testcontainers' + + group :test do + gem 'webmock' + end end -require "rspec" -require "rspec/autorun" +require 'rspec' +require 'rspec/autorun' +require 'webmock/rspec' RSpec.configure do |config| end -describe "Wkhtmltopdf Example" do - let(:url) { "https://google.com" } +RSpec::Matchers.define :exist_file do + match do |file_path| + File.exist?(file_path) + end +end + +describe 'Wkhtmltopdf Example' do + let(:url) { 'https://getbootstrap.com/docs/5.3/examples/sticky-footer/' } let(:pdfs_path) { "#{__dir__}/pdfs:/pdfs:rw" } let(:command) { [url, file_name] } - let(:file_name) { "/pdfs/google.pdf" } + let(:file_name) { '/pdfs/document.pdf' } + let(:file_path) { "#{__dir__}#{file_name}" } let(:container) do - Testcontainers::DockerContainer.new("surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small") + Testcontainers::DockerContainer.new('surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small') end - let(:file_path) { __dir__ + file_name } before do + WebMock.allow_net_connect! container.with_filesystem_binds([pdfs_path]) - .with_command(command) - .start end - after do + after(:each) do + WebMock.allow_net_connect! container&.stop container&.remove end - it "generates a PDF page from URL" do - expect(File).to exist(file_path) + context 'when using html' do + before(:each) do + stub_request(:get, url).to_return( + body: File.read("#{__dir__}/fixtures/web_page/index.html") + ) + end + + it 'generates a PDF page' do + container.with_command(command) + container.start + + expect(file_path).to exist_file + end end end From 6f317d5be0e8e7374fa440ea305b6efe8a5ac9c8 Mon Sep 17 00:00:00 2001 From: Luis Porras Date: Tue, 30 May 2023 20:03:15 -0500 Subject: [PATCH 5/7] standard fix --- examples/wkhtmltopdf_rspec.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/wkhtmltopdf_rspec.rb b/examples/wkhtmltopdf_rspec.rb index 37d9656..37868ab 100644 --- a/examples/wkhtmltopdf_rspec.rb +++ b/examples/wkhtmltopdf_rspec.rb @@ -1,19 +1,19 @@ -require 'bundler/inline' +require "bundler/inline" gemfile do - source 'https://rubygems.org' - gem 'rspec' - gem 'wkhtmltopdf-binary' - gem 'testcontainers-core', path: '../core', require: 'testcontainers' + source "https://rubygems.org" + gem "rspec" + gem "wkhtmltopdf-binary" + gem "testcontainers-core", path: "../core", require: "testcontainers" group :test do - gem 'webmock' + gem "webmock" end end -require 'rspec' -require 'rspec/autorun' -require 'webmock/rspec' +require "rspec" +require "rspec/autorun" +require "webmock/rspec" RSpec.configure do |config| end @@ -24,14 +24,14 @@ end end -describe 'Wkhtmltopdf Example' do - let(:url) { 'https://getbootstrap.com/docs/5.3/examples/sticky-footer/' } +describe "Wkhtmltopdf Example" do + let(:url) { "https://getbootstrap.com/docs/5.3/examples/sticky-footer/" } let(:pdfs_path) { "#{__dir__}/pdfs:/pdfs:rw" } let(:command) { [url, file_name] } - let(:file_name) { '/pdfs/document.pdf' } + let(:file_name) { "/pdfs/document.pdf" } let(:file_path) { "#{__dir__}#{file_name}" } let(:container) do - Testcontainers::DockerContainer.new('surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small') + Testcontainers::DockerContainer.new("surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small") end before do @@ -45,14 +45,14 @@ container&.remove end - context 'when using html' do + context "when using html" do before(:each) do stub_request(:get, url).to_return( body: File.read("#{__dir__}/fixtures/web_page/index.html") ) end - it 'generates a PDF page' do + it "generates a PDF page" do container.with_command(command) container.start From a32d5f2b225924d43ce9e99a46b0dc6f1794bd4b Mon Sep 17 00:00:00 2001 From: Luis Porras Date: Tue, 30 May 2023 20:04:34 -0500 Subject: [PATCH 6/7] add end line --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 96c9ae5..a485166 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ /examples/pdfs/*.pdf *.gem .DS_Store -.tool-versions \ No newline at end of file +.tool-versions From 9c1611fdb62bf7395afda7500c6d88e630427a2a Mon Sep 17 00:00:00 2001 From: Luis Porras Date: Wed, 31 May 2023 08:09:54 -0500 Subject: [PATCH 7/7] rename pdfs folder to tmp --- .gitignore | 2 +- examples/{pdfs => tmp}/.gitkeep | 0 examples/wkhtmltopdf_rspec.rb | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) rename examples/{pdfs => tmp}/.gitkeep (100%) diff --git a/.gitignore b/.gitignore index a485166..c73cb0f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ /pkg/ /spec/reports/ /tmp/ -/examples/pdfs/*.pdf +/examples/tmp/*.pdf *.gem .DS_Store .tool-versions diff --git a/examples/pdfs/.gitkeep b/examples/tmp/.gitkeep similarity index 100% rename from examples/pdfs/.gitkeep rename to examples/tmp/.gitkeep diff --git a/examples/wkhtmltopdf_rspec.rb b/examples/wkhtmltopdf_rspec.rb index 37868ab..cd54efe 100644 --- a/examples/wkhtmltopdf_rspec.rb +++ b/examples/wkhtmltopdf_rspec.rb @@ -26,9 +26,9 @@ describe "Wkhtmltopdf Example" do let(:url) { "https://getbootstrap.com/docs/5.3/examples/sticky-footer/" } - let(:pdfs_path) { "#{__dir__}/pdfs:/pdfs:rw" } + let(:pdfs_path) { "#{__dir__}/tmp:/tmp:rw" } let(:command) { [url, file_name] } - let(:file_name) { "/pdfs/document.pdf" } + let(:file_name) { "/tmp/document.pdf" } let(:file_path) { "#{__dir__}#{file_name}" } let(:container) do Testcontainers::DockerContainer.new("surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small")