Skip to content

Commit

Permalink
WkHtmlToPdf backend
Browse files Browse the repository at this point in the history
  • Loading branch information
David FRANCOIS committed Jul 4, 2010
1 parent 4139dad commit 222e8f2
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 8 deletions.
14 changes: 13 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
= Documentalist
Rails gem for easily managing documents, converting them from a format to another,
and merging data into ODF templates.

Rails gem for talking to OpenOffice and merging data into OpenDocument templates using an ERB-like syntax.
= Checking for external dependencies
Documentalist assembles various moving parts that are necessary to perform the actual
conversions, you can run run the documentalist:backends:checks task to see if these
dependencies are met on your system, if it isn't the case it will give you some tips
on how to fix it.

= Examples
Documentalist.convert('/home/somefile.doc', :to => '/home/someotherfile.pdf')

= Installation
Require the gem in your Rails or Ruby application and you should be good to go !
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'rake'
require 'echoe' rescue nil

if Object.const_defined? :Echoe
Echoe.new('documentalist', '0.1.1') do |p|
Echoe.new('documentalist', '0.1.2') do |p|
p.description = "The smooth document management experience, usable as a Rails gem plugin or standalone in any ruby application"
p.url = "http://github.com/davout/documentalist"
p.author = "David FRANCOIS"
Expand Down
3 changes: 0 additions & 3 deletions lib/backends/net_pbm.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# To change this template, choose Tools | Templates
# and open the template in the editor.

module Documentalist
module NetPBM
include Documentalist::Dependencies
Expand Down
11 changes: 11 additions & 0 deletions lib/backends/wkhtmltopdf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Documentalist
module WkHtmlToPdf
include Documentalist::Dependencies

depends_on_binaries! "wkhtmltopdf" => "install wkhtmltopdf package"

def self.convert(file, options)
system "wkhtmltopdf -q #{file} #{options[:to]}"
end
end
end
7 changes: 4 additions & 3 deletions lib/documentalist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ def self.convert(file, options={})
options[:from_format] = File.extname(file).gsub(/\./, "").to_sym

backend = backend_for_conversion(options[:from_format], options[:to_format])
converted = backend.convert(file, options)
backend.convert(file, options)

yield(converted) if block_given?
converted
yield(options[:to]) if block_given?
options[:to]
end

def self.extract_text(file)
converted = convert(file, :to_format => :txt)

if converted and File.exist?(converted)
text = Kconv.toutf8(File.open(converted).read)
FileUtils.rm(converted)
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/fixture_002.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Title</title>
</head>
<body>
<p>
Test content
</p>
</body>
</html>
21 changes: 21 additions & 0 deletions test/wkhtmltopdf_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'test_helper'

class WkHtmlToPdfTest < Test::Unit::TestCase
def test_right_backend_is_picked
assert_equal Documentalist.backend_for_conversion("test.html", "test.pdf"),
Documentalist::WkHtmlToPdf,
"Wrong backend picked"
end

def test_conversion
temp_file = File.join(Dir.tmpdir, "#{rand(10**9)}.pdf")

Documentalist.convert(fixture_002, :to => temp_file)
assert File.exists?(temp_file), "No converted PDF created"

assert_match /Test content/, Documentalist.extract_text(temp_file)

FileUtils.rm temp_file
assert !File.exists?(temp_file), "We didn't clean up properly"
end
end

0 comments on commit 222e8f2

Please sign in to comment.