htmlconverter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +15 -0
- data/PostInstall.txt +2 -0
- data/README.rdoc +47 -0
- data/Rakefile +26 -0
- data/bin/htmlconverter +19 -0
- data/htmlconverter.gemspec +37 -0
- data/lib/htmlconverter.rb +15 -0
- data/lib/htmlconverter/base.rb +103 -0
- data/lib/htmlconverter/renderer/base.rb +29 -0
- data/lib/htmlconverter/renderer/pdf.rb +16 -0
- data/lib/htmlconverter/renderer/ps.rb +17 -0
- data/script/console +10 -0
- data/test/test_helper.rb +3 -0
- data/test/test_htmlconverter.rb +11 -0
- metadata +83 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
PostInstall.txt
|
4
|
+
README.rdoc
|
5
|
+
Rakefile
|
6
|
+
bin/htmlconverter
|
7
|
+
htmlconverter.gemspec
|
8
|
+
lib/htmlconverter.rb
|
9
|
+
lib/htmlconverter/base.rb
|
10
|
+
lib/htmlconverter/renderer/base.rb
|
11
|
+
lib/htmlconverter/renderer/pdf.rb
|
12
|
+
lib/htmlconverter/renderer/ps.rb
|
13
|
+
script/console
|
14
|
+
test/test_helper.rb
|
15
|
+
test/test_htmlconverter.rb
|
data/PostInstall.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
= htmlConverter
|
2
|
+
|
3
|
+
* http://github.com/dcu/htmlconverter
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Convert HTML to PDF, PS, SVG and PNG
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* unknown
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
FIX (code sample of usage)
|
16
|
+
|
17
|
+
== REQUIREMENTS:
|
18
|
+
|
19
|
+
|
20
|
+
== INSTALL:
|
21
|
+
|
22
|
+
* install qt ruby 4
|
23
|
+
|
24
|
+
== LICENSE:
|
25
|
+
|
26
|
+
(The MIT License)
|
27
|
+
|
28
|
+
Copyright (c) 2009 FIXME full name
|
29
|
+
|
30
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
31
|
+
a copy of this software and associated documentation files (the
|
32
|
+
'Software'), to deal in the Software without restriction, including
|
33
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
34
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
35
|
+
permit persons to whom the Software is furnished to do so, subject to
|
36
|
+
the following conditions:
|
37
|
+
|
38
|
+
The above copyright notice and this permission notice shall be
|
39
|
+
included in all copies or substantial portions of the Software.
|
40
|
+
|
41
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
42
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
43
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
44
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
45
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
46
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
47
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/htmlconverter'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
# Hoe.plugin :website
|
9
|
+
# Hoe.plugin :cucumberfeatures
|
10
|
+
|
11
|
+
# Generate all the Rake tasks
|
12
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
|
+
$hoe = Hoe.spec 'htmlconverter' do
|
14
|
+
self.developer 'David Cuadrado', 'krawek@gmail.com'
|
15
|
+
self.post_install_message = 'PostInstall.txt'
|
16
|
+
self.rubyforge_name = self.name
|
17
|
+
# self.extra_deps = [['activesupport','>= 2.0.2']]
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'newgem/tasks'
|
22
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
23
|
+
|
24
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
25
|
+
# remove_task :default
|
26
|
+
# task :default => [:spec, :features]
|
data/bin/htmlconverter
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.dirname(__FILE__) + "/../lib"
|
4
|
+
|
5
|
+
require 'htmlconverter'
|
6
|
+
require 'open-uri'
|
7
|
+
|
8
|
+
if ARGV[0].nil?
|
9
|
+
$stderr.puts "Usage: htmlconverter http://google.com [google_page.pdf]"
|
10
|
+
exit 1
|
11
|
+
end
|
12
|
+
|
13
|
+
converter = HtmlConverter::Base.new
|
14
|
+
s = converter.url_to(ARGV[0], :pdf)
|
15
|
+
|
16
|
+
File.open(ARGV[1]||"output.pdf", "wb") do |f|
|
17
|
+
f.write(s)
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{htmlconverter}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["David Cuadrado"]
|
9
|
+
s.date = %q{2009-10-06}
|
10
|
+
s.default_executable = %q{htmlconverter}
|
11
|
+
s.description = %q{Convert HTML to PDF, PS, SVG and PNG}
|
12
|
+
s.email = ["krawek@gmail.com"]
|
13
|
+
s.executables = ["htmlconverter"]
|
14
|
+
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt"]
|
15
|
+
s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "bin/htmlconverter", "htmlconverter.gemspec", "lib/htmlconverter.rb", "lib/htmlconverter/base.rb", "lib/htmlconverter/renderer/base.rb", "lib/htmlconverter/renderer/pdf.rb", "lib/htmlconverter/renderer/ps.rb", "script/console", "test/test_helper.rb", "test/test_htmlconverter.rb"]
|
16
|
+
s.homepage = %q{http://github.com/dcu/htmlconverter}
|
17
|
+
s.post_install_message = %q{PostInstall.txt}
|
18
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.rubyforge_project = %q{htmlconverter}
|
21
|
+
s.rubygems_version = %q{1.3.5}
|
22
|
+
s.summary = %q{Convert HTML to PDF, PS, SVG and PNG}
|
23
|
+
s.test_files = ["test/test_htmlconverter.rb", "test/test_helper.rb"]
|
24
|
+
|
25
|
+
if s.respond_to? :specification_version then
|
26
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
27
|
+
s.specification_version = 3
|
28
|
+
|
29
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
30
|
+
s.add_development_dependency(%q<hoe>, [">= 2.3.3"])
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<hoe>, [">= 2.3.3"])
|
33
|
+
end
|
34
|
+
else
|
35
|
+
s.add_dependency(%q<hoe>, [">= 2.3.3"])
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'Qt'
|
5
|
+
require 'qtwebkit'
|
6
|
+
require 'tmpdir'
|
7
|
+
|
8
|
+
require 'htmlconverter/base'
|
9
|
+
require 'htmlconverter/renderer/base'
|
10
|
+
require 'htmlconverter/renderer/pdf'
|
11
|
+
require 'htmlconverter/renderer/ps'
|
12
|
+
|
13
|
+
module HtmlConverter
|
14
|
+
VERSION = '0.0.1'
|
15
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module HtmlConverter
|
2
|
+
|
3
|
+
class Base
|
4
|
+
def initialize
|
5
|
+
end
|
6
|
+
|
7
|
+
# HtmlConverter::Base.new.data_to("hello, world", :pdf)
|
8
|
+
def data_to(data, format = :pdf)
|
9
|
+
render_content(data, self.create_renderer(format))
|
10
|
+
end
|
11
|
+
|
12
|
+
# HtmlConverter::Base.new.url_to("http://google.com", :pdf)
|
13
|
+
def url_to(url, format = :pdf)
|
14
|
+
render_url(url, self.create_renderer(format))
|
15
|
+
end
|
16
|
+
|
17
|
+
# HtmlConverter::Base.new.render_content("hello, world", HtmlConverter::Renderer::PDF.new)
|
18
|
+
def render_content(source, renderer)
|
19
|
+
WebRenderer.new(renderer).render_content(read_content(source))
|
20
|
+
end
|
21
|
+
|
22
|
+
# HtmlConverter::Base.new.render_url("http://google.com", HtmlConverter::Renderer::PDF.new)
|
23
|
+
def render_url(url, renderer)
|
24
|
+
WebRenderer.new(renderer).render_url(url)
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
# format
|
29
|
+
# - :pdf
|
30
|
+
# - :svg
|
31
|
+
# - :png
|
32
|
+
# - :ps
|
33
|
+
def create_renderer(format)
|
34
|
+
case format.to_s
|
35
|
+
when "pdf"
|
36
|
+
HtmlConverter::Renderer::PDF.new
|
37
|
+
when "ps"
|
38
|
+
HtmlConverter::Renderer::PS.new
|
39
|
+
else
|
40
|
+
raise StandardError, "Uknown format: #{format}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def read_content(source)
|
45
|
+
if source.kind_of?(IO)
|
46
|
+
source.read
|
47
|
+
elsif File.exist?(source)
|
48
|
+
File.read(source)
|
49
|
+
else
|
50
|
+
source
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class WebRenderer < Qt::Object
|
56
|
+
slots 'render()', "onLoadFinished(bool)"
|
57
|
+
signals 'readyToRender()'
|
58
|
+
|
59
|
+
attr_accessor :content
|
60
|
+
|
61
|
+
def initialize(renderer)
|
62
|
+
super()
|
63
|
+
|
64
|
+
@app = Qt::Application.new([])
|
65
|
+
@page = Qt::WebPage.new
|
66
|
+
@renderer = renderer
|
67
|
+
|
68
|
+
Qt::Object.connect(@page, SIGNAL("loadFinished(bool)"),
|
69
|
+
self, SLOT("onLoadFinished(bool)"))
|
70
|
+
Qt::Object.connect(self, SIGNAL("readyToRender()"),
|
71
|
+
self, SLOT("render()"))
|
72
|
+
end
|
73
|
+
|
74
|
+
def onLoadFinished(ok)
|
75
|
+
Qt::Timer::singleShot(0, self, SIGNAL("readyToRender()"))
|
76
|
+
end
|
77
|
+
|
78
|
+
def render_url(url)
|
79
|
+
@page.mainFrame.load(Qt::Url.new(url))
|
80
|
+
@app.exec
|
81
|
+
@content
|
82
|
+
end
|
83
|
+
|
84
|
+
def render_content(content)
|
85
|
+
@page.mainFrame.setContent(Qt::ByteArray.new(content))
|
86
|
+
@app.exec
|
87
|
+
@content
|
88
|
+
end
|
89
|
+
|
90
|
+
def render
|
91
|
+
raise "Already rendered" if @app.nil?
|
92
|
+
|
93
|
+
@content = @renderer.render(@page)
|
94
|
+
cleanup
|
95
|
+
end
|
96
|
+
|
97
|
+
private
|
98
|
+
def cleanup
|
99
|
+
@app.exit
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module HtmlConverter
|
2
|
+
module Renderer
|
3
|
+
|
4
|
+
class Base
|
5
|
+
def initialize
|
6
|
+
end
|
7
|
+
|
8
|
+
def render(page)
|
9
|
+
raise NotImplementedError
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
def generate(&block)
|
14
|
+
filename = Dir.tmpdir+"/htmlconverter.#{rand(256)}.#{rand(256)}.#{rand(256)}.#{rand(256)}"
|
15
|
+
cont = nil
|
16
|
+
|
17
|
+
yield filename
|
18
|
+
|
19
|
+
if File.exist?(filename)
|
20
|
+
cont = File.read(filename)
|
21
|
+
File.unlink(filename)
|
22
|
+
end
|
23
|
+
|
24
|
+
cont
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module HtmlConverter
|
2
|
+
module Renderer
|
3
|
+
|
4
|
+
class PDF < HtmlConverter::Renderer::Base
|
5
|
+
def render(page)
|
6
|
+
printer = Qt::Printer.new(Qt::Printer::HighResolution)
|
7
|
+
generate do |filename|
|
8
|
+
printer.setOutputFileName(filename)
|
9
|
+
printer.setOutputFormat(Qt::Printer::PdfFormat)
|
10
|
+
page.mainFrame.print(printer)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module HtmlConverter
|
2
|
+
module Renderer
|
3
|
+
|
4
|
+
class PS < HtmlConverter::Renderer::Base
|
5
|
+
def render(page)
|
6
|
+
printer = Qt::Printer.new(Qt::Printer::HighResolution)
|
7
|
+
generate do |filename|
|
8
|
+
printer.setOutputFileName(filename)
|
9
|
+
printer.setOutputFormat(Qt::Printer::PostScriptFormat)
|
10
|
+
|
11
|
+
page.mainFrame.print(printer)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/htmlconverter.rb'}"
|
9
|
+
puts "Loading htmlconverter gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: htmlconverter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- David Cuadrado
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-06 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.3
|
24
|
+
version:
|
25
|
+
description: Convert HTML to PDF, PS, SVG and PNG
|
26
|
+
email:
|
27
|
+
- krawek@gmail.com
|
28
|
+
executables:
|
29
|
+
- htmlconverter
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- History.txt
|
34
|
+
- Manifest.txt
|
35
|
+
- PostInstall.txt
|
36
|
+
files:
|
37
|
+
- History.txt
|
38
|
+
- Manifest.txt
|
39
|
+
- PostInstall.txt
|
40
|
+
- README.rdoc
|
41
|
+
- Rakefile
|
42
|
+
- bin/htmlconverter
|
43
|
+
- htmlconverter.gemspec
|
44
|
+
- lib/htmlconverter.rb
|
45
|
+
- lib/htmlconverter/base.rb
|
46
|
+
- lib/htmlconverter/renderer/base.rb
|
47
|
+
- lib/htmlconverter/renderer/pdf.rb
|
48
|
+
- lib/htmlconverter/renderer/ps.rb
|
49
|
+
- script/console
|
50
|
+
- test/test_helper.rb
|
51
|
+
- test/test_htmlconverter.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/dcu/htmlconverter
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message: PostInstall.txt
|
57
|
+
rdoc_options:
|
58
|
+
- --main
|
59
|
+
- README.rdoc
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project: htmlconverter
|
77
|
+
rubygems_version: 1.3.5
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Convert HTML to PDF, PS, SVG and PNG
|
81
|
+
test_files:
|
82
|
+
- test/test_htmlconverter.rb
|
83
|
+
- test/test_helper.rb
|