Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions lib/chromium/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,39 @@ 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<String>] 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: 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
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
21 changes: 17 additions & 4 deletions test/chromium/test_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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