some cleanup/organization

This commit is contained in:
Gered 2017-04-02 12:52:01 -04:00
parent bcfca8cf38
commit b7676fcab1
2 changed files with 39 additions and 35 deletions

View file

@ -1,9 +1,7 @@
(ns clj-htmltopdf.core (ns clj-htmltopdf.core
(:require (:require
[clojure.java.io :as io] [clojure.java.io :as io]
[clojure.string :as string]
[hiccup.page :as h] [hiccup.page :as h]
[clj-htmltopdf.css :as css]
[clj-htmltopdf.options :as o]) [clj-htmltopdf.options :as o])
(:import (:import
[java.io OutputStream] [java.io OutputStream]
@ -11,11 +9,10 @@
[com.openhtmltopdf.pdfboxout PdfRendererBuilder] [com.openhtmltopdf.pdfboxout PdfRendererBuilder]
[com.openhtmltopdf.util XRLog] [com.openhtmltopdf.util XRLog]
[org.jsoup Jsoup] [org.jsoup Jsoup]
[org.jsoup.nodes Document Element] [org.jsoup.nodes Document]))
[org.jsoup.parser Tag]))
(defn- read-html (defn- read-html-string
[in] ^String [in]
(cond (cond
(string? in) in (string? in) in
(sequential? in) (h/html5 {} in) (sequential? in) (h/html5 {} in)
@ -23,35 +20,42 @@
(slurp r)))) (slurp r))))
(defn- ->output-stream (defn- ->output-stream
[out] ^OutputStream [out]
(if (instance? OutputStream out) (if (instance? OutputStream out)
out out
(io/output-stream out))) (io/output-stream out)))
(defn- set-jsoup-html-doc (defn configure-logging!
[^PdfRendererBuilder builder jsoup-doc base-uri] [options]
(let [doc (DOMBuilder/jsoup2DOM jsoup-doc)] (if (:logging? options)
(.withW3cDocument builder doc base-uri))) (do
(if-let [logger (:logger options)]
(XRLog/setLoggerImpl logger))
(XRLog/setLoggingEnabled true))
; NOTE: a bug in how Open HTML to PDF's XRLog class initializes itself will always result
; in an initial little bit of logging output regardless of when we set this to false.
(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)
html-doc))
(defn write-pdf!
[^Document html-doc ^String base-uri out]
(let [builder (PdfRendererBuilder.)
os (->output-stream out)]
(.withW3cDocument builder (DOMBuilder/jsoup2DOM html-doc) base-uri)
(with-open [os os]
(.toStream builder os)
(.run builder)
os)))
(defn ->pdf (defn ->pdf
[in out & [options]] [in out & [options]]
(let [options (merge o/default-options options) (let [options (merge o/default-options options)
builder (PdfRendererBuilder.) html-doc (prepare-html in options)]
html (read-html in) (configure-logging! options)
html-doc (Jsoup/parse html) (write-pdf! html-doc (o/->base-uri options) out)))
html-doc (o/inject-options-into-html html-doc options)
output (->output-stream out)]
(if (:logging? options)
(let [logger (:logger options)]
(if logger (XRLog/setLoggerImpl logger))
(XRLog/setLoggingEnabled true))
(XRLog/setLoggingEnabled false))
(set-jsoup-html-doc builder html-doc (o/->base-uri options))
(with-open [os output]
(.toStream builder os)
(try
(.run builder)
os
(catch Exception ex
(throw (Exception. "Failed to convert to PDF." ex)))))))

View file

@ -41,7 +41,7 @@
(reduce #(assoc %1 (first %2) (second %2)) {}))] (reduce #(assoc %1 (first %2) (second %2)) {}))]
[["@page" styles]])) [["@page" styles]]))
(defn append-page-options-style-tag (defn append-page-options-style-tag!
^Element [^Element parent options] ^Element [^Element parent options]
(let [styles (-> (:page options) (let [styles (-> (:page options)
(page-options->css) (page-options->css)
@ -50,17 +50,17 @@
(.attr element "type" "text/css") (.attr element "type" "text/css")
(.text element styles))) (.text element styles)))
(defn append-base-css-link-tag (defn append-base-css-link-tag!
^Element [^Element parent] ^Element [^Element parent]
(let [element (.appendElement parent "link")] (let [element (.appendElement parent "link")]
(.attr element "type" "text/css") (.attr element "type" "text/css")
(.attr element "rel" "stylesheet") (.attr element "rel" "stylesheet")
(.attr element "href" (str (io/resource "htmltopdf-base.css"))))) (.attr element "href" (str (io/resource "htmltopdf-base.css")))))
(defn inject-options-into-html (defn inject-options-into-html!
[^Document doc options] [^Document doc options]
(let [base-uri (->base-uri options) (let [base-uri (->base-uri options)
head-tag (-> doc (.select "head") (.first))] head-tag (-> doc (.select "head") (.first))]
(append-page-options-style-tag head-tag options) (append-page-options-style-tag! head-tag options)
(if (:include-base-css? options) (append-base-css-link-tag head-tag)) (if (:include-base-css? options) (append-base-css-link-tag! head-tag))
doc)) doc))