wkhtmltopdf for Crystal
Crystal wrapper for C library libwkhtmltox.
wkhtmltopdf and wkhtmltoimage permit to render HTML into PDF and various image formats using the Qt WebKit rendering engine - see wkhtmltopdf.org
Requirements
- libwkhtmltox must be installed
- pkg-config must be available
Installation
- Add this to your application's
shard.yml
:
dependencies:
wkhtmltopdf-crystal:
github: blocknotes/wkhtmltopdf-crystal
- If wkhtmltox library is installed but missing for Crystal compiler: copy wkhtmltox.pc in a pkg-config folder (ex. /usr/local/lib/pkgconfig) or set the environment variable PKG_CONFIG_PATH with the path to wkhtmltox.pc before compiling
- Optinally edit wkhtmltox.pc with the correct path to wkhtmltox (default headers path: /usr/local/include/wkhtmltox)
Usage
HTML to PDF:
require "wkhtmltopdf"
Wkhtmltopdf::WkPdf.new( "test.pdf" ).convert( "<h3>Just a test</h3>" )
Fetch URL content and convert it to JPG:
require "wkhtmltopdf"
img = Wkhtmltopdf::WkImage.new
img.set_url "http://www.google.com"
img.set_output "test.jpg"
img.set "quality", "90"
img.convert
Write to buffer (only if no output is specified):
require "wkhtmltopdf"
pdf = Wkhtmltopdf::WkPdf.new
pdf.convert "<h3>Just a test</h3>"
pdf.object_setting "footer.right", "[page] / [topage]" # Set page counter on footer
unless pdf.buffer.nil?
puts "PDF buffer size: " + pdf.buffer.try( &.size ).to_s
end
Lib settings (available with set
/ object_setting
methods on wrappers): libwkhtmltox pagesettings
More examples
See examples folder.
Contributors
- Mattia Roccoberton - creator, maintainer