fix pdf renderer exceptions being ignore due to the write-pdf! future
This commit is contained in:
parent
797232f2a6
commit
8d1cde6129
|
@ -46,31 +46,38 @@
|
||||||
html-doc))
|
html-doc))
|
||||||
|
|
||||||
(defn write-pdf!
|
(defn write-pdf!
|
||||||
^InputStream [^Document html-doc options]
|
[^Document html-doc options]
|
||||||
(let [builder (PdfRendererBuilder.)
|
(let [builder (PdfRendererBuilder.)
|
||||||
base-uri (o/->base-uri options)]
|
base-uri (o/->base-uri options)]
|
||||||
(obj/set-object-drawer-factory builder options)
|
(obj/set-object-drawer-factory builder options)
|
||||||
(.withW3cDocument builder (DOMBuilder/jsoup2DOM html-doc) base-uri)
|
(.withW3cDocument builder (DOMBuilder/jsoup2DOM html-doc) base-uri)
|
||||||
(let [piped-in (PipedInputStream.)
|
(let [piped-in (PipedInputStream.)
|
||||||
piped-out (PipedOutputStream. piped-in)]
|
piped-out (PipedOutputStream. piped-in)
|
||||||
(future
|
renderer (future
|
||||||
(try
|
(try
|
||||||
(with-open [os piped-out]
|
(with-open [os piped-out]
|
||||||
(.toStream builder os)
|
(.toStream builder os)
|
||||||
(.run builder))
|
(.run builder))
|
||||||
(catch Exception ex
|
(catch Exception ex
|
||||||
(println "Exception while rendering PDF" ex))))
|
(throw (Exception. "Exception while rendering PDF" ex)))))]
|
||||||
piped-in)))
|
{:pdf piped-in
|
||||||
|
:renderer renderer})))
|
||||||
|
|
||||||
(defn ->pdf
|
(defn ->pdf
|
||||||
[in out & [options]]
|
[in out & [options]]
|
||||||
(let [options (o/get-final-options options)
|
(let [options (o/get-final-options options)
|
||||||
html-doc (prepare-html in options)]
|
html-doc (prepare-html in options)]
|
||||||
(configure-logging! options)
|
(configure-logging! options)
|
||||||
(let [pdf (write-pdf! html-doc options)
|
(let [{:keys [pdf renderer]} (write-pdf! html-doc options)
|
||||||
out (->output-stream out)]
|
out (->output-stream out)
|
||||||
(if (:watermark options)
|
result (if (:watermark options)
|
||||||
(w/write-watermark! pdf out options)
|
(w/write-watermark! pdf out options)
|
||||||
(with-open [os out]
|
(with-open [os out]
|
||||||
(io/copy pdf os)
|
(io/copy pdf os)
|
||||||
os)))))
|
os))]
|
||||||
|
; this is a little weird, but because of the whole piped stream thing in write-pdf!, we need to render the
|
||||||
|
; PDF in a future. if something throws an exception during rendering, it would otherwise get eaten silently by
|
||||||
|
; the future... except if we deref the future! thus the explicit call to deref it here
|
||||||
|
(deref renderer)
|
||||||
|
result)))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue