Skip to content

Commit

Permalink
Add support for multiple documents
Browse files Browse the repository at this point in the history
  • Loading branch information
bibstha committed Sep 19, 2018
1 parent 45653f9 commit 2a3be0f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,37 @@ def pdf_from_string(string, options = {})
string_file.close! if string_file
end

def pdf_from_url(url, options = {})
def pdf_from_multiple_strings(strings, options = {})
strings = Array(strings)
options = options.dup
options.merge!(WickedPdf.config) { |_key, option, _config| option }
string_files = strings.map do |string|
string_file = WickedPdfTempfile.new('wicked_pdf.html', options[:temp_path])
string_file.binmode
string_file.write(string)
string_file.close
string_file
end

string_file_paths = string_files.map { |string_file| string_file.path }
pdf = pdf_from_multiple_html_files(string_file_paths, options)
pdf
end

def pdf_from_multiple_html_files(filepaths, options = {})
urls = filepaths.map { |filepath| "file:///#{filepath}" }
pdf_from_url(urls, options)
end

def pdf_from_url(urls, options = {})
urls = Array(urls)
# 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 << url
urls.each { |url| command << url }
command << generated_pdf_file.path.to_s

print_command(command.inspect) if in_development_mode?
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/document_with_short_line.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div>
An HTML file with a short line.
</div>
</body>
</html>







35 changes: 35 additions & 0 deletions test/unit/wicked_pdf_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test_helper'
require "pdf/inspector"

WickedPdf.config = { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf' }
HTML_DOCUMENT = '<html><body>Hello World</body></html>'.freeze
Expand Down Expand Up @@ -30,6 +31,9 @@ def setup
assert pdf.start_with?('%PDF-1.4')
assert pdf.rstrip.end_with?('%%EOF')
assert pdf.length > 100

page_analysis = PDF::Inspector::Page.analyze(pdf)
assert_equal 1, page_analysis.pages.size
end

test 'should generate PDF from html document with long lines' do
Expand All @@ -46,6 +50,37 @@ def setup
assert pdf.start_with?('%PDF-1.4')
assert pdf.rstrip.end_with?('%%EOF')
assert pdf.length > 100

page_analysis = PDF::Inspector::Page.analyze(pdf)
assert_equal 2, page_analysis.pages.size
end

test 'should generate PDF from multiple html document strings' do
pdf = @wp.pdf_from_multiple_strings [HTML_DOCUMENT, HTML_DOCUMENT]
assert pdf.start_with?('%PDF-1.4')
assert pdf.rstrip.end_with?('%%EOF')
assert pdf.length > 100

page_analysis = PDF::Inspector::Page.analyze(pdf)
assert_equal 2, page_analysis.pages.size
end

test 'should generate PDF from multiple existing HTML files without converting it to string' do
filepath1 = File.join(Dir.pwd, 'test/fixtures/document_with_long_line.html')
filepath2 = File.join(Dir.pwd, 'test/fixtures/document_with_short_line.html')
pdf = @wp.pdf_from_multiple_html_files([filepath1, filepath2])

assert pdf.start_with?('%PDF-1.4')
assert pdf.rstrip.end_with?('%%EOF')
assert pdf.length > 100

text_analysis = PDF::Inspector::Text.analyze(pdf)
strings = text_analysis.strings.join.gsub("\t", " ")
assert strings.include?("Lorem ipsum")
assert strings.include?("An HTML file with a short line.")

page_analysis = PDF::Inspector::Page.analyze(pdf)
assert_equal 3, page_analysis.pages.size
end

test 'should raise exception when no path to wkhtmltopdf' do
Expand Down
1 change: 1 addition & 0 deletions wicked_pdf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ DESC
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'mocha', '= 1.3'
spec.add_development_dependency 'test-unit'
spec.add_development_dependency 'pdf-inspector'
end

0 comments on commit 2a3be0f

Please sign in to comment.