add more input argument flexibility to embed-image
the main benefit here is now being able to accept byte arrays directly
This commit is contained in:
parent
4d495368f0
commit
5c32c3fba1
|
@ -9,6 +9,7 @@
|
||||||
[com.openhtmltopdf/openhtmltopdf-pdfbox "0.0.1-RC11"]
|
[com.openhtmltopdf/openhtmltopdf-pdfbox "0.0.1-RC11"]
|
||||||
[com.openhtmltopdf/openhtmltopdf-rtl-support "0.0.1-RC11"]
|
[com.openhtmltopdf/openhtmltopdf-rtl-support "0.0.1-RC11"]
|
||||||
[com.openhtmltopdf/openhtmltopdf-svg-support "0.0.1-RC11"]
|
[com.openhtmltopdf/openhtmltopdf-svg-support "0.0.1-RC11"]
|
||||||
|
[commons-io/commons-io "2.5"]
|
||||||
[hiccup "1.0.5"]]
|
[hiccup "1.0.5"]]
|
||||||
|
|
||||||
:profiles {:provided
|
:profiles {:provided
|
||||||
|
|
|
@ -14,27 +14,26 @@
|
||||||
[com.openhtmltopdf.pdfboxout PdfRendererBuilder]
|
[com.openhtmltopdf.pdfboxout PdfRendererBuilder]
|
||||||
[com.openhtmltopdf.svgsupport BatikSVGDrawer]
|
[com.openhtmltopdf.svgsupport BatikSVGDrawer]
|
||||||
[com.openhtmltopdf.util XRLog]
|
[com.openhtmltopdf.util XRLog]
|
||||||
|
[org.apache.commons.io IOUtils]
|
||||||
[org.jsoup Jsoup]
|
[org.jsoup Jsoup]
|
||||||
[org.jsoup.nodes Document]))
|
[org.jsoup.nodes Document]))
|
||||||
|
|
||||||
(defn embed-image
|
(defn embed-image
|
||||||
"Reads an image file and encodes it as a base64 string suitable for use in a data url for displaying
|
"Reads an image (provided as a filename, InputStream or byte array) and encodes it as a base64 string suitable for
|
||||||
inline images in <img> tags or for use in CSS."
|
use in a data url for displaying inline images in <img> tags or for use in CSS."
|
||||||
[image-file]
|
[image]
|
||||||
(try
|
(try
|
||||||
(let [image-file (io/file image-file)
|
(let [is (if-not (instance? InputStream image)
|
||||||
is (io/input-stream image-file)
|
(io/input-stream image)
|
||||||
|
image)
|
||||||
mime-type (URLConnection/guessContentTypeFromStream is)
|
mime-type (URLConnection/guessContentTypeFromStream is)
|
||||||
image-bytes (byte-array (.length image-file))]
|
image-bytes (if (u/byte-array? image) image (IOUtils/toByteArray ^InputStream is))]
|
||||||
(with-open [is is]
|
|
||||||
(.reset is)
|
|
||||||
(.read is image-bytes))
|
|
||||||
(let [b64-str (.encodeToString (Base64/getEncoder) image-bytes)]
|
(let [b64-str (.encodeToString (Base64/getEncoder) image-bytes)]
|
||||||
(if (nil? mime-type)
|
(if (nil? mime-type)
|
||||||
(str "data:" b64-str)
|
(str "data:" b64-str)
|
||||||
(str "data:" mime-type ";base64," b64-str))))
|
(str "data:" mime-type ";base64," b64-str))))
|
||||||
(catch Exception ex
|
(catch Exception ex
|
||||||
(throw (Exception. (str "Exception converting image to inline base64 string: " image-file) ex)))))
|
(throw (ex-info "Exception converting image to inline base64 string: " {:image image} ex)))))
|
||||||
|
|
||||||
(defn- read-html-string
|
(defn- read-html-string
|
||||||
^String [in]
|
^String [in]
|
||||||
|
|
|
@ -3,9 +3,16 @@
|
||||||
[java.io File]
|
[java.io File]
|
||||||
[java.net URL MalformedURLException]))
|
[java.net URL MalformedURLException]))
|
||||||
|
|
||||||
|
(def ^{:private true} bytes-class (Class/forName "[B"))
|
||||||
|
|
||||||
|
(defn byte-array?
|
||||||
|
[x]
|
||||||
|
(boolean (and x (= (.getClass x) bytes-class))))
|
||||||
|
|
||||||
(defn string->url-or-file
|
(defn string->url-or-file
|
||||||
[^String s]
|
[^String s]
|
||||||
(try
|
(try
|
||||||
(URL. s)
|
(URL. s)
|
||||||
(catch MalformedURLException _
|
(catch MalformedURLException _
|
||||||
(File. (str "file:" s)))))
|
(File. (str "file:" s)))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue