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..c73cb0f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,7 @@ /pkg/ /spec/reports/ /tmp/ +/examples/tmp/*.pdf *.gem +.DS_Store +.tool-versions diff --git a/Gemfile.lock b/Gemfile.lock index 10951db..114f94c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -173,7 +173,9 @@ GEM yard (0.9.36) PLATFORMS + -darwin-21 arm64-darwin-21 + arm64-darwin-22 ruby 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.

+
+
+ + + + + + 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/tmp/.gitkeep b/examples/tmp/.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..340937b --- /dev/null +++ b/examples/wkhtmltopdf_rspec.rb @@ -0,0 +1,76 @@ +require "bundler/inline" + +gemfile do + 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 "webmock/rspec" + +RSpec.configure do |config| +end + +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(:host_tmp_dir) { "#{__dir__}/tmp" } + let(:container_tmp_dir) { "/tmp" } + let(:file_name) { "document.pdf" } + let(:file_path) { "#{host_tmp_dir}/#{file_name}" } + let(:container_file_path) { "#{container_tmp_dir}/#{file_name}" } + # wkhtmltopdf typically expects 'wkhtmltopdf [url] [output]' as command + let(:container) do + Testcontainers::DockerContainer.new("surnet/alpine-wkhtmltopdf:3.17.0-0.12.6-small") + .with_entrypoint("wkhtmltopdf") + .with_command([url, container_file_path]) + end + + before do + FileUtils.mkdir_p(host_tmp_dir) + WebMock.allow_net_connect! + container.with_filesystem_binds(["#{host_tmp_dir}:#{container_tmp_dir}:rw"]) + stub_request(:get, url).to_return( + body: File.read("#{__dir__}/fixtures/web_page/index.html") + ) + end + + after(:each) do + WebMock.allow_net_connect! + if container.running? + puts "Container logs: #{container.logs}" + container.stop + end + container&.remove + FileUtils.rm_f(file_path) + end + + context "when using html" do + it "generates a PDF page" do + container.start + # Wait with a timeout and check periodically + 10.times do |i| + break if File.exist?(file_path) + puts "Waiting for PDF (attempt #{i + 1}/10)..." + sleep 1 + end + + puts "Checking for file at: #{file_path}" + puts "File exists? #{File.exist?(file_path)}" + puts "Directory contents: #{Dir.entries(host_tmp_dir)}" + + expect(file_path).to exist_file + end + end +end