Module: Html2Pdf
- Defined in:
- lib/html2pdf/cli.rb,
lib/html2pdf/version.rb,
lib/html2pdf/html2pdf.rb,
lib/html2pdf/configuration.rb
Defined Under Namespace
Classes: CLI, Configuration
Constant Summary collapse
- VERSION =
"0.2.1"
Class Attribute Summary collapse
-
.configuration ⇒ Object
Configure Pdfs2Pdf someplace sensible, like config/initializers/html2pdf.rb.
Class Method Summary collapse
- .configure {|configuration| ... } ⇒ Object
-
.softwares_installed? ⇒ Boolean
Check and return if the ‘wkhtmltopdf’ is available.
-
.to_pdf(filename) ⇒ Object
Convert ‘*.xhtml’ or ‘*.html’ to pdf.
-
.to_pdfs(files) ⇒ Object
Batch convert to pdf using ‘wkhtmltopdf` tool.
Class Attribute Details
.configuration ⇒ Object
Configure Pdfs2Pdf someplace sensible, like config/initializers/html2pdf.rb
Html2Pdf.configure do |config|
# set appropriate options
config.options[:wkhtmltopdf] = '/usr/bin/wkhtmltopdf'
config.options[:page_settings] = [ "--margin-top 4",
"--margin-bottom 4",
..
]
end
41 42 43 |
# File 'lib/html2pdf/configuration.rb', line 41 def configuration @configuration end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
45 46 47 |
# File 'lib/html2pdf/configuration.rb', line 45 def configure yield(configuration) end |
.softwares_installed? ⇒ Boolean
Check and return if the ‘wkhtmltopdf’ is available
37 38 39 |
# File 'lib/html2pdf/html2pdf.rb', line 37 def softwares_installed? AgileUtils::Helper.which("wkhtmltopdf") end |
.to_pdf(filename) ⇒ Object
Convert ‘*.xhtml’ or ‘*.html’ to pdf
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/html2pdf/html2pdf.rb', line 21 def to_pdf(filename) fail "Invalid input file #{filename}" unless File.exist?(filename) wkhtmltopdf = Html2Pdf.configuration.[:wkhtmltopdf] page_settings = Html2Pdf.configuration.[:page_settings] command = [ wkhtmltopdf, *page_settings, filename, "#{filename}.pdf", "> /dev/null" ] _stdin, _stderr, status = Open3.capture3(command.join(" ")) fail "Problem processing #{filename}" unless status.success? end |
.to_pdfs(files) ⇒ Object
Batch convert to pdf using ‘wkhtmltopdf` tool
11 12 13 14 15 16 |
# File 'lib/html2pdf/html2pdf.rb', line 11 def to_pdfs(files) files.each_with_index do |file, index| puts "Convert file #{index + 1} of #{files.size} : #{file}" to_pdf(file) end end |