Fork me on GitHub
#clojure-uk
<
2021-02-16
>
djm07:02:40

๐Ÿ‘‹

jiriknesl08:02:18

Good morning

maleghast09:02:09

Morning All ๐Ÿ™‚

maleghast09:02:38

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

maleghast10:02:43

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))
    out))

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

maleghast12:02:25

Thx so much this is really awesome ๐Ÿ™‚

๐Ÿ‘ 3
maleghast10:02:24

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

maleghast10:02:30

(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)

maleghast10:02:41

brb - morning standup