clj-htmltopdf/src/clj_htmltopdf/core.clj

71 lines
2 KiB
Clojure
Raw Normal View History

2017-04-01 14:58:23 -04:00
(ns clj-htmltopdf.core
(:require
[clojure.java.io :as io]
[hiccup.page :as h]
[clj-htmltopdf.options :as o]
[clj-htmltopdf.watermark :as w])
2017-04-01 14:58:23 -04:00
(:import
2017-05-04 21:02:31 -04:00
[java.io InputStream OutputStream PipedInputStream PipedOutputStream]
2017-04-01 14:58:23 -04:00
[com.openhtmltopdf DOMBuilder]
[com.openhtmltopdf.pdfboxout PdfRendererBuilder]
[com.openhtmltopdf.util XRLog]
[org.jsoup Jsoup]
2017-04-02 12:52:01 -04:00
[org.jsoup.nodes Document]))
2017-04-01 14:58:23 -04:00
2017-04-02 12:52:01 -04:00
(defn- read-html-string
^String [in]
2017-04-02 12:24:58 -04:00
(cond
(string? in) in
(sequential? in) (h/html5 {} in)
:else (with-open [r (io/reader in)]
(slurp r))))
2017-04-01 14:58:23 -04:00
(defn- ->output-stream
2017-04-02 12:52:01 -04:00
^OutputStream [out]
2017-04-02 12:24:58 -04:00
(if (instance? OutputStream out)
out
(io/output-stream out)))
2017-04-01 14:58:23 -04:00
2017-04-02 12:52:01 -04:00
(defn configure-logging!
[options]
(if (:logging? options)
(do
(if-let [logger (:logger options)]
(XRLog/setLoggerImpl logger))
(XRLog/setLoggingEnabled true))
(XRLog/setLoggingEnabled false)))
(defn prepare-html
[in options]
(let [html (read-html-string in)
html-doc (Jsoup/parse html)]
(o/inject-options-into-html! html-doc options)
2017-04-02 13:20:24 -04:00
(if (get-in options [:debug :display-html?])
(println (str html-doc)))
2017-04-02 12:52:01 -04:00
html-doc))
(defn write-pdf!
^InputStream [^Document html-doc ^String base-uri]
2017-05-04 21:02:31 -04:00
(let [builder (PdfRendererBuilder.)]
2017-04-02 12:52:01 -04:00
(.withW3cDocument builder (DOMBuilder/jsoup2DOM html-doc) base-uri)
2017-05-04 21:02:31 -04:00
(let [piped-in (PipedInputStream.)
piped-out (PipedOutputStream. piped-in)]
(future
(with-open [os piped-out]
(.toStream builder os)
(.run builder)))
piped-in)))
2017-04-01 14:58:23 -04:00
(defn ->pdf
[in out & [options]]
2017-04-02 13:20:24 -04:00
(let [options (o/get-final-options options)
2017-04-02 12:52:01 -04:00
html-doc (prepare-html in options)]
(configure-logging! options)
(let [pdf (write-pdf! html-doc (o/->base-uri options))
out (->output-stream out)]
2017-05-04 21:02:31 -04:00
(if (:watermark options)
(w/write-watermark! pdf out options)
2017-05-04 21:02:31 -04:00
(with-open [os out]
(io/copy pdf os)
2017-05-04 21:02:31 -04:00
os)))))