pdfkit / pdfkit-clj

Clojure library for generating PDFs using wkhtmltopdf.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pdfkit-clj

A Clojure library for generating PDFs from HTML. Uses wkhtmltopdf, and insipred by Ruby's pdfkit and ring-wicked-pdf.

Install

Leiningen installation:

[pdfkit-clj "0.1.7"]

Usage

(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) ...)

Options:

(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")

Defaults:

:path "wkhtmltopdf"
:tmp "/tmp"
:asset-path "resources/public"
:margin {:top 10 :right 10 :bottom 10 :left 10} ;; in mm
:page-size "A4"

Images

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.

Heroku

If you're like me, everything must work on Heroku. Here's the setup:

1. Download wkhtmltopdf to the ./bin directory of your Leiningen project.

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.

2. Call gen-pdf with appropriate paths:

(gen-pdf html :path "bin/wkhtmltopdf")

License

Copyright © 2018 Banzai Inc.

Distributed under the Eclipse Public License, the same as Clojure.

About

Clojure library for generating PDFs using wkhtmltopdf.


Languages

Language:Clojure 97.2%Language:CSS 1.5%Language:HTML 1.3%