2017-04-01 14:58:23 -04:00
|
|
|
(ns clj-htmltopdf.core
|
|
|
|
(:require
|
|
|
|
[clojure.java.io :as io]
|
2017-04-02 12:07:00 -04:00
|
|
|
[clojure.string :as string]
|
|
|
|
[hiccup.page :as h]
|
|
|
|
[clj-htmltopdf.css :as css]
|
|
|
|
[clj-htmltopdf.options :as o])
|
2017-04-01 14:58:23 -04:00
|
|
|
(:import
|
|
|
|
[java.io OutputStream]
|
|
|
|
[com.openhtmltopdf DOMBuilder]
|
|
|
|
[com.openhtmltopdf.pdfboxout PdfRendererBuilder]
|
|
|
|
[com.openhtmltopdf.util XRLog]
|
2017-04-02 12:07:00 -04:00
|
|
|
[org.jsoup Jsoup]
|
|
|
|
[org.jsoup.nodes Document Element]
|
|
|
|
[org.jsoup.parser Tag]))
|
2017-04-01 14:58:23 -04:00
|
|
|
|
|
|
|
(defn- read-html
|
|
|
|
[in]
|
|
|
|
(try
|
|
|
|
(cond
|
|
|
|
(string? in) in
|
|
|
|
(sequential? in) (h/html5 {} in)
|
|
|
|
:else (with-open [r (io/reader in)]
|
|
|
|
(slurp r)))
|
|
|
|
(catch Exception ex
|
|
|
|
(throw (Exception. "Error reading HTML from input." ex)))))
|
|
|
|
|
|
|
|
(defn- ->output-stream
|
|
|
|
[out]
|
|
|
|
(try
|
|
|
|
(if (instance? OutputStream out)
|
|
|
|
out
|
|
|
|
(io/output-stream out))
|
|
|
|
(catch Exception ex
|
|
|
|
(throw (Exception. "Error preparing an OutputStream from output given." ex)))))
|
|
|
|
|
2017-04-02 12:07:00 -04:00
|
|
|
(defn- parse-jsoup-html
|
2017-04-01 14:58:23 -04:00
|
|
|
[^String html]
|
|
|
|
(try
|
2017-04-02 12:07:00 -04:00
|
|
|
(Jsoup/parse html)
|
2017-04-01 14:58:23 -04:00
|
|
|
(catch Exception ex
|
2017-04-02 12:07:00 -04:00
|
|
|
(throw (Exception. "Error parsing input HTML to Jsoup Document." ex)))))
|
2017-04-01 14:58:23 -04:00
|
|
|
|
2017-04-02 12:07:00 -04:00
|
|
|
(defn- set-jsoup-html-doc
|
|
|
|
[^PdfRendererBuilder builder jsoup-doc base-uri]
|
|
|
|
(try
|
|
|
|
(let [doc (DOMBuilder/jsoup2DOM jsoup-doc)]
|
|
|
|
(.withW3cDocument builder doc base-uri))
|
|
|
|
(catch Exception ex
|
|
|
|
(throw (Exception. "Error setting org.w3c.dom.Document HTML." ex)))))
|
2017-04-01 15:23:02 -04:00
|
|
|
|
2017-04-01 14:58:23 -04:00
|
|
|
(defn ->pdf
|
|
|
|
[in out & [options]]
|
2017-04-02 12:07:00 -04:00
|
|
|
(let [options (merge o/default-options options)
|
2017-04-01 15:23:02 -04:00
|
|
|
builder (PdfRendererBuilder.)
|
2017-04-01 14:58:23 -04:00
|
|
|
html (read-html in)
|
2017-04-02 12:07:00 -04:00
|
|
|
html-doc (parse-jsoup-html html)
|
|
|
|
html-doc (o/inject-options-into-html html-doc options)
|
2017-04-01 14:58:23 -04:00
|
|
|
output (->output-stream out)]
|
2017-04-02 12:07:00 -04:00
|
|
|
html-doc
|
2017-04-01 14:58:23 -04:00
|
|
|
(if (:logging? options)
|
|
|
|
(let [logger (:logger options)]
|
|
|
|
(if logger (XRLog/setLoggerImpl logger))
|
|
|
|
(XRLog/setLoggingEnabled true))
|
|
|
|
(XRLog/setLoggingEnabled false))
|
2017-04-02 12:07:00 -04:00
|
|
|
(set-jsoup-html-doc builder html-doc (o/->base-uri options))
|
2017-04-01 14:58:23 -04:00
|
|
|
(with-open [os output]
|
|
|
|
(.toStream builder os)
|
|
|
|
(try
|
|
|
|
(.run builder)
|
|
|
|
os
|
|
|
|
(catch Exception ex
|
|
|
|
(throw (Exception. "Failed to convert to PDF." ex)))))))
|
|
|
|
|