From 7e6140ba512f0aca302f1cbc3060299fab1f07d9 Mon Sep 17 00:00:00 2001 From: Michael Hale Date: Fri, 23 Jan 2026 12:08:06 -0500 Subject: [PATCH 1/3] call passed block --- lib/chromium/pdf.rb | 10 +++++----- test/chromium/test_pdf.rb | 21 +++++++++++++++++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/chromium/pdf.rb b/lib/chromium/pdf.rb index 3fca3fd..6f84a4f 100644 --- a/lib/chromium/pdf.rb +++ b/lib/chromium/pdf.rb @@ -14,24 +14,24 @@ module Pdf # @param arguments [Array] An array of arguments to pass to the Chrome binary. # @yield [file, filename] Yields the file object and the filename to the block. # - def generate_pdf!(unescaped_filename, print_url, arguments: ['--headless --disable-gpu']) + def generate_pdf!(unescaped_filename, print_url, arguments: ['--headless --disable-gpu'], &block) chrome_path = ENV.fetch('GOOGLE_CHROME_BIN', nil) filename = unescaped_filename.gsub('&', 'and') Dir.mktmpdir do |path| filepath = "#{path}/#{filename}" - chrome_print chrome_path, print_url, filename, filepath, arguments + chrome_print chrome_path, print_url, filename, filepath, arguments, &block end end protected - def chrome_print(chrome_path, print_url, filename, filepath, arguments) - system("LD_PRELOAD='' #{chrome_path} --print-to-pdf='#{filepath}' #{arguments.join(' ')} #{print_url}") + def chrome_print(chrome_path, print_url, filename, filepath, arguments, &block) + Kernel.system("LD_PRELOAD='' #{chrome_path} --print-to-pdf='#{filepath}' #{arguments.join(' ')} #{print_url}") File.open(filepath) do |file| - yield file, filename + block&.call(file, filename) end end end diff --git a/test/chromium/test_pdf.rb b/test/chromium/test_pdf.rb index ca67338..6e2f214 100644 --- a/test/chromium/test_pdf.rb +++ b/test/chromium/test_pdf.rb @@ -17,11 +17,24 @@ def test_generate_pdf_calls_chrome_print def test_chrome_print_calls_file_open job = TestGeneratePdfJob.new - File.stub :open, :ran do - result = job.send(:chrome_print, 'chrome', 'url', 'name', 'path', ['argument']) do |_file, filename| - assert_equal 'name', filename + Kernel.stub :system, true do + File.stub :open, :ran do + result = job.send(:chrome_print, 'chrome', 'url', 'name', 'path', ['argument']) do |_file, filename| + assert_equal 'name', filename + end + assert_equal :ran, result + end + end + end + + def test_generate_pdf_yields_to_block + job = TestGeneratePdfJob.new + Kernel.stub :system, true do + File.stub :open, :ran do + job.generate_pdf!('filename', 'url') do |_file, filename| + assert_equal 'filename', filename + end end - assert_equal :ran, result end end end From a732cbeb9dcb71ea46a85a64c6c2c49ea896ab3b Mon Sep 17 00:00:00 2001 From: Michael Hale Date: Fri, 23 Jan 2026 17:22:29 -0500 Subject: [PATCH 2/3] fix key --- .../chromium/pdf/install/templates/app/jobs/generate_pdf_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/chromium/pdf/install/templates/app/jobs/generate_pdf_job.rb b/lib/generators/chromium/pdf/install/templates/app/jobs/generate_pdf_job.rb index 0b75bdf..b49cc2e 100644 --- a/lib/generators/chromium/pdf/install/templates/app/jobs/generate_pdf_job.rb +++ b/lib/generators/chromium/pdf/install/templates/app/jobs/generate_pdf_job.rb @@ -12,7 +12,7 @@ class GeneratePdfJob < ApplicationJob good_job_control_concurrency_with( enqueue_limit: 1, perform_limit: 1, - key: arguments.first.id + key: -> { "generate_pdf_#{arguments.first.id}" } ) around_perform do |_job, block| From c5f81d4dd6b27f839aedb4ef904cf2e0b973f57d Mon Sep 17 00:00:00 2001 From: Michael Hale Date: Fri, 23 Jan 2026 17:50:29 -0500 Subject: [PATCH 3/3] more default arguments --- lib/chromium/pdf.rb | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/chromium/pdf.rb b/lib/chromium/pdf.rb index 6f84a4f..7e4f9b5 100644 --- a/lib/chromium/pdf.rb +++ b/lib/chromium/pdf.rb @@ -8,19 +8,28 @@ module Chromium module Pdf extend ActiveSupport::Concern + DEFAULT_CHROME_ARGUMENTS = [ + '--headless', + '--disable-gpu', + '--no-pdf-header-footer', + '--run-all-compositor-stages-before-draw', + '--no-sandbox', + '--disable-dev-shm-usage', + '--disable-background-networking' + ].freeze + ## # @param unescaped_filename [String] The filename to save the PDF as. # @param print_url [String] The URL of the page you want to be processed. # @param arguments [Array] An array of arguments to pass to the Chrome binary. # @yield [file, filename] Yields the file object and the filename to the block. # - def generate_pdf!(unescaped_filename, print_url, arguments: ['--headless --disable-gpu'], &block) + def generate_pdf!(unescaped_filename, print_url, arguments: DEFAULT_CHROME_ARGUMENTS, &block) chrome_path = ENV.fetch('GOOGLE_CHROME_BIN', nil) filename = unescaped_filename.gsub('&', 'and') Dir.mktmpdir do |path| filepath = "#{path}/#{filename}" - chrome_print chrome_path, print_url, filename, filepath, arguments, &block end end