Fork me on GitHub



Good morning


Morning All ๐Ÿ™‚


Hey I was wondering if anyone in here has any experience with converting HTML to PDF using Clojure?

Jordan Robinson10:02:42

I've done this fairly recently actually, I'd like to type it up but I'm basically in the meeting dimension for the foreseeable. From memory instead of writing to disk I was passing around the output of PDFBox, and there was an example from the book "Web Development with Clojure" which had the bulk of what I ended up doing. It was Liberator and not reitit but it should be pretty similar. If I get a chance to not be in meetings for more than 5 mins today I'll see if I can find the code

Jordan Robinson10:02:15

if I remember correctly finding the right java type to return that would actually convince the browser to render it as a pdf was the tricky bit


That's a bunch of useful insights and confirmations - particularly the Java type issue for returning a pdf straight to the browser. Thx If you do manage to find the code (srsly no pressure) that would be great, but thanks for this already ๐Ÿ™‚

Jordan Robinson10:02:13

no problem, I can't guarantee it'll be today but I'll see what I can find ๐Ÿ™‚

Jordan Robinson12:02:15

so the main bulk of it is this:

(defn html->pdf-stream [html]
  (with-open [out (ByteArrayOutputStream.)]
    (let [builder (doto (PdfRendererBuilder.)
                    (.withHtmlContent html (str (io/resource ".")))
                    (.toStream out))]
      (.run builder))

Jordan Robinson12:02:51

that we then converted like so: (.toByteArray stream) later on

Jordan Robinson12:02:14

and I think that's basically what we ended up returning

Jordan Robinson12:02:29

we were using pdfbox as the java pdf converter bit

Jordan Robinson12:02:59

unsure how much that helps but I hope it does a bit

Jordan Robinson12:02:10

you're likely to also have to set a media type header of "application/pdf" but I think that's pretty much it for the hard parts


Thx so much this is really awesome ๐Ÿ™‚

๐Ÿ‘ 3

Specifically I want to render a PDF and send it as a web response from a reitit handler...


(I've got the actual render working with a clojure library that wraps the Java standard approach, but it's quite opinionated and wants to write the output straight to disk)


brb - morning standup