html2pdf 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86055351d30cb9e5bd6264f6204ccdddd2bb98f0
4
+ data.tar.gz: f23ee237e3db6277ae2f6f69f92d2113be1fc8e3
5
+ SHA512:
6
+ metadata.gz: 9e4d6713bffc9498dc839303d0a24ded97615d3ea18b4f947e22263ade100c17ee8c999491bc2cf36bbd4cd245933428f1297dc8c4573f94b1a7bc1735f76ee4
7
+ data.tar.gz: 53877751901e76b3046b57b1844e2f09a056f4312fb3b55f2c9192f36657d1862a2923352b4c64328f1b70ed3de5c29f5f9f7962076403c18a2cd49ecf7f4ed7
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ # ctags
19
+ tags
20
+ # generated file
21
+ output.tar.gz
data/.rubocop.yml ADDED
@@ -0,0 +1 @@
1
+ inherit_from: rubocop-todo.yml
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in code_exporter.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,15 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+ guard 'minitest' do
4
+
5
+ # with Minitest::Unit
6
+ watch(%r|^test/(.*)\/?test_(.*)\.rb|)
7
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
8
+ watch(%r|^test/test_helper\.rb|) { "test" }
9
+
10
+ # with Minitest::Spec
11
+ # watch(%r|^spec/(.*)_spec\.rb|)
12
+ # watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
13
+ # watch(%r|^spec/spec_helper\.rb|) { "spec" }
14
+
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Burin Choomnuan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ ## Html2Pdf
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/html2pdf.svg)](http://badge.fury.io/rb/html2pdf)
4
+
5
+ Convert multiple html or xhtml files to pdfs using the power of
6
+ [Ghostscript][] and [Wkhtmltopdf][].
7
+
8
+ ### Installation
9
+
10
+ #### Mandatory Requirement
11
+
12
+ - [Ghostscript][]
13
+ - [Wkhtmltopdf][]
14
+
15
+ This gem is built on top of my other gems
16
+
17
+ - [agile_utils][] gem
18
+ - [code_lister][] gem
19
+
20
+ ### Dependencies
21
+
22
+ - wkhtmltopdf (Linux/OSX)
23
+ - Ghostscript (Linux/OSX)
24
+ - OSX or Linux (no Windows sorry)
25
+
26
+ ## Usage
27
+ ```sh
28
+ gem install html2pdf
29
+ ```
30
+ For list of usage type
31
+
32
+ ```sh
33
+ html2pdf
34
+ ```
35
+ Which should give the following options
36
+ ```
37
+ Usage:
38
+ html2pdf export [OPTIONS]
39
+
40
+ Options:
41
+ -b, [--base-dir=BASE_DIR] # Base directory
42
+ # Default: . (current directory)
43
+ -n, [--inc-words=one two three] # List of words to be included in the result
44
+ -x, [--exc-words=one two three] # List of words to be excluded from the result
45
+ -i, [--ignore-case], [--no-ignore-case] # Match case insensitively
46
+ # Default: true
47
+ -r, [--recursive], [--no-recursive] # Search for files recursively
48
+ # Default: true
49
+ -v, [--version], [--no-version] # Display version information
50
+ ```
51
+ ### Example Usage
52
+
53
+ - Convert all `xhtml` and `html` from `test/fixtures/samples` directory
54
+ recursively
55
+ ```
56
+ html2pdf -b test/fixtures/samples -e html xhtml -r
57
+ ```
58
+ Before the run
59
+ ```
60
+ test/fixtures/samples/
61
+ ├── demo1_xxx.rb.xhtml
62
+ ├── demo2_xxx.rb.xhtml
63
+ └── sub_dir
64
+ ├── demo3_xxx.rb.xhtml
65
+ └── demo4_xxx.rb.xhtml
66
+ ```
67
+ After the above command is executed:
68
+ ```
69
+ test/fixtures/samples/
70
+ ├── demo1_xxx.rb.xhtml
71
+ ├── demo1_xxx.rb.xhtml.pdf
72
+ ├── demo2_xxx.rb.xhtml
73
+ ├── demo2_xxx.rb.xhtml.pdf
74
+ └── sub_dir
75
+ ├── demo3_xxx.rb.xhtml
76
+ ├── demo3_xxx.rb.xhtml.pdf
77
+ ├── demo4_xxx.rb.xhtml
78
+ └── demo4_xxx.rb.xhtml.pdf
79
+ ```
80
+
81
+ ### Usage Tips
82
+
83
+ This is my personal use case
84
+
85
+ - Use [vim_printer][] which export any source code to list of (x)html files in
86
+ a single tar.gzipped file.
87
+
88
+ - Use the output from [vim_printer][] as input to this program so that you get
89
+ the pdf version of it.
90
+
91
+ - Then combine the generated pdf files using [pdfs2pdf][] gem to produce one pdf file
92
+
93
+ ### Changelogs
94
+
95
+ #### 0.0.1
96
+
97
+ - Initial release
98
+
99
+ ### Contributing
100
+
101
+ 1. Fork it ( http://github.com/<my-github-username>/code_exporter/fork )
102
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
103
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
104
+ 4. Push to the branch (`git push origin my-new-feature`)
105
+ 5. Create new Pull Request
106
+
107
+ [Ghostscript]: http://www.ghostscript.com/
108
+ [Wkhtmltopdf]: http://wkhtmltopdf.org/
109
+ [agile_utils]: https://github.com/agilecreativity/agile_utils
110
+ [code_lister]: https://github.com/agilecreativity/code_lister
111
+ [vim_printer]: https://github.com/agilecreativity/vim_printer
112
+ [pdfs2pdf]: https://github.com/agilecreativity/pdfs2pdf
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ project_name = 'html2pdf'
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "lib/#{project_name}"
7
+ t.test_files = FileList["test/lib/#{project_name}/test_*.rb"]
8
+ t.verbose = true
9
+ end
10
+
11
+ task default: :test
12
+
13
+ task :pry do
14
+ require 'pry'
15
+ require 'awesome_print'
16
+ require_relative './lib/html2pdf'
17
+ include Html2Pdf
18
+ ARGV.clear
19
+ Pry.start
20
+ end
21
+
22
+ require 'rubocop/rake_task'
23
+ desc 'Run RuboCop on the lib directory'
24
+ Rubocop::RakeTask.new(:rubocop) do |task|
25
+ task.patterns = ['lib/**/*.rb']
26
+ # only show the files with failures
27
+ task.formatters = ['files']
28
+ # don't abort rake on failure
29
+ task.fail_on_error = false
30
+ end
data/TODOs.md ADDED
@@ -0,0 +1,13 @@
1
+ ### TODOs
2
+
3
+ - Remove the options that need to be default
4
+ e.g. `--exts xhtml html` should be default and should not be changed by user.
5
+
6
+ - Find minitest related to file-system test so that the code can be hooked to
7
+ [TravisCI][]
8
+
9
+ - Add test using [minitest-filesystem][]
10
+
11
+ - Add more tests
12
+
13
+ [minitest-filesystem]: https://github.com/stefanozanella/minitest-filesystem
data/bin/html2pdf ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/html2pdf'
3
+ include Html2Pdf
4
+ Html2Pdf::CLI.start(ARGV)
data/html2pdf.gemspec ADDED
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'html2pdf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'html2pdf'
8
+ spec.version = Html2Pdf::VERSION
9
+ spec.authors = ['Burin Choomnuan']
10
+ spec.email = ['agilecreativity@gmail.com']
11
+ spec.summary = %q{Export any list of html or xhtml files to pdfs}
12
+ spec.description = %q{Batch export html files to pdfs}
13
+ spec.homepage = 'https://github.com/agilecreativity/html2pdf'
14
+ spec.license = 'MIT'
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency 'thor'
21
+ spec.add_runtime_dependency 'agile_utils', '~> 0.0.5'
22
+ spec.add_runtime_dependency 'code_lister', '~> 0.0.6'
23
+
24
+ spec.add_development_dependency 'awesome_print', '~> 1.2'
25
+ spec.add_development_dependency 'bundler', '~> 1.5'
26
+ spec.add_development_dependency 'gem-ctags', '~> 1.0'
27
+ spec.add_development_dependency 'guard', '~> 2.6'
28
+ spec.add_development_dependency 'guard-minitest', '~> 2.2'
29
+ spec.add_development_dependency 'minitest', '~> 4.2'
30
+ spec.add_development_dependency 'minitest-filesystem', '~> 1.2.0'
31
+ spec.add_development_dependency 'minitest-spec-context', '~> 0.0.3'
32
+ spec.add_development_dependency 'pry', '~> 0.9'
33
+ spec.add_development_dependency 'rake', '~> 10.1'
34
+ spec.add_development_dependency 'rubocop', '~> 0.20'
35
+ spec.add_development_dependency 'yard', '~> 0.8'
36
+
37
+ end
data/lib/html2pdf.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'agile_utils'
2
+ require 'code_lister'
3
+ require_relative './html2pdf/utils'
4
+ require_relative './html2pdf/cli'
5
+ include AgileUtils::Options
6
+ include CodeLister
@@ -0,0 +1,53 @@
1
+ require 'thor'
2
+ require 'tmpdir'
3
+ require 'fileutils'
4
+ require_relative '../html2pdf'
5
+ module Html2Pdf
6
+ class CLI < Thor
7
+ desc 'export', 'export html files to pdfs'
8
+
9
+ method_option *AgileUtils::Options::BASE_DIR
10
+ method_option *AgileUtils::Options::INC_WORDS
11
+ method_option *AgileUtils::Options::EXC_WORDS
12
+ method_option *AgileUtils::Options::IGNORE_CASE
13
+ method_option *AgileUtils::Options::RECURSIVE
14
+ method_option *AgileUtils::Options::VERSION
15
+
16
+ def export
17
+ unless Html2Pdf::Utils.required_softwares?
18
+ fail 'You must have valid `wkhtmltopdf` and `ghostscript` installation'
19
+ end
20
+
21
+ input_files = CodeLister.files base_dir: options[:base_dir],
22
+ exts: %w(html xhtml),
23
+ recursive: true
24
+ elapsed = AgileUtils::FileUtil.time do
25
+ Html2Pdf::Utils.to_pdfs(input_files)
26
+ end
27
+ puts "Convert files to pdfs took #{elapsed} ms"
28
+ end
29
+
30
+ desc 'usage', 'Display usage information'
31
+ def usage
32
+ puts <<-EOT
33
+ Usage:
34
+ html2pdf export [OPTIONS]
35
+
36
+ Options:
37
+ -b, [--base-dir=BASE_DIR] # Base directory
38
+ # Default: /Users/agilecreativity/codes/github/html2pdf
39
+ -n, [--inc-words=one two three] # List of words to be included in the result
40
+ -x, [--exc-words=one two three] # List of words to be excluded from the result
41
+ -i, [--ignore-case], [--no-ignore-case] # Match case insensitively
42
+ # Default: true
43
+ -r, [--recursive], [--no-recursive] # Search for files recursively
44
+ # Default: true
45
+ -v, [--version], [--no-version] # Display version information
46
+
47
+ export html files to pdfs
48
+ EOT
49
+ end
50
+
51
+ default_task :usage
52
+ end
53
+ end
@@ -0,0 +1,63 @@
1
+ require 'open3'
2
+ require 'fileutils'
3
+ require_relative '../html2pdf'
4
+
5
+ module Html2Pdf
6
+ module Utils
7
+ class << self
8
+ # Batch convert to pdf using `wkhtmltopdf` tool
9
+ #
10
+ # @param [Array<String>] files the input file list
11
+ # @param [String] base_dir the base directory
12
+ def to_pdfs(files)
13
+ files.each_with_index do |file, index|
14
+ puts "Convert file #{index + 1} of #{files.size} : #{file}"
15
+ to_pdf(file)
16
+ end
17
+ end
18
+
19
+ # Convert '*.xhtml' or '*.html' to pdf
20
+ #
21
+ # @param filename input filename
22
+ def to_pdf(filename)
23
+ # see: http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html#Footers And Headers
24
+ # - may be only allow "*.html" and "*.xhtml"
25
+ # - allow the options to be passed in so that we can use different theme
26
+ # '--no-background'
27
+ fail "Invalid input file #{filename}" unless File.exist?(filename)
28
+ command = [
29
+ 'wkhtmltopdf',
30
+ '--margin-top 4',
31
+ '--margin-bottom 4',
32
+ '--margin-left 4',
33
+ '--margin-right 4',
34
+ # Note: working correctly but long URL
35
+ '--header-center "[webpage] :: [page]/[topage]"',
36
+ # header section
37
+ # TODO: not yet working properly
38
+ # "--header-center #{filename.gsub(base_dir,File.basename(base_dir))} \"[page]/[topage]\"",
39
+ # "--header-center #{filename} \"[page]/[topage]\"",
40
+ '--header-spacing 1',
41
+ '--header-font-size 8',
42
+ '--header-line',
43
+ # footer section
44
+ '--footer-spacing 1',
45
+ '--footer-font-size 8',
46
+ '--footer-line',
47
+ "#{filename}",
48
+ "#{filename}.pdf",
49
+ '> /dev/null']
50
+ _stdin, _stderr, status = Open3.capture3(command.join(' '))
51
+ # puts "FYI: to_pdf command: #{command.join(' ')}"
52
+ # Note: may be log it and continue
53
+ fail "Problem processing #{filename}" unless status.success?
54
+ end
55
+
56
+ # Check and verify that the proper softwares are available.
57
+ #
58
+ def required_softwares?
59
+ AgileUtils::Helper.which('gs') && AgileUtils::Helper.which('gs')
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module Html2Pdf
2
+ VERSION = '0.0.1'
3
+ end
data/rubocop-todo.yml ADDED
@@ -0,0 +1,84 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2014-04-23 20:34:48 +1000 using RuboCop version 0.20.1.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+ # Offense count: 9
9
+ AmbiguousOperator:
10
+ Enabled: false
11
+
12
+ # Offense count: 1
13
+ # Cop supports --auto-correct.
14
+ # Configuration parameters: PreferredMethods.
15
+ CollectionMethods:
16
+ Enabled: false
17
+
18
+ # Offense count: 1
19
+ CyclomaticComplexity:
20
+ Max: 7
21
+
22
+ # Offense count: 5
23
+ Documentation:
24
+ Enabled: false
25
+
26
+ # Offense count: 1
27
+ # Cop supports --auto-correct.
28
+ EmptyLinesAroundBody:
29
+ Enabled: true
30
+
31
+ # Offense count: 1
32
+ # Cop supports --auto-correct.
33
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
34
+ HashSyntax:
35
+ Enabled: true
36
+
37
+ # Offense count: 4
38
+ # Cop supports --auto-correct.
39
+ LeadingCommentSpace:
40
+ Enabled: true
41
+
42
+ # Offense count: 18
43
+ LineLength:
44
+ Max: 108
45
+
46
+ # Offense count: 8
47
+ # Configuration parameters: CountComments.
48
+ MethodLength:
49
+ Max: 30
50
+
51
+ # Offense count: 1
52
+ # Cop supports --auto-correct.
53
+ SpaceAfterComma:
54
+ Enabled: true
55
+
56
+ # Offense count: 1
57
+ # Cop supports --auto-correct.
58
+ # Configuration parameters: EnforcedStyle, SupportedStyles, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
59
+ SpaceInsideBlockBraces:
60
+ Enabled: true
61
+
62
+ # Offense count: 1
63
+ # Cop supports --auto-correct.
64
+ SpecialGlobalVars:
65
+ Enabled: true
66
+
67
+ # Offense count: 16
68
+ # Cop supports --auto-correct.
69
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
70
+ StringLiterals:
71
+ Enabled: false
72
+
73
+ # Offense count: 1
74
+ UnreachableCode:
75
+ Enabled: true
76
+
77
+ # Offense count: 1
78
+ UselessAssignment:
79
+ Enabled: false
80
+
81
+ # Offense count: 1
82
+ # Cop supports --auto-correct.
83
+ WordArray:
84
+ MinSize: 2