clojure-uk

A place for people in the UK, near the UK, visiting the UK, planning to visit the UK or just vaguely interested to randomly chat about things (often vi and emacs, occasionally clojure). More general the #ldnclj
Kai 2021-02-16T00:22:30.336200Z

Good to know. Thanks!

dharrigan 2021-02-16T07:12:33.336700Z

Morning!

djm 2021-02-16T07:29:40.336900Z

πŸ‘‹

jiriknesl 2021-02-16T08:15:18.337100Z

Good morning

mccraigmccraig 2021-02-16T08:28:36.337400Z

Β‘mawning

thomas 2021-02-16T08:31:16.337600Z

πŸ‘‹

maleghast 2021-02-16T09:59:09.338200Z

Morning All πŸ™‚

maleghast 2021-02-16T09:59:38.338800Z

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

maleghast 2021-02-17T12:33:25.349200Z

Thx so much this is really awesome πŸ™‚

πŸ‘ 1
maleghast 2021-02-16T10:00:24.339700Z

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

maleghast 2021-02-16T10:01:30.341Z

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

maleghast 2021-02-16T10:01:41.341300Z

brb - morning standup

2021-02-16T10:07:09.341700Z

Morn'

danm 2021-02-16T10:10:13.341900Z

Morning

2021-02-16T10:23:42.342Z

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

2021-02-16T10:24:15.342200Z

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

maleghast 2021-02-16T10:27:43.342400Z

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 πŸ™‚

alexlynham 2021-02-16T10:28:57.342600Z

morning

2021-02-16T10:34:13.343500Z

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

maleghast 2021-02-16T10:41:19.343700Z

:thumbsup:

2021-02-16T12:29:15.344300Z

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

2021-02-16T12:29:51.344500Z

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

2021-02-16T12:30:14.344700Z

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

2021-02-16T12:30:29.344900Z

we were using pdfbox as the java pdf converter bit

2021-02-16T12:30:59.345100Z

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

2021-02-16T12:35:10.345300Z

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