https://github.com/pdfkit/pdfkit-clj
Clojure library for generating PDFs using wkhtmltopdf.
https://github.com/pdfkit/pdfkit-clj
Last synced: about 1 year ago
JSON representation
Clojure library for generating PDFs using wkhtmltopdf.
- Host: GitHub
- URL: https://github.com/pdfkit/pdfkit-clj
- Owner: pdfkit
- Created: 2013-08-24T19:38:38.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2017-12-23T03:05:09.000Z (over 8 years ago)
- Last Synced: 2024-10-28T13:39:05.851Z (over 1 year ago)
- Language: Clojure
- Size: 15.6 KB
- Stars: 30
- Watchers: 6
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome - pdfkit/pdfkit-clj - Clojure library for generating PDFs using wkhtmltopdf. (<a name="Clojure"></a>Clojure)
README
# pdfkit-clj
A Clojure library for generating PDFs from HTML. Uses wkhtmltopdf, and insipred by Ruby's [pdfkit](https://github.com/pdfkit/pdfkit) and [ring-wicked-pdf](https://github.com/gberenfield/ring-wicked-pdf).
### Install
Leiningen installation:
```
[pdfkit-clj "0.1.7"]
```
### Usage
```clojure
(require '[pdfkit-clj.core :refer :all])
(def html "Hello!")
(gen-pdf html)
#
```
You can also convert your file to an InputStream, ready for consumption by a browser (helpful for Ring applications):
```clojure
(as-stream (gen-pdf html))
#
```
pdfkit-clj's `gen-pdf` can also accept HTML nodes (e.g. Enlive):
```clojure
(defsnippet my-snippet
...)
(gen-pdf (my-snippet) ...)
```
### Options:
```clojure
(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:
```clojure
: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:
```clojure
(gen-pdf html :path "bin/wkhtmltopdf")
```
### License
Copyright © 2018 Banzai Inc.
Distributed under the Eclipse Public License, the same as Clojure.