A Clojure library for generating PDFs from HTML. Uses wkhtmltopdf, and insipred by Ruby's pdfkit and ring-wicked-pdf.
Leiningen installation:
[pdfkit-clj "0.1.7"]
(require '[pdfkit-clj.core :refer :all])
(def html "<html><body>Hello!</body></html>")
(gen-pdf html)
#<File /tmp/pdfkit-20130821T161228935Z.pdf>
You can also convert your file to an InputStream, ready for consumption by a browser (helpful for Ring applications):
(as-stream (gen-pdf html))
#<BufferedInputStream java.io.BufferedInputStream@43bda1e0>
pdfkit-clj's gen-pdf
can also accept HTML nodes (e.g. Enlive):
(defsnippet my-snippet
...)
(gen-pdf (my-snippet) ...)
(gen-pdf html
:asset-path "public" ; Relative to your "resources" directory
:stylesheets ["stylesheets/main.css" ; Local file
"stylesheets/invoices.css"
"https://public.yourdomain.com/stylesheets/main.css"] ; External asset
:path "bin/wkhtmltopdf-amd64"
:margin {:top 20 :right 15 :bottom 50 :left 15}
:page-size "A4"
:tmp "other/tmp")
:path "wkhtmltopdf"
:tmp "/tmp"
:asset-path "resources/public"
:margin {:top 10 :right 10 :bottom 10 :left 10} ;; in mm
:page-size "A4"
Right now, pdfkit-clj requires your image tags reference an absolute URL or URI on disk. Simply upload your image to S3, for example, and wkhtmltopdf will have access to it via the file's full URL.
If you're like me, everything must work on Heroku. Here's the setup:
mkdir bin
cd bin
wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
Finally, Unzip the file and rename it to wkhtmltopdf
.
(gen-pdf html :path "bin/wkhtmltopdf")
Copyright © 2018 Banzai Inc.
Distributed under the Eclipse Public License, the same as Clojure.