Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option use_xvfb to emulate an X server #909

Merged
merged 1 commit into from
Jun 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions generators/wicked_pdf/templates/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
# Layout file to be used for all PDFs
# (but can be overridden in `render :pdf` calls)
# layout: 'pdf.html',

# Using wkhtmltopdf without an X server can be achieved by enabling the
# 'use_xvfb' flag. This will wrap all wkhtmltopdf commands around the
# 'xvfb-run' command, in order to simulate an X server.
#
# use_xvfb: true,
}
14 changes: 13 additions & 1 deletion lib/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def pdf_from_url(url, options = {})
options.merge!(WickedPdf.config) { |_key, option, _config| option }
generated_pdf_file = WickedPdfTempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
command = [@exe_path]
command.unshift(find_xvfb_run_binary_path) if options[:use_xvfb]
command += parse_options(options)
command << url
command << generated_pdf_file.path.to_s
Expand Down Expand Up @@ -326,9 +327,13 @@ def parse_others(options)
r
end

def find_wkhtmltopdf_binary_path
def possible_binary_locations
possible_locations = (ENV['PATH'].split(':') + %w[/usr/bin /usr/local/bin]).uniq
possible_locations += %w[~/bin] if ENV.key?('HOME')
end

def find_wkhtmltopdf_binary_path
possible_locations = possible_binary_locations
exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
exe_path ||= begin
detected_path = (defined?(Bundler) ? Bundler.which('wkhtmltopdf') : `which wkhtmltopdf`).chomp
Expand All @@ -339,4 +344,11 @@ def find_wkhtmltopdf_binary_path
exe_path ||= possible_locations.map { |l| File.expand_path("#{l}/#{EXE_NAME}") }.find { |location| File.exist?(location) }
exe_path || ''
end

def find_xvfb_run_binary_path
possible_locations = possible_binary_locations
path = possible_locations.map { |l| File.expand_path("#{l}/xvfb-run") }.find { |location| File.exist?(location) }
raise StandardError.new('Could not find binary xvfb-run on the system.') unless path
path
end
end