Skip to content

Commit

Permalink
Merge pull request mileszs#417 from jhollinger/url-support
Browse files Browse the repository at this point in the history
Add WickedPdf#pdf_from_url
  • Loading branch information
unixmonkey committed Jun 5, 2015
2 parents 5c06b4c + fbbd185 commit d192b14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')
# Path must be absolute path
pdf = WickedPdf.new.pdf_from_html_file('/your/absolute/path/here')

# create a pdf from a URL
pdf = WickedPdf.new.pdf_from_url('https://github.com/mileszs/wicked_pdf')

# create a pdf from string using templates, layouts and content option for header or footer
WickedPdf.new.pdf_from_string(
render_to_string('templates/pdf.html.erb', layout: 'pdfs/layout_pdf'),
Expand Down
38 changes: 21 additions & 17 deletions lib/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,33 @@ def initialize(wkhtmltopdf_binary_path = nil)
end

def pdf_from_html_file(filepath, options = {})
pdf_from_url("file:///#{filepath}", options)
end

def pdf_from_string(string, options = {})
# merge in global config options
options.merge!(WickedPdf.config) {|key, option, config| option}
string_file = WickedPdfTempfile.new("wicked_pdf.html", options[:temp_path])
string_file.binmode
string_file.write(string)
string_file.close

pdf = pdf_from_html_file(string_file.path, options)
pdf
rescue => e
raise "Error: #{e}"
ensure
string_file.close! if string_file
end

def pdf_from_url(url, options = {})
# merge in global config 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 << '-q' unless on_windows? # suppress errors on stdout
command += parse_options(options)
command << "file:///#{filepath}"
command << url
command << generated_pdf_file.path.to_s

print_command(command.inspect) if in_development_mode?
Expand All @@ -73,22 +93,6 @@ def pdf_from_html_file(filepath, options = {})
generated_pdf_file.close! if generated_pdf_file && !return_file
end

def pdf_from_string(string, options = {})
# merge in global config options
options.merge!(WickedPdf.config) {|key, option, config| option}
string_file = WickedPdfTempfile.new("wicked_pdf.html", options[:temp_path])
string_file.binmode
string_file.write(string)
string_file.close

pdf = pdf_from_html_file(string_file.path, options)
pdf
rescue => e
raise "Error: #{e}"
ensure
string_file.close! if string_file
end

private

def in_development_mode?
Expand Down

0 comments on commit d192b14

Please sign in to comment.