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 support for multiple documents #768

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ AllCops:
TargetRubyVersion: 1.9
Exclude:
- 'test/dummy/**/*'
- 'vendor/**/*'

Metrics/BlockLength:
Exclude:
- 'wicked_pdf.gemspec'

Metrics/ClassLength:
Exclude:
- 'lib/wicked_pdf.rb'

# I'd like wicked_pdf to keep Ruby 1.8 compatibility for now
Style/HashSyntax:
EnforcedStyle: hash_rockets
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ gemfile:
- gemfiles/3.2.gemfile
before_script:
- bundle list
- sudo apt-get install -y wkhtmltopdf
- sudo apt-get install -y xfonts-base xfonts-75dpi
- wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.trusty_amd64.deb
- sudo dpkg -i wkhtmltox_0.12.5-1.trusty_amd64.deb
- sudo apt-get install -f
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
env: WKHTMLTOPDF_BIN=/usr/bin/wkhtmltopdf
env: WKHTMLTOPDF_BIN=/usr/local/bin/wkhtmltopdf
script:
- bundle exec rake
matrix:
Expand Down
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(&: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
9 changes: 9 additions & 0 deletions test/fixtures/document_with_short_line.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!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.tr("\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