WkHtmlToPdf for Crystal Build Status

Crystal wrapper for libwkhtmltox C library.

wkhtmltopdf and wkhtmltoimage permit to render HTML into PDF and various image formats using the Qt WebKit rendering engine - see wkhtmltopdf.org

Requirements

Installation

dependencies:
  wkhtmltopdf-crystal:
    github: blocknotes/wkhtmltopdf-crystal

Usage

HTML to PDF:

require "wkhtmltopdf-crystal"
Wkhtmltopdf::WkPdf.new( "test.pdf" ).convert( "<h3>Just a test</h3>" )

Fetch URL content and convert it to JPG:

require "wkhtmltopdf-crystal"
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-crystal"
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. Includes a Kemal example to print an ECR view in PDF.

Troubleshooting

Invalid memory access

require "wkhtmltopdf-crystal"
pdf = Wkhtmltopdf::WkPdf.new "", true
at_exit do
  pdf.deinitialize
end

pdf.set_output "test1.pdf"
pdf.convert "<h3>Just a test 1</h3>"

pdf.set_output "test2.pdf"
pdf.convert "<h3>Just a test 2</h3>"

Contributors