Skip to content

Commit

Permalink
Merge pull request #1090 from kevinnio/kevinnio/add-config-block
Browse files Browse the repository at this point in the history
Add WickedPdf.configure method
  • Loading branch information
unixmonkey committed Feb 10, 2024
2 parents fe3f8b3 + 7a749c2 commit 0bc0f50
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ You can see what flags are supported for the current version in [wkhtmltopdf's a
If your wkhtmltopdf executable is not on your webserver's path, you can configure it in an initializer:

```ruby
WickedPdf.config = {
exe_path: '/usr/local/bin/wkhtmltopdf',
enable_local_file_access: true
WickedPdf.configure do |c|
c.exe_path = '/usr/local/bin/wkhtmltopdf',
c.enable_local_file_access = true
}
```

Expand Down
14 changes: 7 additions & 7 deletions generators/wicked_pdf/templates/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
#
# https://github.com/mileszs/wicked_pdf/blob/master/README.md

WickedPdf.config = {
WickedPdf.configure do |config|
# Path to the wkhtmltopdf executable: This usually isn't needed if using
# one of the wkhtmltopdf-binary family of gems.
# exe_path: '/usr/local/bin/wkhtmltopdf',
# config.exe_path = '/usr/local/bin/wkhtmltopdf',
# or
# exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
# config.exe_path = Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')

# Needed for wkhtmltopdf 0.12.6+ to use many wicked_pdf asset helpers
# enable_local_file_access: true,
# config.enable_local_file_access = true,

# Layout file to be used for all PDFs
# (but can be overridden in `render :pdf` calls)
# layout: 'pdf.html',
# config.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,
}
# config.use_xvfb = true,
end
19 changes: 18 additions & 1 deletion lib/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,27 @@
class WickedPdf
DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
@@config = {}
cattr_accessor :config
cattr_accessor :config, :silence_deprecations

include Progress

def self.config=(config)
::Kernel.warn 'WickedPdf.config= is deprecated and will be removed in future versions. Use WickedPdf.configure instead.' unless @@silence_deprecations

@@config = config
end

def self.configure
config = OpenStruct.new(@@config)
yield config

@@config.merge! config.to_h
end

def self.clear_config
@@config = {}
end

def initialize(wkhtmltopdf_binary_path = nil)
@binary = Binary.new(wkhtmltopdf_binary_path, DEFAULT_BINARY_VERSION)
end
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'wicked_pdf'

Rails.backtrace_cleaner.remove_silencers!
WickedPdf.silence_deprecations = true

if (assets_dir = Rails.root.join('app/assets')) && File.directory?(assets_dir)
# Copy CSS file
Expand Down
17 changes: 17 additions & 0 deletions test/unit/wicked_pdf_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ def setup
@wp = WickedPdf.new
end

test 'should update config through .configure class method' do
WickedPdf.configure do |c|
c.test = 'foobar'
end

assert WickedPdf.config == { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf', :test => 'foobar' }
end

test 'should clear config through .clear_config class method' do
backup_config = WickedPdf.config

WickedPdf.clear_config
assert WickedPdf.config == {}

WickedPdf.config = backup_config
end

test 'should generate PDF from html document' do
pdf = @wp.pdf_from_string HTML_DOCUMENT
assert pdf.start_with?('%PDF-1.4')
Expand Down

0 comments on commit 0bc0f50

Please sign in to comment.